@@ -16166,7 +16166,7 @@ \subsection{Conditional}
16166
16166
\LMLabel{conditional}
16167
16167
16168
16168
\LMHash{}%
16169
- A \Index{conditional expression} evaluates one of two expressions
16169
+ A \Index{conditional expression} evaluates one of two expressions,
16170
16170
based on a boolean condition.
16171
16171
16172
16172
\begin{grammar}
@@ -16175,34 +16175,23 @@ \subsection{Conditional}
16175
16175
\end{grammar}
16176
16176
16177
16177
\LMHash{}%
16178
- Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16179
- proceeds as follows:
16178
+ Consider a conditional expression $c$ of the form
16179
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16180
+ It is a \Error{compile-time error} if
16181
+ the static type of $e_1$ may not be assigned to \code{bool}.
16182
+ The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16183
+ the static type of $e_2$ and the static type of $e_3$.
16180
16184
16181
16185
\LMHash{}%
16182
- First, $e_1$ is evaluated to an object $o_1$.
16186
+ Evaluation of a conditional expression $c$ of the form
16187
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16188
+ proceeds as follows:
16189
+ Evaluate $e_1$ to an object $o_1$.
16183
16190
% This error can occur due to an implicit cast from \DYNAMIC.
16184
16191
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16185
- If $r $ is \TRUE, then the value of $c$ is
16192
+ If $o_1 $ is the \TRUE{} object , then the value of $c$ is
16186
16193
the result of evaluating the expression $e_2$.
16187
- Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16188
-
16189
- \LMHash{}%
16190
- If $e_1$ shows that a local variable $v$ has type $T$,
16191
- then the type of $v$ is known to be $T$ in $e_2$,
16192
- unless any of the following are true:
16193
- \begin{itemize}
16194
- \item $v$ is potentially mutated in $e_2$,
16195
- \item $v$ is potentially mutated within a function other
16196
- than the one where $v$ is declared, or
16197
- \item $v$ is accessed by a function defined in $e_2$ and
16198
- $v$ is potentially mutated anywhere in the scope of $v$.
16199
- \end{itemize}
16200
-
16201
- \LMHash{}%
16202
- It is a \Error{compile-time error} if
16203
- the static type of $e_1$ may not be assigned to \code{bool}.
16204
- The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16205
- the static type of $e_2$ and the static type of $e_3$.
16194
+ Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
16206
16195
16207
16196
16208
16197
\subsection{If-null Expressions}
@@ -16257,8 +16246,15 @@ \subsection{Logical Boolean Expressions}
16257
16246
an expression $e_1$ with argument $e_2$.
16258
16247
16259
16248
\LMHash{}%
16260
- Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16261
- the evaluation of $e_1$ to an object $o_1$.
16249
+ It is a \Error{compile-time error} if
16250
+ the static type of $e_1$ may not be assigned to \code{bool}
16251
+ or if the static type of $e_2$ may not be assigned to \code{bool}.
16252
+ The static type of a logical boolean expression is \code{bool}.
16253
+
16254
+ \LMHash{}%
16255
+ Evaluation of a logical boolean expression $b$ of the form
16256
+ \code{$e_1$\,\,||\,\,$e_2$}
16257
+ causes the evaluation of $e_1$ to an object $o_1$.
16262
16258
% This error can occur due to an implicit downcast from \DYNAMIC.
16263
16259
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16264
16260
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16268,8 +16264,9 @@ \subsection{Logical Boolean Expressions}
16268
16264
Otherwise the result of evaluating $b$ is $o_2$.
16269
16265
16270
16266
\LMHash{}%
16271
- Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16272
- causes the evaluation of $e_1$ producing an object $o_1$.
16267
+ Evaluation of a logical boolean expression $b$ of the form
16268
+ \code{$e_1$\,\,\&\&\,\,$e_2$}
16269
+ causes the evaluation of $e_1$ to an object $o_1$.
16273
16270
% This error can occur due to an implicit downcast from \DYNAMIC.
16274
16271
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16275
16272
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16278,45 +16275,6 @@ \subsection{Logical Boolean Expressions}
16278
16275
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
16279
16276
Otherwise the result of evaluating $b$ is $o_2$.
16280
16277
16281
- \LMHash{}%
16282
- A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16283
- shows that a local variable $v$ has type $T$
16284
- if both of the following conditions hold:
16285
- \begin{itemize}
16286
- \item Either $e_1$ shows that $v$ has type $T$
16287
- or $e_2$ shows that $v$ has type $T$.
16288
- \item $v$ is not mutated in $e_2$ or within a function
16289
- other than the one where $v$ is declared.
16290
- \end{itemize}
16291
-
16292
- \LMHash{}%
16293
- If $e_1$ shows that a local variable $v$ has type $T$,
16294
- then the type of $v$ is known to be $T$ in $e_2$,
16295
- unless any of the following are true:
16296
- \begin{itemize}
16297
- %% The first item here is unnecessary for soundness,
16298
- %% and is retained mainly for backwards compatibility.
16299
- %% If $e_1$ shows that $v$ has type $T$, then any assignment
16300
- %% in $e_1$ does not invalidate that.
16301
- %% Removing the line is visible in the semantics, though, because:
16302
- %% num x;
16303
- %% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16304
- %% is allowed without the line, and disallowed with.
16305
- %% At time of writing, the analyzer disallows the code.
16306
- \item $v$ is potentially mutated in $e_1$,
16307
- \item $v$ is potentially mutated in $e_2$,
16308
- \item $v$ is potentially mutated within a function other
16309
- than the one where $v$ is declared, or
16310
- \item $v$ is accessed by a function defined in $e_2$ and
16311
- $v$ is potentially mutated anywhere in the scope of $v$.
16312
- \end{itemize}
16313
-
16314
- \LMHash{}%
16315
- It is a \Error{compile-time error} if
16316
- the static type of $e_1$ may not be assigned to \code{bool}
16317
- or if the static type of $e_2$ may not be assigned to \code{bool}.
16318
- The static type of a logical boolean expression is \code{bool}.
16319
-
16320
16278
16321
16279
\subsection{Equality}
16322
16280
\LMLabel{equality}
@@ -18251,37 +18209,41 @@ \subsection{If}
18251
18209
\LMLabel{if}
18252
18210
18253
18211
\LMHash{}%
18254
- The \Index{if statement} allows for conditional execution of statements.
18212
+ The \Index{\IF{} statement} allows for conditional execution of statements.
18255
18213
18256
18214
\begin{grammar}
18257
18215
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
18258
18216
\end{grammar}
18259
18217
18260
18218
\LMHash{}%
18261
- An if statement of the form
18262
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18263
- where $s_1$ is not a block statement is equivalent to the statement
18264
- \code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18265
- An if statement of the form
18266
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18267
- where $s_2$ is not a block statement is equivalent to the statement
18268
- \code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18219
+ An \IF{} statement of the form
18220
+ \code{\IF\,\,($e$)\,\,$S$}
18221
+ is treated as
18222
+ (\ref{overview})
18223
+ \code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18224
+ An \IF{} statement of the form
18225
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18226
+ where $S_1$ is not a block statement is treated as
18227
+ \code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18228
+ An \IF{} statement of the form
18229
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18230
+ where $S_2$ is not a block statement is treated as
18231
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
18269
18232
18270
18233
\rationale{%
18271
18234
The reason for this equivalence is to catch errors such as%
18272
18235
}
18273
18236
18274
18237
\begin{dartCode}
18275
18238
\VOID{} main() \{
18276
- \IF{} (somePredicate)
18277
- \VAR{} v = 2;
18239
+ \IF{} (somePredicate) \VAR{} v = 2;
18278
18240
print(v);
18279
18241
\}
18280
18242
\end{dartCode}
18281
18243
18282
18244
\rationale{%
18283
18245
Under reasonable scope rules such code is problematic.
18284
- If we assume that \code{v} is declared
18246
+ If we specify that \code{v} is declared
18285
18247
in the scope of the method \code{main()},
18286
18248
then when \code{somePredicate} evaluates to the \FALSE{} object,
18287
18249
\code{v} will be uninitialized when accessed.
@@ -18300,37 +18262,21 @@ \subsection{If}
18300
18262
}
18301
18263
18302
18264
\LMHash{}%
18303
- Execution of an if statement of the form
18304
- \ code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18305
- where $s_1$ and $s_2$ are block statements,
18306
- proceeds as follows:
18265
+ Consider an \IF{} statement of any of the forms mentioned above
18266
+ (\commentary{the statement then starts with \ code{\IF\,\,($e$)}}).
18267
+ It is a \Error{compile-time error} if the type of the expression $e$
18268
+ is not assignable to \code{bool}.
18307
18269
18308
18270
\LMHash{}%
18309
- First, the expression $b$ is evaluated to an object $o$.
18271
+ Execution of an \IF{} statement of the form
18272
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18273
+ where $S_1$ and $S_2$ are block statements
18274
+ proceeds as follows:
18275
+ Evaluate the expression $e$ to an object $o$.
18310
18276
% This error can occur due to an implicit downcast from \DYNAMIC.
18311
18277
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18312
- If $o$ is \TRUE, then the block statement $s_1$ is executed,
18313
- otherwise the block statement $s_2$ is executed.
18314
-
18315
- \LMHash{}%
18316
- It is a \Error{compile-time error} if the type of the expression $b$
18317
- may not be assigned to \code{bool}.
18318
-
18319
- \LMHash{}%
18320
- If $b$ shows that a local variable $v$ has type $T$,
18321
- then the type of $v$ is known to be $T$ in $s_1$,
18322
- unless any of the following are true
18323
- \begin{itemize}
18324
- \item $v$ is potentially mutated in $s_1$,
18325
- \item $v$ is potentially mutated within a function other
18326
- than the one where $v$ is declared, or
18327
- \item $v$ is accessed by a function defined in $s_1$ and
18328
- $v$ is potentially mutated anywhere in the scope of $v$.
18329
- \end{itemize}
18330
-
18331
- \LMHash{}%
18332
- An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18333
- the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18278
+ If $o$ is \TRUE, then execute the block statement $S_1$,
18279
+ otherwise execute the block statement $S_2$.
18334
18280
18335
18281
18336
18282
\subsection{For}
@@ -21166,34 +21112,6 @@ \subsection{Static Types}
21166
21112
% objects in constructor args - these cannot represent parameterized types.
21167
21113
21168
21114
21169
- \subsubsection{Local Variable Type Promotion}
21170
- \LMLabel{typePromotion}
21171
-
21172
- \LMHash{}%
21173
- The static type system ascribes a static type to every expression.
21174
- In some cases, the type of a local variable
21175
- (\commentary{which can be a formal parameter})
21176
- may be promoted from the declared type, based on control flow.
21177
-
21178
- \LMHash{}%
21179
- We say that a variable $v$ is known to have type $T$
21180
- whenever we allow the type of $v$ to be promoted.
21181
- The exact circumstances when type promotion is allowed are given in
21182
- the relevant sections of the specification
21183
- (\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21184
-
21185
- \LMHash{}%
21186
- Type promotion for a variable $v$ is allowed only when we can deduce that
21187
- such promotion is valid based on an analysis of certain boolean expressions.
21188
- In such cases, we say that
21189
- the boolean expression $b$ shows that $v$ has type $T$.
21190
- As a rule, for all variables $v$ and types $T$, a boolean expression
21191
- does not show that $v$ has type $T$.
21192
- Those situations where an expression does show that a variable has a type are
21193
- mentioned explicitly in the relevant sections of this specification
21194
- (\ref{typeTest} and \ref{logicalBooleanExpressions}).
21195
-
21196
-
21197
21115
\subsection{Dynamic Type System}
21198
21116
\LMLabel{dynamicTypeSystem}
21199
21117
0 commit comments