Skip to content

Commit d8a2247

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

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

17431+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1743117432
\LMHash{}%
1743217433
We say that a local variable $v$ is \Index{potentially mutated}
1743317434
in some scope $s$
@@ -17436,41 +17437,108 @@ \subsection{Local Variable Declaration}
1743617437
\LMHash{}%
1743717438
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1743817439
\code{\VAR{} $v$ = \NULL;}.
17439-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17440-
\code{$T$ $v$ = \NULL;}.
17441-
17442-
\commentary{%
17443-
This holds regardless of the type $T$.
17444-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17445-
}
17440+
If $T$ is a nullable type
17441+
(\ref{typeNullability})
17442+
then a local variable declaration of the form \code{$T$ $v$;}
17443+
is equivalent to \code{$T$ $v$ = \NULL;}.
17444+
17445+
%% TODO(eernst): Revise when flow analysis is added.
17446+
\commentary{%
17447+
If $T$ is a potentially non-nullable type
17448+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17449+
but an expression that gives rise to evaluation of $v$
17450+
is a compile-time error unless flow analysis
17451+
(\ref{flowAnalysis})
17452+
shows that the variable is guaranteed to have been initialized.%
17453+
}
17454+
17455+
\LMHash{}%
17456+
A local variable has an associated
17457+
\IndexCustom{declared type}{local variable!declared type}
17458+
which is determined from its declaration.
17459+
A local variable also has an associated
17460+
\IndexCustom{type}{local variable!type}
17461+
which is determined by flow analysis
17462+
(\ref{flowAnalysis})
17463+
via a process known as type promotion
17464+
(\ref{typePromotion}).
1744617465

1744717466
\LMHash{}%
17448-
The type of a local variable with a declaration of one of the forms
17449-
\code{$T$ $v$ = $e$;}
17450-
\code{\CONST{} $T$ $v$ = $e$;}
17451-
\code{\FINAL{} $T$ $v$ = $e$;}
17467+
The declared type of a local variable with a declaration of one of the forms
17468+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17469+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17470+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1745217471
is $T$.
17453-
The type of a local variable with a declaration of one of the forms
17454-
\code{\VAR{} $v$ = $e$;}
17455-
\code{\CONST{} $v$ = $e$;}
17456-
\code{\FINAL{} $v$ = $e$;}
17457-
is \DYNAMIC.
17472+
17473+
\LMHash{}%
17474+
The declared type of a local variable with a declaration of one of the forms
17475+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17476+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17477+
\code{\CONST\,\,$v$ = $e$;}
17478+
is determined as follows:
17479+
17480+
\begin{itemize}
17481+
\item
17482+
If the static type of $e$ is \code{Null} then
17483+
the declared type of $v$ is \DYNAMIC.
17484+
\item
17485+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17486+
where $X$ is a type variable
17487+
(\ref{intersectionTypes}),
17488+
the declared type of $v$ is \code{X}.
17489+
\commentary{%
17490+
In this case $v$ is immediately promoted
17491+
(\ref{typePromotion}).%
17492+
}
17493+
\item
17494+
Otherwise, the declared type of $v$ is the static type of $e$.
17495+
\end{itemize}
1745817496

1745917497
\LMHash{}%
1746017498
Let $v$ be a local variable declared by an initializing variable declaration,
1746117499
and let $e$ be the associated initializing expression.
17462-
It is a compile-time error if the static type of $e$ is not assignable to
17463-
the type of $v$.
17464-
It is a compile-time error if a local variable $v$ is final,
17465-
and the declaration of $v$ is not an initializing variable declaration.
17500+
It is a
17501+
\Error{compile-time error} if the static type of $e$ is not assignable to
17502+
the declared type of $v$.
1746617503

17504+
%% TODO(eernst): Revise when flow analysis is added.
1746717505
\commentary{%
17468-
It is also a compile-time error to assign to a final local variable
17506+
If a local variable $v$ is \FINAL{} and not \LATE,
17507+
it is not a compile-time error if the declaration of $v$ is
17508+
not an initializing variable declaration,
17509+
but an expression that gives rise to evaluation of $v$
17510+
is a compile-time error unless flow analysis shows that
17511+
the variable is guaranteed to have been initialized.
17512+
Similarly, an expression that gives rise to an assignment to $v$
17513+
is a compile-time error unless flow analysis shows that
17514+
it is guaranteed that the variable has \emph{not} been initialized.%
17515+
}
17516+
17517+
\commentary{%
17518+
It is a compile-time error to assign to a local variable
17519+
which is \FINAL{} and not \LATE{}
1746917520
(\ref{assignment}).%
1747017521
}
1747117522

1747217523
\LMHash{}%
17473-
It is a compile-time error if
17524+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17525+
that declares a variable $v$,
17526+
which has an initializing expression $e$.
17527+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17528+
(\ref{awaitExpressions}),
17529+
unless there is a function $f$ which is
17530+
the immediately enclosing function for $a$,
17531+
and $f$ is not the immediately enclosing function for $D$.
17532+
17533+
\commentary{%
17534+
In other words,
17535+
the initializing expression cannot await an expression directly,
17536+
any await expressions must be nested inside some other function,
17537+
that is, a function literal.%
17538+
}
17539+
17540+
\LMHash{}%
17541+
It is a \Error{compile-time error} if
1747417542
a local variable is referenced at a source code location that is before
1747517543
the end of its initializing expression, if any,
1747617544
and otherwise before the declaring occurrence of
@@ -17558,15 +17626,39 @@ \subsection{Local Variable Declaration}
1755817626

1755917627
\LMHash{}%
1756017628
The expression $e$ is evaluated to an object $o$.
17561-
Then, the variable $v$ is set to $o$.
17562-
% This error can occur due to implicit casts.
17629+
% The following error can occur due to implicit casts.
1756317630
A dynamic type error occurs
17564-
if the dynamic type of $o$ is not a subtype of the actual type
17631+
if the dynamic type of $o$ is not a subtype of the actual declared type
1756517632
(\ref{actualTypes})
1756617633
of $v$.
17634+
Otherwise, the variable $v$ is bound to $o$.
1756717635

17568-
% The local variable discussion does not mention how to read/write locals.
17569-
% Consider spelling this out, in terms of storage.
17636+
\LMHash{}%
17637+
Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17638+
that declares a variable $v$.
17639+
If an object $o$ is assigned to $v$
17640+
in a situation where $v$ is unbound
17641+
then $v$ is bound to $o$.
17642+
If an object $o$ is assigned to $v$
17643+
in a situation where $v$ is bound to an object $o'$
17644+
then a dynamic error occurs
17645+
(\commentary{it does not matter whether $o$ is the same object as $o'$}).
17646+
17647+
\commentary{%
17648+
Note that this includes the implicit initializing writes induced by
17649+
evaluating the variable.
17650+
Hence, the following program encounters a dynamic error
17651+
when it evaluates \code{x},
17652+
just before it would call \code{print}.%
17653+
}
17654+
17655+
\begin{dartCode}
17656+
\VOID\ main() \{
17657+
int i = 0;
17658+
\LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17659+
print(x);
17660+
\}
17661+
\end{dartCode}
1757017662

1757117663

1757217664
\subsection{Local Function Declaration}
@@ -20981,7 +21073,7 @@ \subsection{Subtypes}
2098121073
may find the meaning of the given notation obvious,
2098221074
but we still need to clarify a few details about how to handle
2098321075
syntactically different denotations of the same type,
20984-
and how to choose the right initial environment, $\Gamma$.
21076+
and how to choose the right initial environment, $\Delta$.
2098521077
%
2098621078
For a reader who is not familiar with the notation used in this section,
2098721079
the explanations given here should suffice to clarify what it means,
@@ -21084,7 +21176,7 @@ \subsection{Subtypes}
2108421176
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
2108521177
S}{X \& T}
2108621178
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21087-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21179+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2108821180
\end{minipage}
2108921181
\begin{minipage}[c]{0.49\textwidth}
2109021182
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21097,26 +21189,26 @@ \subsection{Subtypes}
2109721189
%
2109821190
\ExtraVSP
2109921191
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21100-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21101-
\Subtype{\Gamma'}{S_0}{T_0} \\
21192+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21193+
\Subtype{\Delta'}{S_0}{T_0} \\
2110221194
n_1 \leq n_2 &
2110321195
n_1 + k_1 \geq n_2 + k_2 &
21104-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21196+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2110521197
\begin{array}{c}
21106-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21198+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2110721199
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2110821200
\end{array}}
2110921201
\ExtraVSP\ExtraVSP
2111021202
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21111-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21112-
\Subtype{\Gamma'}{S_0}{T_0} &
21113-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21203+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21204+
\Subtype{\Delta'}{S_0}{T_0} &
21205+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2111421206
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2111521207
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21116-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21208+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2111721209
\begin{array}{c}
21118-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21119-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21210+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21211+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2112021212
\end{array}}
2112121213
%
2112221214
\ExtraVSP
@@ -21244,24 +21336,24 @@ \subsubsection{Subtype Rules}
2124421336

2124521337
\LMHash{}%
2124621338
The rules in Figure~\ref{fig:subtypeRules} use
21247-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21339+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2124821340
bounds of type variables.
21249-
$\Gamma$ is a partial function that maps type variables to types.
21341+
$\Delta$ is a partial function that maps type variables to types.
2125021342
At a given location where the type variables in scope are
2125121343
\TypeParametersStd{}
2125221344
(\commentary{as declared by enclosing classes and/or functions}),
2125321345
we define the environment as follows:
21254-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21346+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2125521347
\commentary{%
21256-
That is, $\Gamma(X_1) = B_1$, and so on,
21257-
and $\Gamma$ is undefined when applied to a type variable $Y$
21348+
That is, $\Delta(X_1) = B_1$, and so on,
21349+
and $\Delta$ is undefined when applied to a type variable $Y$
2125821350
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2125921351
}
2126021352
When the rules are used to show that a given subtype relationship exists,
21261-
this is the initial value of $\Gamma$.
21353+
this is the initial value of $\Delta$.
2126221354

2126321355
\LMHash{}%
21264-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21356+
If a generic function type is encountered, an extension of $\Delta$ is used,
2126521357
as shown in the rules~\SrnPositionalFunctionType{}
2126621358
and~\SrnNamedFunctionType{}
2126721359
of Figure~\ref{fig:subtypeRules}.
@@ -21318,9 +21410,9 @@ \subsubsection{Being a subtype}
2131821410

2131921411
\LMHash{}%
2132021412
A type $S$ is shown to be a \Index{subtype} of another type $T$
21321-
in an environment $\Gamma$ by providing
21413+
in an environment $\Delta$ by providing
2132221414
an instantiation of a rule $R$ whose conclusion is
21323-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21415+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2132421416
along with rule instantiations showing
2132521417
each of the premises of $R$,
2132621418
continuing until a rule with no premises is reached.
@@ -21446,19 +21538,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2144621538
a subtype of \code{FutureOr<$T$>}.
2144721539

2144821540
Another example is the wording in rule~\SrnReflexivity{}:
21449-
``\ldots{} in any environment $\Gamma$'',
21541+
``\ldots{} in any environment $\Delta$'',
2145021542
which indicates that the rule can be applied no matter which bindings
2145121543
of type variables to bounds there exist in the environment.
2145221544
It should be noted that the environment matters even with rules
21453-
where it is simply stated as a plain $\Gamma$ in the conclusion
21545+
where it is simply stated as a plain $\Delta$ in the conclusion
2145421546
and in one or more premises,
2145521547
because the proof of those premises could, directly or indirectly,
2145621548
include the application of a rule where the environment is used.
2145721549

2145821550
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2145921551
\begin{itemize}
2146021552
\Item{\SrnReflexivity}{Reflexivity}
21461-
Every type is a subtype of itself, in any environment $\Gamma$.
21553+
Every type is a subtype of itself, in any environment $\Delta$.
2146221554
In the following rules except for a few,
2146321555
the rule is also valid in any environment
2146421556
and the environment is never used explicitly,
@@ -21509,7 +21601,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2150921601
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2151021602
The type variable $X$ is a subtype of a type $T$ if
2151121603
the bound of $X$
21512-
(as specified in the current environment $\Gamma$)
21604+
(as specified in the current environment $\Delta$)
2151321605
is a subtype of $T$.
2151421606
\Item{\SrnRightFunction}{Right Function}
2151521607
Every function type is a subtype of the type \FUNCTION.
@@ -21529,7 +21621,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2152921621
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2153021622
For every subtype relation considered in this rule,
2153121623
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21532-
(as reflected in the use of the extended environment $\Gamma'$).
21624+
(as reflected in the use of the extended environment $\Delta'$).
2153321625
We can assume without loss of generality
2153421626
that the names of type variables are pairwise identical,
2153521627
because we consider types of generic functions to be equivalent under
@@ -21594,14 +21686,14 @@ \subsubsection{Additional Subtyping Concepts}
2159421686
\LMLabel{additionalSubtypingConcepts}
2159521687

2159621688
\LMHash{}%
21597-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21689+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2159821690
written \SupertypeStd{S}{T},
2159921691
if{}f \SubtypeStd{T}{S}.
2160021692

2160121693
\LMHash{}%
2160221694
A type $T$
2160321695
\Index{may be assigned}
21604-
to a type $S$ in an environment $\Gamma$,
21696+
to a type $S$ in an environment $\Delta$,
2160521697
written \AssignableStd{S}{T},
2160621698
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2160721699
In this case we say that the types $S$ and $T$ are
@@ -22710,7 +22802,7 @@ \section*{Appendix: Algorithmic Subtyping}
2271022802
\begin{minipage}[c]{0.49\textwidth}
2271122803
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2271222804
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22713-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22805+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2271422806
\end{minipage}
2271522807
\begin{minipage}[c]{0.49\textwidth}
2271622808
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)