Skip to content

Commit 37854f7

Browse files
committed
Conditional expression; operators && and ||; if statements; delete old section about promotion
1 parent aa7e45a commit 37854f7

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
@@ -16166,7 +16166,7 @@ \subsection{Conditional}
1616616166
\LMLabel{conditional}
1616716167

1616816168
\LMHash{}%
16169-
A \Index{conditional expression} evaluates one of two expressions
16169+
A \Index{conditional expression} evaluates one of two expressions,
1617016170
based on a boolean condition.
1617116171

1617216172
\begin{grammar}
@@ -16175,34 +16175,23 @@ \subsection{Conditional}
1617516175
\end{grammar}
1617616176

1617716177
\LMHash{}%
16178-
Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16179-
proceeds as follows:
16178+
Consider a conditional expression $c$ of the form
16179+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16180+
It is a \Error{compile-time error} if
16181+
the static type of $e_1$ may not be assigned to \code{bool}.
16182+
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16183+
the static type of $e_2$ and the static type of $e_3$.
1618016184

1618116185
\LMHash{}%
16182-
First, $e_1$ is evaluated to an object $o_1$.
16186+
Evaluation of a conditional expression $c$ of the form
16187+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16188+
proceeds as follows:
16189+
Evaluate $e_1$ to an object $o_1$.
1618316190
% This error can occur due to an implicit cast from \DYNAMIC.
1618416191
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16185-
If $r$ is \TRUE, then the value of $c$ is
16192+
If $o_1$ is the \TRUE{} object, then the value of $c$ is
1618616193
the result of evaluating the expression $e_2$.
16187-
Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16188-
16189-
\LMHash{}%
16190-
If $e_1$ shows that a local variable $v$ has type $T$,
16191-
then the type of $v$ is known to be $T$ in $e_2$,
16192-
unless any of the following are true:
16193-
\begin{itemize}
16194-
\item $v$ is potentially mutated in $e_2$,
16195-
\item $v$ is potentially mutated within a function other
16196-
than the one where $v$ is declared, or
16197-
\item $v$ is accessed by a function defined in $e_2$ and
16198-
$v$ is potentially mutated anywhere in the scope of $v$.
16199-
\end{itemize}
16200-
16201-
\LMHash{}%
16202-
It is a \Error{compile-time error} if
16203-
the static type of $e_1$ may not be assigned to \code{bool}.
16204-
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16205-
the static type of $e_2$ and the static type of $e_3$.
16194+
Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
1620616195

1620716196

1620816197
\subsection{If-null Expressions}
@@ -16257,8 +16246,15 @@ \subsection{Logical Boolean Expressions}
1625716246
an expression $e_1$ with argument $e_2$.
1625816247

1625916248
\LMHash{}%
16260-
Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16261-
the evaluation of $e_1$ to an object $o_1$.
16249+
It is a \Error{compile-time error} if
16250+
the static type of $e_1$ may not be assigned to \code{bool}
16251+
or if the static type of $e_2$ may not be assigned to \code{bool}.
16252+
The static type of a logical boolean expression is \code{bool}.
16253+
16254+
\LMHash{}%
16255+
Evaluation of a logical boolean expression $b$ of the form
16256+
\code{$e_1$\,\,||\,\,$e_2$}
16257+
causes the evaluation of $e_1$ to an object $o_1$.
1626216258
% This error can occur due to an implicit downcast from \DYNAMIC.
1626316259
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1626416260
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16268,8 +16264,9 @@ \subsection{Logical Boolean Expressions}
1626816264
Otherwise the result of evaluating $b$ is $o_2$.
1626916265

1627016266
\LMHash{}%
16271-
Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16272-
causes the evaluation of $e_1$ producing an object $o_1$.
16267+
Evaluation of a logical boolean expression $b$ of the form
16268+
\code{$e_1$\,\,\&\&\,\,$e_2$}
16269+
causes the evaluation of $e_1$ to an object $o_1$.
1627316270
% This error can occur due to an implicit downcast from \DYNAMIC.
1627416271
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1627516272
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16278,45 +16275,6 @@ \subsection{Logical Boolean Expressions}
1627816275
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1627916276
Otherwise the result of evaluating $b$ is $o_2$.
1628016277

16281-
\LMHash{}%
16282-
A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16283-
shows that a local variable $v$ has type $T$
16284-
if both of the following conditions hold:
16285-
\begin{itemize}
16286-
\item Either $e_1$ shows that $v$ has type $T$
16287-
or $e_2$ shows that $v$ has type $T$.
16288-
\item $v$ is not mutated in $e_2$ or within a function
16289-
other than the one where $v$ is declared.
16290-
\end{itemize}
16291-
16292-
\LMHash{}%
16293-
If $e_1$ shows that a local variable $v$ has type $T$,
16294-
then the type of $v$ is known to be $T$ in $e_2$,
16295-
unless any of the following are true:
16296-
\begin{itemize}
16297-
%% The first item here is unnecessary for soundness,
16298-
%% and is retained mainly for backwards compatibility.
16299-
%% If $e_1$ shows that $v$ has type $T$, then any assignment
16300-
%% in $e_1$ does not invalidate that.
16301-
%% Removing the line is visible in the semantics, though, because:
16302-
%% num x;
16303-
%% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16304-
%% is allowed without the line, and disallowed with.
16305-
%% At time of writing, the analyzer disallows the code.
16306-
\item $v$ is potentially mutated in $e_1$,
16307-
\item $v$ is potentially mutated in $e_2$,
16308-
\item $v$ is potentially mutated within a function other
16309-
than the one where $v$ is declared, or
16310-
\item $v$ is accessed by a function defined in $e_2$ and
16311-
$v$ is potentially mutated anywhere in the scope of $v$.
16312-
\end{itemize}
16313-
16314-
\LMHash{}%
16315-
It is a \Error{compile-time error} if
16316-
the static type of $e_1$ may not be assigned to \code{bool}
16317-
or if the static type of $e_2$ may not be assigned to \code{bool}.
16318-
The static type of a logical boolean expression is \code{bool}.
16319-
1632016278

1632116279
\subsection{Equality}
1632216280
\LMLabel{equality}
@@ -18251,37 +18209,41 @@ \subsection{If}
1825118209
\LMLabel{if}
1825218210

1825318211
\LMHash{}%
18254-
The \Index{if statement} allows for conditional execution of statements.
18212+
The \Index{\IF{} statement} allows for conditional execution of statements.
1825518213

1825618214
\begin{grammar}
1825718215
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
1825818216
\end{grammar}
1825918217

1826018218
\LMHash{}%
18261-
An if statement of the form
18262-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18263-
where $s_1$ is not a block statement is equivalent to the statement
18264-
\code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18265-
An if statement of the form
18266-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18267-
where $s_2$ is not a block statement is equivalent to the statement
18268-
\code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18219+
An \IF{} statement of the form
18220+
\code{\IF\,\,($e$)\,\,$S$}
18221+
is treated as
18222+
(\ref{overview})
18223+
\code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18224+
An \IF{} statement of the form
18225+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18226+
where $S_1$ is not a block statement is treated as
18227+
\code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18228+
An \IF{} statement of the form
18229+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18230+
where $S_2$ is not a block statement is treated as
18231+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
1826918232

1827018233
\rationale{%
1827118234
The reason for this equivalence is to catch errors such as%
1827218235
}
1827318236

1827418237
\begin{dartCode}
1827518238
\VOID{} main() \{
18276-
\IF{} (somePredicate)
18277-
\VAR{} v = 2;
18239+
\IF{} (somePredicate) \VAR{} v = 2;
1827818240
print(v);
1827918241
\}
1828018242
\end{dartCode}
1828118243

1828218244
\rationale{%
1828318245
Under reasonable scope rules such code is problematic.
18284-
If we assume that \code{v} is declared
18246+
If we specify that \code{v} is declared
1828518247
in the scope of the method \code{main()},
1828618248
then when \code{somePredicate} evaluates to the \FALSE{} object,
1828718249
\code{v} will be uninitialized when accessed.
@@ -18300,37 +18262,21 @@ \subsection{If}
1830018262
}
1830118263

1830218264
\LMHash{}%
18303-
Execution of an if statement of the form
18304-
\code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18305-
where $s_1$ and $s_2$ are block statements,
18306-
proceeds as follows:
18265+
Consider an \IF{} statement of any of the forms mentioned above
18266+
(\commentary{the statement then starts with \code{\IF\,\,($e$)}}).
18267+
It is a \Error{compile-time error} if the type of the expression $e$
18268+
is not assignable to \code{bool}.
1830718269

1830818270
\LMHash{}%
18309-
First, the expression $b$ is evaluated to an object $o$.
18271+
Execution of an \IF{} statement of the form
18272+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18273+
where $S_1$ and $S_2$ are block statements
18274+
proceeds as follows:
18275+
Evaluate the expression $e$ to an object $o$.
1831018276
% This error can occur due to an implicit downcast from \DYNAMIC.
1831118277
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18312-
If $o$ is \TRUE, then the block statement $s_1$ is executed,
18313-
otherwise the block statement $s_2$ is executed.
18314-
18315-
\LMHash{}%
18316-
It is a \Error{compile-time error} if the type of the expression $b$
18317-
may not be assigned to \code{bool}.
18318-
18319-
\LMHash{}%
18320-
If $b$ shows that a local variable $v$ has type $T$,
18321-
then the type of $v$ is known to be $T$ in $s_1$,
18322-
unless any of the following are true
18323-
\begin{itemize}
18324-
\item $v$ is potentially mutated in $s_1$,
18325-
\item $v$ is potentially mutated within a function other
18326-
than the one where $v$ is declared, or
18327-
\item $v$ is accessed by a function defined in $s_1$ and
18328-
$v$ is potentially mutated anywhere in the scope of $v$.
18329-
\end{itemize}
18330-
18331-
\LMHash{}%
18332-
An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18333-
the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18278+
If $o$ is \TRUE, then execute the block statement $S_1$,
18279+
otherwise execute the block statement $S_2$.
1833418280

1833518281

1833618282
\subsection{For}
@@ -21166,34 +21112,6 @@ \subsection{Static Types}
2116621112
% objects in constructor args - these cannot represent parameterized types.
2116721113

2116821114

21169-
\subsubsection{Local Variable Type Promotion}
21170-
\LMLabel{typePromotion}
21171-
21172-
\LMHash{}%
21173-
The static type system ascribes a static type to every expression.
21174-
In some cases, the type of a local variable
21175-
(\commentary{which can be a formal parameter})
21176-
may be promoted from the declared type, based on control flow.
21177-
21178-
\LMHash{}%
21179-
We say that a variable $v$ is known to have type $T$
21180-
whenever we allow the type of $v$ to be promoted.
21181-
The exact circumstances when type promotion is allowed are given in
21182-
the relevant sections of the specification
21183-
(\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21184-
21185-
\LMHash{}%
21186-
Type promotion for a variable $v$ is allowed only when we can deduce that
21187-
such promotion is valid based on an analysis of certain boolean expressions.
21188-
In such cases, we say that
21189-
the boolean expression $b$ shows that $v$ has type $T$.
21190-
As a rule, for all variables $v$ and types $T$, a boolean expression
21191-
does not show that $v$ has type $T$.
21192-
Those situations where an expression does show that a variable has a type are
21193-
mentioned explicitly in the relevant sections of this specification
21194-
(\ref{typeTest} and \ref{logicalBooleanExpressions}).
21195-
21196-
2119721115
\subsection{Dynamic Type System}
2119821116
\LMLabel{dynamicTypeSystem}
2119921117

0 commit comments

Comments
 (0)