@@ -16505,7 +16505,7 @@ \subsection{Conditional}
16505
16505
\LMLabel{conditional}
16506
16506
16507
16507
\LMHash{}%
16508
- A \Index{conditional expression} evaluates one of two expressions
16508
+ A \Index{conditional expression} evaluates one of two expressions,
16509
16509
based on a boolean condition.
16510
16510
16511
16511
\begin{grammar}
@@ -16514,34 +16514,23 @@ \subsection{Conditional}
16514
16514
\end{grammar}
16515
16515
16516
16516
\LMHash{}%
16517
- Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16518
- proceeds as follows:
16517
+ Consider a conditional expression $c$ of the form
16518
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16519
+ It is a \Error{compile-time error} if
16520
+ the static type of $e_1$ may not be assigned to \code{bool}.
16521
+ The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16522
+ the static type of $e_2$ and the static type of $e_3$.
16519
16523
16520
16524
\LMHash{}%
16521
- First, $e_1$ is evaluated to an object $o_1$.
16525
+ Evaluation of a conditional expression $c$ of the form
16526
+ \code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16527
+ proceeds as follows:
16528
+ Evaluate $e_1$ to an object $o_1$.
16522
16529
% This error can occur due to an implicit cast from \DYNAMIC.
16523
16530
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16524
- If $r $ is \TRUE, then the value of $c$ is
16531
+ If $o_1 $ is the \TRUE{} object , then the value of $c$ is
16525
16532
the result of evaluating the expression $e_2$.
16526
- Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16527
-
16528
- \LMHash{}%
16529
- If $e_1$ shows that a local variable $v$ has type $T$,
16530
- then the type of $v$ is known to be $T$ in $e_2$,
16531
- unless any of the following are true:
16532
- \begin{itemize}
16533
- \item $v$ is potentially mutated in $e_2$,
16534
- \item $v$ is potentially mutated within a function other
16535
- than the one where $v$ is declared, or
16536
- \item $v$ is accessed by a function defined in $e_2$ and
16537
- $v$ is potentially mutated anywhere in the scope of $v$.
16538
- \end{itemize}
16539
-
16540
- \LMHash{}%
16541
- It is a \Error{compile-time error} if
16542
- the static type of $e_1$ may not be assigned to \code{bool}.
16543
- The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16544
- the static type of $e_2$ and the static type of $e_3$.
16533
+ Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
16545
16534
16546
16535
16547
16536
\subsection{If-null Expressions}
@@ -16596,8 +16585,15 @@ \subsection{Logical Boolean Expressions}
16596
16585
an expression $e_1$ with argument $e_2$.
16597
16586
16598
16587
\LMHash{}%
16599
- Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16600
- the evaluation of $e_1$ to an object $o_1$.
16588
+ It is a \Error{compile-time error} if
16589
+ the static type of $e_1$ may not be assigned to \code{bool}
16590
+ or if the static type of $e_2$ may not be assigned to \code{bool}.
16591
+ The static type of a logical boolean expression is \code{bool}.
16592
+
16593
+ \LMHash{}%
16594
+ Evaluation of a logical boolean expression $b$ of the form
16595
+ \code{$e_1$\,\,||\,\,$e_2$}
16596
+ causes the evaluation of $e_1$ to an object $o_1$.
16601
16597
% This error can occur due to an implicit downcast from \DYNAMIC.
16602
16598
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16603
16599
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16607,8 +16603,9 @@ \subsection{Logical Boolean Expressions}
16607
16603
Otherwise the result of evaluating $b$ is $o_2$.
16608
16604
16609
16605
\LMHash{}%
16610
- Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16611
- causes the evaluation of $e_1$ producing an object $o_1$.
16606
+ Evaluation of a logical boolean expression $b$ of the form
16607
+ \code{$e_1$\,\,\&\&\,\,$e_2$}
16608
+ causes the evaluation of $e_1$ to an object $o_1$.
16612
16609
% This error can occur due to an implicit downcast from \DYNAMIC.
16613
16610
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16614
16611
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16617,45 +16614,6 @@ \subsection{Logical Boolean Expressions}
16617
16614
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
16618
16615
Otherwise the result of evaluating $b$ is $o_2$.
16619
16616
16620
- \LMHash{}%
16621
- A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16622
- shows that a local variable $v$ has type $T$
16623
- if both of the following conditions hold:
16624
- \begin{itemize}
16625
- \item Either $e_1$ shows that $v$ has type $T$
16626
- or $e_2$ shows that $v$ has type $T$.
16627
- \item $v$ is not mutated in $e_2$ or within a function
16628
- other than the one where $v$ is declared.
16629
- \end{itemize}
16630
-
16631
- \LMHash{}%
16632
- If $e_1$ shows that a local variable $v$ has type $T$,
16633
- then the type of $v$ is known to be $T$ in $e_2$,
16634
- unless any of the following are true:
16635
- \begin{itemize}
16636
- %% The first item here is unnecessary for soundness,
16637
- %% and is retained mainly for backwards compatibility.
16638
- %% If $e_1$ shows that $v$ has type $T$, then any assignment
16639
- %% in $e_1$ does not invalidate that.
16640
- %% Removing the line is visible in the semantics, though, because:
16641
- %% num x;
16642
- %% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16643
- %% is allowed without the line, and disallowed with.
16644
- %% At time of writing, the analyzer disallows the code.
16645
- \item $v$ is potentially mutated in $e_1$,
16646
- \item $v$ is potentially mutated in $e_2$,
16647
- \item $v$ is potentially mutated within a function other
16648
- than the one where $v$ is declared, or
16649
- \item $v$ is accessed by a function defined in $e_2$ and
16650
- $v$ is potentially mutated anywhere in the scope of $v$.
16651
- \end{itemize}
16652
-
16653
- \LMHash{}%
16654
- It is a \Error{compile-time error} if
16655
- the static type of $e_1$ may not be assigned to \code{bool}
16656
- or if the static type of $e_2$ may not be assigned to \code{bool}.
16657
- The static type of a logical boolean expression is \code{bool}.
16658
-
16659
16617
16660
16618
\subsection{Equality}
16661
16619
\LMLabel{equality}
@@ -18663,37 +18621,41 @@ \subsection{If}
18663
18621
\LMLabel{if}
18664
18622
18665
18623
\LMHash{}%
18666
- The \Index{if statement} allows for conditional execution of statements.
18624
+ The \Index{\IF{} statement} allows for conditional execution of statements.
18667
18625
18668
18626
\begin{grammar}
18669
18627
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
18670
18628
\end{grammar}
18671
18629
18672
18630
\LMHash{}%
18673
- An if statement of the form
18674
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18675
- where $s_1$ is not a block statement is equivalent to the statement
18676
- \code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18677
- An if statement of the form
18678
- \code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18679
- where $s_2$ is not a block statement is equivalent to the statement
18680
- \code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18631
+ An \IF{} statement of the form
18632
+ \code{\IF\,\,($e$)\,\,$S$}
18633
+ is treated as
18634
+ (\ref{overview})
18635
+ \code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18636
+ An \IF{} statement of the form
18637
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18638
+ where $S_1$ is not a block statement is treated as
18639
+ \code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18640
+ An \IF{} statement of the form
18641
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18642
+ where $S_2$ is not a block statement is treated as
18643
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
18681
18644
18682
18645
\rationale{%
18683
18646
The reason for this equivalence is to catch errors such as%
18684
18647
}
18685
18648
18686
18649
\begin{dartCode}
18687
18650
\VOID{} main() \{
18688
- \IF{} (somePredicate)
18689
- \VAR{} v = 2;
18651
+ \IF{} (somePredicate) \VAR{} v = 2;
18690
18652
print(v);
18691
18653
\}
18692
18654
\end{dartCode}
18693
18655
18694
18656
\rationale{%
18695
18657
Under reasonable scope rules such code is problematic.
18696
- If we assume that \code{v} is declared
18658
+ If we specify that \code{v} is declared
18697
18659
in the scope of the method \code{main()},
18698
18660
then when \code{somePredicate} evaluates to the \FALSE{} object,
18699
18661
\code{v} will be uninitialized when accessed.
@@ -18712,37 +18674,21 @@ \subsection{If}
18712
18674
}
18713
18675
18714
18676
\LMHash{}%
18715
- Execution of an if statement of the form
18716
- \ code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18717
- where $s_1$ and $s_2$ are block statements,
18718
- proceeds as follows:
18677
+ Consider an \IF{} statement of any of the forms mentioned above
18678
+ (\commentary{the statement then starts with \ code{\IF\,\,($e$)}}).
18679
+ It is a \Error{compile-time error} if the type of the expression $e$
18680
+ is not assignable to \code{bool}.
18719
18681
18720
18682
\LMHash{}%
18721
- First, the expression $b$ is evaluated to an object $o$.
18683
+ Execution of an \IF{} statement of the form
18684
+ \code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18685
+ where $S_1$ and $S_2$ are block statements
18686
+ proceeds as follows:
18687
+ Evaluate the expression $e$ to an object $o$.
18722
18688
% This error can occur due to an implicit downcast from \DYNAMIC.
18723
18689
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18724
- If $o$ is \TRUE, then the block statement $s_1$ is executed,
18725
- otherwise the block statement $s_2$ is executed.
18726
-
18727
- \LMHash{}%
18728
- It is a \Error{compile-time error} if the type of the expression $b$
18729
- may not be assigned to \code{bool}.
18730
-
18731
- \LMHash{}%
18732
- If $b$ shows that a local variable $v$ has type $T$,
18733
- then the type of $v$ is known to be $T$ in $s_1$,
18734
- unless any of the following are true
18735
- \begin{itemize}
18736
- \item $v$ is potentially mutated in $s_1$,
18737
- \item $v$ is potentially mutated within a function other
18738
- than the one where $v$ is declared, or
18739
- \item $v$ is accessed by a function defined in $s_1$ and
18740
- $v$ is potentially mutated anywhere in the scope of $v$.
18741
- \end{itemize}
18742
-
18743
- \LMHash{}%
18744
- An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18745
- the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18690
+ If $o$ is \TRUE, then execute the block statement $S_1$,
18691
+ otherwise execute the block statement $S_2$.
18746
18692
18747
18693
18748
18694
\subsection{For}
@@ -21614,34 +21560,6 @@ \subsection{Static Types}
21614
21560
% objects in constructor args - these cannot represent parameterized types.
21615
21561
21616
21562
21617
- \subsubsection{Local Variable Type Promotion}
21618
- \LMLabel{typePromotion}
21619
-
21620
- \LMHash{}%
21621
- The static type system ascribes a static type to every expression.
21622
- In some cases, the type of a local variable
21623
- (\commentary{which can be a formal parameter})
21624
- may be promoted from the declared type, based on control flow.
21625
-
21626
- \LMHash{}%
21627
- We say that a variable $v$ is known to have type $T$
21628
- whenever we allow the type of $v$ to be promoted.
21629
- The exact circumstances when type promotion is allowed are given in
21630
- the relevant sections of the specification
21631
- (\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21632
-
21633
- \LMHash{}%
21634
- Type promotion for a variable $v$ is allowed only when we can deduce that
21635
- such promotion is valid based on an analysis of certain boolean expressions.
21636
- In such cases, we say that
21637
- the boolean expression $b$ shows that $v$ has type $T$.
21638
- As a rule, for all variables $v$ and types $T$, a boolean expression
21639
- does not show that $v$ has type $T$.
21640
- Those situations where an expression does show that a variable has a type are
21641
- mentioned explicitly in the relevant sections of this specification
21642
- (\ref{typeTest} and \ref{logicalBooleanExpressions}).
21643
-
21644
-
21645
21563
\subsection{Dynamic Type System}
21646
21564
\LMLabel{dynamicTypeSystem}
21647
21565
0 commit comments