Skip to content

Commit 004da13

Browse files
committed
Constant types; type variable elimination in constants
1 parent effd66a commit 004da13

File tree

1 file changed

+131
-71
lines changed

1 file changed

+131
-71
lines changed

specification/dartLangSpec.tex

Lines changed: 131 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8665,8 +8665,8 @@ \subsection{Constants}
86658665
If generic function instantiation does apply to $e$
86668666
and the provided actual type arguments are \List{T}{1}{s}
86678667
then $e$ is a potentially constant and constant expression
8668-
if{}f each $T_j, j \in 1 .. s$, is a constant type expression
8669-
(\ref{constants}).
8668+
if{}f each $T_j, j \in 1 .. s$, is a constant type
8669+
(\ref{constantTypes}).
86708670
\item
86718671
An identifier expression denoting a parameter of a constant constructor
86728672
(\ref{constantConstructors})
@@ -8690,7 +8690,8 @@ \subsection{Constants}
86908690
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
86918691
\code{<$T$>[$e_1$, \ldots, $e_n$]}
86928692
that occurs in a constant context,
8693-
is a potentially constant expression if $T$ is a constant type expression,
8693+
is a potentially constant expression if $T$ is a constant type
8694+
(\ref{constantTypes}),
86948695
and $e_1$, \ldots{} , $e_n$ are constant expressions.
86958696
It is further a constant expression
86968697
if the list literal evaluates to an object.
@@ -8700,7 +8701,8 @@ \subsection{Constants}
87008701
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
87018702
that occurs in a constant context,
87028703
is a potentially constant expression
8703-
if $T$ is a constant type expression,
8704+
if $T$ is a constant type
8705+
(\ref{constantTypes}),
87048706
and $e_1$, \ldots{} , $e_n$ are constant expressions.
87058707
It is further a constant expression
87068708
if the list literal evaluates to an object.
@@ -8843,7 +8845,8 @@ \subsection{Constants}
88438845
\item
88448846
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
88458847
if $e$ is a potentially constant expression
8846-
and $T$ is a potentially constant type expression,
8848+
and $T$ is a potentially constant type
8849+
(\ref{constantTypes}),
88478850
and it is further constant if $e$ is constant.
88488851
\commentary{%
88498852
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8853,18 +8856,57 @@ \subsection{Constants}
88538856
\item
88548857
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
88558858
if $e$ is a potentially constant expression
8856-
and $T$ is a constant type expression,
8859+
and $T$ is a constant type
8860+
(\ref{constantTypes}),
88578861
and it is further constant if $e$ is constant.
88588862
\item
88598863
An expression of the form \code{$e$\,\,is!\,\,$T$}
88608864
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
88618865
including whether it's potentially constant or constant.
88628866
\end{itemize}
88638867

8868+
% Being potentially constant is entirely structural, not type based,
8869+
% but the program still has to satisfy strong-mode typing.
8870+
8871+
% Constant expressions (like "const Foo(42)") always evaluate to the
8872+
% same value, with at most one value per source location.
8873+
% Potentially constant expressions that are not constant only
8874+
% allow simple operations on basic types (num, String, bool, Null). These can
8875+
% be computed statically without running user code.
8876+
88648877
\LMHash{}%
8865-
We introduce \Index{constant type expressions} and
8866-
\Index{potentially constant type expressions}
8867-
as follows:
8878+
It is a \Error{compile-time error} if an expression is required to be
8879+
a constant expression,
8880+
but its evaluation would throw an exception.
8881+
It is a \Error{compile-time error} if an assertion is evaluated as part of
8882+
a constant object expression evaluation,
8883+
and the assertion would throw an exception.
8884+
8885+
\LMHash{}%
8886+
It is a \Error{compile-time error} if the value of a constant expression
8887+
depends on itself.
8888+
8889+
\commentary{%
8890+
As an example, consider:%
8891+
}
8892+
8893+
\begin{dartCode}
8894+
\CLASS{} CircularConsts \{
8895+
// \comment{Illegal program - mutually recursive compile-time constants}
8896+
\STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8897+
\STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8898+
\}
8899+
\end{dartCode}
8900+
8901+
8902+
\subsubsection{Constant Types}
8903+
\LMLabel{constantTypes}
8904+
8905+
\LMHash{}%
8906+
A \Index{potentially constant type} respectively \Index{constant type}
8907+
is a term derived from \synt{type} which is used as a type
8908+
(\commentary{not as an expression that yields an instance of \code{Type}})
8909+
that satisfy the following criteria:
88688910
\begin{itemize}
88698911
\item
88708912
Consider a type $T$
@@ -8873,70 +8915,105 @@ \subsection{Constants}
88738915
which is not qualified by a deferred prefix,
88748916
and which is optionally followed by
88758917
type arguments of the form \code{<\List{T}{1}{r}>}.
8876-
$T$ is a potentially constant type expression
8877-
respectively a constant type expression
8878-
if{}f $T_j$ is a potentially constant respectively constant type expression
8918+
$T$ is a potentially constant type respectively a constant type
8919+
if{}f $T_j$ is a potentially constant respectively constant type
88798920
for each $j \in 1 .. r$.
88808921
\item
88818922
A type of the form \code{FutureOr<$T$>}
8882-
is a potentially constant type expression
8883-
respectively a constant type expression
8884-
if{}f $T$ is a potentially constant type expression
8885-
respectively a constant type expression.
8923+
is a potentially constant type
8924+
respectively a constant type
8925+
if{}f $T$ is a potentially constant type
8926+
respectively a constant type.
88868927
\item
88878928
%% TODO(eernst): This does not allow for type variables introduced by
8888-
%% the type itself. `Function<X>(X)` could be a constant type expression,
8929+
%% the type itself. `Function<X>(X)` could be a constant type,
88898930
%% but that is not covered by the current rules: `X` is a type variable,
8890-
%% and they are never constant type expressions.
8931+
%% and they are never constant type.
88918932
A function type
88928933
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
88938934
(where $R$ and \code{<\metavar{typeParameters}>} may be omitted)
8894-
is a is a potentially constant type expression
8895-
respectively a constant type expression
8935+
is a is a potentially constant type
8936+
respectively a constant type
88968937
if $R$, \metavar{typeParameters}, and \metavar{parameterTypes}
8897-
(if present) contain only potentially constant type expressions
8898-
respectively constant type expressions.
8938+
(if present) contain only potentially constant types
8939+
respectively constant types.
88998940
\item
8900-
The type \VOID{} is a potentially constant and a constant type expression.
8941+
The type \VOID{} is a potentially constant and constant type.
89018942
\item
8902-
The type \DYNAMIC{} is a potentially constant and a constant type expression.
8943+
The type \DYNAMIC{} is a potentially constant and constant type.
89038944
\item
8904-
A type variable is a potentially constant type expression.
8945+
A type variable is a potentially constant type.
89058946
\end{itemize}
89068947

8907-
% Being potentially constant is entirely structural, not type based,
8908-
% but the program still has to satisfy strong-mode typing.
89098948

8910-
% Constant expressions (like "const Foo(42)") always evaluate to the
8911-
% same value, with at most one value per source location.
8912-
% Potentially constant expressions that are not constant only
8913-
% allow simple operations on basic types (num, String, bool, Null). These can
8914-
% be computed statically without running user code.
8949+
\subsubsection{Type Variable Elimination in Constants}
8950+
\LMLabel{typeVariableEliminationInConstants}
89158951

89168952
\LMHash{}%
8917-
It is a \Error{compile-time error} if an expression is required to be
8918-
a constant expression,
8919-
but its evaluation would throw an exception.
8920-
It is a \Error{compile-time error} if an assertion is evaluated as part of
8921-
a constant object expression evaluation,
8922-
and the assertion would throw an exception.
8953+
\BlindDefineSymbol{e, e'}%
8954+
Let $e$ be an expression that occurs in a constant context
8955+
or whose first token is \CONST,
8956+
and $e'$ be the expression yielded by type inference
8957+
(\ref{typeInference})
8958+
applied to $e$.
8959+
In this case an additional transformation is applied:
89238960

89248961
\LMHash{}%
8925-
It is a \Error{compile-time error} if the value of a constant expression
8926-
depends on itself.
8962+
Let \List{X}{1}{r} be the free type variables declared by
8963+
a class, mixin, extension, or method that contains $e$,
8964+
such that for each $j \in 1 .. r$, each $X_j$
8965+
occurs in a list of actual type arguments
8966+
which was added by type inference
8967+
(\commentary{%
8968+
so $X_j$ has one or more occurrences in $e'$,
8969+
but not all of those occurrences exist in $e$%
8970+
}).
8971+
For each $j \in 1 .. r$,
8972+
if a specific occurrence of $X_j$ in $e'$ was added by type inference,
8973+
the outermost enclosing type argument which was added by type inference
8974+
and which contains this occurrence
8975+
is replaced by its least closure with respect to \List{X}{1}{r}
8976+
(\ref{leastAndGreatestClosureOfTypes}).
89278977

89288978
\commentary{%
8929-
As an example, consider:%
8979+
Note that a free type variable which is explicitly used
8980+
as or in a type argument in a constant expression
8981+
is still a compile-time error.
8982+
For example:%
89308983
}
89318984

89328985
\begin{dartCode}
8933-
\CLASS{} CircularConsts \{
8934-
// \comment{Illegal program - mutually recursive compile-time constants}
8935-
\STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8936-
\STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8986+
\CLASS\ G<X> \{
8987+
\VOID\ foo() \{
8988+
\CONST\ List<X> c = <X>[]; // \comment{Compile-time error.}
8989+
\CONST\ List<X> d = []; // \comment{Infers \code{<X>[]}, then becomes \code{<Never>[]}.}
8990+
\}
89378991
\}
89388992
\end{dartCode}
89398993

8994+
\rationale{%
8995+
The type variable is not a constant type
8996+
(\ref{constantTypes}),
8997+
which implies that the inferred expression $e'$ is a compile-time error.
8998+
The additional transformation yields a subtype of the inferred type.
8999+
It typically replaces the type variable by \code{Never},
9000+
but it may also replace the type variable by a top type
9001+
(if the type variable occurs contravariantly),
9002+
or the type may be changed more radically
9003+
(e.g., a generic function type using $X_j$ as a type parameter bound
9004+
may be changed to \FUNCTION{} or \code{Never}).
9005+
In any case, the chosen subtype often yields
9006+
a constant which has no errors and is useful.%
9007+
}
9008+
9009+
\commentary{%
9010+
For example, \code{\CONST\,\,<Never>[]} is actually usable as
9011+
a list of any type,
9012+
and it is not a problem that every invocation of \code{add}
9013+
on this list will fail (statically or dynamically),
9014+
because that is already true for every constant list.%
9015+
}
9016+
89409017

89419018
\subsubsection{Further Remarks on Constants and Potential Constants}
89429019
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10697,8 +10774,8 @@ \subsubsection{Lists}
1069710774
if an element of a constant list literal is not constant.
1069810775
It is a \Error{compile-time error} if the type argument of a constant list literal
1069910776
(\commentary{no matter whether it is explicit or inferred})
10700-
is not a constant type expression
10701-
(\ref{constants}).
10777+
is not a constant type
10778+
(\ref{constantTypes}).
1070210779

1070310780
\rationale{%
1070410781
The binding of a formal type parameter of an enclosing class or function
@@ -11510,8 +11587,8 @@ \subsubsection{Sets}
1151011587
(\ref{equality}).
1151111588
It is a \Error{compile-time error} if the type argument of a constant set literal
1151211589
(\commentary{no matter whether it is explicit or inferred})
11513-
is not a constant type expression
11514-
(\ref{constants}).
11590+
is not a constant type
11591+
(\ref{constantTypes}).
1151511592

1151611593
\rationale{%
1151711594
The binding of a formal type parameter of an enclosing class or function
@@ -11715,8 +11792,8 @@ \subsubsection{Maps}
1171511792
(\ref{equality}).
1171611793
It is a \Error{compile-time error} if a type argument of a constant map literal
1171711794
(\commentary{no matter whether it is explicit or inferred})
11718-
is not a constant type expression
11719-
(\ref{constants}).
11795+
is not a constant type
11796+
(\ref{constantTypes}).
1172011797

1172111798
\rationale{%
1172211799
The binding of a formal type parameter of an enclosing class or function
@@ -12466,7 +12543,7 @@ \subsubsection{Const}
1246612543
\LMHash{}%
1246712544
If $T$ is a parameterized type,
1246812545
it is a
12469-
\Error{compile-time error} if $U_j$ is not a constant type expression for any
12546+
\Error{compile-time error} if $U_j$ is not a constant type for any
1247012547
$j \in 1 .. m$.
1247112548

1247212549
\begin{itemize}
@@ -24993,28 +25070,11 @@ \section{Null safety} %% !!!TODO!!!
2499325070
%% !!!At the end: Search Null, change to Never where appropriate
2499425071
%% !!!Search all `TODO`.*null
2499525072

24996-
\subsubsection{Const type variable elimination}
24997-
\LMLabel{}
24998-
24999-
If performing inference on a constant value of a generic class results in
25000-
inferred type arguments to the generic class which contain free type variables
25001-
from an enclosing generic class or method, the free type variables shall be
25002-
eliminated by taking the least closure of the inferred type with respect to the
25003-
free type variables. Note that free type variables which are explicitly used as
25004-
type arguments in constant generic instances are still considered erroneous.
25005-
25006-
\begin{dartCode}
25007-
\CLASS\ G<T> \{
25008-
\VOID\ foo() \{
25009-
\CONST\ List<T> c = <T>[]; // Error
25010-
\CONST\ List<T> d = []; // The list literal is inferred as <Never>[]
25011-
\}
25012-
\}
25013-
\end{dartCode}
25014-
2501525073
\subsubsection{Null promotion}
2501625074
\LMLabel{}
2501725075

25076+
!!!
25077+
2501825078
The machinery of type promotion is extended to promote the type of
2501925079
variables based on nullability checks subject to the same set of
2502025080
restrictions as normal promotion. The relevant checks and the types

0 commit comments

Comments
 (0)