@@ -17316,6 +17316,7 @@ \subsection{Local Variable Declaration}
1731617316apply to local variables with the same definitions as for other variables
1731717317(\ref{variables}).
1731817318
17319+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
1731917320\LMHash{}%
1732017321We say that a local variable $v$ is \Index{potentially mutated}
1732117322in some scope $s$
@@ -17324,41 +17325,108 @@ \subsection{Local Variable Declaration}
1732417325\LMHash{}%
1732517326A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
1732617327\code{\VAR{} $v$ = \NULL;}.
17327- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17328- \code{$T$ $v$ = \NULL;}.
17329-
17330- \commentary{%
17331- This holds regardless of the type $T$.
17332- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17333- }
17328+ If $T$ is a nullable type
17329+ (\ref{typeNullability})
17330+ then a local variable declaration of the form \code{$T$ $v$;}
17331+ is equivalent to \code{$T$ $v$ = \NULL;}.
17332+
17333+ %% TODO(eernst): Revise when flow analysis is added.
17334+ \commentary{%
17335+ If $T$ is a potentially non-nullable type
17336+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17337+ but an expression that gives rise to evaluation of $v$
17338+ is a compile-time error unless flow analysis
17339+ (\ref{flowAnalysis})
17340+ shows that the variable is guaranteed to have been initialized.%
17341+ }
17342+
17343+ \LMHash{}%
17344+ A local variable has an associated
17345+ \IndexCustom{declared type}{local variable!declared type}
17346+ which is determined from its declaration.
17347+ A local variable also has an associated
17348+ \IndexCustom{type}{local variable!type}
17349+ which is determined by flow analysis
17350+ (\ref{flowAnalysis})
17351+ via a process known as type promotion
17352+ (\ref{typePromotion}).
1733417353
1733517354\LMHash{}%
17336- The type of a local variable with a declaration of one of the forms
17337- \code{$T$ $v$ = $e$;}
17338- \code{\CONST{} $T$ $v$ = $e$;}
17339- \code{\FINAL{} $T$ $v$ = $e$;}
17355+ The declared type of a local variable with a declaration of one of the forms
17356+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17357+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17358+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
1734017359is $T$.
17341- The type of a local variable with a declaration of one of the forms
17342- \code{\VAR{} $v$ = $e$;}
17343- \code{\CONST{} $v$ = $e$;}
17344- \code{\FINAL{} $v$ = $e$;}
17345- is \DYNAMIC.
17360+
17361+ \LMHash{}%
17362+ The declared type of a local variable with a declaration of one of the forms
17363+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17364+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17365+ \code{\CONST\,\,$v$ = $e$;}
17366+ is determined as follows:
17367+
17368+ \begin{itemize}
17369+ \item
17370+ If the static type of $e$ is \code{Null} then
17371+ the declared type of $v$ is \DYNAMIC.
17372+ \item
17373+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17374+ where $X$ is a type variable
17375+ (\ref{intersectionTypes}),
17376+ the declared type of $v$ is \code{X}.
17377+ \commentary{%
17378+ In this case $v$ is immediately promoted
17379+ (\ref{typePromotion}).%
17380+ }
17381+ \item
17382+ Otherwise, the declared type of $v$ is the static type of $e$.
17383+ \end{itemize}
1734617384
1734717385\LMHash{}%
1734817386Let $v$ be a local variable declared by an initializing variable declaration,
1734917387and let $e$ be the associated initializing expression.
17350- It is a compile-time error if the static type of $e$ is not assignable to
17351- the type of $v$.
17352- It is a compile-time error if a local variable $v$ is final,
17353- and the declaration of $v$ is not an initializing variable declaration.
17388+ It is a
17389+ \Error{compile-time error} if the static type of $e$ is not assignable to
17390+ the declared type of $v$.
1735417391
17392+ %% TODO(eernst): Revise when flow analysis is added.
1735517393\commentary{%
17356- It is also a compile-time error to assign to a final local variable
17394+ If a local variable $v$ is \FINAL{} and not \LATE,
17395+ it is not a compile-time error if the declaration of $v$ is
17396+ not an initializing variable declaration,
17397+ but an expression that gives rise to evaluation of $v$
17398+ is a compile-time error unless flow analysis shows that
17399+ the variable is guaranteed to have been initialized.
17400+ Similarly, an expression that gives rise to an assignment to $v$
17401+ is a compile-time error unless flow analysis shows that
17402+ it is guaranteed that the variable has \emph{not} been initialized.%
17403+ }
17404+
17405+ \commentary{%
17406+ It is a compile-time error to assign to a local variable
17407+ which is \FINAL{} and not \LATE{}
1735717408(\ref{assignment}).%
1735817409}
1735917410
1736017411\LMHash{}%
17361- It is a compile-time error if
17412+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17413+ that declares a variable $v$,
17414+ which has an initializing expression $e$.
17415+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17416+ (\ref{awaitExpressions}),
17417+ unless there is a function $f$ which is
17418+ the immediately enclosing function for $a$,
17419+ and $f$ is not the immediately enclosing function for $D$.
17420+
17421+ \commentary{%
17422+ In other words,
17423+ the initializing expression cannot await an expression directly,
17424+ any await expressions must be nested inside some other function,
17425+ that is, a function literal.%
17426+ }
17427+
17428+ \LMHash{}%
17429+ It is a \Error{compile-time error} if
1736217430a local variable is referenced at a source code location that is before
1736317431the end of its initializing expression, if any,
1736417432and otherwise before the declaring occurrence of
@@ -17446,15 +17514,39 @@ \subsection{Local Variable Declaration}
1744617514
1744717515\LMHash{}%
1744817516The expression $e$ is evaluated to an object $o$.
17449- Then, the variable $v$ is set to $o$.
17450- % This error can occur due to implicit casts.
17517+ % The following error can occur due to implicit casts.
1745117518A dynamic type error occurs
17452- if the dynamic type of $o$ is not a subtype of the actual type
17519+ if the dynamic type of $o$ is not a subtype of the actual declared type
1745317520(\ref{actualTypes})
1745417521of $v$.
17522+ Otherwise, the variable $v$ is bound to $o$.
1745517523
17456- % The local variable discussion does not mention how to read/write locals.
17457- % Consider spelling this out, in terms of storage.
17524+ \LMHash{}%
17525+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17526+ that declares a variable $v$.
17527+ If an object $o$ is assigned to $v$
17528+ in a situation where $v$ is unbound
17529+ then $v$ is bound to $o$.
17530+ If an object $o$ is assigned to $v$
17531+ in a situation where $v$ is bound to an object $o'$
17532+ then a dynamic error occurs
17533+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17534+
17535+ \commentary{%
17536+ Note that this includes the implicit initializing writes induced by
17537+ evaluating the variable.
17538+ Hence, the following program encounters a dynamic error
17539+ when it evaluates \code{x},
17540+ just before it would call \code{print}.%
17541+ }
17542+
17543+ \begin{dartCode}
17544+ \VOID\ main() \{
17545+ int i = 0;
17546+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17547+ print(x);
17548+ \}
17549+ \end{dartCode}
1745817550
1745917551
1746017552\subsection{Local Function Declaration}
@@ -20864,7 +20956,7 @@ \subsection{Subtypes}
2086420956may find the meaning of the given notation obvious,
2086520957but we still need to clarify a few details about how to handle
2086620958syntactically different denotations of the same type,
20867- and how to choose the right initial environment, $\Gamma $.
20959+ and how to choose the right initial environment, $\Delta $.
2086820960%
2086920961For a reader who is not familiar with the notation used in this section,
2087020962the explanations given here should suffice to clarify what it means,
@@ -20967,7 +21059,7 @@ \subsection{Subtypes}
2096721059 \RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
2096821060 S}{X \& T}
2096921061 \Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20970- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21062+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
2097121063 \end{minipage}
2097221064 \begin{minipage}[c]{0.49\textwidth}
2097321065 \Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20980,26 +21072,26 @@ \subsection{Subtypes}
2098021072 %
2098121073 \ExtraVSP
2098221074 \RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20983- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20984- \Subtype{\Gamma '}{S_0}{T_0} \\
21075+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21076+ \Subtype{\Delta '}{S_0}{T_0} \\
2098521077 n_1 \leq n_2 &
2098621078 n_1 + k_1 \geq n_2 + k_2 &
20987- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21079+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
2098821080 \begin{array}{c}
20989- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21081+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
2099021082 \RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
2099121083 \end{array}}
2099221084 \ExtraVSP\ExtraVSP
2099321085 \RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
20994- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20995- \Subtype{\Gamma '}{S_0}{T_0} &
20996- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21086+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21087+ \Subtype{\Delta '}{S_0}{T_0} &
21088+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
2099721089 \{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
2099821090 \forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
20999- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21091+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
2100021092 \begin{array}{c}
21001- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21002- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21093+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21094+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
2100321095 \end{array}}
2100421096 %
2100521097 \ExtraVSP
@@ -21127,24 +21219,24 @@ \subsubsection{Subtype Rules}
2112721219
2112821220\LMHash{}%
2112921221The rules in Figure~\ref{fig:subtypeRules} use
21130- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21222+ the symbol \Index{$\Delta $} to denote the given knowledge about the
2113121223bounds of type variables.
21132- $\Gamma $ is a partial function that maps type variables to types.
21224+ $\Delta $ is a partial function that maps type variables to types.
2113321225At a given location where the type variables in scope are
2113421226\TypeParametersStd{}
2113521227(\commentary{as declared by enclosing classes and/or functions}),
2113621228we define the environment as follows:
21137- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21229+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
2113821230\commentary{%
21139- That is, $\Gamma (X_1) = B_1$, and so on,
21140- and $\Gamma $ is undefined when applied to a type variable $Y$
21231+ That is, $\Delta (X_1) = B_1$, and so on,
21232+ and $\Delta $ is undefined when applied to a type variable $Y$
2114121233which is not in $\{\,\List{X}{1}{s}\,\}$.%
2114221234}
2114321235When the rules are used to show that a given subtype relationship exists,
21144- this is the initial value of $\Gamma $.
21236+ this is the initial value of $\Delta $.
2114521237
2114621238\LMHash{}%
21147- If a generic function type is encountered, an extension of $\Gamma $ is used,
21239+ If a generic function type is encountered, an extension of $\Delta $ is used,
2114821240as shown in the rules~\SrnPositionalFunctionType{}
2114921241and~\SrnNamedFunctionType{}
2115021242of Figure~\ref{fig:subtypeRules}.
@@ -21201,9 +21293,9 @@ \subsubsection{Being a subtype}
2120121293
2120221294\LMHash{}%
2120321295A type $S$ is shown to be a \Index{subtype} of another type $T$
21204- in an environment $\Gamma $ by providing
21296+ in an environment $\Delta $ by providing
2120521297an instantiation of a rule $R$ whose conclusion is
21206- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21298+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
2120721299along with rule instantiations showing
2120821300each of the premises of $R$,
2120921301continuing until a rule with no premises is reached.
@@ -21329,19 +21421,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
2132921421a subtype of \code{FutureOr<$T$>}.
2133021422
2133121423Another example is the wording in rule~\SrnReflexivity{}:
21332- ``\ldots{} in any environment $\Gamma $'',
21424+ ``\ldots{} in any environment $\Delta $'',
2133321425which indicates that the rule can be applied no matter which bindings
2133421426of type variables to bounds there exist in the environment.
2133521427It should be noted that the environment matters even with rules
21336- where it is simply stated as a plain $\Gamma $ in the conclusion
21428+ where it is simply stated as a plain $\Delta $ in the conclusion
2133721429and in one or more premises,
2133821430because the proof of those premises could, directly or indirectly,
2133921431include the application of a rule where the environment is used.
2134021432
2134121433\def\Item#1#2{\item[#1]{\textbf{#2:}}}
2134221434\begin{itemize}
2134321435\Item{\SrnReflexivity}{Reflexivity}
21344- Every type is a subtype of itself, in any environment $\Gamma $.
21436+ Every type is a subtype of itself, in any environment $\Delta $.
2134521437 In the following rules except for a few,
2134621438 the rule is also valid in any environment
2134721439 and the environment is never used explicitly,
@@ -21392,7 +21484,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2139221484\Item{\SrnLeftVariableBound}{Left Variable Bound}
2139321485 The type variable $X$ is a subtype of a type $T$ if
2139421486 the bound of $X$
21395- (as specified in the current environment $\Gamma $)
21487+ (as specified in the current environment $\Delta $)
2139621488 is a subtype of $T$.
2139721489\Item{\SrnRightFunction}{Right Function}
2139821490 Every function type is a subtype of the type \FUNCTION.
@@ -21412,7 +21504,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
2141221504 is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
2141321505 For every subtype relation considered in this rule,
2141421506 the formal type parameters of $F_1$ and $F_2$ must be taken into account
21415- (as reflected in the use of the extended environment $\Gamma '$).
21507+ (as reflected in the use of the extended environment $\Delta '$).
2141621508 We can assume without loss of generality
2141721509 that the names of type variables are pairwise identical,
2141821510 because we consider types of generic functions to be equivalent under
@@ -21477,14 +21569,14 @@ \subsubsection{Additional Subtyping Concepts}
2147721569\LMLabel{additionalSubtypingConcepts}
2147821570
2147921571\LMHash{}%
21480- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21572+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
2148121573written \SupertypeStd{S}{T},
2148221574if{}f \SubtypeStd{T}{S}.
2148321575
2148421576\LMHash{}%
2148521577A type $T$
2148621578\Index{may be assigned}
21487- to a type $S$ in an environment $\Gamma $,
21579+ to a type $S$ in an environment $\Delta $,
2148821580written \AssignableStd{S}{T},
2148921581if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
2149021582In this case we say that the types $S$ and $T$ are
@@ -22583,7 +22675,7 @@ \section*{Appendix: Algorithmic Subtyping}
2258322675 \begin{minipage}[c]{0.49\textwidth}
2258422676 \RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
2258522677 \Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22586- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22678+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
2258722679 \end{minipage}
2258822680 \begin{minipage}[c]{0.49\textwidth}
2258922681 \Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments