@@ -8494,8 +8494,8 @@ \subsection{Constants}
8494
8494
If generic function instantiation does apply to $e$
8495
8495
and the provided actual type arguments are \List{T}{1}{s}
8496
8496
then $e$ is a potentially constant and constant expression
8497
- if{}f each $T_j, j \in 1 .. s$, is a constant type expression
8498
- (\ref{constants }).
8497
+ if{}f each $T_j, j \in 1 .. s$, is a constant type
8498
+ (\ref{constantTypes }).
8499
8499
\item
8500
8500
An identifier expression denoting a parameter of a constant constructor
8501
8501
(\ref{constantConstructors})
@@ -8519,7 +8519,8 @@ \subsection{Constants}
8519
8519
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
8520
8520
\code{<$T$>[$e_1$, \ldots, $e_n$]}
8521
8521
that occurs in a constant context,
8522
- is a potentially constant expression if $T$ is a constant type expression,
8522
+ is a potentially constant expression if $T$ is a constant type
8523
+ (\ref{constantTypes}),
8523
8524
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8524
8525
It is further a constant expression
8525
8526
if the list literal evaluates to an object.
@@ -8529,7 +8530,8 @@ \subsection{Constants}
8529
8530
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
8530
8531
that occurs in a constant context,
8531
8532
is a potentially constant expression
8532
- if $T$ is a constant type expression,
8533
+ if $T$ is a constant type
8534
+ (\ref{constantTypes}),
8533
8535
and $e_1$, \ldots{} , $e_n$ are constant expressions.
8534
8536
It is further a constant expression
8535
8537
if the list literal evaluates to an object.
@@ -8672,7 +8674,8 @@ \subsection{Constants}
8672
8674
\item
8673
8675
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
8674
8676
if $e$ is a potentially constant expression
8675
- and $T$ is a potentially constant type expression,
8677
+ and $T$ is a potentially constant type
8678
+ (\ref{constantTypes}),
8676
8679
and it is further constant if $e$ is constant.
8677
8680
\commentary{%
8678
8681
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8682,18 +8685,57 @@ \subsection{Constants}
8682
8685
\item
8683
8686
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
8684
8687
if $e$ is a potentially constant expression
8685
- and $T$ is a constant type expression,
8688
+ and $T$ is a constant type
8689
+ (\ref{constantTypes}),
8686
8690
and it is further constant if $e$ is constant.
8687
8691
\item
8688
8692
An expression of the form \code{$e$\,\,is!\,\,$T$}
8689
8693
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
8690
8694
including whether it's potentially constant or constant.
8691
8695
\end{itemize}
8692
8696
8697
+ % Being potentially constant is entirely structural, not type based,
8698
+ % but the program still has to satisfy strong-mode typing.
8699
+
8700
+ % Constant expressions (like "const Foo(42)") always evaluate to the
8701
+ % same value, with at most one value per source location.
8702
+ % Potentially constant expressions that are not constant only
8703
+ % allow simple operations on basic types (num, String, bool, Null). These can
8704
+ % be computed statically without running user code.
8705
+
8693
8706
\LMHash{}%
8694
- We introduce \Index{constant type expressions} and
8695
- \Index{potentially constant type expressions}
8696
- as follows:
8707
+ It is a \Error{compile-time error} if an expression is required to be
8708
+ a constant expression,
8709
+ but its evaluation would throw an exception.
8710
+ It is a \Error{compile-time error} if an assertion is evaluated as part of
8711
+ a constant object expression evaluation,
8712
+ and the assertion would throw an exception.
8713
+
8714
+ \LMHash{}%
8715
+ It is a \Error{compile-time error} if the value of a constant expression
8716
+ depends on itself.
8717
+
8718
+ \commentary{%
8719
+ As an example, consider:%
8720
+ }
8721
+
8722
+ \begin{dartCode}
8723
+ \CLASS{} CircularConsts \{
8724
+ // \comment{Illegal program - mutually recursive compile-time constants}
8725
+ \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8726
+ \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8727
+ \}
8728
+ \end{dartCode}
8729
+
8730
+
8731
+ \subsubsection{Constant Types}
8732
+ \LMLabel{constantTypes}
8733
+
8734
+ \LMHash{}%
8735
+ A \Index{potentially constant type} respectively \Index{constant type}
8736
+ is a term derived from \synt{type} which is used as a type
8737
+ (\commentary{not as an expression that yields an instance of \code{Type}})
8738
+ that satisfy the following criteria:
8697
8739
\begin{itemize}
8698
8740
\item
8699
8741
Consider a type $T$
@@ -8702,70 +8744,105 @@ \subsection{Constants}
8702
8744
which is not qualified by a deferred prefix,
8703
8745
and which is optionally followed by
8704
8746
type arguments of the form \code{<\List{T}{1}{r}>}.
8705
- $T$ is a potentially constant type expression
8706
- respectively a constant type expression
8707
- if{}f $T_j$ is a potentially constant respectively constant type expression
8747
+ $T$ is a potentially constant type respectively a constant type
8748
+ if{}f $T_j$ is a potentially constant respectively constant type
8708
8749
for each $j \in 1 .. r$.
8709
8750
\item
8710
8751
A type of the form \code{FutureOr<$T$>}
8711
- is a potentially constant type expression
8712
- respectively a constant type expression
8713
- if{}f $T$ is a potentially constant type expression
8714
- respectively a constant type expression .
8752
+ is a potentially constant type
8753
+ respectively a constant type
8754
+ if{}f $T$ is a potentially constant type
8755
+ respectively a constant type.
8715
8756
\item
8716
8757
%% TODO(eernst): This does not allow for type variables introduced by
8717
- %% the type itself. `Function<X>(X)` could be a constant type expression ,
8758
+ %% the type itself. `Function<X>(X)` could be a constant type,
8718
8759
%% but that is not covered by the current rules: `X` is a type variable,
8719
- %% and they are never constant type expressions .
8760
+ %% and they are never constant type.
8720
8761
A function type
8721
8762
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
8722
8763
(where $R$ and \code{<\metavar{typeParameters}>} may be omitted)
8723
- is a is a potentially constant type expression
8724
- respectively a constant type expression
8764
+ is a is a potentially constant type
8765
+ respectively a constant type
8725
8766
if $R$, \metavar{typeParameters}, and \metavar{parameterTypes}
8726
- (if present) contain only potentially constant type expressions
8727
- respectively constant type expressions .
8767
+ (if present) contain only potentially constant types
8768
+ respectively constant types .
8728
8769
\item
8729
- The type \VOID{} is a potentially constant and a constant type expression .
8770
+ The type \VOID{} is a potentially constant and constant type.
8730
8771
\item
8731
- The type \DYNAMIC{} is a potentially constant and a constant type expression .
8772
+ The type \DYNAMIC{} is a potentially constant and constant type.
8732
8773
\item
8733
- A type variable is a potentially constant type expression .
8774
+ A type variable is a potentially constant type.
8734
8775
\end{itemize}
8735
8776
8736
- % Being potentially constant is entirely structural, not type based,
8737
- % but the program still has to satisfy strong-mode typing.
8738
8777
8739
- % Constant expressions (like "const Foo(42)") always evaluate to the
8740
- % same value, with at most one value per source location.
8741
- % Potentially constant expressions that are not constant only
8742
- % allow simple operations on basic types (num, String, bool, Null). These can
8743
- % be computed statically without running user code.
8778
+ \subsubsection{Type Variable Elimination in Constants}
8779
+ \LMLabel{typeVariableEliminationInConstants}
8744
8780
8745
8781
\LMHash{}%
8746
- It is a \Error{compile-time error} if an expression is required to be
8747
- a constant expression,
8748
- but its evaluation would throw an exception.
8749
- It is a \Error{compile-time error} if an assertion is evaluated as part of
8750
- a constant object expression evaluation,
8751
- and the assertion would throw an exception.
8782
+ \BlindDefineSymbol{e, e'}%
8783
+ Let $e$ be an expression that occurs in a constant context
8784
+ or whose first token is \CONST,
8785
+ and $e'$ be the expression yielded by type inference
8786
+ (\ref{typeInference})
8787
+ applied to $e$.
8788
+ In this case an additional transformation is applied:
8752
8789
8753
8790
\LMHash{}%
8754
- It is a \Error{compile-time error} if the value of a constant expression
8755
- depends on itself.
8791
+ Let \List{X}{1}{r} be the free type variables declared by
8792
+ a class, mixin, extension, or method that contains $e$,
8793
+ such that for each $j \in 1 .. r$, each $X_j$
8794
+ occurs in a list of actual type arguments
8795
+ which was added by type inference
8796
+ (\commentary{%
8797
+ so $X_j$ has one or more occurrences in $e'$,
8798
+ but not all of those occurrences exist in $e$%
8799
+ }).
8800
+ For each $j \in 1 .. r$,
8801
+ if a specific occurrence of $X_j$ in $e'$ was added by type inference,
8802
+ the outermost enclosing type argument which was added by type inference
8803
+ and which contains this occurrence
8804
+ is replaced by its least closure with respect to \List{X}{1}{r}
8805
+ (\ref{leastAndGreatestClosureOfTypes}).
8756
8806
8757
8807
\commentary{%
8758
- As an example, consider:%
8808
+ Note that a free type variable which is explicitly used
8809
+ as or in a type argument in a constant expression
8810
+ is still a compile-time error.
8811
+ For example:%
8759
8812
}
8760
8813
8761
8814
\begin{dartCode}
8762
- \CLASS{} CircularConsts \{
8763
- // \comment{Illegal program - mutually recursive compile-time constants}
8764
- \STATIC{} \CONST{} i = j; // \comment{a compile-time constant}
8765
- \STATIC{} \CONST{} j = i; // \comment{a compile-time constant}
8815
+ \CLASS\ G<X> \{
8816
+ \VOID\ foo() \{
8817
+ \CONST\ List<X> c = <X>[]; // \comment{Compile-time error.}
8818
+ \CONST\ List<X> d = []; // \comment{Infers \code{<X>[]}, then becomes \code{<Never>[]}.}
8819
+ \}
8766
8820
\}
8767
8821
\end{dartCode}
8768
8822
8823
+ \rationale{%
8824
+ The type variable is not a constant type
8825
+ (\ref{constantTypes}),
8826
+ which implies that the inferred expression $e'$ is a compile-time error.
8827
+ The additional transformation yields a subtype of the inferred type.
8828
+ It typically replaces the type variable by \code{Never},
8829
+ but it may also replace the type variable by a top type
8830
+ (if the type variable occurs contravariantly),
8831
+ or the type may be changed more radically
8832
+ (e.g., a generic function type using $X_j$ as a type parameter bound
8833
+ may be changed to \FUNCTION{} or \code{Never}).
8834
+ In any case, the chosen subtype often yields
8835
+ a constant which has no errors and is useful.%
8836
+ }
8837
+
8838
+ \commentary{%
8839
+ For example, \code{\CONST\,\,<Never>[]} is actually usable as
8840
+ a list of any type,
8841
+ and it is not a problem that every invocation of \code{add}
8842
+ on this list will fail (statically or dynamically),
8843
+ because that is already true for every constant list.%
8844
+ }
8845
+
8769
8846
8770
8847
\subsubsection{Further Remarks on Constants and Potential Constants}
8771
8848
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10526,8 +10603,8 @@ \subsubsection{Lists}
10526
10603
if an element of a constant list literal is not constant.
10527
10604
It is a \Error{compile-time error} if the type argument of a constant list literal
10528
10605
(\commentary{no matter whether it is explicit or inferred})
10529
- is not a constant type expression
10530
- (\ref{constants }).
10606
+ is not a constant type
10607
+ (\ref{constantTypes }).
10531
10608
10532
10609
\rationale{%
10533
10610
The binding of a formal type parameter of an enclosing class or function
@@ -11339,8 +11416,8 @@ \subsubsection{Sets}
11339
11416
(\ref{equality}).
11340
11417
It is a \Error{compile-time error} if the type argument of a constant set literal
11341
11418
(\commentary{no matter whether it is explicit or inferred})
11342
- is not a constant type expression
11343
- (\ref{constants }).
11419
+ is not a constant type
11420
+ (\ref{constantTypes }).
11344
11421
11345
11422
\rationale{%
11346
11423
The binding of a formal type parameter of an enclosing class or function
@@ -11544,8 +11621,8 @@ \subsubsection{Maps}
11544
11621
(\ref{equality}).
11545
11622
It is a \Error{compile-time error} if a type argument of a constant map literal
11546
11623
(\commentary{no matter whether it is explicit or inferred})
11547
- is not a constant type expression
11548
- (\ref{constants }).
11624
+ is not a constant type
11625
+ (\ref{constantTypes }).
11549
11626
11550
11627
\rationale{%
11551
11628
The binding of a formal type parameter of an enclosing class or function
@@ -12289,7 +12366,7 @@ \subsubsection{Const}
12289
12366
\LMHash{}%
12290
12367
If $T$ is a parameterized type,
12291
12368
it is a
12292
- \Error{compile-time error} if $U_j$ is not a constant type expression for any
12369
+ \Error{compile-time error} if $U_j$ is not a constant type for any
12293
12370
$j \in 1 .. m$.
12294
12371
12295
12372
\begin{itemize}
@@ -24810,28 +24887,11 @@ \section{Null safety} %% !!!TODO!!!
24810
24887
%% !!!At the end: Search Null, change to Never where appropriate
24811
24888
%% !!!Search all `TODO`.*null
24812
24889
24813
- \subsubsection{Const type variable elimination}
24814
- \LMLabel{}
24815
-
24816
- If performing inference on a constant value of a generic class results in
24817
- inferred type arguments to the generic class which contain free type variables
24818
- from an enclosing generic class or method, the free type variables shall be
24819
- eliminated by taking the least closure of the inferred type with respect to the
24820
- free type variables. Note that free type variables which are explicitly used as
24821
- type arguments in constant generic instances are still considered erroneous.
24822
-
24823
- \begin{dartCode}
24824
- \CLASS\ G<T> \{
24825
- \VOID\ foo() \{
24826
- \CONST\ List<T> c = <T>[]; // Error
24827
- \CONST\ List<T> d = []; // The list literal is inferred as <Never>[]
24828
- \}
24829
- \}
24830
- \end{dartCode}
24831
-
24832
24890
\subsubsection{Null promotion}
24833
24891
\LMLabel{}
24834
24892
24893
+ !!!
24894
+
24835
24895
The machinery of type promotion is extended to promote the type of
24836
24896
variables based on nullability checks subject to the same set of
24837
24897
restrictions as normal promotion. The relevant checks and the types
0 commit comments