@@ -17582,6 +17582,7 @@ \subsection{Local Variable Declaration}
17582
17582
apply to local variables with the same definitions as for other variables
17583
17583
(\ref{variables}).
17584
17584
17585
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17585
17586
\LMHash{}%
17586
17587
We say that a local variable $v$ is \Index{potentially mutated}
17587
17588
in some scope $s$
@@ -17590,41 +17591,108 @@ \subsection{Local Variable Declaration}
17590
17591
\LMHash{}%
17591
17592
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17592
17593
\code{\VAR{} $v$ = \NULL;}.
17593
- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17594
- \code{$T$ $v$ = \NULL;}.
17595
-
17596
- \commentary{%
17597
- This holds regardless of the type $T$.
17598
- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17599
- }
17594
+ If $T$ is a nullable type
17595
+ (\ref{typeNullability})
17596
+ then a local variable declaration of the form \code{$T$ $v$;}
17597
+ is equivalent to \code{$T$ $v$ = \NULL;}.
17598
+
17599
+ %% TODO(eernst): Revise when flow analysis is added.
17600
+ \commentary{%
17601
+ If $T$ is a potentially non-nullable type
17602
+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17603
+ but an expression that gives rise to evaluation of $v$
17604
+ is a compile-time error unless flow analysis
17605
+ (\ref{flowAnalysis})
17606
+ shows that the variable is guaranteed to have been initialized.%
17607
+ }
17608
+
17609
+ \LMHash{}%
17610
+ A local variable has an associated
17611
+ \IndexCustom{declared type}{local variable!declared type}
17612
+ which is determined from its declaration.
17613
+ A local variable also has an associated
17614
+ \IndexCustom{type}{local variable!type}
17615
+ which is determined by flow analysis
17616
+ (\ref{flowAnalysis})
17617
+ via a process known as type promotion
17618
+ (\ref{typePromotion}).
17600
17619
17601
17620
\LMHash{}%
17602
- The type of a local variable with a declaration of one of the forms
17603
- \code{$T$ $v$ = $e$;}
17604
- \code{\CONST{} $T$ $v$ = $e$;}
17605
- \code{\FINAL{} $T$ $v$ = $e$;}
17621
+ The declared type of a local variable with a declaration of one of the forms
17622
+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17623
+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17624
+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
17606
17625
is $T$.
17607
- The type of a local variable with a declaration of one of the forms
17608
- \code{\VAR{} $v$ = $e$;}
17609
- \code{\CONST{} $v$ = $e$;}
17610
- \code{\FINAL{} $v$ = $e$;}
17611
- is \DYNAMIC.
17626
+
17627
+ \LMHash{}%
17628
+ The declared type of a local variable with a declaration of one of the forms
17629
+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17630
+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17631
+ \code{\CONST\,\,$v$ = $e$;}
17632
+ is determined as follows:
17633
+
17634
+ \begin{itemize}
17635
+ \item
17636
+ If the static type of $e$ is \code{Null} then
17637
+ the declared type of $v$ is \DYNAMIC.
17638
+ \item
17639
+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17640
+ where $X$ is a type variable
17641
+ (\ref{intersectionTypes}),
17642
+ the declared type of $v$ is \code{X}.
17643
+ \commentary{%
17644
+ In this case $v$ is immediately promoted
17645
+ (\ref{typePromotion}).%
17646
+ }
17647
+ \item
17648
+ Otherwise, the declared type of $v$ is the static type of $e$.
17649
+ \end{itemize}
17612
17650
17613
17651
\LMHash{}%
17614
17652
Let $v$ be a local variable declared by an initializing variable declaration,
17615
17653
and let $e$ be the associated initializing expression.
17616
- It is a compile-time error if the static type of $e$ is not assignable to
17617
- the type of $v$.
17618
- It is a compile-time error if a local variable $v$ is final,
17619
- and the declaration of $v$ is not an initializing variable declaration.
17654
+ It is a
17655
+ \Error{compile-time error} if the static type of $e$ is not assignable to
17656
+ the declared type of $v$.
17620
17657
17658
+ %% TODO(eernst): Revise when flow analysis is added.
17621
17659
\commentary{%
17622
- It is also a compile-time error to assign to a final local variable
17660
+ If a local variable $v$ is \FINAL{} and not \LATE,
17661
+ it is not a compile-time error if the declaration of $v$ is
17662
+ not an initializing variable declaration,
17663
+ but an expression that gives rise to evaluation of $v$
17664
+ is a compile-time error unless flow analysis shows that
17665
+ the variable is guaranteed to have been initialized.
17666
+ Similarly, an expression that gives rise to an assignment to $v$
17667
+ is a compile-time error unless flow analysis shows that
17668
+ it is guaranteed that the variable has \emph{not} been initialized.%
17669
+ }
17670
+
17671
+ \commentary{%
17672
+ It is a compile-time error to assign to a local variable
17673
+ which is \FINAL{} and not \LATE{}
17623
17674
(\ref{assignment}).%
17624
17675
}
17625
17676
17626
17677
\LMHash{}%
17627
- It is a compile-time error if
17678
+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17679
+ that declares a variable $v$,
17680
+ which has an initializing expression $e$.
17681
+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17682
+ (\ref{awaitExpressions}),
17683
+ unless there is a function $f$ which is
17684
+ the immediately enclosing function for $a$,
17685
+ and $f$ is not the immediately enclosing function for $D$.
17686
+
17687
+ \commentary{%
17688
+ In other words,
17689
+ the initializing expression cannot await an expression directly,
17690
+ any await expressions must be nested inside some other function,
17691
+ that is, a function literal.%
17692
+ }
17693
+
17694
+ \LMHash{}%
17695
+ It is a \Error{compile-time error} if
17628
17696
a local variable is referenced at a source code location that is before
17629
17697
the end of its initializing expression, if any,
17630
17698
and otherwise before the declaring occurrence of
@@ -17712,15 +17780,39 @@ \subsection{Local Variable Declaration}
17712
17780
17713
17781
\LMHash{}%
17714
17782
The expression $e$ is evaluated to an object $o$.
17715
- Then, the variable $v$ is set to $o$.
17716
- % This error can occur due to implicit casts.
17783
+ % The following error can occur due to implicit casts.
17717
17784
A dynamic type error occurs
17718
- if the dynamic type of $o$ is not a subtype of the actual type
17785
+ if the dynamic type of $o$ is not a subtype of the actual declared type
17719
17786
(\ref{actualTypes})
17720
17787
of $v$.
17788
+ Otherwise, the variable $v$ is bound to $o$.
17721
17789
17722
- % The local variable discussion does not mention how to read/write locals.
17723
- % Consider spelling this out, in terms of storage.
17790
+ \LMHash{}%
17791
+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17792
+ that declares a variable $v$.
17793
+ If an object $o$ is assigned to $v$
17794
+ in a situation where $v$ is unbound
17795
+ then $v$ is bound to $o$.
17796
+ If an object $o$ is assigned to $v$
17797
+ in a situation where $v$ is bound to an object $o'$
17798
+ then a dynamic error occurs
17799
+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17800
+
17801
+ \commentary{%
17802
+ Note that this includes the implicit initializing writes induced by
17803
+ evaluating the variable.
17804
+ Hence, the following program encounters a dynamic error
17805
+ when it evaluates \code{x},
17806
+ just before it would call \code{print}.%
17807
+ }
17808
+
17809
+ \begin{dartCode}
17810
+ \VOID\ main() \{
17811
+ int i = 0;
17812
+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17813
+ print(x);
17814
+ \}
17815
+ \end{dartCode}
17724
17816
17725
17817
17726
17818
\subsection{Local Function Declaration}
@@ -21151,7 +21243,7 @@ \subsection{Subtypes}
21151
21243
may find the meaning of the given notation obvious,
21152
21244
but we still need to clarify a few details about how to handle
21153
21245
syntactically different denotations of the same type,
21154
- and how to choose the right initial environment, $\Gamma $.
21246
+ and how to choose the right initial environment, $\Delta $.
21155
21247
%
21156
21248
For a reader who is not familiar with the notation used in this section,
21157
21249
the explanations given here should suffice to clarify what it means,
@@ -21254,7 +21346,7 @@ \subsection{Subtypes}
21254
21346
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
21255
21347
S}{X \& T}
21256
21348
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21257
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21349
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
21258
21350
\end{minipage}
21259
21351
\begin{minipage}[c]{0.49\textwidth}
21260
21352
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21267,26 +21359,26 @@ \subsection{Subtypes}
21267
21359
%
21268
21360
\ExtraVSP
21269
21361
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21270
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21271
- \Subtype{\Gamma '}{S_0}{T_0} \\
21362
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21363
+ \Subtype{\Delta '}{S_0}{T_0} \\
21272
21364
n_1 \leq n_2 &
21273
21365
n_1 + k_1 \geq n_2 + k_2 &
21274
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21366
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
21275
21367
\begin{array}{c}
21276
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21368
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21277
21369
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
21278
21370
\end{array}}
21279
21371
\ExtraVSP\ExtraVSP
21280
21372
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21281
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21282
- \Subtype{\Gamma '}{S_0}{T_0} &
21283
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21373
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21374
+ \Subtype{\Delta '}{S_0}{T_0} &
21375
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
21284
21376
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
21285
21377
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21286
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21378
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21287
21379
\begin{array}{c}
21288
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21289
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21380
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21381
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21290
21382
\end{array}}
21291
21383
%
21292
21384
\ExtraVSP
@@ -21414,24 +21506,24 @@ \subsubsection{Subtype Rules}
21414
21506
21415
21507
\LMHash{}%
21416
21508
The rules in Figure~\ref{fig:subtypeRules} use
21417
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21509
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21418
21510
bounds of type variables.
21419
- $\Gamma $ is a partial function that maps type variables to types.
21511
+ $\Delta $ is a partial function that maps type variables to types.
21420
21512
At a given location where the type variables in scope are
21421
21513
\TypeParametersStd{}
21422
21514
(\commentary{as declared by enclosing classes and/or functions}),
21423
21515
we define the environment as follows:
21424
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21516
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21425
21517
\commentary{%
21426
- That is, $\Gamma (X_1) = B_1$, and so on,
21427
- and $\Gamma $ is undefined when applied to a type variable $Y$
21518
+ That is, $\Delta (X_1) = B_1$, and so on,
21519
+ and $\Delta $ is undefined when applied to a type variable $Y$
21428
21520
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21429
21521
}
21430
21522
When the rules are used to show that a given subtype relationship exists,
21431
- this is the initial value of $\Gamma $.
21523
+ this is the initial value of $\Delta $.
21432
21524
21433
21525
\LMHash{}%
21434
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21526
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21435
21527
as shown in the rules~\SrnPositionalFunctionType{}
21436
21528
and~\SrnNamedFunctionType{}
21437
21529
of Figure~\ref{fig:subtypeRules}.
@@ -21488,9 +21580,9 @@ \subsubsection{Being a subtype}
21488
21580
21489
21581
\LMHash{}%
21490
21582
A type $S$ is shown to be a \Index{subtype} of another type $T$
21491
- in an environment $\Gamma $ by providing
21583
+ in an environment $\Delta $ by providing
21492
21584
an instantiation of a rule $R$ whose conclusion is
21493
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21585
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21494
21586
along with rule instantiations showing
21495
21587
each of the premises of $R$,
21496
21588
continuing until a rule with no premises is reached.
@@ -21616,19 +21708,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21616
21708
a subtype of \code{FutureOr<$T$>}.
21617
21709
21618
21710
Another example is the wording in rule~\SrnReflexivity{}:
21619
- ``\ldots{} in any environment $\Gamma $'',
21711
+ ``\ldots{} in any environment $\Delta $'',
21620
21712
which indicates that the rule can be applied no matter which bindings
21621
21713
of type variables to bounds there exist in the environment.
21622
21714
It should be noted that the environment matters even with rules
21623
- where it is simply stated as a plain $\Gamma $ in the conclusion
21715
+ where it is simply stated as a plain $\Delta $ in the conclusion
21624
21716
and in one or more premises,
21625
21717
because the proof of those premises could, directly or indirectly,
21626
21718
include the application of a rule where the environment is used.
21627
21719
21628
21720
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21629
21721
\begin{itemize}
21630
21722
\Item{\SrnReflexivity}{Reflexivity}
21631
- Every type is a subtype of itself, in any environment $\Gamma $.
21723
+ Every type is a subtype of itself, in any environment $\Delta $.
21632
21724
In the following rules except for a few,
21633
21725
the rule is also valid in any environment
21634
21726
and the environment is never used explicitly,
@@ -21679,7 +21771,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21679
21771
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21680
21772
The type variable $X$ is a subtype of a type $T$ if
21681
21773
the bound of $X$
21682
- (as specified in the current environment $\Gamma $)
21774
+ (as specified in the current environment $\Delta $)
21683
21775
is a subtype of $T$.
21684
21776
\Item{\SrnRightFunction}{Right Function}
21685
21777
Every function type is a subtype of the type \FUNCTION.
@@ -21699,7 +21791,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21699
21791
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21700
21792
For every subtype relation considered in this rule,
21701
21793
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21702
- (as reflected in the use of the extended environment $\Gamma '$).
21794
+ (as reflected in the use of the extended environment $\Delta '$).
21703
21795
We can assume without loss of generality
21704
21796
that the names of type variables are pairwise identical,
21705
21797
because we consider types of generic functions to be equivalent under
@@ -21764,14 +21856,14 @@ \subsubsection{Additional Subtyping Concepts}
21764
21856
\LMLabel{additionalSubtypingConcepts}
21765
21857
21766
21858
\LMHash{}%
21767
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21859
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21768
21860
written \SupertypeStd{S}{T},
21769
21861
if{}f \SubtypeStd{T}{S}.
21770
21862
21771
21863
\LMHash{}%
21772
21864
A type $T$
21773
21865
\Index{may be assigned}
21774
- to a type $S$ in an environment $\Gamma $,
21866
+ to a type $S$ in an environment $\Delta $,
21775
21867
written \AssignableStd{S}{T},
21776
21868
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21777
21869
In this case we say that the types $S$ and $T$ are
@@ -22881,7 +22973,7 @@ \section*{Appendix: Algorithmic Subtyping}
22881
22973
\begin{minipage}[c]{0.49\textwidth}
22882
22974
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22883
22975
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22884
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22976
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22885
22977
\end{minipage}
22886
22978
\begin{minipage}[c]{0.49\textwidth}
22887
22979
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments