@@ -17474,6 +17474,7 @@ \subsection{Local Variable Declaration}
17474
17474
apply to local variables with the same definitions as for other variables
17475
17475
(\ref{variables}).
17476
17476
17477
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17477
17478
\LMHash{}%
17478
17479
We say that a local variable $v$ is \Index{potentially mutated}
17479
17480
in some scope $s$
@@ -17482,41 +17483,108 @@ \subsection{Local Variable Declaration}
17482
17483
\LMHash{}%
17483
17484
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17484
17485
\code{\VAR{} $v$ = \NULL;}.
17485
- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17486
- \code{$T$ $v$ = \NULL;}.
17487
-
17488
- \commentary{%
17489
- This holds regardless of the type $T$.
17490
- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17491
- }
17486
+ If $T$ is a nullable type
17487
+ (\ref{typeNullability})
17488
+ then a local variable declaration of the form \code{$T$ $v$;}
17489
+ is equivalent to \code{$T$ $v$ = \NULL;}.
17490
+
17491
+ %% TODO(eernst): Revise when flow analysis is added.
17492
+ \commentary{%
17493
+ If $T$ is a potentially non-nullable type
17494
+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17495
+ but an expression that gives rise to evaluation of $v$
17496
+ is a compile-time error unless flow analysis
17497
+ (\ref{flowAnalysis})
17498
+ shows that the variable is guaranteed to have been initialized.%
17499
+ }
17500
+
17501
+ \LMHash{}%
17502
+ A local variable has an associated
17503
+ \IndexCustom{declared type}{local variable!declared type}
17504
+ which is determined from its declaration.
17505
+ A local variable also has an associated
17506
+ \IndexCustom{type}{local variable!type}
17507
+ which is determined by flow analysis
17508
+ (\ref{flowAnalysis})
17509
+ via a process known as type promotion
17510
+ (\ref{typePromotion}).
17492
17511
17493
17512
\LMHash{}%
17494
- The type of a local variable with a declaration of one of the forms
17495
- \code{$T$ $v$ = $e$;}
17496
- \code{\CONST{} $T$ $v$ = $e$;}
17497
- \code{\FINAL{} $T$ $v$ = $e$;}
17513
+ The declared type of a local variable with a declaration of one of the forms
17514
+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17515
+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17516
+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
17498
17517
is $T$.
17499
- The type of a local variable with a declaration of one of the forms
17500
- \code{\VAR{} $v$ = $e$;}
17501
- \code{\CONST{} $v$ = $e$;}
17502
- \code{\FINAL{} $v$ = $e$;}
17503
- is \DYNAMIC.
17518
+
17519
+ \LMHash{}%
17520
+ The declared type of a local variable with a declaration of one of the forms
17521
+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17522
+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17523
+ \code{\CONST\,\,$v$ = $e$;}
17524
+ is determined as follows:
17525
+
17526
+ \begin{itemize}
17527
+ \item
17528
+ If the static type of $e$ is \code{Null} then
17529
+ the declared type of $v$ is \DYNAMIC.
17530
+ \item
17531
+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17532
+ where $X$ is a type variable
17533
+ (\ref{intersectionTypes}),
17534
+ the declared type of $v$ is \code{X}.
17535
+ \commentary{%
17536
+ In this case $v$ is immediately promoted
17537
+ (\ref{typePromotion}).%
17538
+ }
17539
+ \item
17540
+ Otherwise, the declared type of $v$ is the static type of $e$.
17541
+ \end{itemize}
17504
17542
17505
17543
\LMHash{}%
17506
17544
Let $v$ be a local variable declared by an initializing variable declaration,
17507
17545
and let $e$ be the associated initializing expression.
17508
- It is a compile-time error if the static type of $e$ is not assignable to
17509
- the type of $v$.
17510
- It is a compile-time error if a local variable $v$ is final,
17511
- and the declaration of $v$ is not an initializing variable declaration.
17546
+ It is a
17547
+ \Error{compile-time error} if the static type of $e$ is not assignable to
17548
+ the declared type of $v$.
17512
17549
17550
+ %% TODO(eernst): Revise when flow analysis is added.
17513
17551
\commentary{%
17514
- It is also a compile-time error to assign to a final local variable
17552
+ If a local variable $v$ is \FINAL{} and not \LATE,
17553
+ it is not a compile-time error if the declaration of $v$ is
17554
+ not an initializing variable declaration,
17555
+ but an expression that gives rise to evaluation of $v$
17556
+ is a compile-time error unless flow analysis shows that
17557
+ the variable is guaranteed to have been initialized.
17558
+ Similarly, an expression that gives rise to an assignment to $v$
17559
+ is a compile-time error unless flow analysis shows that
17560
+ it is guaranteed that the variable has \emph{not} been initialized.%
17561
+ }
17562
+
17563
+ \commentary{%
17564
+ It is a compile-time error to assign to a local variable
17565
+ which is \FINAL{} and not \LATE{}
17515
17566
(\ref{assignment}).%
17516
17567
}
17517
17568
17518
17569
\LMHash{}%
17519
- It is a compile-time error if
17570
+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17571
+ that declares a variable $v$,
17572
+ which has an initializing expression $e$.
17573
+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17574
+ (\ref{awaitExpressions}),
17575
+ unless there is a function $f$ which is
17576
+ the immediately enclosing function for $a$,
17577
+ and $f$ is not the immediately enclosing function for $D$.
17578
+
17579
+ \commentary{%
17580
+ In other words,
17581
+ the initializing expression cannot await an expression directly,
17582
+ any await expressions must be nested inside some other function,
17583
+ that is, a function literal.%
17584
+ }
17585
+
17586
+ \LMHash{}%
17587
+ It is a \Error{compile-time error} if
17520
17588
a local variable is referenced at a source code location that is before
17521
17589
the end of its initializing expression, if any,
17522
17590
and otherwise before the declaring occurrence of
@@ -17604,15 +17672,39 @@ \subsection{Local Variable Declaration}
17604
17672
17605
17673
\LMHash{}%
17606
17674
The expression $e$ is evaluated to an object $o$.
17607
- Then, the variable $v$ is set to $o$.
17608
- % This error can occur due to implicit casts.
17675
+ % The following error can occur due to implicit casts.
17609
17676
A dynamic type error occurs
17610
- if the dynamic type of $o$ is not a subtype of the actual type
17677
+ if the dynamic type of $o$ is not a subtype of the actual declared type
17611
17678
(\ref{actualTypes})
17612
17679
of $v$.
17680
+ Otherwise, the variable $v$ is bound to $o$.
17613
17681
17614
- % The local variable discussion does not mention how to read/write locals.
17615
- % Consider spelling this out, in terms of storage.
17682
+ \LMHash{}%
17683
+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17684
+ that declares a variable $v$.
17685
+ If an object $o$ is assigned to $v$
17686
+ in a situation where $v$ is unbound
17687
+ then $v$ is bound to $o$.
17688
+ If an object $o$ is assigned to $v$
17689
+ in a situation where $v$ is bound to an object $o'$
17690
+ then a dynamic error occurs
17691
+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17692
+
17693
+ \commentary{%
17694
+ Note that this includes the implicit initializing writes induced by
17695
+ evaluating the variable.
17696
+ Hence, the following program encounters a dynamic error
17697
+ when it evaluates \code{x},
17698
+ just before it would call \code{print}.%
17699
+ }
17700
+
17701
+ \begin{dartCode}
17702
+ \VOID\ main() \{
17703
+ int i = 0;
17704
+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17705
+ print(x);
17706
+ \}
17707
+ \end{dartCode}
17616
17708
17617
17709
17618
17710
\subsection{Local Function Declaration}
@@ -21043,7 +21135,7 @@ \subsection{Subtypes}
21043
21135
may find the meaning of the given notation obvious,
21044
21136
but we still need to clarify a few details about how to handle
21045
21137
syntactically different denotations of the same type,
21046
- and how to choose the right initial environment, $\Gamma $.
21138
+ and how to choose the right initial environment, $\Delta $.
21047
21139
%
21048
21140
For a reader who is not familiar with the notation used in this section,
21049
21141
the explanations given here should suffice to clarify what it means,
@@ -21146,7 +21238,7 @@ \subsection{Subtypes}
21146
21238
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
21147
21239
S}{X \& T}
21148
21240
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21149
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21241
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
21150
21242
\end{minipage}
21151
21243
\begin{minipage}[c]{0.49\textwidth}
21152
21244
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21159,26 +21251,26 @@ \subsection{Subtypes}
21159
21251
%
21160
21252
\ExtraVSP
21161
21253
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21162
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21163
- \Subtype{\Gamma '}{S_0}{T_0} \\
21254
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21255
+ \Subtype{\Delta '}{S_0}{T_0} \\
21164
21256
n_1 \leq n_2 &
21165
21257
n_1 + k_1 \geq n_2 + k_2 &
21166
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21258
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
21167
21259
\begin{array}{c}
21168
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21260
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21169
21261
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
21170
21262
\end{array}}
21171
21263
\ExtraVSP\ExtraVSP
21172
21264
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21173
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21174
- \Subtype{\Gamma '}{S_0}{T_0} &
21175
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21265
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21266
+ \Subtype{\Delta '}{S_0}{T_0} &
21267
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
21176
21268
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
21177
21269
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21178
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21270
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21179
21271
\begin{array}{c}
21180
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21181
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21272
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21273
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21182
21274
\end{array}}
21183
21275
%
21184
21276
\ExtraVSP
@@ -21306,24 +21398,24 @@ \subsubsection{Subtype Rules}
21306
21398
21307
21399
\LMHash{}%
21308
21400
The rules in Figure~\ref{fig:subtypeRules} use
21309
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21401
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21310
21402
bounds of type variables.
21311
- $\Gamma $ is a partial function that maps type variables to types.
21403
+ $\Delta $ is a partial function that maps type variables to types.
21312
21404
At a given location where the type variables in scope are
21313
21405
\TypeParametersStd{}
21314
21406
(\commentary{as declared by enclosing classes and/or functions}),
21315
21407
we define the environment as follows:
21316
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21408
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21317
21409
\commentary{%
21318
- That is, $\Gamma (X_1) = B_1$, and so on,
21319
- and $\Gamma $ is undefined when applied to a type variable $Y$
21410
+ That is, $\Delta (X_1) = B_1$, and so on,
21411
+ and $\Delta $ is undefined when applied to a type variable $Y$
21320
21412
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21321
21413
}
21322
21414
When the rules are used to show that a given subtype relationship exists,
21323
- this is the initial value of $\Gamma $.
21415
+ this is the initial value of $\Delta $.
21324
21416
21325
21417
\LMHash{}%
21326
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21418
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21327
21419
as shown in the rules~\SrnPositionalFunctionType{}
21328
21420
and~\SrnNamedFunctionType{}
21329
21421
of Figure~\ref{fig:subtypeRules}.
@@ -21380,9 +21472,9 @@ \subsubsection{Being a subtype}
21380
21472
21381
21473
\LMHash{}%
21382
21474
A type $S$ is shown to be a \Index{subtype} of another type $T$
21383
- in an environment $\Gamma $ by providing
21475
+ in an environment $\Delta $ by providing
21384
21476
an instantiation of a rule $R$ whose conclusion is
21385
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21477
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21386
21478
along with rule instantiations showing
21387
21479
each of the premises of $R$,
21388
21480
continuing until a rule with no premises is reached.
@@ -21508,19 +21600,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21508
21600
a subtype of \code{FutureOr<$T$>}.
21509
21601
21510
21602
Another example is the wording in rule~\SrnReflexivity{}:
21511
- ``\ldots{} in any environment $\Gamma $'',
21603
+ ``\ldots{} in any environment $\Delta $'',
21512
21604
which indicates that the rule can be applied no matter which bindings
21513
21605
of type variables to bounds there exist in the environment.
21514
21606
It should be noted that the environment matters even with rules
21515
- where it is simply stated as a plain $\Gamma $ in the conclusion
21607
+ where it is simply stated as a plain $\Delta $ in the conclusion
21516
21608
and in one or more premises,
21517
21609
because the proof of those premises could, directly or indirectly,
21518
21610
include the application of a rule where the environment is used.
21519
21611
21520
21612
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21521
21613
\begin{itemize}
21522
21614
\Item{\SrnReflexivity}{Reflexivity}
21523
- Every type is a subtype of itself, in any environment $\Gamma $.
21615
+ Every type is a subtype of itself, in any environment $\Delta $.
21524
21616
In the following rules except for a few,
21525
21617
the rule is also valid in any environment
21526
21618
and the environment is never used explicitly,
@@ -21571,7 +21663,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21571
21663
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21572
21664
The type variable $X$ is a subtype of a type $T$ if
21573
21665
the bound of $X$
21574
- (as specified in the current environment $\Gamma $)
21666
+ (as specified in the current environment $\Delta $)
21575
21667
is a subtype of $T$.
21576
21668
\Item{\SrnRightFunction}{Right Function}
21577
21669
Every function type is a subtype of the type \FUNCTION.
@@ -21591,7 +21683,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21591
21683
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21592
21684
For every subtype relation considered in this rule,
21593
21685
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21594
- (as reflected in the use of the extended environment $\Gamma '$).
21686
+ (as reflected in the use of the extended environment $\Delta '$).
21595
21687
We can assume without loss of generality
21596
21688
that the names of type variables are pairwise identical,
21597
21689
because we consider types of generic functions to be equivalent under
@@ -21656,14 +21748,14 @@ \subsubsection{Additional Subtyping Concepts}
21656
21748
\LMLabel{additionalSubtypingConcepts}
21657
21749
21658
21750
\LMHash{}%
21659
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21751
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21660
21752
written \SupertypeStd{S}{T},
21661
21753
if{}f \SubtypeStd{T}{S}.
21662
21754
21663
21755
\LMHash{}%
21664
21756
A type $T$
21665
21757
\Index{may be assigned}
21666
- to a type $S$ in an environment $\Gamma $,
21758
+ to a type $S$ in an environment $\Delta $,
21667
21759
written \AssignableStd{S}{T},
21668
21760
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21669
21761
In this case we say that the types $S$ and $T$ are
@@ -22773,7 +22865,7 @@ \section*{Appendix: Algorithmic Subtyping}
22773
22865
\begin{minipage}[c]{0.49\textwidth}
22774
22866
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22775
22867
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22776
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22868
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22777
22869
\end{minipage}
22778
22870
\begin{minipage}[c]{0.49\textwidth}
22779
22871
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments