Skip to content

Commit bec447f

Browse files
committed
Update section Local Variable Declaration, update \Gamma to \Delta
1 parent 251ddb0 commit bec447f

File tree

1 file changed

+148
-56
lines changed

1 file changed

+148
-56
lines changed

specification/dartLangSpec.tex

+148-56
Original file line numberDiff line numberDiff line change
@@ -17316,6 +17316,7 @@ \subsection{Local Variable Declaration}
1731617316
apply to local variables with the same definitions as for other variables
1731717317
(\ref{variables}).
1731817318

17319+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1731917320
\LMHash{}%
1732017321
We say that a local variable $v$ is \Index{potentially mutated}
1732117322
in some scope $s$
@@ -17324,41 +17325,108 @@ \subsection{Local Variable Declaration}
1732417325
\LMHash{}%
1732517326
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1732617327
\code{\VAR{} $v$ = \NULL;}.
17327-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17328-
\code{$T$ $v$ = \NULL;}.
17329-
17330-
\commentary{%
17331-
This holds regardless of the type $T$.
17332-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17333-
}
17328+
If $T$ is a nullable type
17329+
(\ref{typeNullability})
17330+
then a local variable declaration of the form \code{$T$ $v$;}
17331+
is equivalent to \code{$T$ $v$ = \NULL;}.
17332+
17333+
%% TODO(eernst): Revise when flow analysis is added.
17334+
\commentary{%
17335+
If $T$ is a potentially non-nullable type
17336+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17337+
but an expression that gives rise to evaluation of $v$
17338+
is a compile-time error unless flow analysis
17339+
(\ref{flowAnalysis})
17340+
shows that the variable is guaranteed to have been initialized.%
17341+
}
17342+
17343+
\LMHash{}%
17344+
A local variable has an associated
17345+
\IndexCustom{declared type}{local variable!declared type}
17346+
which is determined from its declaration.
17347+
A local variable also has an associated
17348+
\IndexCustom{type}{local variable!type}
17349+
which is determined by flow analysis
17350+
(\ref{flowAnalysis})
17351+
via a process known as type promotion
17352+
(\ref{typePromotion}).
1733417353

1733517354
\LMHash{}%
17336-
The type of a local variable with a declaration of one of the forms
17337-
\code{$T$ $v$ = $e$;}
17338-
\code{\CONST{} $T$ $v$ = $e$;}
17339-
\code{\FINAL{} $T$ $v$ = $e$;}
17355+
The declared type of a local variable with a declaration of one of the forms
17356+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17357+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17358+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1734017359
is $T$.
17341-
The type of a local variable with a declaration of one of the forms
17342-
\code{\VAR{} $v$ = $e$;}
17343-
\code{\CONST{} $v$ = $e$;}
17344-
\code{\FINAL{} $v$ = $e$;}
17345-
is \DYNAMIC.
17360+
17361+
\LMHash{}%
17362+
The declared type of a local variable with a declaration of one of the forms
17363+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17364+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17365+
\code{\CONST\,\,$v$ = $e$;}
17366+
is determined as follows:
17367+
17368+
\begin{itemize}
17369+
\item
17370+
If the static type of $e$ is \code{Null} then
17371+
the declared type of $v$ is \DYNAMIC.
17372+
\item
17373+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17374+
where $X$ is a type variable
17375+
(\ref{intersectionTypes}),
17376+
the declared type of $v$ is \code{X}.
17377+
\commentary{%
17378+
In this case $v$ is immediately promoted
17379+
(\ref{typePromotion}).%
17380+
}
17381+
\item
17382+
Otherwise, the declared type of $v$ is the static type of $e$.
17383+
\end{itemize}
1734617384

1734717385
\LMHash{}%
1734817386
Let $v$ be a local variable declared by an initializing variable declaration,
1734917387
and let $e$ be the associated initializing expression.
17350-
It is a compile-time error if the static type of $e$ is not assignable to
17351-
the type of $v$.
17352-
It is a compile-time error if a local variable $v$ is final,
17353-
and the declaration of $v$ is not an initializing variable declaration.
17388+
It is a
17389+
\Error{compile-time error} if the static type of $e$ is not assignable to
17390+
the declared type of $v$.
1735417391

17392+
%% TODO(eernst): Revise when flow analysis is added.
1735517393
\commentary{%
17356-
It is also a compile-time error to assign to a final local variable
17394+
If a local variable $v$ is \FINAL{} and not \LATE,
17395+
it is not a compile-time error if the declaration of $v$ is
17396+
not an initializing variable declaration,
17397+
but an expression that gives rise to evaluation of $v$
17398+
is a compile-time error unless flow analysis shows that
17399+
the variable is guaranteed to have been initialized.
17400+
Similarly, an expression that gives rise to an assignment to $v$
17401+
is a compile-time error unless flow analysis shows that
17402+
it is guaranteed that the variable has \emph{not} been initialized.%
17403+
}
17404+
17405+
\commentary{%
17406+
It is a compile-time error to assign to a local variable
17407+
which is \FINAL{} and not \LATE{}
1735717408
(\ref{assignment}).%
1735817409
}
1735917410

1736017411
\LMHash{}%
17361-
It is a compile-time error if
17412+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17413+
that declares a variable $v$,
17414+
which has an initializing expression $e$.
17415+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17416+
(\ref{awaitExpressions}),
17417+
unless there is a function $f$ which is
17418+
the immediately enclosing function for $a$,
17419+
and $f$ is not the immediately enclosing function for $D$.
17420+
17421+
\commentary{%
17422+
In other words,
17423+
the initializing expression cannot await an expression directly,
17424+
any await expressions must be nested inside some other function,
17425+
that is, a function literal.%
17426+
}
17427+
17428+
\LMHash{}%
17429+
It is a \Error{compile-time error} if
1736217430
a local variable is referenced at a source code location that is before
1736317431
the end of its initializing expression, if any,
1736417432
and otherwise before the declaring occurrence of
@@ -17446,15 +17514,39 @@ \subsection{Local Variable Declaration}
1744617514

1744717515
\LMHash{}%
1744817516
The expression $e$ is evaluated to an object $o$.
17449-
Then, the variable $v$ is set to $o$.
17450-
% This error can occur due to implicit casts.
17517+
% The following error can occur due to implicit casts.
1745117518
A dynamic type error occurs
17452-
if the dynamic type of $o$ is not a subtype of the actual type
17519+
if the dynamic type of $o$ is not a subtype of the actual declared type
1745317520
(\ref{actualTypes})
1745417521
of $v$.
17522+
Otherwise, the variable $v$ is bound to $o$.
1745517523

17456-
% The local variable discussion does not mention how to read/write locals.
17457-
% Consider spelling this out, in terms of storage.
17524+
\LMHash{}%
17525+
Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17526+
that declares a variable $v$.
17527+
If an object $o$ is assigned to $v$
17528+
in a situation where $v$ is unbound
17529+
then $v$ is bound to $o$.
17530+
If an object $o$ is assigned to $v$
17531+
in a situation where $v$ is bound to an object $o'$
17532+
then a dynamic error occurs
17533+
(\commentary{it does not matter whether $o$ is the same object as $o'$}).
17534+
17535+
\commentary{%
17536+
Note that this includes the implicit initializing writes induced by
17537+
evaluating the variable.
17538+
Hence, the following program encounters a dynamic error
17539+
when it evaluates \code{x},
17540+
just before it would call \code{print}.%
17541+
}
17542+
17543+
\begin{dartCode}
17544+
\VOID\ main() \{
17545+
int i = 0;
17546+
\LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17547+
print(x);
17548+
\}
17549+
\end{dartCode}
1745817550

1745917551

1746017552
\subsection{Local Function Declaration}
@@ -20872,7 +20964,7 @@ \subsection{Subtypes}
2087220964
may find the meaning of the given notation obvious,
2087320965
but we still need to clarify a few details about how to handle
2087420966
syntactically different denotations of the same type,
20875-
and how to choose the right initial environment, $\Gamma$.
20967+
and how to choose the right initial environment, $\Delta$.
2087620968
%
2087720969
For a reader who is not familiar with the notation used in this section,
2087820970
the explanations given here should suffice to clarify what it means,
@@ -20975,7 +21067,7 @@ \subsection{Subtypes}
2097521067
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
2097621068
S}{X \& T}
2097721069
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20978-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21070+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2097921071
\end{minipage}
2098021072
\begin{minipage}[c]{0.49\textwidth}
2098121073
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20988,26 +21080,26 @@ \subsection{Subtypes}
2098821080
%
2098921081
\ExtraVSP
2099021082
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20991-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20992-
\Subtype{\Gamma'}{S_0}{T_0} \\
21083+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21084+
\Subtype{\Delta'}{S_0}{T_0} \\
2099321085
n_1 \leq n_2 &
2099421086
n_1 + k_1 \geq n_2 + k_2 &
20995-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21087+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2099621088
\begin{array}{c}
20997-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21089+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2099821090
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2099921091
\end{array}}
2100021092
\ExtraVSP\ExtraVSP
2100121093
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
21002-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21003-
\Subtype{\Gamma'}{S_0}{T_0} &
21004-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21094+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21095+
\Subtype{\Delta'}{S_0}{T_0} &
21096+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2100521097
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2100621098
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21007-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21099+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2100821100
\begin{array}{c}
21009-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21010-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21101+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21102+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2101121103
\end{array}}
2101221104
%
2101321105
\ExtraVSP
@@ -21135,24 +21227,24 @@ \subsubsection{Subtype Rules}
2113521227

2113621228
\LMHash{}%
2113721229
The rules in Figure~\ref{fig:subtypeRules} use
21138-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21230+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2113921231
bounds of type variables.
21140-
$\Gamma$ is a partial function that maps type variables to types.
21232+
$\Delta$ is a partial function that maps type variables to types.
2114121233
At a given location where the type variables in scope are
2114221234
\TypeParametersStd{}
2114321235
(\commentary{as declared by enclosing classes and/or functions}),
2114421236
we define the environment as follows:
21145-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21237+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2114621238
\commentary{%
21147-
That is, $\Gamma(X_1) = B_1$, and so on,
21148-
and $\Gamma$ is undefined when applied to a type variable $Y$
21239+
That is, $\Delta(X_1) = B_1$, and so on,
21240+
and $\Delta$ is undefined when applied to a type variable $Y$
2114921241
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2115021242
}
2115121243
When the rules are used to show that a given subtype relationship exists,
21152-
this is the initial value of $\Gamma$.
21244+
this is the initial value of $\Delta$.
2115321245

2115421246
\LMHash{}%
21155-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21247+
If a generic function type is encountered, an extension of $\Delta$ is used,
2115621248
as shown in the rules~\SrnPositionalFunctionType{}
2115721249
and~\SrnNamedFunctionType{}
2115821250
of Figure~\ref{fig:subtypeRules}.
@@ -21209,9 +21301,9 @@ \subsubsection{Being a subtype}
2120921301

2121021302
\LMHash{}%
2121121303
A type $S$ is shown to be a \Index{subtype} of another type $T$
21212-
in an environment $\Gamma$ by providing
21304+
in an environment $\Delta$ by providing
2121321305
an instantiation of a rule $R$ whose conclusion is
21214-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21306+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2121521307
along with rule instantiations showing
2121621308
each of the premises of $R$,
2121721309
continuing until a rule with no premises is reached.
@@ -21337,19 +21429,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2133721429
a subtype of \code{FutureOr<$T$>}.
2133821430

2133921431
Another example is the wording in rule~\SrnReflexivity{}:
21340-
``\ldots{} in any environment $\Gamma$'',
21432+
``\ldots{} in any environment $\Delta$'',
2134121433
which indicates that the rule can be applied no matter which bindings
2134221434
of type variables to bounds there exist in the environment.
2134321435
It should be noted that the environment matters even with rules
21344-
where it is simply stated as a plain $\Gamma$ in the conclusion
21436+
where it is simply stated as a plain $\Delta$ in the conclusion
2134521437
and in one or more premises,
2134621438
because the proof of those premises could, directly or indirectly,
2134721439
include the application of a rule where the environment is used.
2134821440

2134921441
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2135021442
\begin{itemize}
2135121443
\Item{\SrnReflexivity}{Reflexivity}
21352-
Every type is a subtype of itself, in any environment $\Gamma$.
21444+
Every type is a subtype of itself, in any environment $\Delta$.
2135321445
In the following rules except for a few,
2135421446
the rule is also valid in any environment
2135521447
and the environment is never used explicitly,
@@ -21400,7 +21492,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2140021492
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2140121493
The type variable $X$ is a subtype of a type $T$ if
2140221494
the bound of $X$
21403-
(as specified in the current environment $\Gamma$)
21495+
(as specified in the current environment $\Delta$)
2140421496
is a subtype of $T$.
2140521497
\Item{\SrnRightFunction}{Right Function}
2140621498
Every function type is a subtype of the type \FUNCTION.
@@ -21420,7 +21512,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2142021512
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2142121513
For every subtype relation considered in this rule,
2142221514
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21423-
(as reflected in the use of the extended environment $\Gamma'$).
21515+
(as reflected in the use of the extended environment $\Delta'$).
2142421516
We can assume without loss of generality
2142521517
that the names of type variables are pairwise identical,
2142621518
because we consider types of generic functions to be equivalent under
@@ -21485,14 +21577,14 @@ \subsubsection{Additional Subtyping Concepts}
2148521577
\LMLabel{additionalSubtypingConcepts}
2148621578

2148721579
\LMHash{}%
21488-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21580+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2148921581
written \SupertypeStd{S}{T},
2149021582
if{}f \SubtypeStd{T}{S}.
2149121583

2149221584
\LMHash{}%
2149321585
A type $T$
2149421586
\Index{may be assigned}
21495-
to a type $S$ in an environment $\Gamma$,
21587+
to a type $S$ in an environment $\Delta$,
2149621588
written \AssignableStd{S}{T},
2149721589
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2149821590
In this case we say that the types $S$ and $T$ are
@@ -22591,7 +22683,7 @@ \section*{Appendix: Algorithmic Subtyping}
2259122683
\begin{minipage}[c]{0.49\textwidth}
2259222684
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2259322685
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22594-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22686+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2259522687
\end{minipage}
2259622688
\begin{minipage}[c]{0.49\textwidth}
2259722689
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)