@@ -8446,8 +8446,8 @@ \subsection{Constants}
8446
8446
If generic function instantiation does apply to $e$
8447
8447
and the provided actual type arguments are \List{T}{1}{s}
8448
8448
then $e$ is a potentially constant and constant expression
8449
- if{}f each $T_j, j \in 1 .. s$, is a constant type expression
8450
- (\ref{constants }).
8449
+ if{}f each $T_j, j \in 1 .. s$, is a constant type
8450
+ (\ref{constantTypes }).
8451
8451
\item
8452
8452
An identifier expression denoting a parameter of a constant constructor
8453
8453
(\ref{constantConstructors})
@@ -8471,7 +8471,8 @@ \subsection{Constants}
8471
8471
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
8472
8472
\code{<$T$>[$e_1$, \ldots, $e_n$]}
8473
8473
that occurs in a constant context,
8474
- is a potentially constant expression if $T$ is a constant type expression,
8474
+ is a potentially constant expression if $T$ is a constant type
8475
+ (\ref{constantTypes}),
8475
8476
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8476
8477
It is further a constant expression
8477
8478
if the list literal evaluates to an object.
@@ -8481,7 +8482,8 @@ \subsection{Constants}
8481
8482
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
8482
8483
that occurs in a constant context,
8483
8484
is a potentially constant expression
8484
- if $T$ is a constant type expression,
8485
+ if $T$ is a constant type
8486
+ (\ref{constantTypes}),
8485
8487
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8486
8488
It is further a constant expression
8487
8489
if the list literal evaluates to an object.
@@ -8622,7 +8624,8 @@ \subsection{Constants}
8622
8624
\item
8623
8625
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
8624
8626
if $e$ is a potentially constant expression
8625
- and $T$ is a potentially constant type expression,
8627
+ and $T$ is a potentially constant type
8628
+ (\ref{constantTypes}),
8626
8629
and it is further constant if $e$ is constant.
8627
8630
\commentary{%
8628
8631
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8632,18 +8635,57 @@ \subsection{Constants}
8632
8635
\item
8633
8636
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
8634
8637
if $e$ is a potentially constant expression
8635
- and $T$ is a constant type expression,
8638
+ and $T$ is a constant type
8639
+ (\ref{constantTypes}),
8636
8640
and it is further constant if $e$ is constant.
8637
8641
\item
8638
8642
An expression of the form \code{$e$\,\,is!\,\,$T$}
8639
8643
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
8640
8644
including whether it's potentially constant or constant.
8641
8645
\end{itemize}
8642
8646
8647
+ % Being potentially constant is entirely structural, not type based,
8648
+ % but the program still has to satisfy strong-mode typing.
8649
+
8650
+ % Constant expressions (like "const Foo(42)") always evaluate to the
8651
+ % same value, with at most one value per source location.
8652
+ % Potentially constant expressions that are not constant only
8653
+ % allow simple operations on basic types (num, String, bool, Null). These can
8654
+ % be computed statically without running user code.
8655
+
8643
8656
\LMHash{}%
8644
- We introduce \Index{constant type expressions} and
8645
- \Index{potentially constant type expressions}
8646
- as follows:
8657
+ It is a \Error{compile-time error} if an expression is required to be
8658
+ a constant expression,
8659
+ but its evaluation would throw an exception.
8660
+ It is a \Error{compile-time error} if an assertion is evaluated as part of
8661
+ a constant object expression evaluation,
8662
+ and the assertion would throw an exception.
8663
+
8664
+ \LMHash{}%
8665
+ It is a \Error{compile-time error} if the value of a constant expression
8666
+ depends on itself.
8667
+
8668
+ \commentary{%
8669
+ As an example, consider:%
8670
+ }
8671
+
8672
+ \begin{dartCode}
8673
+ \CLASS{} CircularConsts \{
8674
+ // \comment{Illegal program - mutually recursive compile-time constants}
8675
+ \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8676
+ \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8677
+ \}
8678
+ \end{dartCode}
8679
+
8680
+
8681
+ \subsubsection{Constant Types}
8682
+ \LMLabel{constantTypes}
8683
+
8684
+ \LMHash{}%
8685
+ A \Index{potentially constant type} respectively \Index{constant type}
8686
+ is a term derived from \synt{type} which is used as a type
8687
+ (\commentary{not as an expression that yields an instance of \code{Type}})
8688
+ that satisfy the following criteria:
8647
8689
\begin{itemize}
8648
8690
\item
8649
8691
Consider a type $T$
@@ -8652,70 +8694,105 @@ \subsection{Constants}
8652
8694
which is not qualified by a deferred prefix,
8653
8695
and which is optionally followed by
8654
8696
type arguments of the form \code{<\List{T}{1}{r}>}.
8655
- $T$ is a potentially constant type expression
8656
- respectively a constant type expression
8657
- if{}f $T_j$ is a potentially constant respectively constant type expression
8697
+ $T$ is a potentially constant type respectively a constant type
8698
+ if{}f $T_j$ is a potentially constant respectively constant type
8658
8699
for each $j \in 1 .. r$.
8659
8700
\item
8660
8701
A type of the form \code{FutureOr<$T$>}
8661
- is a potentially constant type expression
8662
- respectively a constant type expression
8663
- if{}f $T$ is a potentially constant type expression
8664
- respectively a constant type expression .
8702
+ is a potentially constant type
8703
+ respectively a constant type
8704
+ if{}f $T$ is a potentially constant type
8705
+ respectively a constant type.
8665
8706
\item
8666
8707
%% TODO(eernst): This does not allow for type variables introduced by
8667
- %% the type itself. `Function<X>(X)` could be a constant type expression ,
8708
+ %% the type itself. `Function<X>(X)` could be a constant type,
8668
8709
%% but that is not covered by the current rules: `X` is a type variable,
8669
- %% and they are never constant type expressions .
8710
+ %% and they are never constant type.
8670
8711
A function type
8671
8712
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
8672
8713
(where $R$ and \code{<\metavar{typeParameters}>} may be omitted)
8673
- is a is a potentially constant type expression
8674
- respectively a constant type expression
8714
+ is a is a potentially constant type
8715
+ respectively a constant type
8675
8716
if $R$, \metavar{typeParameters}, and \metavar{parameterTypes}
8676
- (if present) contain only potentially constant type expressions
8677
- respectively constant type expressions .
8717
+ (if present) contain only potentially constant types
8718
+ respectively constant types .
8678
8719
\item
8679
- The type \VOID{} is a potentially constant and a constant type expression .
8720
+ The type \VOID{} is a potentially constant and constant type.
8680
8721
\item
8681
- The type \DYNAMIC{} is a potentially constant and a constant type expression .
8722
+ The type \DYNAMIC{} is a potentially constant and constant type.
8682
8723
\item
8683
- A type variable is a potentially constant type expression .
8724
+ A type variable is a potentially constant type.
8684
8725
\end{itemize}
8685
8726
8686
- % Being potentially constant is entirely structural, not type based,
8687
- % but the program still has to satisfy strong-mode typing.
8688
8727
8689
- % Constant expressions (like "const Foo(42)") always evaluate to the
8690
- % same value, with at most one value per source location.
8691
- % Potentially constant expressions that are not constant only
8692
- % allow simple operations on basic types (num, String, bool, Null). These can
8693
- % be computed statically without running user code.
8728
+ \subsubsection{Type Variable Elimination in Constants}
8729
+ \LMLabel{typeVariableEliminationInConstants}
8694
8730
8695
8731
\LMHash{}%
8696
- It is a \Error{compile-time error} if an expression is required to be
8697
- a constant expression,
8698
- but its evaluation would throw an exception.
8699
- It is a \Error{compile-time error} if an assertion is evaluated as part of
8700
- a constant object expression evaluation,
8701
- and the assertion would throw an exception.
8732
+ \BlindDefineSymbol{e, e'}%
8733
+ Let $e$ be an expression that occurs in a constant context
8734
+ or whose first token is \CONST,
8735
+ and $e'$ be the expression yielded by type inference
8736
+ (\ref{typeInference})
8737
+ applied to $e$.
8738
+ In this case an additional transformation is applied:
8702
8739
8703
8740
\LMHash{}%
8704
- It is a \Error{compile-time error} if the value of a constant expression
8705
- depends on itself.
8741
+ Let \List{X}{1}{r} be the free type variables declared by
8742
+ a class, mixin, extension, or method that contains $e$,
8743
+ such that for each $j \in 1 .. r$, each $X_j$
8744
+ occurs in a list of actual type arguments
8745
+ which was added by type inference
8746
+ (\commentary{%
8747
+ so $X_j$ has one or more occurrences in $e'$,
8748
+ but not all of those occurrences exist in $e$%
8749
+ }).
8750
+ For each $j \in 1 .. r$,
8751
+ if a specific occurrence of $X_j$ in $e'$ was added by type inference,
8752
+ the outermost enclosing type argument which was added by type inference
8753
+ and which contains this occurrence
8754
+ is replaced by its least closure with respect to \List{X}{1}{r}
8755
+ (\ref{leastAndGreatestClosureOfTypes}).
8706
8756
8707
8757
\commentary{%
8708
- As an example, consider:%
8758
+ Note that a free type variable which is explicitly used
8759
+ as or in a type argument in a constant expression
8760
+ is still a compile-time error.
8761
+ For example:%
8709
8762
}
8710
8763
8711
8764
\begin{dartCode}
8712
- \CLASS{} CircularConsts \{
8713
- // \comment{Illegal program - mutually recursive compile-time constants}
8714
- \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8715
- \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8765
+ \CLASS\ G<X> \{
8766
+ \VOID\ foo() \{
8767
+ \CONST\ List<X> c = <X>[]; // \comment{Compile-time error.}
8768
+ \CONST\ List<X> d = []; // \comment{Infers \code{<X>[]}, then becomes \code{<Never>[]}.}
8769
+ \}
8716
8770
\}
8717
8771
\end{dartCode}
8718
8772
8773
+ \rationale{%
8774
+ The type variable is not a constant type
8775
+ (\ref{constantTypes}),
8776
+ which implies that the inferred expression $e'$ is a compile-time error.
8777
+ The additional transformation yields a subtype of the inferred type.
8778
+ It typically replaces the type variable by \code{Never},
8779
+ but it may also replace the type variable by a top type
8780
+ (if the type variable occurs contravariantly),
8781
+ or the type may be changed more radically
8782
+ (e.g., a generic function type using $X_j$ as a type parameter bound
8783
+ may be changed to \FUNCTION{} or \code{Never}).
8784
+ In any case, the chosen subtype often yields
8785
+ a constant which has no errors and is useful.%
8786
+ }
8787
+
8788
+ \commentary{%
8789
+ For example, \code{\CONST\,\,<Never>[]} is actually usable as
8790
+ a list of any type,
8791
+ and it is not a problem that every invocation of \code{add}
8792
+ on this list will fail (statically or dynamically),
8793
+ because that is already true for every constant list.%
8794
+ }
8795
+
8719
8796
8720
8797
\subsubsection{Further Remarks on Constants and Potential Constants}
8721
8798
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10456,8 +10533,8 @@ \subsubsection{Lists}
10456
10533
if an element of a constant list literal is not constant.
10457
10534
It is a \Error{compile-time error} if the type argument of a constant list literal
10458
10535
(\commentary{no matter whether it is explicit or inferred})
10459
- is not a constant type expression
10460
- (\ref{constants }).
10536
+ is not a constant type
10537
+ (\ref{constantTypes }).
10461
10538
10462
10539
\rationale{%
10463
10540
The binding of a formal type parameter of an enclosing class or function
@@ -11269,8 +11346,8 @@ \subsubsection{Sets}
11269
11346
(\ref{equality}).
11270
11347
It is a \Error{compile-time error} if the type argument of a constant set literal
11271
11348
(\commentary{no matter whether it is explicit or inferred})
11272
- is not a constant type expression
11273
- (\ref{constants }).
11349
+ is not a constant type
11350
+ (\ref{constantTypes }).
11274
11351
11275
11352
\rationale{%
11276
11353
The binding of a formal type parameter of an enclosing class or function
@@ -11474,8 +11551,8 @@ \subsubsection{Maps}
11474
11551
(\ref{equality}).
11475
11552
It is a \Error{compile-time error} if a type argument of a constant map literal
11476
11553
(\commentary{no matter whether it is explicit or inferred})
11477
- is not a constant type expression
11478
- (\ref{constants }).
11554
+ is not a constant type
11555
+ (\ref{constantTypes }).
11479
11556
11480
11557
\rationale{%
11481
11558
The binding of a formal type parameter of an enclosing class or function
@@ -12190,7 +12267,7 @@ \subsubsection{Const}
12190
12267
\LMHash{}%
12191
12268
If $T$ is a parameterized type,
12192
12269
it is a
12193
- \Error{compile-time error} if $U_j$ is not a constant type expression for any
12270
+ \Error{compile-time error} if $U_j$ is not a constant type for any
12194
12271
$j \in 1 .. m$.
12195
12272
12196
12273
\begin{itemize}
@@ -24542,28 +24619,11 @@ \section{Null safety} %% !!!TODO!!!
24542
24619
%% !!!At the end: Search Null, change to Never where appropriate
24543
24620
%% !!!Search all `TODO`.*null
24544
24621
24545
- \subsubsection{Const type variable elimination}
24546
- \LMLabel{}
24547
-
24548
- If performing inference on a constant value of a generic class results in
24549
- inferred type arguments to the generic class which contain free type variables
24550
- from an enclosing generic class or method, the free type variables shall be
24551
- eliminated by taking the least closure of the inferred type with respect to the
24552
- free type variables. Note that free type variables which are explicitly used as
24553
- type arguments in constant generic instances are still considered erroneous.
24554
-
24555
- \begin{dartCode}
24556
- \CLASS\ G<T> \{
24557
- \VOID\ foo() \{
24558
- \CONST\ List<T> c = <T>[]; // Error
24559
- \CONST\ List<T> d = []; // The list literal is inferred as <Never>[]
24560
- \}
24561
- \}
24562
- \end{dartCode}
24563
-
24564
24622
\subsubsection{Null promotion}
24565
24623
\LMLabel{}
24566
24624
24625
+ !!!
24626
+
24567
24627
The machinery of type promotion is extended to promote the type of
24568
24628
variables based on nullability checks subject to the same set of
24569
24629
restrictions as normal promotion. The relevant checks and the types
0 commit comments