Skip to content

Commit e98b731

Browse files
committed
Constant types; type variable elimination in constants
1 parent ab2f231 commit e98b731

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
@@ -8486,8 +8486,8 @@ \subsection{Constants}
84868486
If generic function instantiation does apply to $e$
84878487
and the provided actual type arguments are \List{T}{1}{s}
84888488
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}).
84918491
\item
84928492
An identifier expression denoting a parameter of a constant constructor
84938493
(\ref{constantConstructors})
@@ -8511,7 +8511,8 @@ \subsection{Constants}
85118511
\code{\CONST{} <$T$>[$e_1$, \ldots, $e_n$]}, or
85128512
\code{<$T$>[$e_1$, \ldots, $e_n$]}
85138513
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}),
85158516
and $e_1$, \ldots{} , $e_n$ are constant expressions.
85168517
It is further a constant expression
85178518
if the list literal evaluates to an object.
@@ -8521,7 +8522,8 @@ \subsection{Constants}
85218522
\code{<$T$>\{$e_1$, \ldots, $e_n$\}}
85228523
that occurs in a constant context,
85238524
is a potentially constant expression
8524-
if $T$ is a constant type expression,
8525+
if $T$ is a constant type
8526+
(\ref{constantTypes}),
85258527
and $e_1$, \ldots{} , $e_n$ are constant expressions.
85268528
It is further a constant expression
85278529
if the list literal evaluates to an object.
@@ -8662,7 +8664,8 @@ \subsection{Constants}
86628664
\item
86638665
An expression of the form \code{$e$\,\,\AS\,\,$T$} is potentially constant
86648666
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}),
86668669
and it is further constant if $e$ is constant.
86678670
\commentary{%
86688671
It is a \Error{compile-time error} to evaluate this constant expression
@@ -8672,18 +8675,57 @@ \subsection{Constants}
86728675
\item
86738676
An expression of the form \code{$e$\,\,\IS\,\,$T$} is potentially constant
86748677
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}),
86768680
and it is further constant if $e$ is constant.
86778681
\item
86788682
An expression of the form \code{$e$\,\,is!\,\,$T$}
86798683
is equivalent to \code{!($e$\,\,is\,\,$T$)} in every way,
86808684
including whether it's potentially constant or constant.
86818685
\end{itemize}
86828686

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+
86838696
\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:
86878729
\begin{itemize}
86888730
\item
86898731
Consider a type $T$
@@ -8692,70 +8734,105 @@ \subsection{Constants}
86928734
which is not qualified by a deferred prefix,
86938735
and which is optionally followed by
86948736
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
86988739
for each $j \in 1 .. r$.
86998740
\item
87008741
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.
87058746
\item
87068747
%% 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,
87088749
%% 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.
87108751
A function type
87118752
\code{$R$ Function<\metavar{typeParameters}>(\metavar{parameterTypes})}
87128753
(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
87158756
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.
87188759
\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.
87208761
\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.
87228763
\item
8723-
A type variable is a potentially constant type expression.
8764+
A type variable is a potentially constant type.
87248765
\end{itemize}
87258766

8726-
% Being potentially constant is entirely structural, not type based,
8727-
% but the program still has to satisfy strong-mode typing.
87288767

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

87358771
\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:
87428779

87438780
\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}).
87468796

87478797
\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:%
87498802
}
87508803

87518804
\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+
\}
87568810
\}
87578811
\end{dartCode}
87588812

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

87608837
\subsubsection{Further Remarks on Constants and Potential Constants}
87618838
\LMLabel{furtherCommentsOnConstantsAndPotentiallyConstants}
@@ -10512,8 +10589,8 @@ \subsubsection{Lists}
1051210589
if an element of a constant list literal is not constant.
1051310590
It is a \Error{compile-time error} if the type argument of a constant list literal
1051410591
(\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}).
1051710594

1051810595
\rationale{%
1051910596
The binding of a formal type parameter of an enclosing class or function
@@ -11325,8 +11402,8 @@ \subsubsection{Sets}
1132511402
(\ref{equality}).
1132611403
It is a \Error{compile-time error} if the type argument of a constant set literal
1132711404
(\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}).
1133011407

1133111408
\rationale{%
1133211409
The binding of a formal type parameter of an enclosing class or function
@@ -11530,8 +11607,8 @@ \subsubsection{Maps}
1153011607
(\ref{equality}).
1153111608
It is a \Error{compile-time error} if a type argument of a constant map literal
1153211609
(\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}).
1153511612

1153611613
\rationale{%
1153711614
The binding of a formal type parameter of an enclosing class or function
@@ -12275,7 +12352,7 @@ \subsubsection{Const}
1227512352
\LMHash{}%
1227612353
If $T$ is a parameterized type,
1227712354
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
1227912356
$j \in 1 .. m$.
1228012357

1228112358
\begin{itemize}
@@ -24671,28 +24748,11 @@ \section{Null safety} %% !!!TODO!!!
2467124748
%% !!!At the end: Search Null, change to Never where appropriate
2467224749
%% !!!Search all `TODO`.*null
2467324750

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-
2469324751
\subsubsection{Null promotion}
2469424752
\LMLabel{}
2469524753

24754+
!!!
24755+
2469624756
The machinery of type promotion is extended to promote the type of
2469724757
variables based on nullability checks subject to the same set of
2469824758
restrictions as normal promotion. The relevant checks and the types

0 commit comments

Comments
 (0)