@@ -8486,8 +8486,8 @@ \subsection{Constants}
8486
8486
If generic function instantiation does apply to $e$
8487
8487
and the provided actual type arguments are \List{T}{1}{s}
8488
8488
then $e$ is a potentially constant and constant expression
8489
- if{}f each $T_j, j \in 1 .. s$, is a constant type expression
8490
- (\ref{constants }).
8489
+ if{}f each $T_j, j \in 1 .. s$, is a constant type
8490
+ (\ref{constantTypes }).
8491
8491
\item
8492
8492
An identifier expression denoting a parameter of a constant constructor
8493
8493
(\ref{constantConstructors})
@@ -8511,7 +8511,8 @@ \subsection{Constants}
8511
8511
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
8512
8512
\code{<$T$>[$e_1$, \ldots, $e_n$]}
8513
8513
that occurs in a constant context,
8514
- is a potentially constant expression if $T$ is a constant type expression,
8514
+ is a potentially constant expression if $T$ is a constant type
8515
+ (\ref{constantTypes}),
8515
8516
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8516
8517
It is further a constant expression
8517
8518
if the list literal evaluates to an object.
@@ -8521,7 +8522,8 @@ \subsection{Constants}
8521
8522
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
8522
8523
that occurs in a constant context,
8523
8524
is a potentially constant expression
8524
- if $T$ is a constant type expression,
8525
+ if $T$ is a constant type
8526
+ (\ref{constantTypes}),
8525
8527
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8526
8528
It is further a constant expression
8527
8529
if the list literal evaluates to an object.
@@ -8662,7 +8664,8 @@ \subsection{Constants}
8662
8664
\item
8663
8665
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
8664
8666
if $e$ is a potentially constant expression
8665
- and $T$ is a potentially constant type expression,
8667
+ and $T$ is a potentially constant type
8668
+ (\ref{constantTypes}),
8666
8669
and it is further constant if $e$ is constant.
8667
8670
\commentary{%
8668
8671
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8672,18 +8675,57 @@ \subsection{Constants}
8672
8675
\item
8673
8676
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
8674
8677
if $e$ is a potentially constant expression
8675
- and $T$ is a constant type expression,
8678
+ and $T$ is a constant type
8679
+ (\ref{constantTypes}),
8676
8680
and it is further constant if $e$ is constant.
8677
8681
\item
8678
8682
An expression of the form \code{$e$\,\,is!\,\,$T$}
8679
8683
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
8680
8684
including whether it's potentially constant or constant.
8681
8685
\end{itemize}
8682
8686
8687
+ % Being potentially constant is entirely structural, not type based,
8688
+ % but the program still has to satisfy strong-mode typing.
8689
+
8690
+ % Constant expressions (like "const Foo(42)") always evaluate to the
8691
+ % same value, with at most one value per source location.
8692
+ % Potentially constant expressions that are not constant only
8693
+ % allow simple operations on basic types (num, String, bool, Null). These can
8694
+ % be computed statically without running user code.
8695
+
8683
8696
\LMHash{}%
8684
- We introduce \Index{constant type expressions} and
8685
- \Index{potentially constant type expressions}
8686
- as follows:
8697
+ It is a \Error{compile-time error} if an expression is required to be
8698
+ a constant expression,
8699
+ but its evaluation would throw an exception.
8700
+ It is a \Error{compile-time error} if an assertion is evaluated as part of
8701
+ a constant object expression evaluation,
8702
+ and the assertion would throw an exception.
8703
+
8704
+ \LMHash{}%
8705
+ It is a \Error{compile-time error} if the value of a constant expression
8706
+ depends on itself.
8707
+
8708
+ \commentary{%
8709
+ As an example, consider:%
8710
+ }
8711
+
8712
+ \begin{dartCode}
8713
+ \CLASS{} CircularConsts \{
8714
+ // \comment{Illegal program - mutually recursive compile-time constants}
8715
+ \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8716
+ \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8717
+ \}
8718
+ \end{dartCode}
8719
+
8720
+
8721
+ \subsubsection{Constant Types}
8722
+ \LMLabel{constantTypes}
8723
+
8724
+ \LMHash{}%
8725
+ A \Index{potentially constant type} respectively \Index{constant type}
8726
+ is a term derived from \synt{type} which is used as a type
8727
+ (\commentary{not as an expression that yields an instance of \code{Type}})
8728
+ that satisfy the following criteria:
8687
8729
\begin{itemize}
8688
8730
\item
8689
8731
Consider a type $T$
@@ -8692,70 +8734,105 @@ \subsection{Constants}
8692
8734
which is not qualified by a deferred prefix,
8693
8735
and which is optionally followed by
8694
8736
type arguments of the form \code{<\List{T}{1}{r}>}.
8695
- $T$ is a potentially constant type expression
8696
- respectively a constant type expression
8697
- if{}f $T_j$ is a potentially constant respectively constant type expression
8737
+ $T$ is a potentially constant type respectively a constant type
8738
+ if{}f $T_j$ is a potentially constant respectively constant type
8698
8739
for each $j \in 1 .. r$.
8699
8740
\item
8700
8741
A type of the form \code{FutureOr<$T$>}
8701
- is a potentially constant type expression
8702
- respectively a constant type expression
8703
- if{}f $T$ is a potentially constant type expression
8704
- respectively a constant type expression .
8742
+ is a potentially constant type
8743
+ respectively a constant type
8744
+ if{}f $T$ is a potentially constant type
8745
+ respectively a constant type.
8705
8746
\item
8706
8747
%% TODO(eernst): This does not allow for type variables introduced by
8707
- %% the type itself. `Function<X>(X)` could be a constant type expression ,
8748
+ %% the type itself. `Function<X>(X)` could be a constant type,
8708
8749
%% but that is not covered by the current rules: `X` is a type variable,
8709
- %% and they are never constant type expressions .
8750
+ %% and they are never constant type.
8710
8751
A function type
8711
8752
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
8712
8753
(where $R$ and \code{<\metavar{typeParameters}>} may be omitted)
8713
- is a is a potentially constant type expression
8714
- respectively a constant type expression
8754
+ is a is a potentially constant type
8755
+ respectively a constant type
8715
8756
if $R$, \metavar{typeParameters}, and \metavar{parameterTypes}
8716
- (if present) contain only potentially constant type expressions
8717
- respectively constant type expressions .
8757
+ (if present) contain only potentially constant types
8758
+ respectively constant types .
8718
8759
\item
8719
- The type \VOID{} is a potentially constant and a constant type expression .
8760
+ The type \VOID{} is a potentially constant and constant type.
8720
8761
\item
8721
- The type \DYNAMIC{} is a potentially constant and a constant type expression .
8762
+ The type \DYNAMIC{} is a potentially constant and constant type.
8722
8763
\item
8723
- A type variable is a potentially constant type expression .
8764
+ A type variable is a potentially constant type.
8724
8765
\end{itemize}
8725
8766
8726
- % Being potentially constant is entirely structural, not type based,
8727
- % but the program still has to satisfy strong-mode typing.
8728
8767
8729
- % Constant expressions (like "const Foo(42)") always evaluate to the
8730
- % same value, with at most one value per source location.
8731
- % Potentially constant expressions that are not constant only
8732
- % allow simple operations on basic types (num, String, bool, Null). These can
8733
- % be computed statically without running user code.
8768
+ \subsubsection{Type Variable Elimination in Constants}
8769
+ \LMLabel{typeVariableEliminationInConstants}
8734
8770
8735
8771
\LMHash{}%
8736
- It is a \Error{compile-time error} if an expression is required to be
8737
- a constant expression,
8738
- but its evaluation would throw an exception.
8739
- It is a \Error{compile-time error} if an assertion is evaluated as part of
8740
- a constant object expression evaluation,
8741
- and the assertion would throw an exception.
8772
+ \BlindDefineSymbol{e, e'}%
8773
+ Let $e$ be an expression that occurs in a constant context
8774
+ or whose first token is \CONST,
8775
+ and $e'$ be the expression yielded by type inference
8776
+ (\ref{typeInference})
8777
+ applied to $e$.
8778
+ In this case an additional transformation is applied:
8742
8779
8743
8780
\LMHash{}%
8744
- It is a \Error{compile-time error} if the value of a constant expression
8745
- depends on itself.
8781
+ Let \List{X}{1}{r} be the free type variables declared by
8782
+ a class, mixin, extension, or method that contains $e$,
8783
+ such that for each $j \in 1 .. r$, each $X_j$
8784
+ occurs in a list of actual type arguments
8785
+ which was added by type inference
8786
+ (\commentary{%
8787
+ so $X_j$ has one or more occurrences in $e'$,
8788
+ but not all of those occurrences exist in $e$%
8789
+ }).
8790
+ For each $j \in 1 .. r$,
8791
+ if a specific occurrence of $X_j$ in $e'$ was added by type inference,
8792
+ the outermost enclosing type argument which was added by type inference
8793
+ and which contains this occurrence
8794
+ is replaced by its least closure with respect to \List{X}{1}{r}
8795
+ (\ref{leastAndGreatestClosureOfTypes}).
8746
8796
8747
8797
\commentary{%
8748
- As an example, consider:%
8798
+ Note that a free type variable which is explicitly used
8799
+ as or in a type argument in a constant expression
8800
+ is still a compile-time error.
8801
+ For example:%
8749
8802
}
8750
8803
8751
8804
\begin{dartCode}
8752
- \CLASS{} CircularConsts \{
8753
- // \comment{Illegal program - mutually recursive compile-time constants}
8754
- \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8755
- \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8805
+ \CLASS\ G<X> \{
8806
+ \VOID\ foo() \{
8807
+ \CONST\ List<X> c = <X>[]; // \comment{Compile-time error.}
8808
+ \CONST\ List<X> d = []; // \comment{Infers \code{<X>[]}, then becomes \code{<Never>[]}.}
8809
+ \}
8756
8810
\}
8757
8811
\end{dartCode}
8758
8812
8813
+ \rationale{%
8814
+ The type variable is not a constant type
8815
+ (\ref{constantTypes}),
8816
+ which implies that the inferred expression $e'$ is a compile-time error.
8817
+ The additional transformation yields a subtype of the inferred type.
8818
+ It typically replaces the type variable by \code{Never},
8819
+ but it may also replace the type variable by a top type
8820
+ (if the type variable occurs contravariantly),
8821
+ or the type may be changed more radically
8822
+ (e.g., a generic function type using $X_j$ as a type parameter bound
8823
+ may be changed to \FUNCTION{} or \code{Never}).
8824
+ In any case, the chosen subtype often yields
8825
+ a constant which has no errors and is useful.%
8826
+ }
8827
+
8828
+ \commentary{%
8829
+ For example, \code{\CONST\,\,<Never>[]} is actually usable as
8830
+ a list of any type,
8831
+ and it is not a problem that every invocation of \code{add}
8832
+ on this list will fail (statically or dynamically),
8833
+ because that is already true for every constant list.%
8834
+ }
8835
+
8759
8836
8760
8837
\subsubsection{Further Remarks on Constants and Potential Constants}
8761
8838
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10512,8 +10589,8 @@ \subsubsection{Lists}
10512
10589
if an element of a constant list literal is not constant.
10513
10590
It is a \Error{compile-time error} if the type argument of a constant list literal
10514
10591
(\commentary{no matter whether it is explicit or inferred})
10515
- is not a constant type expression
10516
- (\ref{constants }).
10592
+ is not a constant type
10593
+ (\ref{constantTypes }).
10517
10594
10518
10595
\rationale{%
10519
10596
The binding of a formal type parameter of an enclosing class or function
@@ -11325,8 +11402,8 @@ \subsubsection{Sets}
11325
11402
(\ref{equality}).
11326
11403
It is a \Error{compile-time error} if the type argument of a constant set literal
11327
11404
(\commentary{no matter whether it is explicit or inferred})
11328
- is not a constant type expression
11329
- (\ref{constants }).
11405
+ is not a constant type
11406
+ (\ref{constantTypes }).
11330
11407
11331
11408
\rationale{%
11332
11409
The binding of a formal type parameter of an enclosing class or function
@@ -11530,8 +11607,8 @@ \subsubsection{Maps}
11530
11607
(\ref{equality}).
11531
11608
It is a \Error{compile-time error} if a type argument of a constant map literal
11532
11609
(\commentary{no matter whether it is explicit or inferred})
11533
- is not a constant type expression
11534
- (\ref{constants }).
11610
+ is not a constant type
11611
+ (\ref{constantTypes }).
11535
11612
11536
11613
\rationale{%
11537
11614
The binding of a formal type parameter of an enclosing class or function
@@ -12275,7 +12352,7 @@ \subsubsection{Const}
12275
12352
\LMHash{}%
12276
12353
If $T$ is a parameterized type,
12277
12354
it is a
12278
- \Error{compile-time error} if $U_j$ is not a constant type expression for any
12355
+ \Error{compile-time error} if $U_j$ is not a constant type for any
12279
12356
$j \in 1 .. m$.
12280
12357
12281
12358
\begin{itemize}
@@ -24671,28 +24748,11 @@ \section{Null safety} %% !!!TODO!!!
24671
24748
%% !!!At the end: Search Null, change to Never where appropriate
24672
24749
%% !!!Search all `TODO`.*null
24673
24750
24674
- \subsubsection{Const type variable elimination}
24675
- \LMLabel{}
24676
-
24677
- If performing inference on a constant value of a generic class results in
24678
- inferred type arguments to the generic class which contain free type variables
24679
- from an enclosing generic class or method, the free type variables shall be
24680
- eliminated by taking the least closure of the inferred type with respect to the
24681
- free type variables. Note that free type variables which are explicitly used as
24682
- type arguments in constant generic instances are still considered erroneous.
24683
-
24684
- \begin{dartCode}
24685
- \CLASS\ G<T> \{
24686
- \VOID\ foo() \{
24687
- \CONST\ List<T> c = <T>[]; // Error
24688
- \CONST\ List<T> d = []; // The list literal is inferred as <Never>[]
24689
- \}
24690
- \}
24691
- \end{dartCode}
24692
-
24693
24751
\subsubsection{Null promotion}
24694
24752
\LMLabel{}
24695
24753
24754
+ !!!
24755
+
24696
24756
The machinery of type promotion is extended to promote the type of
24697
24757
variables based on nullability checks subject to the same set of
24698
24758
restrictions as normal promotion. The relevant checks and the types
0 commit comments