Skip to content

Commit 14860e8

Browse files
committed
Update section Local Variable Declaration, update \Gamma to \Delta
1 parent c67bab7 commit 14860e8

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
@@ -17474,6 +17474,7 @@ \subsection{Local Variable Declaration}
1747417474
apply to local variables with the same definitions as for other variables
1747517475
(\ref{variables}).
1747617476

17477+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1747717478
\LMHash{}%
1747817479
We say that a local variable $v$ is \Index{potentially mutated}
1747917480
in some scope $s$
@@ -17482,41 +17483,108 @@ \subsection{Local Variable Declaration}
1748217483
\LMHash{}%
1748317484
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1748417485
\code{\VAR{} $v$ = \NULL;}.
17485-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17486-
\code{$T$ $v$ = \NULL;}.
17487-
17488-
\commentary{%
17489-
This holds regardless of the type $T$.
17490-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17491-
}
17486+
If $T$ is a nullable type
17487+
(\ref{typeNullability})
17488+
then a local variable declaration of the form \code{$T$ $v$;}
17489+
is equivalent to \code{$T$ $v$ = \NULL;}.
17490+
17491+
%% TODO(eernst): Revise when flow analysis is added.
17492+
\commentary{%
17493+
If $T$ is a potentially non-nullable type
17494+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17495+
but an expression that gives rise to evaluation of $v$
17496+
is a compile-time error unless flow analysis
17497+
(\ref{flowAnalysis})
17498+
shows that the variable is guaranteed to have been initialized.%
17499+
}
17500+
17501+
\LMHash{}%
17502+
A local variable has an associated
17503+
\IndexCustom{declared type}{local variable!declared type}
17504+
which is determined from its declaration.
17505+
A local variable also has an associated
17506+
\IndexCustom{type}{local variable!type}
17507+
which is determined by flow analysis
17508+
(\ref{flowAnalysis})
17509+
via a process known as type promotion
17510+
(\ref{typePromotion}).
1749217511

1749317512
\LMHash{}%
17494-
The type of a local variable with a declaration of one of the forms
17495-
\code{$T$ $v$ = $e$;}
17496-
\code{\CONST{} $T$ $v$ = $e$;}
17497-
\code{\FINAL{} $T$ $v$ = $e$;}
17513+
The declared type of a local variable with a declaration of one of the forms
17514+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17515+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17516+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1749817517
is $T$.
17499-
The type of a local variable with a declaration of one of the forms
17500-
\code{\VAR{} $v$ = $e$;}
17501-
\code{\CONST{} $v$ = $e$;}
17502-
\code{\FINAL{} $v$ = $e$;}
17503-
is \DYNAMIC.
17518+
17519+
\LMHash{}%
17520+
The declared type of a local variable with a declaration of one of the forms
17521+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17522+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17523+
\code{\CONST\,\,$v$ = $e$;}
17524+
is determined as follows:
17525+
17526+
\begin{itemize}
17527+
\item
17528+
If the static type of $e$ is \code{Null} then
17529+
the declared type of $v$ is \DYNAMIC.
17530+
\item
17531+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17532+
where $X$ is a type variable
17533+
(\ref{intersectionTypes}),
17534+
the declared type of $v$ is \code{X}.
17535+
\commentary{%
17536+
In this case $v$ is immediately promoted
17537+
(\ref{typePromotion}).%
17538+
}
17539+
\item
17540+
Otherwise, the declared type of $v$ is the static type of $e$.
17541+
\end{itemize}
1750417542

1750517543
\LMHash{}%
1750617544
Let $v$ be a local variable declared by an initializing variable declaration,
1750717545
and let $e$ be the associated initializing expression.
17508-
It is a compile-time error if the static type of $e$ is not assignable to
17509-
the type of $v$.
17510-
It is a compile-time error if a local variable $v$ is final,
17511-
and the declaration of $v$ is not an initializing variable declaration.
17546+
It is a
17547+
\Error{compile-time error} if the static type of $e$ is not assignable to
17548+
the declared type of $v$.
1751217549

17550+
%% TODO(eernst): Revise when flow analysis is added.
1751317551
\commentary{%
17514-
It is also a compile-time error to assign to a final local variable
17552+
If a local variable $v$ is \FINAL{} and not \LATE,
17553+
it is not a compile-time error if the declaration of $v$ is
17554+
not an initializing variable declaration,
17555+
but an expression that gives rise to evaluation of $v$
17556+
is a compile-time error unless flow analysis shows that
17557+
the variable is guaranteed to have been initialized.
17558+
Similarly, an expression that gives rise to an assignment to $v$
17559+
is a compile-time error unless flow analysis shows that
17560+
it is guaranteed that the variable has \emph{not} been initialized.%
17561+
}
17562+
17563+
\commentary{%
17564+
It is a compile-time error to assign to a local variable
17565+
which is \FINAL{} and not \LATE{}
1751517566
(\ref{assignment}).%
1751617567
}
1751717568

1751817569
\LMHash{}%
17519-
It is a compile-time error if
17570+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17571+
that declares a variable $v$,
17572+
which has an initializing expression $e$.
17573+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17574+
(\ref{awaitExpressions}),
17575+
unless there is a function $f$ which is
17576+
the immediately enclosing function for $a$,
17577+
and $f$ is not the immediately enclosing function for $D$.
17578+
17579+
\commentary{%
17580+
In other words,
17581+
the initializing expression cannot await an expression directly,
17582+
any await expressions must be nested inside some other function,
17583+
that is, a function literal.%
17584+
}
17585+
17586+
\LMHash{}%
17587+
It is a \Error{compile-time error} if
1752017588
a local variable is referenced at a source code location that is before
1752117589
the end of its initializing expression, if any,
1752217590
and otherwise before the declaring occurrence of
@@ -17604,15 +17672,39 @@ \subsection{Local Variable Declaration}
1760417672

1760517673
\LMHash{}%
1760617674
The expression $e$ is evaluated to an object $o$.
17607-
Then, the variable $v$ is set to $o$.
17608-
% This error can occur due to implicit casts.
17675+
% The following error can occur due to implicit casts.
1760917676
A dynamic type error occurs
17610-
if the dynamic type of $o$ is not a subtype of the actual type
17677+
if the dynamic type of $o$ is not a subtype of the actual declared type
1761117678
(\ref{actualTypes})
1761217679
of $v$.
17680+
Otherwise, the variable $v$ is bound to $o$.
1761317681

17614-
% The local variable discussion does not mention how to read/write locals.
17615-
% Consider spelling this out, in terms of storage.
17682+
\LMHash{}%
17683+
Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17684+
that declares a variable $v$.
17685+
If an object $o$ is assigned to $v$
17686+
in a situation where $v$ is unbound
17687+
then $v$ is bound to $o$.
17688+
If an object $o$ is assigned to $v$
17689+
in a situation where $v$ is bound to an object $o'$
17690+
then a dynamic error occurs
17691+
(\commentary{it does not matter whether $o$ is the same object as $o'$}).
17692+
17693+
\commentary{%
17694+
Note that this includes the implicit initializing writes induced by
17695+
evaluating the variable.
17696+
Hence, the following program encounters a dynamic error
17697+
when it evaluates \code{x},
17698+
just before it would call \code{print}.%
17699+
}
17700+
17701+
\begin{dartCode}
17702+
\VOID\ main() \{
17703+
int i = 0;
17704+
\LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17705+
print(x);
17706+
\}
17707+
\end{dartCode}
1761617708

1761717709

1761817710
\subsection{Local Function Declaration}
@@ -21043,7 +21135,7 @@ \subsection{Subtypes}
2104321135
may find the meaning of the given notation obvious,
2104421136
but we still need to clarify a few details about how to handle
2104521137
syntactically different denotations of the same type,
21046-
and how to choose the right initial environment, $\Gamma$.
21138+
and how to choose the right initial environment, $\Delta$.
2104721139
%
2104821140
For a reader who is not familiar with the notation used in this section,
2104921141
the explanations given here should suffice to clarify what it means,
@@ -21146,7 +21238,7 @@ \subsection{Subtypes}
2114621238
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
2114721239
S}{X \& T}
2114821240
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21149-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21241+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2115021242
\end{minipage}
2115121243
\begin{minipage}[c]{0.49\textwidth}
2115221244
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21159,26 +21251,26 @@ \subsection{Subtypes}
2115921251
%
2116021252
\ExtraVSP
2116121253
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21162-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21163-
\Subtype{\Gamma'}{S_0}{T_0} \\
21254+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21255+
\Subtype{\Delta'}{S_0}{T_0} \\
2116421256
n_1 \leq n_2 &
2116521257
n_1 + k_1 \geq n_2 + k_2 &
21166-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21258+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2116721259
\begin{array}{c}
21168-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21260+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2116921261
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2117021262
\end{array}}
2117121263
\ExtraVSP\ExtraVSP
2117221264
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21173-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21174-
\Subtype{\Gamma'}{S_0}{T_0} &
21175-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21265+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21266+
\Subtype{\Delta'}{S_0}{T_0} &
21267+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2117621268
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2117721269
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21178-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21270+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2117921271
\begin{array}{c}
21180-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21181-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21272+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21273+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2118221274
\end{array}}
2118321275
%
2118421276
\ExtraVSP
@@ -21306,24 +21398,24 @@ \subsubsection{Subtype Rules}
2130621398

2130721399
\LMHash{}%
2130821400
The rules in Figure~\ref{fig:subtypeRules} use
21309-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21401+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2131021402
bounds of type variables.
21311-
$\Gamma$ is a partial function that maps type variables to types.
21403+
$\Delta$ is a partial function that maps type variables to types.
2131221404
At a given location where the type variables in scope are
2131321405
\TypeParametersStd{}
2131421406
(\commentary{as declared by enclosing classes and/or functions}),
2131521407
we define the environment as follows:
21316-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21408+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2131721409
\commentary{%
21318-
That is, $\Gamma(X_1) = B_1$, and so on,
21319-
and $\Gamma$ is undefined when applied to a type variable $Y$
21410+
That is, $\Delta(X_1) = B_1$, and so on,
21411+
and $\Delta$ is undefined when applied to a type variable $Y$
2132021412
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2132121413
}
2132221414
When the rules are used to show that a given subtype relationship exists,
21323-
this is the initial value of $\Gamma$.
21415+
this is the initial value of $\Delta$.
2132421416

2132521417
\LMHash{}%
21326-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21418+
If a generic function type is encountered, an extension of $\Delta$ is used,
2132721419
as shown in the rules~\SrnPositionalFunctionType{}
2132821420
and~\SrnNamedFunctionType{}
2132921421
of Figure~\ref{fig:subtypeRules}.
@@ -21380,9 +21472,9 @@ \subsubsection{Being a subtype}
2138021472

2138121473
\LMHash{}%
2138221474
A type $S$ is shown to be a \Index{subtype} of another type $T$
21383-
in an environment $\Gamma$ by providing
21475+
in an environment $\Delta$ by providing
2138421476
an instantiation of a rule $R$ whose conclusion is
21385-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21477+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2138621478
along with rule instantiations showing
2138721479
each of the premises of $R$,
2138821480
continuing until a rule with no premises is reached.
@@ -21508,19 +21600,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2150821600
a subtype of \code{FutureOr<$T$>}.
2150921601

2151021602
Another example is the wording in rule~\SrnReflexivity{}:
21511-
``\ldots{} in any environment $\Gamma$'',
21603+
``\ldots{} in any environment $\Delta$'',
2151221604
which indicates that the rule can be applied no matter which bindings
2151321605
of type variables to bounds there exist in the environment.
2151421606
It should be noted that the environment matters even with rules
21515-
where it is simply stated as a plain $\Gamma$ in the conclusion
21607+
where it is simply stated as a plain $\Delta$ in the conclusion
2151621608
and in one or more premises,
2151721609
because the proof of those premises could, directly or indirectly,
2151821610
include the application of a rule where the environment is used.
2151921611

2152021612
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2152121613
\begin{itemize}
2152221614
\Item{\SrnReflexivity}{Reflexivity}
21523-
Every type is a subtype of itself, in any environment $\Gamma$.
21615+
Every type is a subtype of itself, in any environment $\Delta$.
2152421616
In the following rules except for a few,
2152521617
the rule is also valid in any environment
2152621618
and the environment is never used explicitly,
@@ -21571,7 +21663,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2157121663
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2157221664
The type variable $X$ is a subtype of a type $T$ if
2157321665
the bound of $X$
21574-
(as specified in the current environment $\Gamma$)
21666+
(as specified in the current environment $\Delta$)
2157521667
is a subtype of $T$.
2157621668
\Item{\SrnRightFunction}{Right Function}
2157721669
Every function type is a subtype of the type \FUNCTION.
@@ -21591,7 +21683,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2159121683
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2159221684
For every subtype relation considered in this rule,
2159321685
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21594-
(as reflected in the use of the extended environment $\Gamma'$).
21686+
(as reflected in the use of the extended environment $\Delta'$).
2159521687
We can assume without loss of generality
2159621688
that the names of type variables are pairwise identical,
2159721689
because we consider types of generic functions to be equivalent under
@@ -21656,14 +21748,14 @@ \subsubsection{Additional Subtyping Concepts}
2165621748
\LMLabel{additionalSubtypingConcepts}
2165721749

2165821750
\LMHash{}%
21659-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21751+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2166021752
written \SupertypeStd{S}{T},
2166121753
if{}f \SubtypeStd{T}{S}.
2166221754

2166321755
\LMHash{}%
2166421756
A type $T$
2166521757
\Index{may be assigned}
21666-
to a type $S$ in an environment $\Gamma$,
21758+
to a type $S$ in an environment $\Delta$,
2166721759
written \AssignableStd{S}{T},
2166821760
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2166921761
In this case we say that the types $S$ and $T$ are
@@ -22773,7 +22865,7 @@ \section*{Appendix: Algorithmic Subtyping}
2277322865
\begin{minipage}[c]{0.49\textwidth}
2277422866
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2277522867
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22776-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22868+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2277722869
\end{minipage}
2277822870
\begin{minipage}[c]{0.49\textwidth}
2277922871
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)