Skip to content

Commit 6bc3840

Browse files
committed
Update section Local Variable Declaration, update \Gamma to \Delta
1 parent 34bc126 commit 6bc3840

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

17346+
%% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1734617347
\LMHash{}%
1734717348
We say that a local variable $v$ is \Index{potentially mutated}
1734817349
in some scope $s$
@@ -17351,41 +17352,108 @@ \subsection{Local Variable Declaration}
1735117352
\LMHash{}%
1735217353
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1735317354
\code{\VAR{} $v$ = \NULL;}.
17354-
A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17355-
\code{$T$ $v$ = \NULL;}.
17356-
17357-
\commentary{%
17358-
This holds regardless of the type $T$.
17359-
E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17360-
}
17355+
If $T$ is a nullable type
17356+
(\ref{typeNullability})
17357+
then a local variable declaration of the form \code{$T$ $v$;}
17358+
is equivalent to \code{$T$ $v$ = \NULL;}.
17359+
17360+
%% TODO(eernst): Revise when flow analysis is added.
17361+
\commentary{%
17362+
If $T$ is a potentially non-nullable type
17363+
then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17364+
but an expression that gives rise to evaluation of $v$
17365+
is a compile-time error unless flow analysis
17366+
(\ref{flowAnalysis})
17367+
shows that the variable is guaranteed to have been initialized.%
17368+
}
17369+
17370+
\LMHash{}%
17371+
A local variable has an associated
17372+
\IndexCustom{declared type}{local variable!declared type}
17373+
which is determined from its declaration.
17374+
A local variable also has an associated
17375+
\IndexCustom{type}{local variable!type}
17376+
which is determined by flow analysis
17377+
(\ref{flowAnalysis})
17378+
via a process known as type promotion
17379+
(\ref{typePromotion}).
1736117380

1736217381
\LMHash{}%
17363-
The type of a local variable with a declaration of one of the forms
17364-
\code{$T$ $v$ = $e$;}
17365-
\code{\CONST{} $T$ $v$ = $e$;}
17366-
\code{\FINAL{} $T$ $v$ = $e$;}
17382+
The declared type of a local variable with a declaration of one of the forms
17383+
\code{\LATE?\,\,$T$\,\,$v$ = $e$;}
17384+
\code{\LATE?\,\,\FINAL\,\,$T$\,\,$v$ = $e$;}
17385+
\code{\CONST\,\,$T$\,\,$v$ = $e$;}
1736717386
is $T$.
17368-
The type of a local variable with a declaration of one of the forms
17369-
\code{\VAR{} $v$ = $e$;}
17370-
\code{\CONST{} $v$ = $e$;}
17371-
\code{\FINAL{} $v$ = $e$;}
17372-
is \DYNAMIC.
17387+
17388+
\LMHash{}%
17389+
The declared type of a local variable with a declaration of one of the forms
17390+
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17391+
\code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17392+
\code{\CONST\,\,$v$ = $e$;}
17393+
is determined as follows:
17394+
17395+
\begin{itemize}
17396+
\item
17397+
If the static type of $e$ is \code{Null} then
17398+
the declared type of $v$ is \DYNAMIC.
17399+
\item
17400+
If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17401+
where $X$ is a type variable
17402+
(\ref{intersectionTypes}),
17403+
the declared type of $v$ is \code{X}.
17404+
\commentary{%
17405+
In this case $v$ is immediately promoted
17406+
(\ref{typePromotion}).%
17407+
}
17408+
\item
17409+
Otherwise, the declared type of $v$ is the static type of $e$.
17410+
\end{itemize}
1737317411

1737417412
\LMHash{}%
1737517413
Let $v$ be a local variable declared by an initializing variable declaration,
1737617414
and let $e$ be the associated initializing expression.
17377-
It is a compile-time error if the static type of $e$ is not assignable to
17378-
the type of $v$.
17379-
It is a compile-time error if a local variable $v$ is final,
17380-
and the declaration of $v$ is not an initializing variable declaration.
17415+
It is a
17416+
\Error{compile-time error} if the static type of $e$ is not assignable to
17417+
the declared type of $v$.
1738117418

17419+
%% TODO(eernst): Revise when flow analysis is added.
1738217420
\commentary{%
17383-
It is also a compile-time error to assign to a final local variable
17421+
If a local variable $v$ is \FINAL{} and not \LATE,
17422+
it is not a compile-time error if the declaration of $v$ is
17423+
not an initializing variable declaration,
17424+
but an expression that gives rise to evaluation of $v$
17425+
is a compile-time error unless flow analysis shows that
17426+
the variable is guaranteed to have been initialized.
17427+
Similarly, an expression that gives rise to an assignment to $v$
17428+
is a compile-time error unless flow analysis shows that
17429+
it is guaranteed that the variable has \emph{not} been initialized.%
17430+
}
17431+
17432+
\commentary{%
17433+
It is a compile-time error to assign to a local variable
17434+
which is \FINAL{} and not \LATE{}
1738417435
(\ref{assignment}).%
1738517436
}
1738617437

1738717438
\LMHash{}%
17388-
It is a compile-time error if
17439+
Assume that $D$ is a local variable declaration with the modifier \LATE{}
17440+
that declares a variable $v$,
17441+
which has an initializing expression $e$.
17442+
It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17443+
(\ref{awaitExpressions}),
17444+
unless there is a function $f$ which is
17445+
the immediately enclosing function for $a$,
17446+
and $f$ is not the immediately enclosing function for $D$.
17447+
17448+
\commentary{%
17449+
In other words,
17450+
the initializing expression cannot await an expression directly,
17451+
any await expressions must be nested inside some other function,
17452+
that is, a function literal.%
17453+
}
17454+
17455+
\LMHash{}%
17456+
It is a \Error{compile-time error} if
1738917457
a local variable is referenced at a source code location that is before
1739017458
the end of its initializing expression, if any,
1739117459
and otherwise before the declaring occurrence of
@@ -17473,15 +17541,39 @@ \subsection{Local Variable Declaration}
1747317541

1747417542
\LMHash{}%
1747517543
The expression $e$ is evaluated to an object $o$.
17476-
Then, the variable $v$ is set to $o$.
17477-
% This error can occur due to implicit casts.
17544+
% The following error can occur due to implicit casts.
1747817545
A dynamic type error occurs
17479-
if the dynamic type of $o$ is not a subtype of the actual type
17546+
if the dynamic type of $o$ is not a subtype of the actual declared type
1748017547
(\ref{actualTypes})
1748117548
of $v$.
17549+
Otherwise, the variable $v$ is bound to $o$.
1748217550

17483-
% The local variable discussion does not mention how to read/write locals.
17484-
% Consider spelling this out, in terms of storage.
17551+
\LMHash{}%
17552+
Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17553+
that declares a variable $v$.
17554+
If an object $o$ is assigned to $v$
17555+
in a situation where $v$ is unbound
17556+
then $v$ is bound to $o$.
17557+
If an object $o$ is assigned to $v$
17558+
in a situation where $v$ is bound to an object $o'$
17559+
then a dynamic error occurs
17560+
(\commentary{it does not matter whether $o$ is the same object as $o'$}).
17561+
17562+
\commentary{%
17563+
Note that this includes the implicit initializing writes induced by
17564+
evaluating the variable.
17565+
Hence, the following program encounters a dynamic error
17566+
when it evaluates \code{x},
17567+
just before it would call \code{print}.%
17568+
}
17569+
17570+
\begin{dartCode}
17571+
\VOID\ main() \{
17572+
int i = 0;
17573+
\LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17574+
print(x);
17575+
\}
17576+
\end{dartCode}
1748517577

1748617578

1748717579
\subsection{Local Function Declaration}
@@ -20896,7 +20988,7 @@ \subsection{Subtypes}
2089620988
may find the meaning of the given notation obvious,
2089720989
but we still need to clarify a few details about how to handle
2089820990
syntactically different denotations of the same type,
20899-
and how to choose the right initial environment, $\Gamma$.
20991+
and how to choose the right initial environment, $\Delta$.
2090020992
%
2090120993
For a reader who is not familiar with the notation used in this section,
2090220994
the explanations given here should suffice to clarify what it means,
@@ -20999,7 +21091,7 @@ \subsection{Subtypes}
2099921091
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
2100021092
S}{X \& T}
2100121093
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21002-
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma(X)}{T}{X}{T}
21094+
\Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta(X)}{T}{X}{T}
2100321095
\end{minipage}
2100421096
\begin{minipage}[c]{0.49\textwidth}
2100521097
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21012,26 +21104,26 @@ \subsection{Subtypes}
2101221104
%
2101321105
\ExtraVSP
2101421106
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21015-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21016-
\Subtype{\Gamma'}{S_0}{T_0} \\
21107+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21108+
\Subtype{\Delta'}{S_0}{T_0} \\
2101721109
n_1 \leq n_2 &
2101821110
n_1 + k_1 \geq n_2 + k_2 &
21019-
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma'}{T_j}{S_j}}{%
21111+
\forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta'}{T_j}{S_j}}{%
2102021112
\begin{array}{c}
21021-
\Gamma\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21113+
\Delta\vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2102221114
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2102321115
\end{array}}
2102421116
\ExtraVSP\ExtraVSP
2102521117
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
21026-
\Gamma' = \Gamma\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21027-
\Subtype{\Gamma'}{S_0}{T_0} &
21028-
\forall j \in 1 .. n\!:\;\Subtype{\Gamma'}{T_j}{S_j} \\
21118+
\Delta' = \Delta\uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21119+
\Subtype{\Delta'}{S_0}{T_0} &
21120+
\forall j \in 1 .. n\!:\;\Subtype{\Delta'}{T_j}{S_j} \\
2102921121
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2103021122
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21031-
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma'}{T_{n+p}}{S_{n+q}}}{%
21123+
y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta'}{T_{n+p}}{S_{n+q}}}{%
2103221124
\begin{array}{c}
21033-
\Gamma\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21034-
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21125+
\Delta\vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r}\;<:\;\\
21126+
\RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2103521127
\end{array}}
2103621128
%
2103721129
\ExtraVSP
@@ -21159,24 +21251,24 @@ \subsubsection{Subtype Rules}
2115921251

2116021252
\LMHash{}%
2116121253
The rules in Figure~\ref{fig:subtypeRules} use
21162-
the symbol \Index{$\Gamma$} to denote the given knowledge about the
21254+
the symbol \Index{$\Delta$} to denote the given knowledge about the
2116321255
bounds of type variables.
21164-
$\Gamma$ is a partial function that maps type variables to types.
21256+
$\Delta$ is a partial function that maps type variables to types.
2116521257
At a given location where the type variables in scope are
2116621258
\TypeParametersStd{}
2116721259
(\commentary{as declared by enclosing classes and/or functions}),
2116821260
we define the environment as follows:
21169-
$\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21261+
$\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2117021262
\commentary{%
21171-
That is, $\Gamma(X_1) = B_1$, and so on,
21172-
and $\Gamma$ is undefined when applied to a type variable $Y$
21263+
That is, $\Delta(X_1) = B_1$, and so on,
21264+
and $\Delta$ is undefined when applied to a type variable $Y$
2117321265
which is not in $\{\,\List{X}{1}{s}\,\}$.%
2117421266
}
2117521267
When the rules are used to show that a given subtype relationship exists,
21176-
this is the initial value of $\Gamma$.
21268+
this is the initial value of $\Delta$.
2117721269

2117821270
\LMHash{}%
21179-
If a generic function type is encountered, an extension of $\Gamma$ is used,
21271+
If a generic function type is encountered, an extension of $\Delta$ is used,
2118021272
as shown in the rules~\SrnPositionalFunctionType{}
2118121273
and~\SrnNamedFunctionType{}
2118221274
of Figure~\ref{fig:subtypeRules}.
@@ -21233,9 +21325,9 @@ \subsubsection{Being a subtype}
2123321325

2123421326
\LMHash{}%
2123521327
A type $S$ is shown to be a \Index{subtype} of another type $T$
21236-
in an environment $\Gamma$ by providing
21328+
in an environment $\Delta$ by providing
2123721329
an instantiation of a rule $R$ whose conclusion is
21238-
\IndexCustom{\SubtypeStd{S}{T}}{$\Gamma$@\SubtypeStd{S}{T}},
21330+
\IndexCustom{\SubtypeStd{S}{T}}{$\Delta$@\SubtypeStd{S}{T}},
2123921331
along with rule instantiations showing
2124021332
each of the premises of $R$,
2124121333
continuing until a rule with no premises is reached.
@@ -21361,19 +21453,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2136121453
a subtype of \code{FutureOr<$T$>}.
2136221454

2136321455
Another example is the wording in rule~\SrnReflexivity{}:
21364-
``\ldots{} in any environment $\Gamma$'',
21456+
``\ldots{} in any environment $\Delta$'',
2136521457
which indicates that the rule can be applied no matter which bindings
2136621458
of type variables to bounds there exist in the environment.
2136721459
It should be noted that the environment matters even with rules
21368-
where it is simply stated as a plain $\Gamma$ in the conclusion
21460+
where it is simply stated as a plain $\Delta$ in the conclusion
2136921461
and in one or more premises,
2137021462
because the proof of those premises could, directly or indirectly,
2137121463
include the application of a rule where the environment is used.
2137221464

2137321465
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2137421466
\begin{itemize}
2137521467
\Item{\SrnReflexivity}{Reflexivity}
21376-
Every type is a subtype of itself, in any environment $\Gamma$.
21468+
Every type is a subtype of itself, in any environment $\Delta$.
2137721469
In the following rules except for a few,
2137821470
the rule is also valid in any environment
2137921471
and the environment is never used explicitly,
@@ -21424,7 +21516,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2142421516
\Item{\SrnLeftVariableBound}{Left Variable Bound}
2142521517
The type variable $X$ is a subtype of a type $T$ if
2142621518
the bound of $X$
21427-
(as specified in the current environment $\Gamma$)
21519+
(as specified in the current environment $\Delta$)
2142821520
is a subtype of $T$.
2142921521
\Item{\SrnRightFunction}{Right Function}
2143021522
Every function type is a subtype of the type \FUNCTION.
@@ -21444,7 +21536,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2144421536
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2144521537
For every subtype relation considered in this rule,
2144621538
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21447-
(as reflected in the use of the extended environment $\Gamma'$).
21539+
(as reflected in the use of the extended environment $\Delta'$).
2144821540
We can assume without loss of generality
2144921541
that the names of type variables are pairwise identical,
2145021542
because we consider types of generic functions to be equivalent under
@@ -21509,14 +21601,14 @@ \subsubsection{Additional Subtyping Concepts}
2150921601
\LMLabel{additionalSubtypingConcepts}
2151021602

2151121603
\LMHash{}%
21512-
$S$ is a \Index{supertype} of $T$ in a given environment $\Gamma$,
21604+
$S$ is a \Index{supertype} of $T$ in a given environment $\Delta$,
2151321605
written \SupertypeStd{S}{T},
2151421606
if{}f \SubtypeStd{T}{S}.
2151521607

2151621608
\LMHash{}%
2151721609
A type $T$
2151821610
\Index{may be assigned}
21519-
to a type $S$ in an environment $\Gamma$,
21611+
to a type $S$ in an environment $\Delta$,
2152021612
written \AssignableStd{S}{T},
2152121613
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2152221614
In this case we say that the types $S$ and $T$ are
@@ -22625,7 +22717,7 @@ \section*{Appendix: Algorithmic Subtyping}
2262522717
\begin{minipage}[c]{0.49\textwidth}
2262622718
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2262722719
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22628-
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22720+
\Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta(X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2262922721
\end{minipage}
2263022722
\begin{minipage}[c]{0.49\textwidth}
2263122723
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}

0 commit comments

Comments
 (0)