Skip to content

Commit 1470431

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

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
@@ -16254,7 +16254,7 @@ \subsection{Conditional}
1625416254
\LMLabel{conditional}
1625516255

1625616256
\LMHash{}%
16257-
A \Index{conditional expression} evaluates one of two expressions
16257+
A \Index{conditional expression} evaluates one of two expressions,
1625816258
based on a boolean condition.
1625916259

1626016260
\begin{grammar}
@@ -16263,34 +16263,23 @@ \subsection{Conditional}
1626316263
\end{grammar}
1626416264

1626516265
\LMHash{}%
16266-
Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16267-
proceeds as follows:
16266+
Consider a conditional expression $c$ of the form
16267+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16268+
It is a \Error{compile-time error} if
16269+
the static type of $e_1$ may not be assigned to \code{bool}.
16270+
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16271+
the static type of $e_2$ and the static type of $e_3$.
1626816272

1626916273
\LMHash{}%
16270-
First, $e_1$ is evaluated to an object $o_1$.
16274+
Evaluation of a conditional expression $c$ of the form
16275+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16276+
proceeds as follows:
16277+
Evaluate $e_1$ to an object $o_1$.
1627116278
% This error can occur due to an implicit cast from \DYNAMIC.
1627216279
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16273-
If $r$ is \TRUE, then the value of $c$ is
16280+
If $o_1$ is the \TRUE{} object, then the value of $c$ is
1627416281
the result of evaluating the expression $e_2$.
16275-
Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16276-
16277-
\LMHash{}%
16278-
If $e_1$ shows that a local variable $v$ has type $T$,
16279-
then the type of $v$ is known to be $T$ in $e_2$,
16280-
unless any of the following are true:
16281-
\begin{itemize}
16282-
\item $v$ is potentially mutated in $e_2$,
16283-
\item $v$ is potentially mutated within a function other
16284-
than the one where $v$ is declared, or
16285-
\item $v$ is accessed by a function defined in $e_2$ and
16286-
$v$ is potentially mutated anywhere in the scope of $v$.
16287-
\end{itemize}
16288-
16289-
\LMHash{}%
16290-
It is a \Error{compile-time error} if
16291-
the static type of $e_1$ may not be assigned to \code{bool}.
16292-
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16293-
the static type of $e_2$ and the static type of $e_3$.
16282+
Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
1629416283

1629516284

1629616285
\subsection{If-null Expressions}
@@ -16345,8 +16334,15 @@ \subsection{Logical Boolean Expressions}
1634516334
an expression $e_1$ with argument $e_2$.
1634616335

1634716336
\LMHash{}%
16348-
Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16349-
the evaluation of $e_1$ to an object $o_1$.
16337+
It is a \Error{compile-time error} if
16338+
the static type of $e_1$ may not be assigned to \code{bool}
16339+
or if the static type of $e_2$ may not be assigned to \code{bool}.
16340+
The static type of a logical boolean expression is \code{bool}.
16341+
16342+
\LMHash{}%
16343+
Evaluation of a logical boolean expression $b$ of the form
16344+
\code{$e_1$\,\,||\,\,$e_2$}
16345+
causes the evaluation of $e_1$ to an object $o_1$.
1635016346
% This error can occur due to an implicit downcast from \DYNAMIC.
1635116347
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1635216348
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16356,8 +16352,9 @@ \subsection{Logical Boolean Expressions}
1635616352
Otherwise the result of evaluating $b$ is $o_2$.
1635716353

1635816354
\LMHash{}%
16359-
Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16360-
causes the evaluation of $e_1$ producing an object $o_1$.
16355+
Evaluation of a logical boolean expression $b$ of the form
16356+
\code{$e_1$\,\,\&\&\,\,$e_2$}
16357+
causes the evaluation of $e_1$ to an object $o_1$.
1636116358
% This error can occur due to an implicit downcast from \DYNAMIC.
1636216359
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1636316360
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16366,45 +16363,6 @@ \subsection{Logical Boolean Expressions}
1636616363
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1636716364
Otherwise the result of evaluating $b$ is $o_2$.
1636816365

16369-
\LMHash{}%
16370-
A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16371-
shows that a local variable $v$ has type $T$
16372-
if both of the following conditions hold:
16373-
\begin{itemize}
16374-
\item Either $e_1$ shows that $v$ has type $T$
16375-
or $e_2$ shows that $v$ has type $T$.
16376-
\item $v$ is not mutated in $e_2$ or within a function
16377-
other than the one where $v$ is declared.
16378-
\end{itemize}
16379-
16380-
\LMHash{}%
16381-
If $e_1$ shows that a local variable $v$ has type $T$,
16382-
then the type of $v$ is known to be $T$ in $e_2$,
16383-
unless any of the following are true:
16384-
\begin{itemize}
16385-
%% The first item here is unnecessary for soundness,
16386-
%% and is retained mainly for backwards compatibility.
16387-
%% If $e_1$ shows that $v$ has type $T$, then any assignment
16388-
%% in $e_1$ does not invalidate that.
16389-
%% Removing the line is visible in the semantics, though, because:
16390-
%% num x;
16391-
%% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16392-
%% is allowed without the line, and disallowed with.
16393-
%% At time of writing, the analyzer disallows the code.
16394-
\item $v$ is potentially mutated in $e_1$,
16395-
\item $v$ is potentially mutated in $e_2$,
16396-
\item $v$ is potentially mutated within a function other
16397-
than the one where $v$ is declared, or
16398-
\item $v$ is accessed by a function defined in $e_2$ and
16399-
$v$ is potentially mutated anywhere in the scope of $v$.
16400-
\end{itemize}
16401-
16402-
\LMHash{}%
16403-
It is a \Error{compile-time error} if
16404-
the static type of $e_1$ may not be assigned to \code{bool}
16405-
or if the static type of $e_2$ may not be assigned to \code{bool}.
16406-
The static type of a logical boolean expression is \code{bool}.
16407-
1640816366

1640916367
\subsection{Equality}
1641016368
\LMLabel{equality}
@@ -18360,37 +18318,41 @@ \subsection{If}
1836018318
\LMLabel{if}
1836118319

1836218320
\LMHash{}%
18363-
The \Index{if statement} allows for conditional execution of statements.
18321+
The \Index{\IF{} statement} allows for conditional execution of statements.
1836418322

1836518323
\begin{grammar}
1836618324
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
1836718325
\end{grammar}
1836818326

1836918327
\LMHash{}%
18370-
An if statement of the form
18371-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18372-
where $s_1$ is not a block statement is equivalent to the statement
18373-
\code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18374-
An if statement of the form
18375-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18376-
where $s_2$ is not a block statement is equivalent to the statement
18377-
\code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18328+
An \IF{} statement of the form
18329+
\code{\IF\,\,($e$)\,\,$S$}
18330+
is treated as
18331+
(\ref{overview})
18332+
\code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18333+
An \IF{} statement of the form
18334+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18335+
where $S_1$ is not a block statement is treated as
18336+
\code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18337+
An \IF{} statement of the form
18338+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18339+
where $S_2$ is not a block statement is treated as
18340+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
1837818341

1837918342
\rationale{%
1838018343
The reason for this equivalence is to catch errors such as%
1838118344
}
1838218345

1838318346
\begin{dartCode}
1838418347
\VOID{} main() \{
18385-
\IF{} (somePredicate)
18386-
\VAR{} v = 2;
18348+
\IF{} (somePredicate) \VAR{} v = 2;
1838718349
print(v);
1838818350
\}
1838918351
\end{dartCode}
1839018352

1839118353
\rationale{%
1839218354
Under reasonable scope rules such code is problematic.
18393-
If we assume that \code{v} is declared
18355+
If we specify that \code{v} is declared
1839418356
in the scope of the method \code{main()},
1839518357
then when \code{somePredicate} evaluates to the \FALSE{} object,
1839618358
\code{v} will be uninitialized when accessed.
@@ -18409,37 +18371,21 @@ \subsection{If}
1840918371
}
1841018372

1841118373
\LMHash{}%
18412-
Execution of an if statement of the form
18413-
\code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18414-
where $s_1$ and $s_2$ are block statements,
18415-
proceeds as follows:
18374+
Consider an \IF{} statement of any of the forms mentioned above
18375+
(\commentary{the statement then starts with \code{\IF\,\,($e$)}}).
18376+
It is a \Error{compile-time error} if the type of the expression $e$
18377+
is not assignable to \code{bool}.
1841618378

1841718379
\LMHash{}%
18418-
First, the expression $b$ is evaluated to an object $o$.
18380+
Execution of an \IF{} statement of the form
18381+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18382+
where $S_1$ and $S_2$ are block statements
18383+
proceeds as follows:
18384+
Evaluate the expression $e$ to an object $o$.
1841918385
% This error can occur due to an implicit downcast from \DYNAMIC.
1842018386
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18421-
If $o$ is \TRUE, then the block statement $s_1$ is executed,
18422-
otherwise the block statement $s_2$ is executed.
18423-
18424-
\LMHash{}%
18425-
It is a \Error{compile-time error} if the type of the expression $b$
18426-
may not be assigned to \code{bool}.
18427-
18428-
\LMHash{}%
18429-
If $b$ shows that a local variable $v$ has type $T$,
18430-
then the type of $v$ is known to be $T$ in $s_1$,
18431-
unless any of the following are true
18432-
\begin{itemize}
18433-
\item $v$ is potentially mutated in $s_1$,
18434-
\item $v$ is potentially mutated within a function other
18435-
than the one where $v$ is declared, or
18436-
\item $v$ is accessed by a function defined in $s_1$ and
18437-
$v$ is potentially mutated anywhere in the scope of $v$.
18438-
\end{itemize}
18439-
18440-
\LMHash{}%
18441-
An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18442-
the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18387+
If $o$ is \TRUE, then execute the block statement $S_1$,
18388+
otherwise execute the block statement $S_2$.
1844318389

1844418390

1844518391
\subsection{For}
@@ -21295,34 +21241,6 @@ \subsection{Static Types}
2129521241
% objects in constructor args - these cannot represent parameterized types.
2129621242

2129721243

21298-
\subsubsection{Local Variable Type Promotion}
21299-
\LMLabel{typePromotion}
21300-
21301-
\LMHash{}%
21302-
The static type system ascribes a static type to every expression.
21303-
In some cases, the type of a local variable
21304-
(\commentary{which can be a formal parameter})
21305-
may be promoted from the declared type, based on control flow.
21306-
21307-
\LMHash{}%
21308-
We say that a variable $v$ is known to have type $T$
21309-
whenever we allow the type of $v$ to be promoted.
21310-
The exact circumstances when type promotion is allowed are given in
21311-
the relevant sections of the specification
21312-
(\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21313-
21314-
\LMHash{}%
21315-
Type promotion for a variable $v$ is allowed only when we can deduce that
21316-
such promotion is valid based on an analysis of certain boolean expressions.
21317-
In such cases, we say that
21318-
the boolean expression $b$ shows that $v$ has type $T$.
21319-
As a rule, for all variables $v$ and types $T$, a boolean expression
21320-
does not show that $v$ has type $T$.
21321-
Those situations where an expression does show that a variable has a type are
21322-
mentioned explicitly in the relevant sections of this specification
21323-
(\ref{typeTest} and \ref{logicalBooleanExpressions}).
21324-
21325-
2132621244
\subsection{Dynamic Type System}
2132721245
\LMLabel{dynamicTypeSystem}
2132821246

0 commit comments

Comments
 (0)