Skip to content

Commit aa7e45a

Browse files
committed
Constant types; type variable elimination in constants
1 parent 81c9eb1 commit aa7e45a

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
@@ -8446,8 +8446,8 @@ \subsection{Constants}
84468446
If generic function instantiation does apply to $e$
84478447
and the provided actual type arguments are \List{T}{1}{s}
84488448
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}).
84518451
\item
84528452
An identifier expression denoting a parameter of a constant constructor
84538453
(\ref{constantConstructors})
@@ -8471,7 +8471,8 @@ \subsection{Constants}
84718471
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
84728472
\code{<$T$>[$e_1$, \ldots, $e_n$]}
84738473
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}),
84758476
and $e_1$, \ldots{} , $e_n$ are constant expressions.
84768477
It is further a constant expression
84778478
if the list literal evaluates to an object.
@@ -8481,7 +8482,8 @@ \subsection{Constants}
84818482
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
84828483
that occurs in a constant context,
84838484
is a potentially constant expression
8484-
if $T$ is a constant type expression,
8485+
if $T$ is a constant type
8486+
(\ref{constantTypes}),
84858487
and $e_1$, \ldots{} , $e_n$ are constant expressions.
84868488
It is further a constant expression
84878489
if the list literal evaluates to an object.
@@ -8622,7 +8624,8 @@ \subsection{Constants}
86228624
\item
86238625
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
86248626
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}),
86268629
and it is further constant if $e$ is constant.
86278630
\commentary{%
86288631
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8632,18 +8635,57 @@ \subsection{Constants}
86328635
\item
86338636
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
86348637
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}),
86368640
and it is further constant if $e$ is constant.
86378641
\item
86388642
An expression of the form \code{$e$\,\,is!\,\,$T$}
86398643
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
86408644
including whether it's potentially constant or constant.
86418645
\end{itemize}
86428646

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+
86438656
\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:
86478689
\begin{itemize}
86488690
\item
86498691
Consider a type $T$
@@ -8652,70 +8694,105 @@ \subsection{Constants}
86528694
which is not qualified by a deferred prefix,
86538695
and which is optionally followed by
86548696
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
86588699
for each $j \in 1 .. r$.
86598700
\item
86608701
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.
86658706
\item
86668707
%% 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,
86688709
%% 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.
86708711
A function type
86718712
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
86728713
(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
86758716
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.
86788719
\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.
86808721
\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.
86828723
\item
8683-
A type variable is a potentially constant type expression.
8724+
A type variable is a potentially constant type.
86848725
\end{itemize}
86858726

8686-
% Being potentially constant is entirely structural, not type based,
8687-
% but the program still has to satisfy strong-mode typing.
86888727

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}
86948730

86958731
\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:
87028739

87038740
\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}).
87068756

87078757
\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:%
87098762
}
87108763

87118764
\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+
\}
87168770
\}
87178771
\end{dartCode}
87188772

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+
87198796

87208797
\subsubsection{Further Remarks on Constants and Potential Constants}
87218798
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10456,8 +10533,8 @@ \subsubsection{Lists}
1045610533
if an element of a constant list literal is not constant.
1045710534
It is a \Error{compile-time error} if the type argument of a constant list literal
1045810535
(\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}).
1046110538

1046210539
\rationale{%
1046310540
The binding of a formal type parameter of an enclosing class or function
@@ -11269,8 +11346,8 @@ \subsubsection{Sets}
1126911346
(\ref{equality}).
1127011347
It is a \Error{compile-time error} if the type argument of a constant set literal
1127111348
(\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}).
1127411351

1127511352
\rationale{%
1127611353
The binding of a formal type parameter of an enclosing class or function
@@ -11474,8 +11551,8 @@ \subsubsection{Maps}
1147411551
(\ref{equality}).
1147511552
It is a \Error{compile-time error} if a type argument of a constant map literal
1147611553
(\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}).
1147911556

1148011557
\rationale{%
1148111558
The binding of a formal type parameter of an enclosing class or function
@@ -12190,7 +12267,7 @@ \subsubsection{Const}
1219012267
\LMHash{}%
1219112268
If $T$ is a parameterized type,
1219212269
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
1219412271
$j \in 1 .. m$.
1219512272

1219612273
\begin{itemize}
@@ -24542,28 +24619,11 @@ \section{Null safety} %% !!!TODO!!!
2454224619
%% !!!At the end: Search Null, change to Never where appropriate
2454324620
%% !!!Search all `TODO`.*null
2454424621

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-
2456424622
\subsubsection{Null promotion}
2456524623
\LMLabel{}
2456624624

24625+
!!!
24626+
2456724627
The machinery of type promotion is extended to promote the type of
2456824628
variables based on nullability checks subject to the same set of
2456924629
restrictions as normal promotion. The relevant checks and the types

0 commit comments

Comments
 (0)