Skip to content

Commit 2894d27

Browse files
committed
Conditional expression; operators && and ||; if statements; delete old section about promotion
1 parent 004da13 commit 2894d27

File tree

1 file changed

+52
-134
lines changed

1 file changed

+52
-134
lines changed

specification/dartLangSpec.tex

Lines changed: 52 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -16505,7 +16505,7 @@ \subsection{Conditional}
1650516505
\LMLabel{conditional}
1650616506

1650716507
\LMHash{}%
16508-
A \Index{conditional expression} evaluates one of two expressions
16508+
A \Index{conditional expression} evaluates one of two expressions,
1650916509
based on a boolean condition.
1651016510

1651116511
\begin{grammar}
@@ -16514,34 +16514,23 @@ \subsection{Conditional}
1651416514
\end{grammar}
1651516515

1651616516
\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$.
1651916523

1652016524
\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$.
1652216529
% This error can occur due to an implicit cast from \DYNAMIC.
1652316530
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
1652516532
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$.
1654516534

1654616535

1654716536
\subsection{If-null Expressions}
@@ -16596,8 +16585,15 @@ \subsection{Logical Boolean Expressions}
1659616585
an expression $e_1$ with argument $e_2$.
1659716586

1659816587
\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$.
1660116597
% This error can occur due to an implicit downcast from \DYNAMIC.
1660216598
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1660316599
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16607,8 +16603,9 @@ \subsection{Logical Boolean Expressions}
1660716603
Otherwise the result of evaluating $b$ is $o_2$.
1660816604

1660916605
\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$.
1661216609
% This error can occur due to an implicit downcast from \DYNAMIC.
1661316610
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1661416611
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16617,45 +16614,6 @@ \subsection{Logical Boolean Expressions}
1661716614
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1661816615
Otherwise the result of evaluating $b$ is $o_2$.
1661916616

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-
1665916617

1666016618
\subsection{Equality}
1666116619
\LMLabel{equality}
@@ -18663,37 +18621,41 @@ \subsection{If}
1866318621
\LMLabel{if}
1866418622

1866518623
\LMHash{}%
18666-
The \Index{if statement} allows for conditional execution of statements.
18624+
The \Index{\IF{} statement} allows for conditional execution of statements.
1866718625

1866818626
\begin{grammar}
1866918627
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
1867018628
\end{grammar}
1867118629

1867218630
\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$\,\}}.
1868118644

1868218645
\rationale{%
1868318646
The reason for this equivalence is to catch errors such as%
1868418647
}
1868518648

1868618649
\begin{dartCode}
1868718650
\VOID{} main() \{
18688-
\IF{} (somePredicate)
18689-
\VAR{} v = 2;
18651+
\IF{} (somePredicate) \VAR{} v = 2;
1869018652
print(v);
1869118653
\}
1869218654
\end{dartCode}
1869318655

1869418656
\rationale{%
1869518657
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
1869718659
in the scope of the method \code{main()},
1869818660
then when \code{somePredicate} evaluates to the \FALSE{} object,
1869918661
\code{v} will be uninitialized when accessed.
@@ -18712,37 +18674,21 @@ \subsection{If}
1871218674
}
1871318675

1871418676
\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}.
1871918681

1872018682
\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$.
1872218688
% This error can occur due to an implicit downcast from \DYNAMIC.
1872318689
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$.
1874618692

1874718693

1874818694
\subsection{For}
@@ -21614,34 +21560,6 @@ \subsection{Static Types}
2161421560
% objects in constructor args - these cannot represent parameterized types.
2161521561

2161621562

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-
2164521563
\subsection{Dynamic Type System}
2164621564
\LMLabel{dynamicTypeSystem}
2164721565

0 commit comments

Comments
 (0)