@@ -8665,8 +8665,8 @@ \subsection{Constants}
8665
8665
If generic function instantiation does apply to $e$
8666
8666
and the provided actual type arguments are \List{T}{1}{s}
8667
8667
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 }).
8670
8670
\item
8671
8671
An identifier expression denoting a parameter of a constant constructor
8672
8672
(\ref{constantConstructors})
@@ -8690,7 +8690,8 @@ \subsection{Constants}
8690
8690
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
8691
8691
\code{<$T$>[$e_1$, \ldots, $e_n$]}
8692
8692
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}),
8694
8695
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8695
8696
It is further a constant expression
8696
8697
if the list literal evaluates to an object.
@@ -8700,7 +8701,8 @@ \subsection{Constants}
8700
8701
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
8701
8702
that occurs in a constant context,
8702
8703
is a potentially constant expression
8703
- if $T$ is a constant type expression,
8704
+ if $T$ is a constant type
8705
+ (\ref{constantTypes}),
8704
8706
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8705
8707
It is further a constant expression
8706
8708
if the list literal evaluates to an object.
@@ -8843,7 +8845,8 @@ \subsection{Constants}
8843
8845
\item
8844
8846
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
8845
8847
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}),
8847
8850
and it is further constant if $e$ is constant.
8848
8851
\commentary{%
8849
8852
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8853,18 +8856,57 @@ \subsection{Constants}
8853
8856
\item
8854
8857
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
8855
8858
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}),
8857
8861
and it is further constant if $e$ is constant.
8858
8862
\item
8859
8863
An expression of the form \code{$e$\,\,is!\,\,$T$}
8860
8864
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
8861
8865
including whether it's potentially constant or constant.
8862
8866
\end{itemize}
8863
8867
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
+
8864
8877
\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:
8868
8910
\begin{itemize}
8869
8911
\item
8870
8912
Consider a type $T$
@@ -8873,70 +8915,105 @@ \subsection{Constants}
8873
8915
which is not qualified by a deferred prefix,
8874
8916
and which is optionally followed by
8875
8917
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
8879
8920
for each $j \in 1 .. r$.
8880
8921
\item
8881
8922
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.
8886
8927
\item
8887
8928
%% 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,
8889
8930
%% 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.
8891
8932
A function type
8892
8933
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
8893
8934
(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
8896
8937
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 .
8899
8940
\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.
8901
8942
\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.
8903
8944
\item
8904
- A type variable is a potentially constant type expression .
8945
+ A type variable is a potentially constant type.
8905
8946
\end{itemize}
8906
8947
8907
- % Being potentially constant is entirely structural, not type based,
8908
- % but the program still has to satisfy strong-mode typing.
8909
8948
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}
8915
8951
8916
8952
\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:
8923
8960
8924
8961
\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}).
8927
8977
8928
8978
\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:%
8930
8983
}
8931
8984
8932
8985
\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
+ \}
8937
8991
\}
8938
8992
\end{dartCode}
8939
8993
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
+
8940
9017
8941
9018
\subsubsection{Further Remarks on Constants and Potential Constants}
8942
9019
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10697,8 +10774,8 @@ \subsubsection{Lists}
10697
10774
if an element of a constant list literal is not constant.
10698
10775
It is a \Error{compile-time error} if the type argument of a constant list literal
10699
10776
(\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 }).
10702
10779
10703
10780
\rationale{%
10704
10781
The binding of a formal type parameter of an enclosing class or function
@@ -11510,8 +11587,8 @@ \subsubsection{Sets}
11510
11587
(\ref{equality}).
11511
11588
It is a \Error{compile-time error} if the type argument of a constant set literal
11512
11589
(\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 }).
11515
11592
11516
11593
\rationale{%
11517
11594
The binding of a formal type parameter of an enclosing class or function
@@ -11715,8 +11792,8 @@ \subsubsection{Maps}
11715
11792
(\ref{equality}).
11716
11793
It is a \Error{compile-time error} if a type argument of a constant map literal
11717
11794
(\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 }).
11720
11797
11721
11798
\rationale{%
11722
11799
The binding of a formal type parameter of an enclosing class or function
@@ -12466,7 +12543,7 @@ \subsubsection{Const}
12466
12543
\LMHash{}%
12467
12544
If $T$ is a parameterized type,
12468
12545
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
12470
12547
$j \in 1 .. m$.
12471
12548
12472
12549
\begin{itemize}
@@ -24993,28 +25070,11 @@ \section{Null safety} %% !!!TODO!!!
24993
25070
%% !!!At the end: Search Null, change to Never where appropriate
24994
25071
%% !!!Search all `TODO`.*null
24995
25072
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
-
25015
25073
\subsubsection{Null promotion}
25016
25074
\LMLabel{}
25017
25075
25076
+ !!!
25077
+
25018
25078
The machinery of type promotion is extended to promote the type of
25019
25079
variables based on nullability checks subject to the same set of
25020
25080
restrictions as normal promotion. The relevant checks and the types
0 commit comments