@@ -16326,7 +16326,7 @@ \subsection{Conditional}
16326
16326
\LMLabel{conditional}
16327
16327
16328
16328
\LMHash{}%
16329
- A \Index{conditional expression} evaluates one of two expressions
16329
+ A \Index{conditional expression} evaluates one of two expressions,
16330
16330
based on a boolean condition.
16331
16331
16332
16332
\begin{grammar}
@@ -16335,34 +16335,23 @@ \subsection{Conditional}
16335
16335
\end{grammar}
16336
16336
16337
16337
\LMHash{}%
16338
- Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16339
- proceeds as follows:
16338
+ Consider a conditional expression $c$ of the form
16339
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16340
+ It is a \Error{compile-time error} if
16341
+ the static type of $e_1$ may not be assigned to \code{bool}.
16342
+ The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16343
+ the static type of $e_2$ and the static type of $e_3$.
16340
16344
16341
16345
\LMHash{}%
16342
- First, $e_1$ is evaluated to an object $o_1$.
16346
+ Evaluation of a conditional expression $c$ of the form
16347
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16348
+ proceeds as follows:
16349
+ Evaluate $e_1$ to an object $o_1$.
16343
16350
% This error can occur due to an implicit cast from \DYNAMIC.
16344
16351
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16345
- If $r $ is \TRUE, then the value of $c$ is
16352
+ If $o_1 $ is the \TRUE{} object , then the value of $c$ is
16346
16353
the result of evaluating the expression $e_2$.
16347
- Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16348
-
16349
- \LMHash{}%
16350
- If $e_1$ shows that a local variable $v$ has type $T$,
16351
- then the type of $v$ is known to be $T$ in $e_2$,
16352
- unless any of the following are true:
16353
- \begin{itemize}
16354
- \item $v$ is potentially mutated in $e_2$,
16355
- \item $v$ is potentially mutated within a function other
16356
- than the one where $v$ is declared, or
16357
- \item $v$ is accessed by a function defined in $e_2$ and
16358
- $v$ is potentially mutated anywhere in the scope of $v$.
16359
- \end{itemize}
16360
-
16361
- \LMHash{}%
16362
- It is a \Error{compile-time error} if
16363
- the static type of $e_1$ may not be assigned to \code{bool}.
16364
- The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16365
- the static type of $e_2$ and the static type of $e_3$.
16354
+ Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
16366
16355
16367
16356
16368
16357
\subsection{If-null Expressions}
@@ -16417,8 +16406,15 @@ \subsection{Logical Boolean Expressions}
16417
16406
an expression $e_1$ with argument $e_2$.
16418
16407
16419
16408
\LMHash{}%
16420
- Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16421
- the evaluation of $e_1$ to an object $o_1$.
16409
+ It is a \Error{compile-time error} if
16410
+ the static type of $e_1$ may not be assigned to \code{bool}
16411
+ or if the static type of $e_2$ may not be assigned to \code{bool}.
16412
+ The static type of a logical boolean expression is \code{bool}.
16413
+
16414
+ \LMHash{}%
16415
+ Evaluation of a logical boolean expression $b$ of the form
16416
+ \code{$e_1$\,\,||\,\,$e_2$}
16417
+ causes the evaluation of $e_1$ to an object $o_1$.
16422
16418
% This error can occur due to an implicit downcast from \DYNAMIC.
16423
16419
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16424
16420
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16428,8 +16424,9 @@ \subsection{Logical Boolean Expressions}
16428
16424
Otherwise the result of evaluating $b$ is $o_2$.
16429
16425
16430
16426
\LMHash{}%
16431
- Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16432
- causes the evaluation of $e_1$ producing an object $o_1$.
16427
+ Evaluation of a logical boolean expression $b$ of the form
16428
+ \code{$e_1$\,\,\&\&\,\,$e_2$}
16429
+ causes the evaluation of $e_1$ to an object $o_1$.
16433
16430
% This error can occur due to an implicit downcast from \DYNAMIC.
16434
16431
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16435
16432
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16438,45 +16435,6 @@ \subsection{Logical Boolean Expressions}
16438
16435
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
16439
16436
Otherwise the result of evaluating $b$ is $o_2$.
16440
16437
16441
- \LMHash{}%
16442
- A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16443
- shows that a local variable $v$ has type $T$
16444
- if both of the following conditions hold:
16445
- \begin{itemize}
16446
- \item Either $e_1$ shows that $v$ has type $T$
16447
- or $e_2$ shows that $v$ has type $T$.
16448
- \item $v$ is not mutated in $e_2$ or within a function
16449
- other than the one where $v$ is declared.
16450
- \end{itemize}
16451
-
16452
- \LMHash{}%
16453
- If $e_1$ shows that a local variable $v$ has type $T$,
16454
- then the type of $v$ is known to be $T$ in $e_2$,
16455
- unless any of the following are true:
16456
- \begin{itemize}
16457
- %% The first item here is unnecessary for soundness,
16458
- %% and is retained mainly for backwards compatibility.
16459
- %% If $e_1$ shows that $v$ has type $T$, then any assignment
16460
- %% in $e_1$ does not invalidate that.
16461
- %% Removing the line is visible in the semantics, though, because:
16462
- %% num x;
16463
- %% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16464
- %% is allowed without the line, and disallowed with.
16465
- %% At time of writing, the analyzer disallows the code.
16466
- \item $v$ is potentially mutated in $e_1$,
16467
- \item $v$ is potentially mutated in $e_2$,
16468
- \item $v$ is potentially mutated within a function other
16469
- than the one where $v$ is declared, or
16470
- \item $v$ is accessed by a function defined in $e_2$ and
16471
- $v$ is potentially mutated anywhere in the scope of $v$.
16472
- \end{itemize}
16473
-
16474
- \LMHash{}%
16475
- It is a \Error{compile-time error} if
16476
- the static type of $e_1$ may not be assigned to \code{bool}
16477
- or if the static type of $e_2$ may not be assigned to \code{bool}.
16478
- The static type of a logical boolean expression is \code{bool}.
16479
-
16480
16438
16481
16439
\subsection{Equality}
16482
16440
\LMLabel{equality}
@@ -18482,37 +18440,41 @@ \subsection{If}
18482
18440
\LMLabel{if}
18483
18441
18484
18442
\LMHash{}%
18485
- The \Index{if statement} allows for conditional execution of statements.
18443
+ The \Index{\IF{} statement} allows for conditional execution of statements.
18486
18444
18487
18445
\begin{grammar}
18488
18446
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
18489
18447
\end{grammar}
18490
18448
18491
18449
\LMHash{}%
18492
- An if statement of the form
18493
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18494
- where $s_1$ is not a block statement is equivalent to the statement
18495
- \code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18496
- An if statement of the form
18497
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18498
- where $s_2$ is not a block statement is equivalent to the statement
18499
- \code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18450
+ An \IF{} statement of the form
18451
+ \code{\IF\,\,($e$)\,\,$S$}
18452
+ is treated as
18453
+ (\ref{overview})
18454
+ \code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18455
+ An \IF{} statement of the form
18456
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18457
+ where $S_1$ is not a block statement is treated as
18458
+ \code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18459
+ An \IF{} statement of the form
18460
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18461
+ where $S_2$ is not a block statement is treated as
18462
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
18500
18463
18501
18464
\rationale{%
18502
18465
The reason for this equivalence is to catch errors such as%
18503
18466
}
18504
18467
18505
18468
\begin{dartCode}
18506
18469
\VOID{} main() \{
18507
- \IF{} (somePredicate)
18508
- \VAR{} v = 2;
18470
+ \IF{} (somePredicate) \VAR{} v = 2;
18509
18471
print(v);
18510
18472
\}
18511
18473
\end{dartCode}
18512
18474
18513
18475
\rationale{%
18514
18476
Under reasonable scope rules such code is problematic.
18515
- If we assume that \code{v} is declared
18477
+ If we specify that \code{v} is declared
18516
18478
in the scope of the method \code{main()},
18517
18479
then when \code{somePredicate} evaluates to the \FALSE{} object,
18518
18480
\code{v} will be uninitialized when accessed.
@@ -18531,37 +18493,21 @@ \subsection{If}
18531
18493
}
18532
18494
18533
18495
\LMHash{}%
18534
- Execution of an if statement of the form
18535
- \ code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18536
- where $s_1$ and $s_2$ are block statements,
18537
- proceeds as follows:
18496
+ Consider an \IF{} statement of any of the forms mentioned above
18497
+ (\commentary{the statement then starts with \ code{\IF\,\,($e$)}}).
18498
+ It is a \Error{compile-time error} if the type of the expression $e$
18499
+ is not assignable to \code{bool}.
18538
18500
18539
18501
\LMHash{}%
18540
- First, the expression $b$ is evaluated to an object $o$.
18502
+ Execution of an \IF{} statement of the form
18503
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18504
+ where $S_1$ and $S_2$ are block statements
18505
+ proceeds as follows:
18506
+ Evaluate the expression $e$ to an object $o$.
18541
18507
% This error can occur due to an implicit downcast from \DYNAMIC.
18542
18508
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18543
- If $o$ is \TRUE, then the block statement $s_1$ is executed,
18544
- otherwise the block statement $s_2$ is executed.
18545
-
18546
- \LMHash{}%
18547
- It is a \Error{compile-time error} if the type of the expression $b$
18548
- may not be assigned to \code{bool}.
18549
-
18550
- \LMHash{}%
18551
- If $b$ shows that a local variable $v$ has type $T$,
18552
- then the type of $v$ is known to be $T$ in $s_1$,
18553
- unless any of the following are true
18554
- \begin{itemize}
18555
- \item $v$ is potentially mutated in $s_1$,
18556
- \item $v$ is potentially mutated within a function other
18557
- than the one where $v$ is declared, or
18558
- \item $v$ is accessed by a function defined in $s_1$ and
18559
- $v$ is potentially mutated anywhere in the scope of $v$.
18560
- \end{itemize}
18561
-
18562
- \LMHash{}%
18563
- An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18564
- the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18509
+ If $o$ is \TRUE, then execute the block statement $S_1$,
18510
+ otherwise execute the block statement $S_2$.
18565
18511
18566
18512
18567
18513
\subsection{For}
@@ -21433,34 +21379,6 @@ \subsection{Static Types}
21433
21379
% objects in constructor args - these cannot represent parameterized types.
21434
21380
21435
21381
21436
- \subsubsection{Local Variable Type Promotion}
21437
- \LMLabel{typePromotion}
21438
-
21439
- \LMHash{}%
21440
- The static type system ascribes a static type to every expression.
21441
- In some cases, the type of a local variable
21442
- (\commentary{which can be a formal parameter})
21443
- may be promoted from the declared type, based on control flow.
21444
-
21445
- \LMHash{}%
21446
- We say that a variable $v$ is known to have type $T$
21447
- whenever we allow the type of $v$ to be promoted.
21448
- The exact circumstances when type promotion is allowed are given in
21449
- the relevant sections of the specification
21450
- (\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21451
-
21452
- \LMHash{}%
21453
- Type promotion for a variable $v$ is allowed only when we can deduce that
21454
- such promotion is valid based on an analysis of certain boolean expressions.
21455
- In such cases, we say that
21456
- the boolean expression $b$ shows that $v$ has type $T$.
21457
- As a rule, for all variables $v$ and types $T$, a boolean expression
21458
- does not show that $v$ has type $T$.
21459
- Those situations where an expression does show that a variable has a type are
21460
- mentioned explicitly in the relevant sections of this specification
21461
- (\ref{typeTest} and \ref{logicalBooleanExpressions}).
21462
-
21463
-
21464
21382
\subsection{Dynamic Type System}
21465
21383
\LMLabel{dynamicTypeSystem}
21466
21384
0 commit comments