@@ -16485,7 +16485,7 @@ \subsection{Conditional}
16485
16485
\LMLabel{conditional}
16486
16486
16487
16487
\LMHash{}%
16488
- A \Index{conditional expression} evaluates one of two expressions
16488
+ A \Index{conditional expression} evaluates one of two expressions,
16489
16489
based on a boolean condition.
16490
16490
16491
16491
\begin{grammar}
@@ -16494,34 +16494,23 @@ \subsection{Conditional}
16494
16494
\end{grammar}
16495
16495
16496
16496
\LMHash{}%
16497
- Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16498
- proceeds as follows:
16497
+ Consider a conditional expression $c$ of the form
16498
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16499
+ It is a \Error{compile-time error} if
16500
+ the static type of $e_1$ may not be assigned to \code{bool}.
16501
+ The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16502
+ the static type of $e_2$ and the static type of $e_3$.
16499
16503
16500
16504
\LMHash{}%
16501
- First, $e_1$ is evaluated to an object $o_1$.
16505
+ Evaluation of a conditional expression $c$ of the form
16506
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16507
+ proceeds as follows:
16508
+ Evaluate $e_1$ to an object $o_1$.
16502
16509
% This error can occur due to an implicit cast from \DYNAMIC.
16503
16510
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16504
- If $r $ is \TRUE, then the value of $c$ is
16511
+ If $o_1 $ is the \TRUE{} object , then the value of $c$ is
16505
16512
the result of evaluating the expression $e_2$.
16506
- Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16507
-
16508
- \LMHash{}%
16509
- If $e_1$ shows that a local variable $v$ has type $T$,
16510
- then the type of $v$ is known to be $T$ in $e_2$,
16511
- unless any of the following are true:
16512
- \begin{itemize}
16513
- \item $v$ is potentially mutated in $e_2$,
16514
- \item $v$ is potentially mutated within a function other
16515
- than the one where $v$ is declared, or
16516
- \item $v$ is accessed by a function defined in $e_2$ and
16517
- $v$ is potentially mutated anywhere in the scope of $v$.
16518
- \end{itemize}
16519
-
16520
- \LMHash{}%
16521
- It is a \Error{compile-time error} if
16522
- the static type of $e_1$ may not be assigned to \code{bool}.
16523
- The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16524
- the static type of $e_2$ and the static type of $e_3$.
16513
+ Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
16525
16514
16526
16515
16527
16516
\subsection{If-null Expressions}
@@ -16576,8 +16565,15 @@ \subsection{Logical Boolean Expressions}
16576
16565
an expression $e_1$ with argument $e_2$.
16577
16566
16578
16567
\LMHash{}%
16579
- Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16580
- the evaluation of $e_1$ to an object $o_1$.
16568
+ It is a \Error{compile-time error} if
16569
+ the static type of $e_1$ may not be assigned to \code{bool}
16570
+ or if the static type of $e_2$ may not be assigned to \code{bool}.
16571
+ The static type of a logical boolean expression is \code{bool}.
16572
+
16573
+ \LMHash{}%
16574
+ Evaluation of a logical boolean expression $b$ of the form
16575
+ \code{$e_1$\,\,||\,\,$e_2$}
16576
+ causes the evaluation of $e_1$ to an object $o_1$.
16581
16577
% This error can occur due to an implicit downcast from \DYNAMIC.
16582
16578
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16583
16579
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16587,8 +16583,9 @@ \subsection{Logical Boolean Expressions}
16587
16583
Otherwise the result of evaluating $b$ is $o_2$.
16588
16584
16589
16585
\LMHash{}%
16590
- Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16591
- causes the evaluation of $e_1$ producing an object $o_1$.
16586
+ Evaluation of a logical boolean expression $b$ of the form
16587
+ \code{$e_1$\,\,\&\&\,\,$e_2$}
16588
+ causes the evaluation of $e_1$ to an object $o_1$.
16592
16589
% This error can occur due to an implicit downcast from \DYNAMIC.
16593
16590
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16594
16591
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16597,45 +16594,6 @@ \subsection{Logical Boolean Expressions}
16597
16594
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
16598
16595
Otherwise the result of evaluating $b$ is $o_2$.
16599
16596
16600
- \LMHash{}%
16601
- A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16602
- shows that a local variable $v$ has type $T$
16603
- if both of the following conditions hold:
16604
- \begin{itemize}
16605
- \item Either $e_1$ shows that $v$ has type $T$
16606
- or $e_2$ shows that $v$ has type $T$.
16607
- \item $v$ is not mutated in $e_2$ or within a function
16608
- other than the one where $v$ is declared.
16609
- \end{itemize}
16610
-
16611
- \LMHash{}%
16612
- If $e_1$ shows that a local variable $v$ has type $T$,
16613
- then the type of $v$ is known to be $T$ in $e_2$,
16614
- unless any of the following are true:
16615
- \begin{itemize}
16616
- %% The first item here is unnecessary for soundness,
16617
- %% and is retained mainly for backwards compatibility.
16618
- %% If $e_1$ shows that $v$ has type $T$, then any assignment
16619
- %% in $e_1$ does not invalidate that.
16620
- %% Removing the line is visible in the semantics, though, because:
16621
- %% num x;
16622
- %% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16623
- %% is allowed without the line, and disallowed with.
16624
- %% At time of writing, the analyzer disallows the code.
16625
- \item $v$ is potentially mutated in $e_1$,
16626
- \item $v$ is potentially mutated in $e_2$,
16627
- \item $v$ is potentially mutated within a function other
16628
- than the one where $v$ is declared, or
16629
- \item $v$ is accessed by a function defined in $e_2$ and
16630
- $v$ is potentially mutated anywhere in the scope of $v$.
16631
- \end{itemize}
16632
-
16633
- \LMHash{}%
16634
- It is a \Error{compile-time error} if
16635
- the static type of $e_1$ may not be assigned to \code{bool}
16636
- or if the static type of $e_2$ may not be assigned to \code{bool}.
16637
- The static type of a logical boolean expression is \code{bool}.
16638
-
16639
16597
16640
16598
\subsection{Equality}
16641
16599
\LMLabel{equality}
@@ -18639,37 +18597,41 @@ \subsection{If}
18639
18597
\LMLabel{if}
18640
18598
18641
18599
\LMHash{}%
18642
- The \Index{if statement} allows for conditional execution of statements.
18600
+ The \Index{\IF{} statement} allows for conditional execution of statements.
18643
18601
18644
18602
\begin{grammar}
18645
18603
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
18646
18604
\end{grammar}
18647
18605
18648
18606
\LMHash{}%
18649
- An if statement of the form
18650
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18651
- where $s_1$ is not a block statement is equivalent to the statement
18652
- \code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18653
- An if statement of the form
18654
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18655
- where $s_2$ is not a block statement is equivalent to the statement
18656
- \code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18607
+ An \IF{} statement of the form
18608
+ \code{\IF\,\,($e$)\,\,$S$}
18609
+ is treated as
18610
+ (\ref{overview})
18611
+ \code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18612
+ An \IF{} statement of the form
18613
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18614
+ where $S_1$ is not a block statement is treated as
18615
+ \code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18616
+ An \IF{} statement of the form
18617
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18618
+ where $S_2$ is not a block statement is treated as
18619
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
18657
18620
18658
18621
\rationale{%
18659
18622
The reason for this equivalence is to catch errors such as%
18660
18623
}
18661
18624
18662
18625
\begin{dartCode}
18663
18626
\VOID{} main() \{
18664
- \IF{} (somePredicate)
18665
- \VAR{} v = 2;
18627
+ \IF{} (somePredicate) \VAR{} v = 2;
18666
18628
print(v);
18667
18629
\}
18668
18630
\end{dartCode}
18669
18631
18670
18632
\rationale{%
18671
18633
Under reasonable scope rules such code is problematic.
18672
- If we assume that \code{v} is declared
18634
+ If we specify that \code{v} is declared
18673
18635
in the scope of the method \code{main()},
18674
18636
then when \code{somePredicate} evaluates to the \FALSE{} object,
18675
18637
\code{v} will be uninitialized when accessed.
@@ -18688,37 +18650,21 @@ \subsection{If}
18688
18650
}
18689
18651
18690
18652
\LMHash{}%
18691
- Execution of an if statement of the form
18692
- \ code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18693
- where $s_1$ and $s_2$ are block statements,
18694
- proceeds as follows:
18653
+ Consider an \IF{} statement of any of the forms mentioned above
18654
+ (\commentary{the statement then starts with \ code{\IF\,\,($e$)}}).
18655
+ It is a \Error{compile-time error} if the type of the expression $e$
18656
+ is not assignable to \code{bool}.
18695
18657
18696
18658
\LMHash{}%
18697
- First, the expression $b$ is evaluated to an object $o$.
18659
+ Execution of an \IF{} statement of the form
18660
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18661
+ where $S_1$ and $S_2$ are block statements
18662
+ proceeds as follows:
18663
+ Evaluate the expression $e$ to an object $o$.
18698
18664
% This error can occur due to an implicit downcast from \DYNAMIC.
18699
18665
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18700
- If $o$ is \TRUE, then the block statement $s_1$ is executed,
18701
- otherwise the block statement $s_2$ is executed.
18702
-
18703
- \LMHash{}%
18704
- It is a \Error{compile-time error} if the type of the expression $b$
18705
- may not be assigned to \code{bool}.
18706
-
18707
- \LMHash{}%
18708
- If $b$ shows that a local variable $v$ has type $T$,
18709
- then the type of $v$ is known to be $T$ in $s_1$,
18710
- unless any of the following are true
18711
- \begin{itemize}
18712
- \item $v$ is potentially mutated in $s_1$,
18713
- \item $v$ is potentially mutated within a function other
18714
- than the one where $v$ is declared, or
18715
- \item $v$ is accessed by a function defined in $s_1$ and
18716
- $v$ is potentially mutated anywhere in the scope of $v$.
18717
- \end{itemize}
18718
-
18719
- \LMHash{}%
18720
- An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18721
- the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18666
+ If $o$ is \TRUE, then execute the block statement $S_1$,
18667
+ otherwise execute the block statement $S_2$.
18722
18668
18723
18669
18724
18670
\subsection{For}
@@ -21593,34 +21539,6 @@ \subsection{Static Types}
21593
21539
% objects in constructor args - these cannot represent parameterized types.
21594
21540
21595
21541
21596
- \subsubsection{Local Variable Type Promotion}
21597
- \LMLabel{typePromotion}
21598
-
21599
- \LMHash{}%
21600
- The static type system ascribes a static type to every expression.
21601
- In some cases, the type of a local variable
21602
- (\commentary{which can be a formal parameter})
21603
- may be promoted from the declared type, based on control flow.
21604
-
21605
- \LMHash{}%
21606
- We say that a variable $v$ is known to have type $T$
21607
- whenever we allow the type of $v$ to be promoted.
21608
- The exact circumstances when type promotion is allowed are given in
21609
- the relevant sections of the specification
21610
- (\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21611
-
21612
- \LMHash{}%
21613
- Type promotion for a variable $v$ is allowed only when we can deduce that
21614
- such promotion is valid based on an analysis of certain boolean expressions.
21615
- In such cases, we say that
21616
- the boolean expression $b$ shows that $v$ has type $T$.
21617
- As a rule, for all variables $v$ and types $T$, a boolean expression
21618
- does not show that $v$ has type $T$.
21619
- Those situations where an expression does show that a variable has a type are
21620
- mentioned explicitly in the relevant sections of this specification
21621
- (\ref{typeTest} and \ref{logicalBooleanExpressions}).
21622
-
21623
-
21624
21542
\subsection{Dynamic Type System}
21625
21543
\LMLabel{dynamicTypeSystem}
21626
21544
0 commit comments