@@ -17428,6 +17428,7 @@ \subsection{Local Variable Declaration}
17428
17428
apply to local variables with the same definitions as for other variables
17429
17429
(\ref{variables}).
17430
17430
17431
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17431
17432
\LMHash{}%
17432
17433
We say that a local variable $v$ is \Index{potentially mutated}
17433
17434
in some scope $s$
@@ -17436,41 +17437,108 @@ \subsection{Local Variable Declaration}
17436
17437
\LMHash{}%
17437
17438
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17438
17439
\code{\VAR{} $v$ = \NULL;}.
17439
- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17440
- \code{$T$ $v$ = \NULL;}.
17441
-
17442
- \commentary{%
17443
- This holds regardless of the type $T$.
17444
- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17445
- }
17440
+ If $T$ is a nullable type
17441
+ (\ref{typeNullability})
17442
+ then a local variable declaration of the form \code{$T$ $v$;}
17443
+ is equivalent to \code{$T$ $v$ = \NULL;}.
17444
+
17445
+ %% TODO(eernst): Revise when flow analysis is added.
17446
+ \commentary{%
17447
+ If $T$ is a potentially non-nullable type
17448
+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17449
+ but an expression that gives rise to evaluation of $v$
17450
+ is a compile-time error unless flow analysis
17451
+ (\ref{flowAnalysis})
17452
+ shows that the variable is guaranteed to have been initialized.%
17453
+ }
17454
+
17455
+ \LMHash{}%
17456
+ A local variable has an associated
17457
+ \IndexCustom{declared type}{local variable!declared type}
17458
+ which is determined from its declaration.
17459
+ A local variable also has an associated
17460
+ \IndexCustom{type}{local variable!type}
17461
+ which is determined by flow analysis
17462
+ (\ref{flowAnalysis})
17463
+ via a process known as type promotion
17464
+ (\ref{typePromotion}).
17446
17465
17447
17466
\LMHash{}%
17448
- The type of a local variable with a declaration of one of the forms
17449
- \code{$T$ $v$ = $e$;}
17450
- \code{\CONST{} $T$ $v$ = $e$;}
17451
- \code{\FINAL{} $T$ $v$ = $e$;}
17467
+ The declared type of a local variable with a declaration of one of the forms
17468
+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17469
+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17470
+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
17452
17471
is $T$.
17453
- The type of a local variable with a declaration of one of the forms
17454
- \code{\VAR{} $v$ = $e$;}
17455
- \code{\CONST{} $v$ = $e$;}
17456
- \code{\FINAL{} $v$ = $e$;}
17457
- is \DYNAMIC.
17472
+
17473
+ \LMHash{}%
17474
+ The declared type of a local variable with a declaration of one of the forms
17475
+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17476
+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17477
+ \code{\CONST\,\,$v$ = $e$;}
17478
+ is determined as follows:
17479
+
17480
+ \begin{itemize}
17481
+ \item
17482
+ If the static type of $e$ is \code{Null} then
17483
+ the declared type of $v$ is \DYNAMIC.
17484
+ \item
17485
+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17486
+ where $X$ is a type variable
17487
+ (\ref{intersectionTypes}),
17488
+ the declared type of $v$ is \code{X}.
17489
+ \commentary{%
17490
+ In this case $v$ is immediately promoted
17491
+ (\ref{typePromotion}).%
17492
+ }
17493
+ \item
17494
+ Otherwise, the declared type of $v$ is the static type of $e$.
17495
+ \end{itemize}
17458
17496
17459
17497
\LMHash{}%
17460
17498
Let $v$ be a local variable declared by an initializing variable declaration,
17461
17499
and let $e$ be the associated initializing expression.
17462
- It is a compile-time error if the static type of $e$ is not assignable to
17463
- the type of $v$.
17464
- It is a compile-time error if a local variable $v$ is final,
17465
- and the declaration of $v$ is not an initializing variable declaration.
17500
+ It is a
17501
+ \Error{compile-time error} if the static type of $e$ is not assignable to
17502
+ the declared type of $v$.
17466
17503
17504
+ %% TODO(eernst): Revise when flow analysis is added.
17467
17505
\commentary{%
17468
- It is also a compile-time error to assign to a final local variable
17506
+ If a local variable $v$ is \FINAL{} and not \LATE,
17507
+ it is not a compile-time error if the declaration of $v$ is
17508
+ not an initializing variable declaration,
17509
+ but an expression that gives rise to evaluation of $v$
17510
+ is a compile-time error unless flow analysis shows that
17511
+ the variable is guaranteed to have been initialized.
17512
+ Similarly, an expression that gives rise to an assignment to $v$
17513
+ is a compile-time error unless flow analysis shows that
17514
+ it is guaranteed that the variable has \emph{not} been initialized.%
17515
+ }
17516
+
17517
+ \commentary{%
17518
+ It is a compile-time error to assign to a local variable
17519
+ which is \FINAL{} and not \LATE{}
17469
17520
(\ref{assignment}).%
17470
17521
}
17471
17522
17472
17523
\LMHash{}%
17473
- It is a compile-time error if
17524
+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17525
+ that declares a variable $v$,
17526
+ which has an initializing expression $e$.
17527
+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17528
+ (\ref{awaitExpressions}),
17529
+ unless there is a function $f$ which is
17530
+ the immediately enclosing function for $a$,
17531
+ and $f$ is not the immediately enclosing function for $D$.
17532
+
17533
+ \commentary{%
17534
+ In other words,
17535
+ the initializing expression cannot await an expression directly,
17536
+ any await expressions must be nested inside some other function,
17537
+ that is, a function literal.%
17538
+ }
17539
+
17540
+ \LMHash{}%
17541
+ It is a \Error{compile-time error} if
17474
17542
a local variable is referenced at a source code location that is before
17475
17543
the end of its initializing expression, if any,
17476
17544
and otherwise before the declaring occurrence of
@@ -17558,15 +17626,39 @@ \subsection{Local Variable Declaration}
17558
17626
17559
17627
\LMHash{}%
17560
17628
The expression $e$ is evaluated to an object $o$.
17561
- Then, the variable $v$ is set to $o$.
17562
- % This error can occur due to implicit casts.
17629
+ % The following error can occur due to implicit casts.
17563
17630
A dynamic type error occurs
17564
- if the dynamic type of $o$ is not a subtype of the actual type
17631
+ if the dynamic type of $o$ is not a subtype of the actual declared type
17565
17632
(\ref{actualTypes})
17566
17633
of $v$.
17634
+ Otherwise, the variable $v$ is bound to $o$.
17567
17635
17568
- % The local variable discussion does not mention how to read/write locals.
17569
- % Consider spelling this out, in terms of storage.
17636
+ \LMHash{}%
17637
+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17638
+ that declares a variable $v$.
17639
+ If an object $o$ is assigned to $v$
17640
+ in a situation where $v$ is unbound
17641
+ then $v$ is bound to $o$.
17642
+ If an object $o$ is assigned to $v$
17643
+ in a situation where $v$ is bound to an object $o'$
17644
+ then a dynamic error occurs
17645
+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17646
+
17647
+ \commentary{%
17648
+ Note that this includes the implicit initializing writes induced by
17649
+ evaluating the variable.
17650
+ Hence, the following program encounters a dynamic error
17651
+ when it evaluates \code{x},
17652
+ just before it would call \code{print}.%
17653
+ }
17654
+
17655
+ \begin{dartCode}
17656
+ \VOID\ main() \{
17657
+ int i = 0;
17658
+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17659
+ print(x);
17660
+ \}
17661
+ \end{dartCode}
17570
17662
17571
17663
17572
17664
\subsection{Local Function Declaration}
@@ -20981,7 +21073,7 @@ \subsection{Subtypes}
20981
21073
may find the meaning of the given notation obvious,
20982
21074
but we still need to clarify a few details about how to handle
20983
21075
syntactically different denotations of the same type,
20984
- and how to choose the right initial environment, $\Gamma $.
21076
+ and how to choose the right initial environment, $\Delta $.
20985
21077
%
20986
21078
For a reader who is not familiar with the notation used in this section,
20987
21079
the explanations given here should suffice to clarify what it means,
@@ -21084,7 +21176,7 @@ \subsection{Subtypes}
21084
21176
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{%
21085
21177
S}{X \& T}
21086
21178
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21087
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21179
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
21088
21180
\end{minipage}
21089
21181
\begin{minipage}[c]{0.49\textwidth}
21090
21182
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21097,26 +21189,26 @@ \subsection{Subtypes}
21097
21189
%
21098
21190
\ExtraVSP
21099
21191
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21100
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21101
- \Subtype{\Gamma '}{S_0}{T_0} \\
21192
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21193
+ \Subtype{\Delta '}{S_0}{T_0} \\
21102
21194
n_1 \leq n_2 &
21103
21195
n_1 + k_1 \geq n_2 + k_2 &
21104
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21196
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
21105
21197
\begin{array}{c}
21106
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21198
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21107
21199
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
21108
21200
\end{array}}
21109
21201
\ExtraVSP\ExtraVSP
21110
21202
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{%
21111
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21112
- \Subtype{\Gamma '}{S_0}{T_0} &
21113
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21203
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21204
+ \Subtype{\Delta '}{S_0}{T_0} &
21205
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
21114
21206
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
21115
21207
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21116
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21208
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21117
21209
\begin{array}{c}
21118
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21119
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21210
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21211
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21120
21212
\end{array}}
21121
21213
%
21122
21214
\ExtraVSP
@@ -21244,24 +21336,24 @@ \subsubsection{Subtype Rules}
21244
21336
21245
21337
\LMHash{}%
21246
21338
The rules in Figure~\ref{fig:subtypeRules} use
21247
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21339
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21248
21340
bounds of type variables.
21249
- $\Gamma $ is a partial function that maps type variables to types.
21341
+ $\Delta $ is a partial function that maps type variables to types.
21250
21342
At a given location where the type variables in scope are
21251
21343
\TypeParametersStd{}
21252
21344
(\commentary{as declared by enclosing classes and/or functions}),
21253
21345
we define the environment as follows:
21254
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21346
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21255
21347
\commentary{%
21256
- That is, $\Gamma (X_1) = B_1$, and so on,
21257
- and $\Gamma $ is undefined when applied to a type variable $Y$
21348
+ That is, $\Delta (X_1) = B_1$, and so on,
21349
+ and $\Delta $ is undefined when applied to a type variable $Y$
21258
21350
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21259
21351
}
21260
21352
When the rules are used to show that a given subtype relationship exists,
21261
- this is the initial value of $\Gamma $.
21353
+ this is the initial value of $\Delta $.
21262
21354
21263
21355
\LMHash{}%
21264
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21356
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21265
21357
as shown in the rules~\SrnPositionalFunctionType{}
21266
21358
and~\SrnNamedFunctionType{}
21267
21359
of Figure~\ref{fig:subtypeRules}.
@@ -21318,9 +21410,9 @@ \subsubsection{Being a subtype}
21318
21410
21319
21411
\LMHash{}%
21320
21412
A type $S$ is shown to be a \Index{subtype} of another type $T$
21321
- in an environment $\Gamma $ by providing
21413
+ in an environment $\Delta $ by providing
21322
21414
an instantiation of a rule $R$ whose conclusion is
21323
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21415
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21324
21416
along with rule instantiations showing
21325
21417
each of the premises of $R$,
21326
21418
continuing until a rule with no premises is reached.
@@ -21446,19 +21538,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21446
21538
a subtype of \code{FutureOr<$T$>}.
21447
21539
21448
21540
Another example is the wording in rule~\SrnReflexivity{}:
21449
- ``\ldots{} in any environment $\Gamma $'',
21541
+ ``\ldots{} in any environment $\Delta $'',
21450
21542
which indicates that the rule can be applied no matter which bindings
21451
21543
of type variables to bounds there exist in the environment.
21452
21544
It should be noted that the environment matters even with rules
21453
- where it is simply stated as a plain $\Gamma $ in the conclusion
21545
+ where it is simply stated as a plain $\Delta $ in the conclusion
21454
21546
and in one or more premises,
21455
21547
because the proof of those premises could, directly or indirectly,
21456
21548
include the application of a rule where the environment is used.
21457
21549
21458
21550
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21459
21551
\begin{itemize}
21460
21552
\Item{\SrnReflexivity}{Reflexivity}
21461
- Every type is a subtype of itself, in any environment $\Gamma $.
21553
+ Every type is a subtype of itself, in any environment $\Delta $.
21462
21554
In the following rules except for a few,
21463
21555
the rule is also valid in any environment
21464
21556
and the environment is never used explicitly,
@@ -21509,7 +21601,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21509
21601
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21510
21602
The type variable $X$ is a subtype of a type $T$ if
21511
21603
the bound of $X$
21512
- (as specified in the current environment $\Gamma $)
21604
+ (as specified in the current environment $\Delta $)
21513
21605
is a subtype of $T$.
21514
21606
\Item{\SrnRightFunction}{Right Function}
21515
21607
Every function type is a subtype of the type \FUNCTION.
@@ -21529,7 +21621,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21529
21621
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21530
21622
For every subtype relation considered in this rule,
21531
21623
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21532
- (as reflected in the use of the extended environment $\Gamma '$).
21624
+ (as reflected in the use of the extended environment $\Delta '$).
21533
21625
We can assume without loss of generality
21534
21626
that the names of type variables are pairwise identical,
21535
21627
because we consider types of generic functions to be equivalent under
@@ -21594,14 +21686,14 @@ \subsubsection{Additional Subtyping Concepts}
21594
21686
\LMLabel{additionalSubtypingConcepts}
21595
21687
21596
21688
\LMHash{}%
21597
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21689
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21598
21690
written \SupertypeStd{S}{T},
21599
21691
if{}f \SubtypeStd{T}{S}.
21600
21692
21601
21693
\LMHash{}%
21602
21694
A type $T$
21603
21695
\Index{may be assigned}
21604
- to a type $S$ in an environment $\Gamma $,
21696
+ to a type $S$ in an environment $\Delta $,
21605
21697
written \AssignableStd{S}{T},
21606
21698
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21607
21699
In this case we say that the types $S$ and $T$ are
@@ -22710,7 +22802,7 @@ \section*{Appendix: Algorithmic Subtyping}
22710
22802
\begin{minipage}[c]{0.49\textwidth}
22711
22803
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22712
22804
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22713
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22805
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22714
22806
\end{minipage}
22715
22807
\begin{minipage}[c]{0.49\textwidth}
22716
22808
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments