Skip to content

Commit e40870b

Browse files
committed
Update section Local Variable Declaration, update \Gamma to \Delta
1 parent f223943 commit e40870b

File tree

1 file changed

+148
-56
lines changed

1 file changed

+148
-56
lines changed

Diff for: specification/dartLangSpec.tex

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

17308+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1730817309
\LMHash{}%
1730917310
We say that a local variable $v$ is \Index{potentially mutated}
1731017311
in some scope $s$
@@ -17313,41 +17314,108 @@ \subsection{Local Variable Declaration}
1731317314
\LMHash{}%
1731417315
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1731517316
\code{\VAR{} $v$ = \NULL;}.
17316-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17317-
\code{$T$ $v$ = \NULL;}.
17318-
17319-
\commentary{%
17320-
This holds regardless of the type $T$.
17321-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17322-
}
17317+
If $T$ is a nullable type
17318+
(\ref{typeNullability})
17319+
then a local variable declaration of the form \code{$T$ $v$;}
17320+
is equivalent to \code{$T$ $v$ = \NULL;}.
17321+
17322+
%% TODO(eernst): Revise when flow analysis is added.
17323+
\commentary{%
17324+
If $T$ is a potentially non-nullable type
17325+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17326+
but an expression that gives rise to evaluation of $v$
17327+
is a compile-time error unless flow analysis
17328+
(\ref{flowAnalysis})
17329+
shows that the variable is guaranteed to have been initialized.%
17330+
}
17331+
17332+
\LMHash{}%
17333+
A local variable has an associated
17334+
\IndexCustom{declared type}{local variable!declared type}
17335+
which is determined from its declaration.
17336+
A local variable also has an associated
17337+
\IndexCustom{type}{local variable!type}
17338+
which is determined by flow analysis
17339+
(\ref{flowAnalysis})
17340+
via a process known as type promotion
17341+
(\ref{typePromotion}).
1732317342

1732417343
\LMHash{}%
17325-
The type of a local variable with a declaration of one of the forms
17326-
\code{$T$ $v$ = $e$;}
17327-
\code{\CONST{} $T$ $v$ = $e$;}
17328-
\code{\FINAL{} $T$ $v$ = $e$;}
17344+
The declared type of a local variable with a declaration of one of the forms
17345+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17346+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17347+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1732917348
is $T$.
17330-
The type of a local variable with a declaration of one of the forms
17331-
\code{\VAR{} $v$ = $e$;}
17332-
\code{\CONST{} $v$ = $e$;}
17333-
\code{\FINAL{} $v$ = $e$;}
17334-
is \DYNAMIC.
17349+
17350+
\LMHash{}%
17351+
The declared type of a local variable with a declaration of one of the forms
17352+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17353+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17354+
\code{\CONST\,\,$v$ = $e$;}
17355+
is determined as follows:
17356+
17357+
\begin{itemize}
17358+
\item
17359+
If the static type of $e$ is \code{Null} then
17360+
the declared type of $v$ is \DYNAMIC.
17361+
\item
17362+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17363+
where $X$ is a type variable
17364+
(\ref{intersectionTypes}),
17365+
the declared type of $v$ is \code{X}.
17366+
\commentary{%
17367+
In this case $v$ is immediately promoted
17368+
(\ref{typePromotion}).%
17369+
}
17370+
\item
17371+
Otherwise, the declared type of $v$ is the static type of $e$.
17372+
\end{itemize}
1733517373

1733617374
\LMHash{}%
1733717375
Let $v$ be a local variable declared by an initializing variable declaration,
1733817376
and let $e$ be the associated initializing expression.
17339-
It is a compile-time error if the static type of $e$ is not assignable to
17340-
the type of $v$.
17341-
It is a compile-time error if a local variable $v$ is final,
17342-
and the declaration of $v$ is not an initializing variable declaration.
17377+
It is a
17378+
\Error{compile-time error} if the static type of $e$ is not assignable to
17379+
the declared type of $v$.
1734317380

17381+
%% TODO(eernst): Revise when flow analysis is added.
1734417382
\commentary{%
17345-
It is also a compile-time error to assign to a final local variable
17383+
If a local variable $v$ is \FINAL{} and not \LATE,
17384+
it is not a compile-time error if the declaration of $v$ is
17385+
not an initializing variable declaration,
17386+
but an expression that gives rise to evaluation of $v$
17387+
is a compile-time error unless flow analysis shows that
17388+
the variable is guaranteed to have been initialized.
17389+
Similarly, an expression that gives rise to an assignment to $v$
17390+
is a compile-time error unless flow analysis shows that
17391+
it is guaranteed that the variable has \emph{not} been initialized.%
17392+
}
17393+
17394+
\commentary{%
17395+
It is a compile-time error to assign to a local variable
17396+
which is \FINAL{} and not \LATE{}
1734617397
(\ref{assignment}).%
1734717398
}
1734817399

1734917400
\LMHash{}%
17350-
It is a compile-time error if
17401+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17402+
that declares a variable $v$,
17403+
which has an initializing expression $e$.
17404+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17405+
(\ref{awaitExpressions}),
17406+
unless there is a function $f$ which is
17407+
the immediately enclosing function for $a$,
17408+
and $f$ is not the immediately enclosing function for $D$.
17409+
17410+
\commentary{%
17411+
In other words,
17412+
the initializing expression cannot await an expression directly,
17413+
any await expressions must be nested inside some other function,
17414+
that is, a function literal.%
17415+
}
17416+
17417+
\LMHash{}%
17418+
It is a \Error{compile-time error} if
1735117419
a local variable is referenced at a source code location that is before
1735217420
the end of its initializing expression, if any,
1735317421
and otherwise before the declaring occurrence of
@@ -17435,15 +17503,39 @@ \subsection{Local Variable Declaration}
1743517503

1743617504
\LMHash{}%
1743717505
The expression $e$ is evaluated to an object $o$.
17438-
Then, the variable $v$ is set to $o$.
17439-
% This error can occur due to implicit casts.
17506+
% The following error can occur due to implicit casts.
1744017507
A dynamic type error occurs
17441-
if the dynamic type of $o$ is not a subtype of the actual type
17508+
if the dynamic type of $o$ is not a subtype of the actual declared type
1744217509
(\ref{actualTypes})
1744317510
of $v$.
17511+
Otherwise, the variable $v$ is bound to $o$.
1744417512

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

1744817540

1744917541
\subsection{Local Function Declaration}
@@ -20853,7 +20945,7 @@ \subsection{Subtypes}
2085320945
may find the meaning of the given notation obvious,
2085420946
but we still need to clarify a few details about how to handle
2085520947
syntactically different denotations of the same type,
20856-
and how to choose the right initial environment, $\Gamma$.
20948+
and how to choose the right initial environment, $\Delta$.
2085720949
%
2085820950
For a reader who is not familiar with the notation used in this section,
2085920951
the explanations given here should suffice to clarify what it means,
@@ -20956,7 +21048,7 @@ \subsection{Subtypes}
2095621048
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
2095721049
S}{X \& T}
2095821050
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20959-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21051+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2096021052
\end{minipage}
2096121053
\begin{minipage}[c]{0.49\textwidth}
2096221054
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20969,26 +21061,26 @@ \subsection{Subtypes}
2096921061
%
2097021062
\ExtraVSP
2097121063
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20972-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20973-
\Subtype{\Gamma'}{S_0}{T_0} \\
21064+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21065+
\Subtype{\Delta'}{S_0}{T_0} \\
2097421066
n_1 \leq n_2 &
2097521067
n_1 + k_1 \geq n_2 + k_2 &
20976-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21068+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2097721069
\begin{array}{c}
20978-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21070+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2097921071
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2098021072
\end{array}}
2098121073
\ExtraVSP\ExtraVSP
2098221074
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
20983-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20984-
\Subtype{\Gamma'}{S_0}{T_0} &
20985-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21075+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21076+
\Subtype{\Delta'}{S_0}{T_0} &
21077+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2098621078
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2098721079
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
20988-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21080+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2098921081
\begin{array}{c}
20990-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
20991-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21082+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21083+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2099221084
\end{array}}
2099321085
%
2099421086
\ExtraVSP
@@ -21116,24 +21208,24 @@ \subsubsection{Subtype Rules}
2111621208

2111721209
\LMHash{}%
2111821210
The rules in Figure~\ref{fig:subtypeRules} use
21119-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21211+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2112021212
bounds of type variables.
21121-
$\Gamma$ is a partial function that maps type variables to types.
21213+
$\Delta$ is a partial function that maps type variables to types.
2112221214
At a given location where the type variables in scope are
2112321215
\TypeParametersStd{}
2112421216
(\commentary{as declared by enclosing classes and/or functions}),
2112521217
we define the environment as follows:
21126-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21218+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2112721219
\commentary{%
21128-
That is, $\Gamma(X_1) = B_1$, and so on,
21129-
and $\Gamma$ is undefined when applied to a type variable $Y$
21220+
That is, $\Delta(X_1) = B_1$, and so on,
21221+
and $\Delta$ is undefined when applied to a type variable $Y$
2113021222
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2113121223
}
2113221224
When the rules are used to show that a given subtype relationship exists,
21133-
this is the initial value of $\Gamma$.
21225+
this is the initial value of $\Delta$.
2113421226

2113521227
\LMHash{}%
21136-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21228+
If a generic function type is encountered, an extension of $\Delta$ is used,
2113721229
as shown in the rules~\SrnPositionalFunctionType{}
2113821230
and~\SrnNamedFunctionType{}
2113921231
of Figure~\ref{fig:subtypeRules}.
@@ -21190,9 +21282,9 @@ \subsubsection{Being a subtype}
2119021282

2119121283
\LMHash{}%
2119221284
A type $S$ is shown to be a \Index{subtype} of another type $T$
21193-
in an environment $\Gamma$ by providing
21285+
in an environment $\Delta$ by providing
2119421286
an instantiation of a rule $R$ whose conclusion is
21195-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21287+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2119621288
along with rule instantiations showing
2119721289
each of the premises of $R$,
2119821290
continuing until a rule with no premises is reached.
@@ -21318,19 +21410,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2131821410
a subtype of \code{FutureOr<$T$>}.
2131921411

2132021412
Another example is the wording in rule~\SrnReflexivity{}:
21321-
``\ldots{} in any environment $\Gamma$'',
21413+
``\ldots{} in any environment $\Delta$'',
2132221414
which indicates that the rule can be applied no matter which bindings
2132321415
of type variables to bounds there exist in the environment.
2132421416
It should be noted that the environment matters even with rules
21325-
where it is simply stated as a plain $\Gamma$ in the conclusion
21417+
where it is simply stated as a plain $\Delta$ in the conclusion
2132621418
and in one or more premises,
2132721419
because the proof of those premises could, directly or indirectly,
2132821420
include the application of a rule where the environment is used.
2132921421

2133021422
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2133121423
\begin{itemize}
2133221424
\Item{\SrnReflexivity}{Reflexivity}
21333-
Every type is a subtype of itself, in any environment $\Gamma$.
21425+
Every type is a subtype of itself, in any environment $\Delta$.
2133421426
In the following rules except for a few,
2133521427
the rule is also valid in any environment
2133621428
and the environment is never used explicitly,
@@ -21381,7 +21473,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2138121473
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2138221474
The type variable $X$ is a subtype of a type $T$ if
2138321475
the bound of $X$
21384-
(as specified in the current environment $\Gamma$)
21476+
(as specified in the current environment $\Delta$)
2138521477
is a subtype of $T$.
2138621478
\Item{\SrnRightFunction}{Right Function}
2138721479
Every function type is a subtype of the type \FUNCTION.
@@ -21401,7 +21493,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2140121493
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2140221494
For every subtype relation considered in this rule,
2140321495
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21404-
(as reflected in the use of the extended environment $\Gamma'$).
21496+
(as reflected in the use of the extended environment $\Delta'$).
2140521497
We can assume without loss of generality
2140621498
that the names of type variables are pairwise identical,
2140721499
because we consider types of generic functions to be equivalent under
@@ -21466,14 +21558,14 @@ \subsubsection{Additional Subtyping Concepts}
2146621558
\LMLabel{additionalSubtypingConcepts}
2146721559

2146821560
\LMHash{}%
21469-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21561+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2147021562
written \SupertypeStd{S}{T},
2147121563
if{}f \SubtypeStd{T}{S}.
2147221564

2147321565
\LMHash{}%
2147421566
A type $T$
2147521567
\Index{may be assigned}
21476-
to a type $S$ in an environment $\Gamma$,
21568+
to a type $S$ in an environment $\Delta$,
2147721569
written \AssignableStd{S}{T},
2147821570
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2147921571
In this case we say that the types $S$ and $T$ are
@@ -22572,7 +22664,7 @@ \section*{Appendix: Algorithmic Subtyping}
2257222664
\begin{minipage}[c]{0.49\textwidth}
2257322665
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2257422666
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22575-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22667+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2257622668
\end{minipage}
2257722669
\begin{minipage}[c]{0.49\textwidth}
2257822670
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)