Skip to content

Commit 1408243

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

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
@@ -16168,7 +16168,7 @@ \subsection{Conditional}
1616816168
\LMLabel{conditional}
1616916169

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

1617416174
\begin{grammar}
@@ -16177,34 +16177,23 @@ \subsection{Conditional}
1617716177
\end{grammar}
1617816178

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

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

1620916198

1621016199
\subsection{If-null Expressions}
@@ -16259,8 +16248,15 @@ \subsection{Logical Boolean Expressions}
1625916248
an expression $e_1$ with argument $e_2$.
1626016249

1626116250
\LMHash{}%
16262-
Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16263-
the evaluation of $e_1$ to an object $o_1$.
16251+
It is a \Error{compile-time error} if
16252+
the static type of $e_1$ may not be assigned to \code{bool}
16253+
or if the static type of $e_2$ may not be assigned to \code{bool}.
16254+
The static type of a logical boolean expression is \code{bool}.
16255+
16256+
\LMHash{}%
16257+
Evaluation of a logical boolean expression $b$ of the form
16258+
\code{$e_1$\,\,||\,\,$e_2$}
16259+
causes the evaluation of $e_1$ to an object $o_1$.
1626416260
% This error can occur due to an implicit downcast from \DYNAMIC.
1626516261
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1626616262
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16270,8 +16266,9 @@ \subsection{Logical Boolean Expressions}
1627016266
Otherwise the result of evaluating $b$ is $o_2$.
1627116267

1627216268
\LMHash{}%
16273-
Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16274-
causes the evaluation of $e_1$ producing an object $o_1$.
16269+
Evaluation of a logical boolean expression $b$ of the form
16270+
\code{$e_1$\,\,\&\&\,\,$e_2$}
16271+
causes the evaluation of $e_1$ to an object $o_1$.
1627516272
% This error can occur due to an implicit downcast from \DYNAMIC.
1627616273
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1627716274
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16280,45 +16277,6 @@ \subsection{Logical Boolean Expressions}
1628016277
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1628116278
Otherwise the result of evaluating $b$ is $o_2$.
1628216279

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

1632316281
\subsection{Equality}
1632416282
\LMLabel{equality}
@@ -18253,37 +18211,41 @@ \subsection{If}
1825318211
\LMLabel{if}
1825418212

1825518213
\LMHash{}%
18256-
The \Index{if statement} allows for conditional execution of statements.
18214+
The \Index{\IF{} statement} allows for conditional execution of statements.
1825718215

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

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

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

1827618239
\begin{dartCode}
1827718240
\VOID{} main() \{
18278-
\IF{} (somePredicate)
18279-
\VAR{} v = 2;
18241+
\IF{} (somePredicate) \VAR{} v = 2;
1828018242
print(v);
1828118243
\}
1828218244
\end{dartCode}
1828318245

1828418246
\rationale{%
1828518247
Under reasonable scope rules such code is problematic.
18286-
If we assume that \code{v} is declared
18248+
If we specify that \code{v} is declared
1828718249
in the scope of the method \code{main()},
1828818250
then when \code{somePredicate} evaluates to the \FALSE{} object,
1828918251
\code{v} will be uninitialized when accessed.
@@ -18302,37 +18264,21 @@ \subsection{If}
1830218264
}
1830318265

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

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

1833718283

1833818284
\subsection{For}
@@ -21168,34 +21114,6 @@ \subsection{Static Types}
2116821114
% objects in constructor args - these cannot represent parameterized types.
2116921115

2117021116

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

0 commit comments

Comments
 (0)