Skip to content

Commit 0ff08fb

Browse files
committed
Update section Local Variable Declaration, update \Gamma to \Delta
1 parent a53f001 commit 0ff08fb

File tree

1 file changed

+148
-56
lines changed

1 file changed

+148
-56
lines changed

specification/dartLangSpec.tex

Lines changed: 148 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17582,6 +17582,7 @@ \subsection{Local Variable Declaration}
1758217582
apply to local variables with the same definitions as for other variables
1758317583
(\ref{variables}).
1758417584

17585+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1758517586
\LMHash{}%
1758617587
We say that a local variable $v$ is \Index{potentially mutated}
1758717588
in some scope $s$
@@ -17590,41 +17591,108 @@ \subsection{Local Variable Declaration}
1759017591
\LMHash{}%
1759117592
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1759217593
\code{\VAR{} $v$ = \NULL;}.
17593-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17594-
\code{$T$ $v$ = \NULL;}.
17595-
17596-
\commentary{%
17597-
This holds regardless of the type $T$.
17598-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17599-
}
17594+
If $T$ is a nullable type
17595+
(\ref{typeNullability})
17596+
then a local variable declaration of the form \code{$T$ $v$;}
17597+
is equivalent to \code{$T$ $v$ = \NULL;}.
17598+
17599+
%% TODO(eernst): Revise when flow analysis is added.
17600+
\commentary{%
17601+
If $T$ is a potentially non-nullable type
17602+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17603+
but an expression that gives rise to evaluation of $v$
17604+
is a compile-time error unless flow analysis
17605+
(\ref{flowAnalysis})
17606+
shows that the variable is guaranteed to have been initialized.%
17607+
}
17608+
17609+
\LMHash{}%
17610+
A local variable has an associated
17611+
\IndexCustom{declared type}{local variable!declared type}
17612+
which is determined from its declaration.
17613+
A local variable also has an associated
17614+
\IndexCustom{type}{local variable!type}
17615+
which is determined by flow analysis
17616+
(\ref{flowAnalysis})
17617+
via a process known as type promotion
17618+
(\ref{typePromotion}).
1760017619

1760117620
\LMHash{}%
17602-
The type of a local variable with a declaration of one of the forms
17603-
\code{$T$ $v$ = $e$;}
17604-
\code{\CONST{} $T$ $v$ = $e$;}
17605-
\code{\FINAL{} $T$ $v$ = $e$;}
17621+
The declared type of a local variable with a declaration of one of the forms
17622+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17623+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17624+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1760617625
is $T$.
17607-
The type of a local variable with a declaration of one of the forms
17608-
\code{\VAR{} $v$ = $e$;}
17609-
\code{\CONST{} $v$ = $e$;}
17610-
\code{\FINAL{} $v$ = $e$;}
17611-
is \DYNAMIC.
17626+
17627+
\LMHash{}%
17628+
The declared type of a local variable with a declaration of one of the forms
17629+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17630+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17631+
\code{\CONST\,\,$v$ = $e$;}
17632+
is determined as follows:
17633+
17634+
\begin{itemize}
17635+
\item
17636+
If the static type of $e$ is \code{Null} then
17637+
the declared type of $v$ is \DYNAMIC.
17638+
\item
17639+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17640+
where $X$ is a type variable
17641+
(\ref{intersectionTypes}),
17642+
the declared type of $v$ is \code{X}.
17643+
\commentary{%
17644+
In this case $v$ is immediately promoted
17645+
(\ref{typePromotion}).%
17646+
}
17647+
\item
17648+
Otherwise, the declared type of $v$ is the static type of $e$.
17649+
\end{itemize}
1761217650

1761317651
\LMHash{}%
1761417652
Let $v$ be a local variable declared by an initializing variable declaration,
1761517653
and let $e$ be the associated initializing expression.
17616-
It is a compile-time error if the static type of $e$ is not assignable to
17617-
the type of $v$.
17618-
It is a compile-time error if a local variable $v$ is final,
17619-
and the declaration of $v$ is not an initializing variable declaration.
17654+
It is a
17655+
\Error{compile-time error} if the static type of $e$ is not assignable to
17656+
the declared type of $v$.
1762017657

17658+
%% TODO(eernst): Revise when flow analysis is added.
1762117659
\commentary{%
17622-
It is also a compile-time error to assign to a final local variable
17660+
If a local variable $v$ is \FINAL{} and not \LATE,
17661+
it is not a compile-time error if the declaration of $v$ is
17662+
not an initializing variable declaration,
17663+
but an expression that gives rise to evaluation of $v$
17664+
is a compile-time error unless flow analysis shows that
17665+
the variable is guaranteed to have been initialized.
17666+
Similarly, an expression that gives rise to an assignment to $v$
17667+
is a compile-time error unless flow analysis shows that
17668+
it is guaranteed that the variable has \emph{not} been initialized.%
17669+
}
17670+
17671+
\commentary{%
17672+
It is a compile-time error to assign to a local variable
17673+
which is \FINAL{} and not \LATE{}
1762317674
(\ref{assignment}).%
1762417675
}
1762517676

1762617677
\LMHash{}%
17627-
It is a compile-time error if
17678+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17679+
that declares a variable $v$,
17680+
which has an initializing expression $e$.
17681+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17682+
(\ref{awaitExpressions}),
17683+
unless there is a function $f$ which is
17684+
the immediately enclosing function for $a$,
17685+
and $f$ is not the immediately enclosing function for $D$.
17686+
17687+
\commentary{%
17688+
In other words,
17689+
the initializing expression cannot await an expression directly,
17690+
any await expressions must be nested inside some other function,
17691+
that is, a function literal.%
17692+
}
17693+
17694+
\LMHash{}%
17695+
It is a \Error{compile-time error} if
1762817696
a local variable is referenced at a source code location that is before
1762917697
the end of its initializing expression, if any,
1763017698
and otherwise before the declaring occurrence of
@@ -17712,15 +17780,39 @@ \subsection{Local Variable Declaration}
1771217780

1771317781
\LMHash{}%
1771417782
The expression $e$ is evaluated to an object $o$.
17715-
Then, the variable $v$ is set to $o$.
17716-
% This error can occur due to implicit casts.
17783+
% The following error can occur due to implicit casts.
1771717784
A dynamic type error occurs
17718-
if the dynamic type of $o$ is not a subtype of the actual type
17785+
if the dynamic type of $o$ is not a subtype of the actual declared type
1771917786
(\ref{actualTypes})
1772017787
of $v$.
17788+
Otherwise, the variable $v$ is bound to $o$.
1772117789

17722-
% The local variable discussion does not mention how to read/write locals.
17723-
% Consider spelling this out, in terms of storage.
17790+
\LMHash{}%
17791+
Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17792+
that declares a variable $v$.
17793+
If an object $o$ is assigned to $v$
17794+
in a situation where $v$ is unbound
17795+
then $v$ is bound to $o$.
17796+
If an object $o$ is assigned to $v$
17797+
in a situation where $v$ is bound to an object $o'$
17798+
then a dynamic error occurs
17799+
(\commentary{it does not matter whether $o$ is the same object as $o'$}).
17800+
17801+
\commentary{%
17802+
Note that this includes the implicit initializing writes induced by
17803+
evaluating the variable.
17804+
Hence, the following program encounters a dynamic error
17805+
when it evaluates \code{x},
17806+
just before it would call \code{print}.%
17807+
}
17808+
17809+
\begin{dartCode}
17810+
\VOID\ main() \{
17811+
int i = 0;
17812+
\LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17813+
print(x);
17814+
\}
17815+
\end{dartCode}
1772417816

1772517817

1772617818
\subsection{Local Function Declaration}
@@ -21151,7 +21243,7 @@ \subsection{Subtypes}
2115121243
may find the meaning of the given notation obvious,
2115221244
but we still need to clarify a few details about how to handle
2115321245
syntactically different denotations of the same type,
21154-
and how to choose the right initial environment, $\Gamma$.
21246+
and how to choose the right initial environment, $\Delta$.
2115521247
%
2115621248
For a reader who is not familiar with the notation used in this section,
2115721249
the explanations given here should suffice to clarify what it means,
@@ -21254,7 +21346,7 @@ \subsection{Subtypes}
2125421346
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
2125521347
S}{X \& T}
2125621348
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21257-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21349+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2125821350
\end{minipage}
2125921351
\begin{minipage}[c]{0.49\textwidth}
2126021352
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21267,26 +21359,26 @@ \subsection{Subtypes}
2126721359
%
2126821360
\ExtraVSP
2126921361
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21270-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21271-
\Subtype{\Gamma'}{S_0}{T_0} \\
21362+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21363+
\Subtype{\Delta'}{S_0}{T_0} \\
2127221364
n_1 \leq n_2 &
2127321365
n_1 + k_1 \geq n_2 + k_2 &
21274-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21366+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2127521367
\begin{array}{c}
21276-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21368+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2127721369
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2127821370
\end{array}}
2127921371
\ExtraVSP\ExtraVSP
2128021372
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21281-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21282-
\Subtype{\Gamma'}{S_0}{T_0} &
21283-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21373+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21374+
\Subtype{\Delta'}{S_0}{T_0} &
21375+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2128421376
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2128521377
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21286-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21378+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2128721379
\begin{array}{c}
21288-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21289-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21380+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21381+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2129021382
\end{array}}
2129121383
%
2129221384
\ExtraVSP
@@ -21414,24 +21506,24 @@ \subsubsection{Subtype Rules}
2141421506

2141521507
\LMHash{}%
2141621508
The rules in Figure~\ref{fig:subtypeRules} use
21417-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21509+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2141821510
bounds of type variables.
21419-
$\Gamma$ is a partial function that maps type variables to types.
21511+
$\Delta$ is a partial function that maps type variables to types.
2142021512
At a given location where the type variables in scope are
2142121513
\TypeParametersStd{}
2142221514
(\commentary{as declared by enclosing classes and/or functions}),
2142321515
we define the environment as follows:
21424-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21516+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2142521517
\commentary{%
21426-
That is, $\Gamma(X_1) = B_1$, and so on,
21427-
and $\Gamma$ is undefined when applied to a type variable $Y$
21518+
That is, $\Delta(X_1) = B_1$, and so on,
21519+
and $\Delta$ is undefined when applied to a type variable $Y$
2142821520
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2142921521
}
2143021522
When the rules are used to show that a given subtype relationship exists,
21431-
this is the initial value of $\Gamma$.
21523+
this is the initial value of $\Delta$.
2143221524

2143321525
\LMHash{}%
21434-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21526+
If a generic function type is encountered, an extension of $\Delta$ is used,
2143521527
as shown in the rules~\SrnPositionalFunctionType{}
2143621528
and~\SrnNamedFunctionType{}
2143721529
of Figure~\ref{fig:subtypeRules}.
@@ -21488,9 +21580,9 @@ \subsubsection{Being a subtype}
2148821580

2148921581
\LMHash{}%
2149021582
A type $S$ is shown to be a \Index{subtype} of another type $T$
21491-
in an environment $\Gamma$ by providing
21583+
in an environment $\Delta$ by providing
2149221584
an instantiation of a rule $R$ whose conclusion is
21493-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21585+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2149421586
along with rule instantiations showing
2149521587
each of the premises of $R$,
2149621588
continuing until a rule with no premises is reached.
@@ -21616,19 +21708,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2161621708
a subtype of \code{FutureOr<$T$>}.
2161721709

2161821710
Another example is the wording in rule~\SrnReflexivity{}:
21619-
``\ldots{} in any environment $\Gamma$'',
21711+
``\ldots{} in any environment $\Delta$'',
2162021712
which indicates that the rule can be applied no matter which bindings
2162121713
of type variables to bounds there exist in the environment.
2162221714
It should be noted that the environment matters even with rules
21623-
where it is simply stated as a plain $\Gamma$ in the conclusion
21715+
where it is simply stated as a plain $\Delta$ in the conclusion
2162421716
and in one or more premises,
2162521717
because the proof of those premises could, directly or indirectly,
2162621718
include the application of a rule where the environment is used.
2162721719

2162821720
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2162921721
\begin{itemize}
2163021722
\Item{\SrnReflexivity}{Reflexivity}
21631-
Every type is a subtype of itself, in any environment $\Gamma$.
21723+
Every type is a subtype of itself, in any environment $\Delta$.
2163221724
In the following rules except for a few,
2163321725
the rule is also valid in any environment
2163421726
and the environment is never used explicitly,
@@ -21679,7 +21771,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2167921771
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2168021772
The type variable $X$ is a subtype of a type $T$ if
2168121773
the bound of $X$
21682-
(as specified in the current environment $\Gamma$)
21774+
(as specified in the current environment $\Delta$)
2168321775
is a subtype of $T$.
2168421776
\Item{\SrnRightFunction}{Right Function}
2168521777
Every function type is a subtype of the type \FUNCTION.
@@ -21699,7 +21791,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2169921791
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2170021792
For every subtype relation considered in this rule,
2170121793
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21702-
(as reflected in the use of the extended environment $\Gamma'$).
21794+
(as reflected in the use of the extended environment $\Delta'$).
2170321795
We can assume without loss of generality
2170421796
that the names of type variables are pairwise identical,
2170521797
because we consider types of generic functions to be equivalent under
@@ -21764,14 +21856,14 @@ \subsubsection{Additional Subtyping Concepts}
2176421856
\LMLabel{additionalSubtypingConcepts}
2176521857

2176621858
\LMHash{}%
21767-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21859+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2176821860
written \SupertypeStd{S}{T},
2176921861
if{}f \SubtypeStd{T}{S}.
2177021862

2177121863
\LMHash{}%
2177221864
A type $T$
2177321865
\Index{may be assigned}
21774-
to a type $S$ in an environment $\Gamma$,
21866+
to a type $S$ in an environment $\Delta$,
2177521867
written \AssignableStd{S}{T},
2177621868
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2177721869
In this case we say that the types $S$ and $T$ are
@@ -22881,7 +22973,7 @@ \section*{Appendix: Algorithmic Subtyping}
2288122973
\begin{minipage}[c]{0.49\textwidth}
2288222974
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2288322975
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22884-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22976+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2288522977
\end{minipage}
2288622978
\begin{minipage}[c]{0.49\textwidth}
2288722979
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)