Skip to content

Commit a6eaef9

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

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
@@ -16326,7 +16326,7 @@ \subsection{Conditional}
1632616326
\LMLabel{conditional}
1632716327

1632816328
\LMHash{}%
16329-
A \Index{conditional expression} evaluates one of two expressions
16329+
A \Index{conditional expression} evaluates one of two expressions,
1633016330
based on a boolean condition.
1633116331

1633216332
\begin{grammar}
@@ -16335,34 +16335,23 @@ \subsection{Conditional}
1633516335
\end{grammar}
1633616336

1633716337
\LMHash{}%
16338-
Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16339-
proceeds as follows:
16338+
Consider a conditional expression $c$ of the form
16339+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16340+
It is a \Error{compile-time error} if
16341+
the static type of $e_1$ may not be assigned to \code{bool}.
16342+
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16343+
the static type of $e_2$ and the static type of $e_3$.
1634016344

1634116345
\LMHash{}%
16342-
First, $e_1$ is evaluated to an object $o_1$.
16346+
Evaluation of a conditional expression $c$ of the form
16347+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16348+
proceeds as follows:
16349+
Evaluate $e_1$ to an object $o_1$.
1634316350
% This error can occur due to an implicit cast from \DYNAMIC.
1634416351
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16345-
If $r$ is \TRUE, then the value of $c$ is
16352+
If $o_1$ is the \TRUE{} object, then the value of $c$ is
1634616353
the result of evaluating the expression $e_2$.
16347-
Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16348-
16349-
\LMHash{}%
16350-
If $e_1$ shows that a local variable $v$ has type $T$,
16351-
then the type of $v$ is known to be $T$ in $e_2$,
16352-
unless any of the following are true:
16353-
\begin{itemize}
16354-
\item $v$ is potentially mutated in $e_2$,
16355-
\item $v$ is potentially mutated within a function other
16356-
than the one where $v$ is declared, or
16357-
\item $v$ is accessed by a function defined in $e_2$ and
16358-
$v$ is potentially mutated anywhere in the scope of $v$.
16359-
\end{itemize}
16360-
16361-
\LMHash{}%
16362-
It is a \Error{compile-time error} if
16363-
the static type of $e_1$ may not be assigned to \code{bool}.
16364-
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16365-
the static type of $e_2$ and the static type of $e_3$.
16354+
Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
1636616355

1636716356

1636816357
\subsection{If-null Expressions}
@@ -16417,8 +16406,15 @@ \subsection{Logical Boolean Expressions}
1641716406
an expression $e_1$ with argument $e_2$.
1641816407

1641916408
\LMHash{}%
16420-
Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16421-
the evaluation of $e_1$ to an object $o_1$.
16409+
It is a \Error{compile-time error} if
16410+
the static type of $e_1$ may not be assigned to \code{bool}
16411+
or if the static type of $e_2$ may not be assigned to \code{bool}.
16412+
The static type of a logical boolean expression is \code{bool}.
16413+
16414+
\LMHash{}%
16415+
Evaluation of a logical boolean expression $b$ of the form
16416+
\code{$e_1$\,\,||\,\,$e_2$}
16417+
causes the evaluation of $e_1$ to an object $o_1$.
1642216418
% This error can occur due to an implicit downcast from \DYNAMIC.
1642316419
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1642416420
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16428,8 +16424,9 @@ \subsection{Logical Boolean Expressions}
1642816424
Otherwise the result of evaluating $b$ is $o_2$.
1642916425

1643016426
\LMHash{}%
16431-
Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16432-
causes the evaluation of $e_1$ producing an object $o_1$.
16427+
Evaluation of a logical boolean expression $b$ of the form
16428+
\code{$e_1$\,\,\&\&\,\,$e_2$}
16429+
causes the evaluation of $e_1$ to an object $o_1$.
1643316430
% This error can occur due to an implicit downcast from \DYNAMIC.
1643416431
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1643516432
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16438,45 +16435,6 @@ \subsection{Logical Boolean Expressions}
1643816435
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1643916436
Otherwise the result of evaluating $b$ is $o_2$.
1644016437

16441-
\LMHash{}%
16442-
A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16443-
shows that a local variable $v$ has type $T$
16444-
if both of the following conditions hold:
16445-
\begin{itemize}
16446-
\item Either $e_1$ shows that $v$ has type $T$
16447-
or $e_2$ shows that $v$ has type $T$.
16448-
\item $v$ is not mutated in $e_2$ or within a function
16449-
other than the one where $v$ is declared.
16450-
\end{itemize}
16451-
16452-
\LMHash{}%
16453-
If $e_1$ shows that a local variable $v$ has type $T$,
16454-
then the type of $v$ is known to be $T$ in $e_2$,
16455-
unless any of the following are true:
16456-
\begin{itemize}
16457-
%% The first item here is unnecessary for soundness,
16458-
%% and is retained mainly for backwards compatibility.
16459-
%% If $e_1$ shows that $v$ has type $T$, then any assignment
16460-
%% in $e_1$ does not invalidate that.
16461-
%% Removing the line is visible in the semantics, though, because:
16462-
%% num x;
16463-
%% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16464-
%% is allowed without the line, and disallowed with.
16465-
%% At time of writing, the analyzer disallows the code.
16466-
\item $v$ is potentially mutated in $e_1$,
16467-
\item $v$ is potentially mutated in $e_2$,
16468-
\item $v$ is potentially mutated within a function other
16469-
than the one where $v$ is declared, or
16470-
\item $v$ is accessed by a function defined in $e_2$ and
16471-
$v$ is potentially mutated anywhere in the scope of $v$.
16472-
\end{itemize}
16473-
16474-
\LMHash{}%
16475-
It is a \Error{compile-time error} if
16476-
the static type of $e_1$ may not be assigned to \code{bool}
16477-
or if the static type of $e_2$ may not be assigned to \code{bool}.
16478-
The static type of a logical boolean expression is \code{bool}.
16479-
1648016438

1648116439
\subsection{Equality}
1648216440
\LMLabel{equality}
@@ -18482,37 +18440,41 @@ \subsection{If}
1848218440
\LMLabel{if}
1848318441

1848418442
\LMHash{}%
18485-
The \Index{if statement} allows for conditional execution of statements.
18443+
The \Index{\IF{} statement} allows for conditional execution of statements.
1848618444

1848718445
\begin{grammar}
1848818446
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
1848918447
\end{grammar}
1849018448

1849118449
\LMHash{}%
18492-
An if statement of the form
18493-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18494-
where $s_1$ is not a block statement is equivalent to the statement
18495-
\code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18496-
An if statement of the form
18497-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18498-
where $s_2$ is not a block statement is equivalent to the statement
18499-
\code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18450+
An \IF{} statement of the form
18451+
\code{\IF\,\,($e$)\,\,$S$}
18452+
is treated as
18453+
(\ref{overview})
18454+
\code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18455+
An \IF{} statement of the form
18456+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18457+
where $S_1$ is not a block statement is treated as
18458+
\code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18459+
An \IF{} statement of the form
18460+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18461+
where $S_2$ is not a block statement is treated as
18462+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
1850018463

1850118464
\rationale{%
1850218465
The reason for this equivalence is to catch errors such as%
1850318466
}
1850418467

1850518468
\begin{dartCode}
1850618469
\VOID{} main() \{
18507-
\IF{} (somePredicate)
18508-
\VAR{} v = 2;
18470+
\IF{} (somePredicate) \VAR{} v = 2;
1850918471
print(v);
1851018472
\}
1851118473
\end{dartCode}
1851218474

1851318475
\rationale{%
1851418476
Under reasonable scope rules such code is problematic.
18515-
If we assume that \code{v} is declared
18477+
If we specify that \code{v} is declared
1851618478
in the scope of the method \code{main()},
1851718479
then when \code{somePredicate} evaluates to the \FALSE{} object,
1851818480
\code{v} will be uninitialized when accessed.
@@ -18531,37 +18493,21 @@ \subsection{If}
1853118493
}
1853218494

1853318495
\LMHash{}%
18534-
Execution of an if statement of the form
18535-
\code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18536-
where $s_1$ and $s_2$ are block statements,
18537-
proceeds as follows:
18496+
Consider an \IF{} statement of any of the forms mentioned above
18497+
(\commentary{the statement then starts with \code{\IF\,\,($e$)}}).
18498+
It is a \Error{compile-time error} if the type of the expression $e$
18499+
is not assignable to \code{bool}.
1853818500

1853918501
\LMHash{}%
18540-
First, the expression $b$ is evaluated to an object $o$.
18502+
Execution of an \IF{} statement of the form
18503+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18504+
where $S_1$ and $S_2$ are block statements
18505+
proceeds as follows:
18506+
Evaluate the expression $e$ to an object $o$.
1854118507
% This error can occur due to an implicit downcast from \DYNAMIC.
1854218508
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18543-
If $o$ is \TRUE, then the block statement $s_1$ is executed,
18544-
otherwise the block statement $s_2$ is executed.
18545-
18546-
\LMHash{}%
18547-
It is a \Error{compile-time error} if the type of the expression $b$
18548-
may not be assigned to \code{bool}.
18549-
18550-
\LMHash{}%
18551-
If $b$ shows that a local variable $v$ has type $T$,
18552-
then the type of $v$ is known to be $T$ in $s_1$,
18553-
unless any of the following are true
18554-
\begin{itemize}
18555-
\item $v$ is potentially mutated in $s_1$,
18556-
\item $v$ is potentially mutated within a function other
18557-
than the one where $v$ is declared, or
18558-
\item $v$ is accessed by a function defined in $s_1$ and
18559-
$v$ is potentially mutated anywhere in the scope of $v$.
18560-
\end{itemize}
18561-
18562-
\LMHash{}%
18563-
An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18564-
the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18509+
If $o$ is \TRUE, then execute the block statement $S_1$,
18510+
otherwise execute the block statement $S_2$.
1856518511

1856618512

1856718513
\subsection{For}
@@ -21433,34 +21379,6 @@ \subsection{Static Types}
2143321379
% objects in constructor args - these cannot represent parameterized types.
2143421380

2143521381

21436-
\subsubsection{Local Variable Type Promotion}
21437-
\LMLabel{typePromotion}
21438-
21439-
\LMHash{}%
21440-
The static type system ascribes a static type to every expression.
21441-
In some cases, the type of a local variable
21442-
(\commentary{which can be a formal parameter})
21443-
may be promoted from the declared type, based on control flow.
21444-
21445-
\LMHash{}%
21446-
We say that a variable $v$ is known to have type $T$
21447-
whenever we allow the type of $v$ to be promoted.
21448-
The exact circumstances when type promotion is allowed are given in
21449-
the relevant sections of the specification
21450-
(\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21451-
21452-
\LMHash{}%
21453-
Type promotion for a variable $v$ is allowed only when we can deduce that
21454-
such promotion is valid based on an analysis of certain boolean expressions.
21455-
In such cases, we say that
21456-
the boolean expression $b$ shows that $v$ has type $T$.
21457-
As a rule, for all variables $v$ and types $T$, a boolean expression
21458-
does not show that $v$ has type $T$.
21459-
Those situations where an expression does show that a variable has a type are
21460-
mentioned explicitly in the relevant sections of this specification
21461-
(\ref{typeTest} and \ref{logicalBooleanExpressions}).
21462-
21463-
2146421382
\subsection{Dynamic Type System}
2146521383
\LMLabel{dynamicTypeSystem}
2146621384

0 commit comments

Comments
 (0)