@@ -17316,6 +17316,7 @@ \subsection{Local Variable Declaration}
17316
17316
apply to local variables with the same definitions as for other variables
17317
17317
(\ref{variables}).
17318
17318
17319
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17319
17320
\LMHash{}%
17320
17321
We say that a local variable $v$ is \Index{potentially mutated}
17321
17322
in some scope $s$
@@ -17324,41 +17325,108 @@ \subsection{Local Variable Declaration}
17324
17325
\LMHash{}%
17325
17326
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17326
17327
\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}).
17334
17353
17335
17354
\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$;}
17340
17359
is $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}
17346
17384
17347
17385
\LMHash{}%
17348
17386
Let $v$ be a local variable declared by an initializing variable declaration,
17349
17387
and 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$.
17354
17391
17392
+ %% TODO(eernst): Revise when flow analysis is added.
17355
17393
\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{}
17357
17408
(\ref{assignment}).%
17358
17409
}
17359
17410
17360
17411
\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
17362
17430
a local variable is referenced at a source code location that is before
17363
17431
the end of its initializing expression, if any,
17364
17432
and otherwise before the declaring occurrence of
@@ -17446,15 +17514,39 @@ \subsection{Local Variable Declaration}
17446
17514
17447
17515
\LMHash{}%
17448
17516
The 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.
17451
17518
A 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
17453
17520
(\ref{actualTypes})
17454
17521
of $v$.
17522
+ Otherwise, the variable $v$ is bound to $o$.
17455
17523
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}
17458
17550
17459
17551
17460
17552
\subsection{Local Function Declaration}
@@ -20872,7 +20964,7 @@ \subsection{Subtypes}
20872
20964
may find the meaning of the given notation obvious,
20873
20965
but we still need to clarify a few details about how to handle
20874
20966
syntactically different denotations of the same type,
20875
- and how to choose the right initial environment, $\Gamma $.
20967
+ and how to choose the right initial environment, $\Delta $.
20876
20968
%
20877
20969
For a reader who is not familiar with the notation used in this section,
20878
20970
the explanations given here should suffice to clarify what it means,
@@ -20975,7 +21067,7 @@ \subsection{Subtypes}
20975
21067
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
20976
21068
S}{X \& T}
20977
21069
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20978
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21070
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
20979
21071
\end{minipage}
20980
21072
\begin{minipage}[c]{0.49\textwidth}
20981
21073
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20988,26 +21080,26 @@ \subsection{Subtypes}
20988
21080
%
20989
21081
\ExtraVSP
20990
21082
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20991
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20992
- \Subtype{\Gamma '}{S_0}{T_0} \\
21083
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21084
+ \Subtype{\Delta '}{S_0}{T_0} \\
20993
21085
n_1 \leq n_2 &
20994
21086
n_1 + k_1 \geq n_2 + k_2 &
20995
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21087
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
20996
21088
\begin{array}{c}
20997
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21089
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
20998
21090
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
20999
21091
\end{array}}
21000
21092
\ExtraVSP\ExtraVSP
21001
21093
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
21002
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21003
- \Subtype{\Gamma '}{S_0}{T_0} &
21004
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21094
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21095
+ \Subtype{\Delta '}{S_0}{T_0} &
21096
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
21005
21097
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
21006
21098
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21007
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21099
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21008
21100
\begin{array}{c}
21009
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21010
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21101
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21102
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21011
21103
\end{array}}
21012
21104
%
21013
21105
\ExtraVSP
@@ -21135,24 +21227,24 @@ \subsubsection{Subtype Rules}
21135
21227
21136
21228
\LMHash{}%
21137
21229
The rules in Figure~\ref{fig:subtypeRules} use
21138
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21230
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21139
21231
bounds of type variables.
21140
- $\Gamma $ is a partial function that maps type variables to types.
21232
+ $\Delta $ is a partial function that maps type variables to types.
21141
21233
At a given location where the type variables in scope are
21142
21234
\TypeParametersStd{}
21143
21235
(\commentary{as declared by enclosing classes and/or functions}),
21144
21236
we define the environment as follows:
21145
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21237
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21146
21238
\commentary{%
21147
- That is, $\Gamma (X_1) = B_1$, and so on,
21148
- and $\Gamma $ is undefined when applied to a type variable $Y$
21239
+ That is, $\Delta (X_1) = B_1$, and so on,
21240
+ and $\Delta $ is undefined when applied to a type variable $Y$
21149
21241
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21150
21242
}
21151
21243
When the rules are used to show that a given subtype relationship exists,
21152
- this is the initial value of $\Gamma $.
21244
+ this is the initial value of $\Delta $.
21153
21245
21154
21246
\LMHash{}%
21155
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21247
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21156
21248
as shown in the rules~\SrnPositionalFunctionType{}
21157
21249
and~\SrnNamedFunctionType{}
21158
21250
of Figure~\ref{fig:subtypeRules}.
@@ -21209,9 +21301,9 @@ \subsubsection{Being a subtype}
21209
21301
21210
21302
\LMHash{}%
21211
21303
A type $S$ is shown to be a \Index{subtype} of another type $T$
21212
- in an environment $\Gamma $ by providing
21304
+ in an environment $\Delta $ by providing
21213
21305
an instantiation of a rule $R$ whose conclusion is
21214
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21306
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21215
21307
along with rule instantiations showing
21216
21308
each of the premises of $R$,
21217
21309
continuing until a rule with no premises is reached.
@@ -21337,19 +21429,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21337
21429
a subtype of \code{FutureOr<$T$>}.
21338
21430
21339
21431
Another example is the wording in rule~\SrnReflexivity{}:
21340
- ``\ldots{} in any environment $\Gamma $'',
21432
+ ``\ldots{} in any environment $\Delta $'',
21341
21433
which indicates that the rule can be applied no matter which bindings
21342
21434
of type variables to bounds there exist in the environment.
21343
21435
It should be noted that the environment matters even with rules
21344
- where it is simply stated as a plain $\Gamma $ in the conclusion
21436
+ where it is simply stated as a plain $\Delta $ in the conclusion
21345
21437
and in one or more premises,
21346
21438
because the proof of those premises could, directly or indirectly,
21347
21439
include the application of a rule where the environment is used.
21348
21440
21349
21441
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21350
21442
\begin{itemize}
21351
21443
\Item{\SrnReflexivity}{Reflexivity}
21352
- Every type is a subtype of itself, in any environment $\Gamma $.
21444
+ Every type is a subtype of itself, in any environment $\Delta $.
21353
21445
In the following rules except for a few,
21354
21446
the rule is also valid in any environment
21355
21447
and the environment is never used explicitly,
@@ -21400,7 +21492,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21400
21492
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21401
21493
The type variable $X$ is a subtype of a type $T$ if
21402
21494
the bound of $X$
21403
- (as specified in the current environment $\Gamma $)
21495
+ (as specified in the current environment $\Delta $)
21404
21496
is a subtype of $T$.
21405
21497
\Item{\SrnRightFunction}{Right Function}
21406
21498
Every function type is a subtype of the type \FUNCTION.
@@ -21420,7 +21512,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21420
21512
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21421
21513
For every subtype relation considered in this rule,
21422
21514
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21423
- (as reflected in the use of the extended environment $\Gamma '$).
21515
+ (as reflected in the use of the extended environment $\Delta '$).
21424
21516
We can assume without loss of generality
21425
21517
that the names of type variables are pairwise identical,
21426
21518
because we consider types of generic functions to be equivalent under
@@ -21485,14 +21577,14 @@ \subsubsection{Additional Subtyping Concepts}
21485
21577
\LMLabel{additionalSubtypingConcepts}
21486
21578
21487
21579
\LMHash{}%
21488
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21580
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21489
21581
written \SupertypeStd{S}{T},
21490
21582
if{}f \SubtypeStd{T}{S}.
21491
21583
21492
21584
\LMHash{}%
21493
21585
A type $T$
21494
21586
\Index{may be assigned}
21495
- to a type $S$ in an environment $\Gamma $,
21587
+ to a type $S$ in an environment $\Delta $,
21496
21588
written \AssignableStd{S}{T},
21497
21589
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21498
21590
In this case we say that the types $S$ and $T$ are
@@ -22591,7 +22683,7 @@ \section*{Appendix: Algorithmic Subtyping}
22591
22683
\begin{minipage}[c]{0.49\textwidth}
22592
22684
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22593
22685
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22594
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22686
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22595
22687
\end{minipage}
22596
22688
\begin{minipage}[c]{0.49\textwidth}
22597
22689
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments