@@ -17343,6 +17343,7 @@ \subsection{Local Variable Declaration}
17343
17343
apply to local variables with the same definitions as for other variables
17344
17344
(\ref{variables}).
17345
17345
17346
+ %% TODO(eernst): May need updates/deletion when flow analysis is integrated.
17346
17347
\LMHash{}%
17347
17348
We say that a local variable $v$ is \Index{potentially mutated}
17348
17349
in some scope $s$
@@ -17351,41 +17352,108 @@ \subsection{Local Variable Declaration}
17351
17352
\LMHash{}%
17352
17353
A local variable declaration of the form \code{\VAR{} $v$;} is equivalent to
17353
17354
\code{\VAR{} $v$ = \NULL;}.
17354
- A local variable declaration of the form \code{$T$ $v$;} is equivalent to
17355
- \code{$T$ $v$ = \NULL;}.
17356
-
17357
- \commentary{%
17358
- This holds regardless of the type $T$.
17359
- E.g., \code{int i;} is equivalent to \code{int i = null;}.%
17360
- }
17355
+ If $T$ is a nullable type
17356
+ (\ref{typeNullability})
17357
+ then a local variable declaration of the form \code{$T$ $v$;}
17358
+ is equivalent to \code{$T$ $v$ = \NULL;}.
17359
+
17360
+ %% TODO(eernst): Revise when flow analysis is added.
17361
+ \commentary{%
17362
+ If $T$ is a potentially non-nullable type
17363
+ then a local variable declaration of the form \code{$T$ $v$;} is allowed,
17364
+ but an expression that gives rise to evaluation of $v$
17365
+ is a compile-time error unless flow analysis
17366
+ (\ref{flowAnalysis})
17367
+ shows that the variable is guaranteed to have been initialized.%
17368
+ }
17369
+
17370
+ \LMHash{}%
17371
+ A local variable has an associated
17372
+ \IndexCustom{declared type}{local variable!declared type}
17373
+ which is determined from its declaration.
17374
+ A local variable also has an associated
17375
+ \IndexCustom{type}{local variable!type}
17376
+ which is determined by flow analysis
17377
+ (\ref{flowAnalysis})
17378
+ via a process known as type promotion
17379
+ (\ref{typePromotion}).
17361
17380
17362
17381
\LMHash{}%
17363
- The type of a local variable with a declaration of one of the forms
17364
- \code{$T$ $v$ = $e$;}
17365
- \code{\CONST{} $T$ $v$ = $e$;}
17366
- \code{\FINAL{} $T$ $v$ = $e$;}
17382
+ The declared type of a local variable with a declaration of one of the forms
17383
+ \code{\LATE?\,\, $T$\,\, $v$ = $e$;}
17384
+ \code{\LATE?\,\,\FINAL\,\, $T$\,\, $v$ = $e$;}
17385
+ \code{\CONST\,\, $T$\,\, $v$ = $e$;}
17367
17386
is $T$.
17368
- The type of a local variable with a declaration of one of the forms
17369
- \code{\VAR{} $v$ = $e$;}
17370
- \code{\CONST{} $v$ = $e$;}
17371
- \code{\FINAL{} $v$ = $e$;}
17372
- is \DYNAMIC.
17387
+
17388
+ \LMHash{}%
17389
+ The declared type of a local variable with a declaration of one of the forms
17390
+ \code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
17391
+ \code{\LATE?\,\,\FINAL\,\,$v$ = $e$;}
17392
+ \code{\CONST\,\,$v$ = $e$;}
17393
+ is determined as follows:
17394
+
17395
+ \begin{itemize}
17396
+ \item
17397
+ If the static type of $e$ is \code{Null} then
17398
+ the declared type of $v$ is \DYNAMIC.
17399
+ \item
17400
+ If the static type of $e$ is of the form \code{$X$\,\&\,$T$}
17401
+ where $X$ is a type variable
17402
+ (\ref{intersectionTypes}),
17403
+ the declared type of $v$ is \code{X}.
17404
+ \commentary{%
17405
+ In this case $v$ is immediately promoted
17406
+ (\ref{typePromotion}).%
17407
+ }
17408
+ \item
17409
+ Otherwise, the declared type of $v$ is the static type of $e$.
17410
+ \end{itemize}
17373
17411
17374
17412
\LMHash{}%
17375
17413
Let $v$ be a local variable declared by an initializing variable declaration,
17376
17414
and let $e$ be the associated initializing expression.
17377
- It is a compile-time error if the static type of $e$ is not assignable to
17378
- the type of $v$.
17379
- It is a compile-time error if a local variable $v$ is final,
17380
- and the declaration of $v$ is not an initializing variable declaration.
17415
+ It is a
17416
+ \Error{compile-time error} if the static type of $e$ is not assignable to
17417
+ the declared type of $v$.
17381
17418
17419
+ %% TODO(eernst): Revise when flow analysis is added.
17382
17420
\commentary{%
17383
- It is also a compile-time error to assign to a final local variable
17421
+ If a local variable $v$ is \FINAL{} and not \LATE,
17422
+ it is not a compile-time error if the declaration of $v$ is
17423
+ not an initializing variable declaration,
17424
+ but an expression that gives rise to evaluation of $v$
17425
+ is a compile-time error unless flow analysis shows that
17426
+ the variable is guaranteed to have been initialized.
17427
+ Similarly, an expression that gives rise to an assignment to $v$
17428
+ is a compile-time error unless flow analysis shows that
17429
+ it is guaranteed that the variable has \emph{not} been initialized.%
17430
+ }
17431
+
17432
+ \commentary{%
17433
+ It is a compile-time error to assign to a local variable
17434
+ which is \FINAL{} and not \LATE{}
17384
17435
(\ref{assignment}).%
17385
17436
}
17386
17437
17387
17438
\LMHash{}%
17388
- It is a compile-time error if
17439
+ Assume that $D$ is a local variable declaration with the modifier \LATE{}
17440
+ that declares a variable $v$,
17441
+ which has an initializing expression $e$.
17442
+ It is a \Error{compile-time error} if $e$ contains an \AWAIT{} expression $a$
17443
+ (\ref{awaitExpressions}),
17444
+ unless there is a function $f$ which is
17445
+ the immediately enclosing function for $a$,
17446
+ and $f$ is not the immediately enclosing function for $D$.
17447
+
17448
+ \commentary{%
17449
+ In other words,
17450
+ the initializing expression cannot await an expression directly,
17451
+ any await expressions must be nested inside some other function,
17452
+ that is, a function literal.%
17453
+ }
17454
+
17455
+ \LMHash{}%
17456
+ It is a \Error{compile-time error} if
17389
17457
a local variable is referenced at a source code location that is before
17390
17458
the end of its initializing expression, if any,
17391
17459
and otherwise before the declaring occurrence of
@@ -17473,15 +17541,39 @@ \subsection{Local Variable Declaration}
17473
17541
17474
17542
\LMHash{}%
17475
17543
The expression $e$ is evaluated to an object $o$.
17476
- Then, the variable $v$ is set to $o$.
17477
- % This error can occur due to implicit casts.
17544
+ % The following error can occur due to implicit casts.
17478
17545
A dynamic type error occurs
17479
- if the dynamic type of $o$ is not a subtype of the actual type
17546
+ if the dynamic type of $o$ is not a subtype of the actual declared type
17480
17547
(\ref{actualTypes})
17481
17548
of $v$.
17549
+ Otherwise, the variable $v$ is bound to $o$.
17482
17550
17483
- % The local variable discussion does not mention how to read/write locals.
17484
- % Consider spelling this out, in terms of storage.
17551
+ \LMHash{}%
17552
+ Let $D$ be a \LATE{} and \FINAL{} local variable declaration
17553
+ that declares a variable $v$.
17554
+ If an object $o$ is assigned to $v$
17555
+ in a situation where $v$ is unbound
17556
+ then $v$ is bound to $o$.
17557
+ If an object $o$ is assigned to $v$
17558
+ in a situation where $v$ is bound to an object $o'$
17559
+ then a dynamic error occurs
17560
+ (\commentary{it does not matter whether $o$ is the same object as $o'$}).
17561
+
17562
+ \commentary{%
17563
+ Note that this includes the implicit initializing writes induced by
17564
+ evaluating the variable.
17565
+ Hence, the following program encounters a dynamic error
17566
+ when it evaluates \code{x},
17567
+ just before it would call \code{print}.%
17568
+ }
17569
+
17570
+ \begin{dartCode}
17571
+ \VOID\ main() \{
17572
+ int i = 0;
17573
+ \LATE\ \FINAL\ int x = i++ == 0 ? x + 1 : 0;
17574
+ print(x);
17575
+ \}
17576
+ \end{dartCode}
17485
17577
17486
17578
17487
17579
\subsection{Local Function Declaration}
@@ -20896,7 +20988,7 @@ \subsection{Subtypes}
20896
20988
may find the meaning of the given notation obvious,
20897
20989
but we still need to clarify a few details about how to handle
20898
20990
syntactically different denotations of the same type,
20899
- and how to choose the right initial environment, $\Gamma $.
20991
+ and how to choose the right initial environment, $\Delta $.
20900
20992
%
20901
20993
For a reader who is not familiar with the notation used in this section,
20902
20994
the explanations given here should suffice to clarify what it means,
@@ -20999,7 +21091,7 @@ \subsection{Subtypes}
20999
21091
\RuleTwo{\SrnRightPromotedVariable}{Right Promoted Variable}{S}{X}{S}{T}{
21000
21092
S}{X \& T}
21001
21093
\Rule{\SrnRightFutureOrB}{Right FutureOr B}{S}{T}{S}{\code{FutureOr<$T$>}}
21002
- \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Gamma (X)}{T}{X}{T}
21094
+ \Rule{\SrnLeftVariableBound}{Left Variable Bound}{\Delta (X)}{T}{X}{T}
21003
21095
\end{minipage}
21004
21096
\begin{minipage}[c]{0.49\textwidth}
21005
21097
\Axiom{\SrnTypeVariableReflexivityA}{Left Promoted Variable A}{X \& S}{X}
@@ -21012,26 +21104,26 @@ \subsection{Subtypes}
21012
21104
%
21013
21105
\ExtraVSP
21014
21106
\RuleRawRaw{\SrnPositionalFunctionType}{Positional Function Types}{%
21015
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21016
- \Subtype{\Gamma '}{S_0}{T_0} \\
21107
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21108
+ \Subtype{\Delta '}{S_0}{T_0} \\
21017
21109
n_1 \leq n_2 &
21018
21110
n_1 + k_1 \geq n_2 + k_2 &
21019
- \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Gamma '}{T_j}{S_j}}{%
21111
+ \forall j \in 1 .. n_2 + k_2\!:\;\Subtype{\Delta '}{T_j}{S_j}}{%
21020
21112
\begin{array}{c}
21021
- \Gamma \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21113
+ \Delta \vdash\RawFunctionTypePositional{S_0}{X}{B}{s}{S}{n_1}{k_1}\;<:\;\\
21022
21114
\RawFunctionTypePositional{T_0}{X}{B}{s}{T}{n_2}{k_2}
21023
21115
\end{array}}
21024
21116
\ExtraVSP\ExtraVSP
21025
21117
\RuleRawRaw{\SrnNamedFunctionType}{Named Function Types}{
21026
- \Gamma ' = \Gamma \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21027
- \Subtype{\Gamma '}{S_0}{T_0} &
21028
- \forall j \in 1 .. n\!:\;\Subtype{\Gamma '}{T_j}{S_j} \\
21118
+ \Delta ' = \Delta \uplus\{X_i\mapsto{}B_i\,|\,1 \leq i \leq s\} &
21119
+ \Subtype{\Delta '}{S_0}{T_0} &
21120
+ \forall j \in 1 .. n\!:\;\Subtype{\Delta '}{T_j}{S_j} \\
21029
21121
\{\,\List{y}{n+1}{n+k_2}\,\} \subseteq \{\,\List{x}{n+1}{n+k_1}\,\} \\
21030
21122
\forall p \in 1 .. k_2, q \in 1 .. k_1:\quad
21031
- y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Gamma '}{T_{n+p}}{S_{n+q}}}{%
21123
+ y_{n+p} = x_{n+q}\quad\Rightarrow\quad\Subtype{\Delta '}{T_{n+p}}{S_{n+q}}}{%
21032
21124
\begin{array}{c}
21033
- \Gamma \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}\;<:\;\\
21034
- \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}
21125
+ \Delta \vdash\RawFunctionTypeNamed{S_0}{X}{B}{s}{S}{n}{x}{k_1}{r }\;<:\;\\
21126
+ \RawFunctionTypeNamed{T_0}{X}{B}{s}{T}{n}{y}{k_2}{r}
21035
21127
\end{array}}
21036
21128
%
21037
21129
\ExtraVSP
@@ -21159,24 +21251,24 @@ \subsubsection{Subtype Rules}
21159
21251
21160
21252
\LMHash{}%
21161
21253
The rules in Figure~\ref{fig:subtypeRules} use
21162
- the symbol \Index{$\Gamma $} to denote the given knowledge about the
21254
+ the symbol \Index{$\Delta $} to denote the given knowledge about the
21163
21255
bounds of type variables.
21164
- $\Gamma $ is a partial function that maps type variables to types.
21256
+ $\Delta $ is a partial function that maps type variables to types.
21165
21257
At a given location where the type variables in scope are
21166
21258
\TypeParametersStd{}
21167
21259
(\commentary{as declared by enclosing classes and/or functions}),
21168
21260
we define the environment as follows:
21169
- $\Gamma = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21261
+ $\Delta = \{\,X_1 \mapsto B_1,\ \ldots\ X_s \mapsto B_s\,\}$.
21170
21262
\commentary{%
21171
- That is, $\Gamma (X_1) = B_1$, and so on,
21172
- and $\Gamma $ is undefined when applied to a type variable $Y$
21263
+ That is, $\Delta (X_1) = B_1$, and so on,
21264
+ and $\Delta $ is undefined when applied to a type variable $Y$
21173
21265
which is not in $\{\,\List{X}{1}{s}\,\}$.%
21174
21266
}
21175
21267
When the rules are used to show that a given subtype relationship exists,
21176
- this is the initial value of $\Gamma $.
21268
+ this is the initial value of $\Delta $.
21177
21269
21178
21270
\LMHash{}%
21179
- If a generic function type is encountered, an extension of $\Gamma $ is used,
21271
+ If a generic function type is encountered, an extension of $\Delta $ is used,
21180
21272
as shown in the rules~\SrnPositionalFunctionType{}
21181
21273
and~\SrnNamedFunctionType{}
21182
21274
of Figure~\ref{fig:subtypeRules}.
@@ -21233,9 +21325,9 @@ \subsubsection{Being a subtype}
21233
21325
21234
21326
\LMHash{}%
21235
21327
A type $S$ is shown to be a \Index{subtype} of another type $T$
21236
- in an environment $\Gamma $ by providing
21328
+ in an environment $\Delta $ by providing
21237
21329
an instantiation of a rule $R$ whose conclusion is
21238
- \IndexCustom{\SubtypeStd{S}{T}}{$\Gamma $@\SubtypeStd{S}{T}},
21330
+ \IndexCustom{\SubtypeStd{S}{T}}{$\Delta $@\SubtypeStd{S}{T}},
21239
21331
along with rule instantiations showing
21240
21332
each of the premises of $R$,
21241
21333
continuing until a rule with no premises is reached.
@@ -21361,19 +21453,19 @@ \subsubsection{Informal Subtype Rule Descriptions}
21361
21453
a subtype of \code{FutureOr<$T$>}.
21362
21454
21363
21455
Another example is the wording in rule~\SrnReflexivity{}:
21364
- ``\ldots{} in any environment $\Gamma $'',
21456
+ ``\ldots{} in any environment $\Delta $'',
21365
21457
which indicates that the rule can be applied no matter which bindings
21366
21458
of type variables to bounds there exist in the environment.
21367
21459
It should be noted that the environment matters even with rules
21368
- where it is simply stated as a plain $\Gamma $ in the conclusion
21460
+ where it is simply stated as a plain $\Delta $ in the conclusion
21369
21461
and in one or more premises,
21370
21462
because the proof of those premises could, directly or indirectly,
21371
21463
include the application of a rule where the environment is used.
21372
21464
21373
21465
\def\Item#1#2{\item[#1]{\textbf{#2:}}}
21374
21466
\begin{itemize}
21375
21467
\Item{\SrnReflexivity}{Reflexivity}
21376
- Every type is a subtype of itself, in any environment $\Gamma $.
21468
+ Every type is a subtype of itself, in any environment $\Delta $.
21377
21469
In the following rules except for a few,
21378
21470
the rule is also valid in any environment
21379
21471
and the environment is never used explicitly,
@@ -21424,7 +21516,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21424
21516
\Item{\SrnLeftVariableBound}{Left Variable Bound}
21425
21517
The type variable $X$ is a subtype of a type $T$ if
21426
21518
the bound of $X$
21427
- (as specified in the current environment $\Gamma $)
21519
+ (as specified in the current environment $\Delta $)
21428
21520
is a subtype of $T$.
21429
21521
\Item{\SrnRightFunction}{Right Function}
21430
21522
Every function type is a subtype of the type \FUNCTION.
@@ -21444,7 +21536,7 @@ \subsubsection{Informal Subtype Rule Descriptions}
21444
21536
is covered by letting $k_2 = 0$ respectively $k_1 = k_2 = 0$.
21445
21537
For every subtype relation considered in this rule,
21446
21538
the formal type parameters of $F_1$ and $F_2$ must be taken into account
21447
- (as reflected in the use of the extended environment $\Gamma '$).
21539
+ (as reflected in the use of the extended environment $\Delta '$).
21448
21540
We can assume without loss of generality
21449
21541
that the names of type variables are pairwise identical,
21450
21542
because we consider types of generic functions to be equivalent under
@@ -21509,14 +21601,14 @@ \subsubsection{Additional Subtyping Concepts}
21509
21601
\LMLabel{additionalSubtypingConcepts}
21510
21602
21511
21603
\LMHash{}%
21512
- $S$ is a \Index{supertype} of $T$ in a given environment $\Gamma $,
21604
+ $S$ is a \Index{supertype} of $T$ in a given environment $\Delta $,
21513
21605
written \SupertypeStd{S}{T},
21514
21606
if{}f \SubtypeStd{T}{S}.
21515
21607
21516
21608
\LMHash{}%
21517
21609
A type $T$
21518
21610
\Index{may be assigned}
21519
- to a type $S$ in an environment $\Gamma $,
21611
+ to a type $S$ in an environment $\Delta $,
21520
21612
written \AssignableStd{S}{T},
21521
21613
if{}f either \SubtypeStd{S}{T} or \SubtypeStd{T}{S}.
21522
21614
In this case we say that the types $S$ and $T$ are
@@ -22625,7 +22717,7 @@ \section*{Appendix: Algorithmic Subtyping}
22625
22717
\begin{minipage}[c]{0.49\textwidth}
22626
22718
\RuleRaw{\AppSrnReflexivity}{Reflexivity}{S\mbox{ not composite}}{S}{S}
22627
22719
\Rule{\AppSrnTypeVariableReflexivityC}{Type Variable Reflexivity B}{X}{T}{X}{X \& T}
22628
- \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Gamma (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22720
+ \Rule{\AppSrnRightFutureOrC}{Right FutureOr C}{\Delta (X)}{\code{FutureOr<$T$>}}{X}{\code{FutureOr<$T$>}}
22629
22721
\end{minipage}
22630
22722
\begin{minipage}[c]{0.49\textwidth}
22631
22723
\Axiom{\AppSrnTypeVariableReflexivityB}{Type Variable Reflexivity}{X}{X}
0 commit comments