@@ -16254,7 +16254,7 @@ \subsection{Conditional}
16254
16254
\LMLabel{conditional}
16255
16255
16256
16256
\LMHash{}%
16257
- A \Index{conditional expression} evaluates one of two expressions
16257
+ A \Index{conditional expression} evaluates one of two expressions,
16258
16258
based on a boolean condition.
16259
16259
16260
16260
\begin{grammar}
@@ -16263,34 +16263,23 @@ \subsection{Conditional}
16263
16263
\end{grammar}
16264
16264
16265
16265
\LMHash{}%
16266
- Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16267
- proceeds as follows:
16266
+ Consider a conditional expression $c$ of the form
16267
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16268
+ It is a \Error{compile-time error} if
16269
+ the static type of $e_1$ may not be assigned to \code{bool}.
16270
+ The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16271
+ the static type of $e_2$ and the static type of $e_3$.
16268
16272
16269
16273
\LMHash{}%
16270
- First, $e_1$ is evaluated to an object $o_1$.
16274
+ Evaluation of a conditional expression $c$ of the form
16275
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16276
+ proceeds as follows:
16277
+ Evaluate $e_1$ to an object $o_1$.
16271
16278
% This error can occur due to an implicit cast from \DYNAMIC.
16272
16279
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16273
- If $r $ is \TRUE, then the value of $c$ is
16280
+ If $o_1 $ is the \TRUE{} object , then the value of $c$ is
16274
16281
the result of evaluating the expression $e_2$.
16275
- Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16276
-
16277
- \LMHash{}%
16278
- If $e_1$ shows that a local variable $v$ has type $T$,
16279
- then the type of $v$ is known to be $T$ in $e_2$,
16280
- unless any of the following are true:
16281
- \begin{itemize}
16282
- \item $v$ is potentially mutated in $e_2$,
16283
- \item $v$ is potentially mutated within a function other
16284
- than the one where $v$ is declared, or
16285
- \item $v$ is accessed by a function defined in $e_2$ and
16286
- $v$ is potentially mutated anywhere in the scope of $v$.
16287
- \end{itemize}
16288
-
16289
- \LMHash{}%
16290
- It is a \Error{compile-time error} if
16291
- the static type of $e_1$ may not be assigned to \code{bool}.
16292
- The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16293
- the static type of $e_2$ and the static type of $e_3$.
16282
+ Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
16294
16283
16295
16284
16296
16285
\subsection{If-null Expressions}
@@ -16345,8 +16334,15 @@ \subsection{Logical Boolean Expressions}
16345
16334
an expression $e_1$ with argument $e_2$.
16346
16335
16347
16336
\LMHash{}%
16348
- Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16349
- the evaluation of $e_1$ to an object $o_1$.
16337
+ It is a \Error{compile-time error} if
16338
+ the static type of $e_1$ may not be assigned to \code{bool}
16339
+ or if the static type of $e_2$ may not be assigned to \code{bool}.
16340
+ The static type of a logical boolean expression is \code{bool}.
16341
+
16342
+ \LMHash{}%
16343
+ Evaluation of a logical boolean expression $b$ of the form
16344
+ \code{$e_1$\,\,||\,\,$e_2$}
16345
+ causes the evaluation of $e_1$ to an object $o_1$.
16350
16346
% This error can occur due to an implicit downcast from \DYNAMIC.
16351
16347
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16352
16348
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16356,8 +16352,9 @@ \subsection{Logical Boolean Expressions}
16356
16352
Otherwise the result of evaluating $b$ is $o_2$.
16357
16353
16358
16354
\LMHash{}%
16359
- Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16360
- causes the evaluation of $e_1$ producing an object $o_1$.
16355
+ Evaluation of a logical boolean expression $b$ of the form
16356
+ \code{$e_1$\,\,\&\&\,\,$e_2$}
16357
+ causes the evaluation of $e_1$ to an object $o_1$.
16361
16358
% This error can occur due to an implicit downcast from \DYNAMIC.
16362
16359
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16363
16360
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16366,45 +16363,6 @@ \subsection{Logical Boolean Expressions}
16366
16363
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
16367
16364
Otherwise the result of evaluating $b$ is $o_2$.
16368
16365
16369
- \LMHash{}%
16370
- A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16371
- shows that a local variable $v$ has type $T$
16372
- if both of the following conditions hold:
16373
- \begin{itemize}
16374
- \item Either $e_1$ shows that $v$ has type $T$
16375
- or $e_2$ shows that $v$ has type $T$.
16376
- \item $v$ is not mutated in $e_2$ or within a function
16377
- other than the one where $v$ is declared.
16378
- \end{itemize}
16379
-
16380
- \LMHash{}%
16381
- If $e_1$ shows that a local variable $v$ has type $T$,
16382
- then the type of $v$ is known to be $T$ in $e_2$,
16383
- unless any of the following are true:
16384
- \begin{itemize}
16385
- %% The first item here is unnecessary for soundness,
16386
- %% and is retained mainly for backwards compatibility.
16387
- %% If $e_1$ shows that $v$ has type $T$, then any assignment
16388
- %% in $e_1$ does not invalidate that.
16389
- %% Removing the line is visible in the semantics, though, because:
16390
- %% num x;
16391
- %% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16392
- %% is allowed without the line, and disallowed with.
16393
- %% At time of writing, the analyzer disallows the code.
16394
- \item $v$ is potentially mutated in $e_1$,
16395
- \item $v$ is potentially mutated in $e_2$,
16396
- \item $v$ is potentially mutated within a function other
16397
- than the one where $v$ is declared, or
16398
- \item $v$ is accessed by a function defined in $e_2$ and
16399
- $v$ is potentially mutated anywhere in the scope of $v$.
16400
- \end{itemize}
16401
-
16402
- \LMHash{}%
16403
- It is a \Error{compile-time error} if
16404
- the static type of $e_1$ may not be assigned to \code{bool}
16405
- or if the static type of $e_2$ may not be assigned to \code{bool}.
16406
- The static type of a logical boolean expression is \code{bool}.
16407
-
16408
16366
16409
16367
\subsection{Equality}
16410
16368
\LMLabel{equality}
@@ -18360,37 +18318,41 @@ \subsection{If}
18360
18318
\LMLabel{if}
18361
18319
18362
18320
\LMHash{}%
18363
- The \Index{if statement} allows for conditional execution of statements.
18321
+ The \Index{\IF{} statement} allows for conditional execution of statements.
18364
18322
18365
18323
\begin{grammar}
18366
18324
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
18367
18325
\end{grammar}
18368
18326
18369
18327
\LMHash{}%
18370
- An if statement of the form
18371
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18372
- where $s_1$ is not a block statement is equivalent to the statement
18373
- \code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18374
- An if statement of the form
18375
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18376
- where $s_2$ is not a block statement is equivalent to the statement
18377
- \code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18328
+ An \IF{} statement of the form
18329
+ \code{\IF\,\,($e$)\,\,$S$}
18330
+ is treated as
18331
+ (\ref{overview})
18332
+ \code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18333
+ An \IF{} statement of the form
18334
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18335
+ where $S_1$ is not a block statement is treated as
18336
+ \code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18337
+ An \IF{} statement of the form
18338
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18339
+ where $S_2$ is not a block statement is treated as
18340
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
18378
18341
18379
18342
\rationale{%
18380
18343
The reason for this equivalence is to catch errors such as%
18381
18344
}
18382
18345
18383
18346
\begin{dartCode}
18384
18347
\VOID{} main() \{
18385
- \IF{} (somePredicate)
18386
- \VAR{} v = 2;
18348
+ \IF{} (somePredicate) \VAR{} v = 2;
18387
18349
print(v);
18388
18350
\}
18389
18351
\end{dartCode}
18390
18352
18391
18353
\rationale{%
18392
18354
Under reasonable scope rules such code is problematic.
18393
- If we assume that \code{v} is declared
18355
+ If we specify that \code{v} is declared
18394
18356
in the scope of the method \code{main()},
18395
18357
then when \code{somePredicate} evaluates to the \FALSE{} object,
18396
18358
\code{v} will be uninitialized when accessed.
@@ -18409,37 +18371,21 @@ \subsection{If}
18409
18371
}
18410
18372
18411
18373
\LMHash{}%
18412
- Execution of an if statement of the form
18413
- \ code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18414
- where $s_1$ and $s_2$ are block statements,
18415
- proceeds as follows:
18374
+ Consider an \IF{} statement of any of the forms mentioned above
18375
+ (\commentary{the statement then starts with \ code{\IF\,\,($e$)}}).
18376
+ It is a \Error{compile-time error} if the type of the expression $e$
18377
+ is not assignable to \code{bool}.
18416
18378
18417
18379
\LMHash{}%
18418
- First, the expression $b$ is evaluated to an object $o$.
18380
+ Execution of an \IF{} statement of the form
18381
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18382
+ where $S_1$ and $S_2$ are block statements
18383
+ proceeds as follows:
18384
+ Evaluate the expression $e$ to an object $o$.
18419
18385
% This error can occur due to an implicit downcast from \DYNAMIC.
18420
18386
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18421
- If $o$ is \TRUE, then the block statement $s_1$ is executed,
18422
- otherwise the block statement $s_2$ is executed.
18423
-
18424
- \LMHash{}%
18425
- It is a \Error{compile-time error} if the type of the expression $b$
18426
- may not be assigned to \code{bool}.
18427
-
18428
- \LMHash{}%
18429
- If $b$ shows that a local variable $v$ has type $T$,
18430
- then the type of $v$ is known to be $T$ in $s_1$,
18431
- unless any of the following are true
18432
- \begin{itemize}
18433
- \item $v$ is potentially mutated in $s_1$,
18434
- \item $v$ is potentially mutated within a function other
18435
- than the one where $v$ is declared, or
18436
- \item $v$ is accessed by a function defined in $s_1$ and
18437
- $v$ is potentially mutated anywhere in the scope of $v$.
18438
- \end{itemize}
18439
-
18440
- \LMHash{}%
18441
- An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18442
- the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18387
+ If $o$ is \TRUE, then execute the block statement $S_1$,
18388
+ otherwise execute the block statement $S_2$.
18443
18389
18444
18390
18445
18391
\subsection{For}
@@ -21295,34 +21241,6 @@ \subsection{Static Types}
21295
21241
% objects in constructor args - these cannot represent parameterized types.
21296
21242
21297
21243
21298
- \subsubsection{Local Variable Type Promotion}
21299
- \LMLabel{typePromotion}
21300
-
21301
- \LMHash{}%
21302
- The static type system ascribes a static type to every expression.
21303
- In some cases, the type of a local variable
21304
- (\commentary{which can be a formal parameter})
21305
- may be promoted from the declared type, based on control flow.
21306
-
21307
- \LMHash{}%
21308
- We say that a variable $v$ is known to have type $T$
21309
- whenever we allow the type of $v$ to be promoted.
21310
- The exact circumstances when type promotion is allowed are given in
21311
- the relevant sections of the specification
21312
- (\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21313
-
21314
- \LMHash{}%
21315
- Type promotion for a variable $v$ is allowed only when we can deduce that
21316
- such promotion is valid based on an analysis of certain boolean expressions.
21317
- In such cases, we say that
21318
- the boolean expression $b$ shows that $v$ has type $T$.
21319
- As a rule, for all variables $v$ and types $T$, a boolean expression
21320
- does not show that $v$ has type $T$.
21321
- Those situations where an expression does show that a variable has a type are
21322
- mentioned explicitly in the relevant sections of this specification
21323
- (\ref{typeTest} and \ref{logicalBooleanExpressions}).
21324
-
21325
-
21326
21244
\subsection{Dynamic Type System}
21327
21245
\LMLabel{dynamicTypeSystem}
21328
21246
0 commit comments