Skip to content

Commit 8062ab0

Browse files
committed
Conditional expression; operators && and ||; if statements; delete old section about promotion
1 parent 58fd148 commit 8062ab0

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
@@ -16485,7 +16485,7 @@ \subsection{Conditional}
1648516485
\LMLabel{conditional}
1648616486

1648716487
\LMHash{}%
16488-
A \Index{conditional expression} evaluates one of two expressions
16488+
A \Index{conditional expression} evaluates one of two expressions,
1648916489
based on a boolean condition.
1649016490

1649116491
\begin{grammar}
@@ -16494,34 +16494,23 @@ \subsection{Conditional}
1649416494
\end{grammar}
1649516495

1649616496
\LMHash{}%
16497-
Evaluation of a conditional expression $c$ of the form $e_1 ? e_2 : e_3$
16498-
proceeds as follows:
16497+
Consider a conditional expression $c$ of the form
16498+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}.
16499+
It is a \Error{compile-time error} if
16500+
the static type of $e_1$ may not be assigned to \code{bool}.
16501+
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16502+
the static type of $e_2$ and the static type of $e_3$.
1649916503

1650016504
\LMHash{}%
16501-
First, $e_1$ is evaluated to an object $o_1$.
16505+
Evaluation of a conditional expression $c$ of the form
16506+
\code{$e_1$\,\,?\,\,$e_2$\,\,:\,\,$e_3$}
16507+
proceeds as follows:
16508+
Evaluate $e_1$ to an object $o_1$.
1650216509
% This error can occur due to an implicit cast from \DYNAMIC.
1650316510
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
16504-
If $r$ is \TRUE, then the value of $c$ is
16511+
If $o_1$ is the \TRUE{} object, then the value of $c$ is
1650516512
the result of evaluating the expression $e_2$.
16506-
Otherwise the value of $c$ is the result of evaluating the expression $e_3$.
16507-
16508-
\LMHash{}%
16509-
If $e_1$ shows that a local variable $v$ has type $T$,
16510-
then the type of $v$ is known to be $T$ in $e_2$,
16511-
unless any of the following are true:
16512-
\begin{itemize}
16513-
\item $v$ is potentially mutated in $e_2$,
16514-
\item $v$ is potentially mutated within a function other
16515-
than the one where $v$ is declared, or
16516-
\item $v$ is accessed by a function defined in $e_2$ and
16517-
$v$ is potentially mutated anywhere in the scope of $v$.
16518-
\end{itemize}
16519-
16520-
\LMHash{}%
16521-
It is a \Error{compile-time error} if
16522-
the static type of $e_1$ may not be assigned to \code{bool}.
16523-
The static type of $c$ is the least upper bound (\ref{leastUpperBounds}) of
16524-
the static type of $e_2$ and the static type of $e_3$.
16513+
Otherwise, the value of $c$ is the result of evaluating the expression $e_3$.
1652516514

1652616515

1652716516
\subsection{If-null Expressions}
@@ -16576,8 +16565,15 @@ \subsection{Logical Boolean Expressions}
1657616565
an expression $e_1$ with argument $e_2$.
1657716566

1657816567
\LMHash{}%
16579-
Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes
16580-
the evaluation of $e_1$ to an object $o_1$.
16568+
It is a \Error{compile-time error} if
16569+
the static type of $e_1$ may not be assigned to \code{bool}
16570+
or if the static type of $e_2$ may not be assigned to \code{bool}.
16571+
The static type of a logical boolean expression is \code{bool}.
16572+
16573+
\LMHash{}%
16574+
Evaluation of a logical boolean expression $b$ of the form
16575+
\code{$e_1$\,\,||\,\,$e_2$}
16576+
causes the evaluation of $e_1$ to an object $o_1$.
1658116577
% This error can occur due to an implicit downcast from \DYNAMIC.
1658216578
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1658316579
If $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE,
@@ -16587,8 +16583,9 @@ \subsection{Logical Boolean Expressions}
1658716583
Otherwise the result of evaluating $b$ is $o_2$.
1658816584

1658916585
\LMHash{}%
16590-
Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$
16591-
causes the evaluation of $e_1$ producing an object $o_1$.
16586+
Evaluation of a logical boolean expression $b$ of the form
16587+
\code{$e_1$\,\,\&\&\,\,$e_2$}
16588+
causes the evaluation of $e_1$ to an object $o_1$.
1659216589
% This error can occur due to an implicit downcast from \DYNAMIC.
1659316590
It is a dynamic error if the run-time type of $o_1$ is not \code{bool}.
1659416591
If $o_1$ is \FALSE, the result of evaluating $b$ is \FALSE,
@@ -16597,45 +16594,6 @@ \subsection{Logical Boolean Expressions}
1659716594
It is a dynamic error if the run-time type of $o_2$ is not \code{bool}.
1659816595
Otherwise the result of evaluating $b$ is $o_2$.
1659916596

16600-
\LMHash{}%
16601-
A logical boolean expression $b$ of the form $e_1 \&\& e_2$
16602-
shows that a local variable $v$ has type $T$
16603-
if both of the following conditions hold:
16604-
\begin{itemize}
16605-
\item Either $e_1$ shows that $v$ has type $T$
16606-
or $e_2$ shows that $v$ has type $T$.
16607-
\item $v$ is not mutated in $e_2$ or within a function
16608-
other than the one where $v$ is declared.
16609-
\end{itemize}
16610-
16611-
\LMHash{}%
16612-
If $e_1$ shows that a local variable $v$ has type $T$,
16613-
then the type of $v$ is known to be $T$ in $e_2$,
16614-
unless any of the following are true:
16615-
\begin{itemize}
16616-
%% The first item here is unnecessary for soundness,
16617-
%% and is retained mainly for backwards compatibility.
16618-
%% If $e_1$ shows that $v$ has type $T$, then any assignment
16619-
%% in $e_1$ does not invalidate that.
16620-
%% Removing the line is visible in the semantics, though, because:
16621-
%% num x;
16622-
%% (x ??= 42) != null && x is int & x.toRadixString(16) != ""
16623-
%% is allowed without the line, and disallowed with.
16624-
%% At time of writing, the analyzer disallows the code.
16625-
\item $v$ is potentially mutated in $e_1$,
16626-
\item $v$ is potentially mutated in $e_2$,
16627-
\item $v$ is potentially mutated within a function other
16628-
than the one where $v$ is declared, or
16629-
\item $v$ is accessed by a function defined in $e_2$ and
16630-
$v$ is potentially mutated anywhere in the scope of $v$.
16631-
\end{itemize}
16632-
16633-
\LMHash{}%
16634-
It is a \Error{compile-time error} if
16635-
the static type of $e_1$ may not be assigned to \code{bool}
16636-
or if the static type of $e_2$ may not be assigned to \code{bool}.
16637-
The static type of a logical boolean expression is \code{bool}.
16638-
1663916597

1664016598
\subsection{Equality}
1664116599
\LMLabel{equality}
@@ -18639,37 +18597,41 @@ \subsection{If}
1863918597
\LMLabel{if}
1864018598

1864118599
\LMHash{}%
18642-
The \Index{if statement} allows for conditional execution of statements.
18600+
The \Index{\IF{} statement} allows for conditional execution of statements.
1864318601

1864418602
\begin{grammar}
1864518603
<ifStatement> ::= \IF{} `(' <expression> `)' <statement> (\ELSE{} <statement>)?
1864618604
\end{grammar}
1864718605

1864818606
\LMHash{}%
18649-
An if statement of the form
18650-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18651-
where $s_1$ is not a block statement is equivalent to the statement
18652-
\code{\IF{} ($e$) \{$s_1$\} \ELSE{} $s_2$}.
18653-
An if statement of the form
18654-
\code{\IF{} ($e$) $s_1$ \ELSE{} $s_2$}
18655-
where $s_2$ is not a block statement is equivalent to the statement
18656-
\code{\IF{} ($e$) $s_1$ \ELSE{} \{$s_2$\}}.
18607+
An \IF{} statement of the form
18608+
\code{\IF\,\,($e$)\,\,$S$}
18609+
is treated as
18610+
(\ref{overview})
18611+
\code{\IF\,\,($e$)\,\,$S$\,\,\ELSE\,\,\{\}}.
18612+
An \IF{} statement of the form
18613+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18614+
where $S_1$ is not a block statement is treated as
18615+
\code{\IF\,\,($e$)\,\,\{\,$S_1$\,\}\,\,\ELSE\,\,$S_2$}.
18616+
An \IF{} statement of the form
18617+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18618+
where $S_2$ is not a block statement is treated as
18619+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,\{\,$S_2$\,\}}.
1865718620

1865818621
\rationale{%
1865918622
The reason for this equivalence is to catch errors such as%
1866018623
}
1866118624

1866218625
\begin{dartCode}
1866318626
\VOID{} main() \{
18664-
\IF{} (somePredicate)
18665-
\VAR{} v = 2;
18627+
\IF{} (somePredicate) \VAR{} v = 2;
1866618628
print(v);
1866718629
\}
1866818630
\end{dartCode}
1866918631

1867018632
\rationale{%
1867118633
Under reasonable scope rules such code is problematic.
18672-
If we assume that \code{v} is declared
18634+
If we specify that \code{v} is declared
1867318635
in the scope of the method \code{main()},
1867418636
then when \code{somePredicate} evaluates to the \FALSE{} object,
1867518637
\code{v} will be uninitialized when accessed.
@@ -18688,37 +18650,21 @@ \subsection{If}
1868818650
}
1868918651

1869018652
\LMHash{}%
18691-
Execution of an if statement of the form
18692-
\code{\IF{} ($b$) $s_1$ \ELSE{} $s_2$}
18693-
where $s_1$ and $s_2$ are block statements,
18694-
proceeds as follows:
18653+
Consider an \IF{} statement of any of the forms mentioned above
18654+
(\commentary{the statement then starts with \code{\IF\,\,($e$)}}).
18655+
It is a \Error{compile-time error} if the type of the expression $e$
18656+
is not assignable to \code{bool}.
1869518657

1869618658
\LMHash{}%
18697-
First, the expression $b$ is evaluated to an object $o$.
18659+
Execution of an \IF{} statement of the form
18660+
\code{\IF\,\,($e$)\,\,$S_1$\,\,\ELSE\,\,$S_2$}
18661+
where $S_1$ and $S_2$ are block statements
18662+
proceeds as follows:
18663+
Evaluate the expression $e$ to an object $o$.
1869818664
% This error can occur due to an implicit downcast from \DYNAMIC.
1869918665
It is a dynamic error if the run-time type of $o$ is not \code{bool}.
18700-
If $o$ is \TRUE, then the block statement $s_1$ is executed,
18701-
otherwise the block statement $s_2$ is executed.
18702-
18703-
\LMHash{}%
18704-
It is a \Error{compile-time error} if the type of the expression $b$
18705-
may not be assigned to \code{bool}.
18706-
18707-
\LMHash{}%
18708-
If $b$ shows that a local variable $v$ has type $T$,
18709-
then the type of $v$ is known to be $T$ in $s_1$,
18710-
unless any of the following are true
18711-
\begin{itemize}
18712-
\item $v$ is potentially mutated in $s_1$,
18713-
\item $v$ is potentially mutated within a function other
18714-
than the one where $v$ is declared, or
18715-
\item $v$ is accessed by a function defined in $s_1$ and
18716-
$v$ is potentially mutated anywhere in the scope of $v$.
18717-
\end{itemize}
18718-
18719-
\LMHash{}%
18720-
An if statement of the form \code{\IF{} ($e$) $s$} is equivalent to
18721-
the if statement \code{\IF{} ($e$) $s$ \ELSE{} \{\}}.
18666+
If $o$ is \TRUE, then execute the block statement $S_1$,
18667+
otherwise execute the block statement $S_2$.
1872218668

1872318669

1872418670
\subsection{For}
@@ -21593,34 +21539,6 @@ \subsection{Static Types}
2159321539
% objects in constructor args - these cannot represent parameterized types.
2159421540

2159521541

21596-
\subsubsection{Local Variable Type Promotion}
21597-
\LMLabel{typePromotion}
21598-
21599-
\LMHash{}%
21600-
The static type system ascribes a static type to every expression.
21601-
In some cases, the type of a local variable
21602-
(\commentary{which can be a formal parameter})
21603-
may be promoted from the declared type, based on control flow.
21604-
21605-
\LMHash{}%
21606-
We say that a variable $v$ is known to have type $T$
21607-
whenever we allow the type of $v$ to be promoted.
21608-
The exact circumstances when type promotion is allowed are given in
21609-
the relevant sections of the specification
21610-
(\ref{logicalBooleanExpressions}, \ref{conditional} and \ref{if}).
21611-
21612-
\LMHash{}%
21613-
Type promotion for a variable $v$ is allowed only when we can deduce that
21614-
such promotion is valid based on an analysis of certain boolean expressions.
21615-
In such cases, we say that
21616-
the boolean expression $b$ shows that $v$ has type $T$.
21617-
As a rule, for all variables $v$ and types $T$, a boolean expression
21618-
does not show that $v$ has type $T$.
21619-
Those situations where an expression does show that a variable has a type are
21620-
mentioned explicitly in the relevant sections of this specification
21621-
(\ref{typeTest} and \ref{logicalBooleanExpressions}).
21622-
21623-
2162421542
\subsection{Dynamic Type System}
2162521543
\LMLabel{dynamicTypeSystem}
2162621544

0 commit comments

Comments
 (0)