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