@@ -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}
@@ -20864,7 +20956,7 @@ \subsection{Subtypes}
20864
20956
may find the meaning of the given notation obvious,
20865
20957
but we still need to clarify a few details about how to handle
20866
20958
syntactically different denotations of the same type,
20867
- and how to choose the right initial environment, $\Gamma $.
20959
+ and how to choose the right initial environment, $\Delta $.
20868
20960
%
20869
20961
For a reader who is not familiar with the notation used in this section,
20870
20962
the explanations given here should suffice to clarify what it means,
@@ -20967,7 +21059,7 @@ \subsection{Subtypes}
20967
21059
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
20968
21060
S}{X \& T}
20969
21061
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
20970
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21062
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
20971
21063
\end{minipage}
20972
21064
\begin{minipage}[c]{0.49\textwidth}
20973
21065
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -20980,26 +21072,26 @@ \subsection{Subtypes}
20980
21072
%
20981
21073
\ExtraVSP
20982
21074
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
20983
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20984
- \Subtype{\Gamma '}{S_0}{T_0} \\
21075
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21076
+ \Subtype{\Delta '}{S_0}{T_0} \\
20985
21077
n_1 \leq n_2 &
20986
21078
n_1 + k_1 \geq n_2 + k_2 &
20987
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21079
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
20988
21080
\begin{array}{c}
20989
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21081
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
20990
21082
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
20991
21083
\end{array}}
20992
21084
\ExtraVSP\ExtraVSP
20993
21085
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
20994
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
20995
- \Subtype{\Gamma '}{S_0}{T_0} &
20996
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21086
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21087
+ \Subtype{\Delta '}{S_0}{T_0} &
21088
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
20997
21089
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
20998
21090
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
20999
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21091
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21000
21092
\begin{array}{c}
21001
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21002
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21093
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21094
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21003
21095
\end{array}}
21004
21096
%
21005
21097
\ExtraVSP
@@ -21127,24 +21219,24 @@ \subsubsection{Subtype Rules}
21127
21219
21128
21220
\LMHash{}%
21129
21221
The rules in Figure~\ref{fig:subtypeRules} use
21130
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21222
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21131
21223
bounds of type variables.
21132
- $\Gamma $ is a partial function that maps type variables to types.
21224
+ $\Delta $ is a partial function that maps type variables to types.
21133
21225
At a given location where the type variables in scope are
21134
21226
\TypeParametersStd{}
21135
21227
(\commentary{as declared by enclosing classes and/or functions}),
21136
21228
we define the environment as follows:
21137
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21229
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21138
21230
\commentary{%
21139
- That is, $\Gamma (X_1) = B_1$, and so on,
21140
- and $\Gamma $ is undefined when applied to a type variable $Y$
21231
+ That is, $\Delta (X_1) = B_1$, and so on,
21232
+ and $\Delta $ is undefined when applied to a type variable $Y$
21141
21233
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21142
21234
}
21143
21235
When the rules are used to show that a given subtype relationship exists,
21144
- this is the initial value of $\Gamma $.
21236
+ this is the initial value of $\Delta $.
21145
21237
21146
21238
\LMHash{}%
21147
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21239
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21148
21240
as shown in the rules~\SrnPositionalFunctionType{}
21149
21241
and~\SrnNamedFunctionType{}
21150
21242
of Figure~\ref{fig:subtypeRules}.
@@ -21201,9 +21293,9 @@ \subsubsection{Being a subtype}
21201
21293
21202
21294
\LMHash{}%
21203
21295
A type $S$ is shown to be a \Index{subtype} of another type $T$
21204
- in an environment $\Gamma $ by providing
21296
+ in an environment $\Delta $ by providing
21205
21297
an instantiation of a rule $R$ whose conclusion is
21206
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21298
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21207
21299
along with rule instantiations showing
21208
21300
each of the premises of $R$,
21209
21301
continuing until a rule with no premises is reached.
@@ -21329,19 +21421,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21329
21421
a subtype of \code{FutureOr<$T$>}.
21330
21422
21331
21423
Another example is the wording in rule~\SrnReflexivity{}:
21332
- ``\ldots{} in any environment $\Gamma $'',
21424
+ ``\ldots{} in any environment $\Delta $'',
21333
21425
which indicates that the rule can be applied no matter which bindings
21334
21426
of type variables to bounds there exist in the environment.
21335
21427
It should be noted that the environment matters even with rules
21336
- where it is simply stated as a plain $\Gamma $ in the conclusion
21428
+ where it is simply stated as a plain $\Delta $ in the conclusion
21337
21429
and in one or more premises,
21338
21430
because the proof of those premises could, directly or indirectly,
21339
21431
include the application of a rule where the environment is used.
21340
21432
21341
21433
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21342
21434
\begin{itemize}
21343
21435
\Item{\SrnReflexivity}{Reflexivity}
21344
- Every type is a subtype of itself, in any environment $\Gamma $.
21436
+ Every type is a subtype of itself, in any environment $\Delta $.
21345
21437
In the following rules except for a few,
21346
21438
the rule is also valid in any environment
21347
21439
and the environment is never used explicitly,
@@ -21392,7 +21484,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21392
21484
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21393
21485
The type variable $X$ is a subtype of a type $T$ if
21394
21486
the bound of $X$
21395
- (as specified in the current environment $\Gamma $)
21487
+ (as specified in the current environment $\Delta $)
21396
21488
is a subtype of $T$.
21397
21489
\Item{\SrnRightFunction}{Right Function}
21398
21490
Every function type is a subtype of the type \FUNCTION.
@@ -21412,7 +21504,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21412
21504
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21413
21505
For every subtype relation considered in this rule,
21414
21506
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21415
- (as reflected in the use of the extended environment $\Gamma '$).
21507
+ (as reflected in the use of the extended environment $\Delta '$).
21416
21508
We can assume without loss of generality
21417
21509
that the names of type variables are pairwise identical,
21418
21510
because we consider types of generic functions to be equivalent under
@@ -21477,14 +21569,14 @@ \subsubsection{Additional Subtyping Concepts}
21477
21569
\LMLabel{additionalSubtypingConcepts}
21478
21570
21479
21571
\LMHash{}%
21480
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21572
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21481
21573
written \SupertypeStd{S}{T},
21482
21574
if{}f \SubtypeStd{T}{S}.
21483
21575
21484
21576
\LMHash{}%
21485
21577
A type $T$
21486
21578
\Index{may be assigned}
21487
- to a type $S$ in an environment $\Gamma $,
21579
+ to a type $S$ in an environment $\Delta $,
21488
21580
written \AssignableStd{S}{T},
21489
21581
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21490
21582
In this case we say that the types $S$ and $T$ are
@@ -22583,7 +22675,7 @@ \section*{Appendix: Algorithmic Subtyping}
22583
22675
\begin{minipage}[c]{0.49\textwidth}
22584
22676
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22585
22677
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22586
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22678
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22587
22679
\end{minipage}
22588
22680
\begin{minipage}[c]{0.49\textwidth}
22589
22681
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments