@@ -17305,6 +17305,7 @@ \subsection{Local Variable Declaration}
17305
17305
apply to local variables with the same definitions as for other variables
17306
17306
(\ref{variables}).
17307
17307
17308
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17308
17309
\LMHash{}%
17309
17310
We say that a local variable $v$ is \Index{potentially mutated}
17310
17311
in some scope $s$
@@ -17313,41 +17314,108 @@ \subsection{Local Variable Declaration}
17313
17314
\LMHash{}%
17314
17315
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17315
17316
\code{\VAR{} $v$ = \NULL;}.
17316
- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17317
- \code{$T$ $v$ = \NULL;}.
17318
-
17319
- \commentary{%
17320
- This holds regardless of the type $T$.
17321
- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17322
- }
17317
+ If $T$ is a nullable type
17318
+ (\ref{typeNullability})
17319
+ then a local variable declaration of the form \code{$T$ $v$;}
17320
+ is equivalent to \code{$T$ $v$ = \NULL;}.
17321
+
17322
+ %% TODO(eernst): Revise when flow analysis is added.
17323
+ \commentary{%
17324
+ If $T$ is a potentially non-nullable type
17325
+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17326
+ but an expression that gives rise to evaluation of $v$
17327
+ is a compile-time error unless flow analysis
17328
+ (\ref{flowAnalysis})
17329
+ shows that the variable is guaranteed to have been initialized.%
17330
+ }
17331
+
17332
+ \LMHash{}%
17333
+ A local variable has an associated
17334
+ \IndexCustom{declared type}{local variable!declared type}
17335
+ which is determined from its declaration.
17336
+ A local variable also has an associated
17337
+ \IndexCustom{type}{local variable!type}
17338
+ which is determined by flow analysis
17339
+ (\ref{flowAnalysis})
17340
+ via a process known as type promotion
17341
+ (\ref{typePromotion}).
17323
17342
17324
17343
\LMHash{}%
17325
- The type of a local variable with a declaration of one of the forms
17326
- \code{$T$ $v$ = $e$;}
17327
- \code{\CONST{} $T$ $v$ = $e$;}
17328
- \code{\FINAL{} $T$ $v$ = $e$;}
17344
+ The declared type of a local variable with a declaration of one of the forms
17345
+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17346
+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17347
+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
17329
17348
is $T$.
17330
- The type of a local variable with a declaration of one of the forms
17331
- \code{\VAR{} $v$ = $e$;}
17332
- \code{\CONST{} $v$ = $e$;}
17333
- \code{\FINAL{} $v$ = $e$;}
17334
- is \DYNAMIC.
17349
+
17350
+ \LMHash{}%
17351
+ The declared type of a local variable with a declaration of one of the forms
17352
+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17353
+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17354
+ \code{\CONST\,\,$v$ = $e$;}
17355
+ is determined as follows:
17356
+
17357
+ \begin{itemize}
17358
+ \item
17359
+ If the static type of $e$ is \code{Null} then
17360
+ the declared type of $v$ is \DYNAMIC.
17361
+ \item
17362
+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17363
+ where $X$ is a type variable
17364
+ (\ref{intersectionTypes}),
17365
+ the declared type of $v$ is \code{X}.
17366
+ \commentary{%
17367
+ In this case $v$ is immediately promoted
17368
+ (\ref{typePromotion}).%
17369
+ }
17370
+ \item
17371
+ Otherwise, the declared type of $v$ is the static type of $e$.
17372
+ \end{itemize}
17335
17373
17336
17374
\LMHash{}%
17337
17375
Let $v$ be a local variable declared by an initializing variable declaration,
17338
17376
and let $e$ be the associated initializing expression.
17339
- It is a compile-time error if the static type of $e$ is not assignable to
17340
- the type of $v$.
17341
- It is a compile-time error if a local variable $v$ is final,
17342
- and the declaration of $v$ is not an initializing variable declaration.
17377
+ It is a
17378
+ \Error{compile-time error} if the static type of $e$ is not assignable to
17379
+ the declared type of $v$.
17343
17380
17381
+ %% TODO(eernst): Revise when flow analysis is added.
17344
17382
\commentary{%
17345
- It is also a compile-time error to assign to a final local variable
17383
+ If a local variable $v$ is \FINAL{} and not \LATE,
17384
+ it is not a compile-time error if the declaration of $v$ is
17385
+ not an initializing variable declaration,
17386
+ but an expression that gives rise to evaluation of $v$
17387
+ is a compile-time error unless flow analysis shows that
17388
+ the variable is guaranteed to have been initialized.
17389
+ Similarly, an expression that gives rise to an assignment to $v$
17390
+ is a compile-time error unless flow analysis shows that
17391
+ it is guaranteed that the variable has \emph{not} been initialized.%
17392
+ }
17393
+
17394
+ \commentary{%
17395
+ It is a compile-time error to assign to a local variable
17396
+ which is \FINAL{} and not \LATE{}
17346
17397
(\ref{assignment}).%
17347
17398
}
17348
17399
17349
17400
\LMHash{}%
17350
- It is a compile-time error if
17401
+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17402
+ that declares a variable $v$,
17403
+ which has an initializing expression $e$.
17404
+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17405
+ (\ref{awaitExpressions}),
17406
+ unless there is a function $f$ which is
17407
+ the immediately enclosing function for $a$,
17408
+ and $f$ is not the immediately enclosing function for $D$.
17409
+
17410
+ \commentary{%
17411
+ In other words,
17412
+ the initializing expression cannot await an expression directly,
17413
+ any await expressions must be nested inside some other function,
17414
+ that is, a function literal.%
17415
+ }
17416
+
17417
+ \LMHash{}%
17418
+ It is a \Error{compile-time error} if
17351
17419
a local variable is referenced at a source code location that is before
17352
17420
the end of its initializing expression, if any,
17353
17421
and otherwise before the declaring occurrence of
@@ -17435,15 +17503,39 @@ \subsection{Local Variable Declaration}
17435
17503
17436
17504
\LMHash{}%
17437
17505
The expression $e$ is evaluated to an object $o$.
17438
- Then, the variable $v$ is set to $o$.
17439
- % This error can occur due to implicit casts.
17506
+ % The following error can occur due to implicit casts.
17440
17507
A dynamic type error occurs
17441
- if the dynamic type of $o$ is not a subtype of the actual type
17508
+ if the dynamic type of $o$ is not a subtype of the actual declared type
17442
17509
(\ref{actualTypes})
17443
17510
of $v$.
17511
+ Otherwise, the variable $v$ is bound to $o$.
17444
17512
17445
- % The local variable discussion does not mention how to read/write locals.
17446
- % Consider spelling this out, in terms of storage.
17513
+ \LMHash{}%
17514
+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17515
+ that declares a variable $v$.
17516
+ If an object $o$ is assigned to $v$
17517
+ in a situation where $v$ is unbound
17518
+ then $v$ is bound to $o$.
17519
+ If an object $o$ is assigned to $v$
17520
+ in a situation where $v$ is bound to an object $o'$
17521
+ then a dynamic error occurs
17522
+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17523
+
17524
+ \commentary{%
17525
+ Note that this includes the implicit initializing writes induced by
17526
+ evaluating the variable.
17527
+ Hence, the following program encounters a dynamic error
17528
+ when it evaluates \code{x},
17529
+ just before it would call \code{print}.%
17530
+ }
17531
+
17532
+ \begin{dartCode}
17533
+ \VOID\ main() \{
17534
+ int i = 0;
17535
+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17536
+ print(x);
17537
+ \}
17538
+ \end{dartCode}
17447
17539
17448
17540
17449
17541
\subsection{Local Function Declaration}
@@ -20853,7 +20945,7 @@ \subsection{Subtypes}
20853
20945
may find the meaning of the given notation obvious,
20854
20946
but we still need to clarify a few details about how to handle
20855
20947
syntactically different denotations of the same type,
20856
- and how to choose the right initial environment, $\Gamma $.
20948
+ and how to choose the right initial environment, $\Delta $.
20857
20949
%
20858
20950
For a reader who is not familiar with the notation used in this section,
20859
20951
the explanations given here should suffice to clarify what it means,
@@ -20956,7 +21048,7 @@ \subsection{Subtypes}
20956
21048
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
20957
21049
S}{X \& T}
20958
21050
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20959
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21051
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
20960
21052
\end{minipage}
20961
21053
\begin{minipage}[c]{0.49\textwidth}
20962
21054
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20969,26 +21061,26 @@ \subsection{Subtypes}
20969
21061
%
20970
21062
\ExtraVSP
20971
21063
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20972
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20973
- \Subtype{\Gamma '}{S_0}{T_0} \\
21064
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21065
+ \Subtype{\Delta '}{S_0}{T_0} \\
20974
21066
n_1 \leq n_2 &
20975
21067
n_1 + k_1 \geq n_2 + k_2 &
20976
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21068
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
20977
21069
\begin{array}{c}
20978
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21070
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
20979
21071
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
20980
21072
\end{array}}
20981
21073
\ExtraVSP\ExtraVSP
20982
21074
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
20983
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20984
- \Subtype{\Gamma '}{S_0}{T_0} &
20985
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21075
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21076
+ \Subtype{\Delta '}{S_0}{T_0} &
21077
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
20986
21078
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
20987
21079
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
20988
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21080
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
20989
21081
\begin{array}{c}
20990
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
20991
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21082
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21083
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
20992
21084
\end{array}}
20993
21085
%
20994
21086
\ExtraVSP
@@ -21116,24 +21208,24 @@ \subsubsection{Subtype Rules}
21116
21208
21117
21209
\LMHash{}%
21118
21210
The rules in Figure~\ref{fig:subtypeRules} use
21119
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21211
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21120
21212
bounds of type variables.
21121
- $\Gamma $ is a partial function that maps type variables to types.
21213
+ $\Delta $ is a partial function that maps type variables to types.
21122
21214
At a given location where the type variables in scope are
21123
21215
\TypeParametersStd{}
21124
21216
(\commentary{as declared by enclosing classes and/or functions}),
21125
21217
we define the environment as follows:
21126
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21218
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21127
21219
\commentary{%
21128
- That is, $\Gamma (X_1) = B_1$, and so on,
21129
- and $\Gamma $ is undefined when applied to a type variable $Y$
21220
+ That is, $\Delta (X_1) = B_1$, and so on,
21221
+ and $\Delta $ is undefined when applied to a type variable $Y$
21130
21222
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21131
21223
}
21132
21224
When the rules are used to show that a given subtype relationship exists,
21133
- this is the initial value of $\Gamma $.
21225
+ this is the initial value of $\Delta $.
21134
21226
21135
21227
\LMHash{}%
21136
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21228
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21137
21229
as shown in the rules~\SrnPositionalFunctionType{}
21138
21230
and~\SrnNamedFunctionType{}
21139
21231
of Figure~\ref{fig:subtypeRules}.
@@ -21190,9 +21282,9 @@ \subsubsection{Being a subtype}
21190
21282
21191
21283
\LMHash{}%
21192
21284
A type $S$ is shown to be a \Index{subtype} of another type $T$
21193
- in an environment $\Gamma $ by providing
21285
+ in an environment $\Delta $ by providing
21194
21286
an instantiation of a rule $R$ whose conclusion is
21195
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21287
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21196
21288
along with rule instantiations showing
21197
21289
each of the premises of $R$,
21198
21290
continuing until a rule with no premises is reached.
@@ -21318,19 +21410,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21318
21410
a subtype of \code{FutureOr<$T$>}.
21319
21411
21320
21412
Another example is the wording in rule~\SrnReflexivity{}:
21321
- ``\ldots{} in any environment $\Gamma $'',
21413
+ ``\ldots{} in any environment $\Delta $'',
21322
21414
which indicates that the rule can be applied no matter which bindings
21323
21415
of type variables to bounds there exist in the environment.
21324
21416
It should be noted that the environment matters even with rules
21325
- where it is simply stated as a plain $\Gamma $ in the conclusion
21417
+ where it is simply stated as a plain $\Delta $ in the conclusion
21326
21418
and in one or more premises,
21327
21419
because the proof of those premises could, directly or indirectly,
21328
21420
include the application of a rule where the environment is used.
21329
21421
21330
21422
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21331
21423
\begin{itemize}
21332
21424
\Item{\SrnReflexivity}{Reflexivity}
21333
- Every type is a subtype of itself, in any environment $\Gamma $.
21425
+ Every type is a subtype of itself, in any environment $\Delta $.
21334
21426
In the following rules except for a few,
21335
21427
the rule is also valid in any environment
21336
21428
and the environment is never used explicitly,
@@ -21381,7 +21473,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21381
21473
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21382
21474
The type variable $X$ is a subtype of a type $T$ if
21383
21475
the bound of $X$
21384
- (as specified in the current environment $\Gamma $)
21476
+ (as specified in the current environment $\Delta $)
21385
21477
is a subtype of $T$.
21386
21478
\Item{\SrnRightFunction}{Right Function}
21387
21479
Every function type is a subtype of the type \FUNCTION.
@@ -21401,7 +21493,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21401
21493
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21402
21494
For every subtype relation considered in this rule,
21403
21495
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21404
- (as reflected in the use of the extended environment $\Gamma '$).
21496
+ (as reflected in the use of the extended environment $\Delta '$).
21405
21497
We can assume without loss of generality
21406
21498
that the names of type variables are pairwise identical,
21407
21499
because we consider types of generic functions to be equivalent under
@@ -21466,14 +21558,14 @@ \subsubsection{Additional Subtyping Concepts}
21466
21558
\LMLabel{additionalSubtypingConcepts}
21467
21559
21468
21560
\LMHash{}%
21469
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21561
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21470
21562
written \SupertypeStd{S}{T},
21471
21563
if{}f \SubtypeStd{T}{S}.
21472
21564
21473
21565
\LMHash{}%
21474
21566
A type $T$
21475
21567
\Index{may be assigned}
21476
- to a type $S$ in an environment $\Gamma $,
21568
+ to a type $S$ in an environment $\Delta $,
21477
21569
written \AssignableStd{S}{T},
21478
21570
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21479
21571
In this case we say that the types $S$ and $T$ are
@@ -22572,7 +22664,7 @@ \section*{Appendix: Algorithmic Subtyping}
22572
22664
\begin{minipage}[c]{0.49\textwidth}
22573
22665
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22574
22666
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22575
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22667
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22576
22668
\end{minipage}
22577
22669
\begin{minipage}[c]{0.49\textwidth}
22578
22670
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments