Skip to content

Commit 51acf3d

Browse files
committed
Integrate Null Promotion; put v==null rules into Type Promotion, and fill in enough commentary to make that section make sense
1 parent a6eaef9 commit 51acf3d

File tree

1 file changed

+92
-39
lines changed

1 file changed

+92
-39
lines changed

specification/dartLangSpec.tex

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24570,15 +24570,6 @@ \subsection{Type Promotion}
2457024570
then $T$ is also a type of interest.%
2457124571
}
2457224572

24573-
\LMHash{}%
24574-
The initial stack of types of interest for $v$ has exactly one element,
24575-
namely the declared type.
24576-
This is the stack of types of interest
24577-
for the declaring occurrence of the name of $v$
24578-
(\commentary{%
24579-
that is, the very first time the variable is mentioned, \ref{variables}%
24580-
}).
24581-
2458224573
\commentary{%
2458324574
The flow analysis will be specified in
2458424575
a future version of this specification.
@@ -24588,9 +24579,21 @@ \subsection{Type Promotion}
2458824579
based on the same properties of locations
2458924580
that are immediate predecessors of $\ell$
2459024581
in the control flow graph,
24591-
ending in the declaration of $v$.%
24582+
ending in the declaration of $v$.
24583+
At this point we just specify the initial step,
24584+
and the situations where promotion may occur.%
2459224585
}
2459324586

24587+
\LMHash{}%
24588+
The initial stack of types of interest for $v$ has exactly one element,
24589+
namely the declared type
24590+
(\ref{localVariableDeclaration}).
24591+
This is the stack of types of interest
24592+
for the declaring occurrence of the name of $v$
24593+
(\commentary{%
24594+
i.e., the very first time the variable is mentioned, \ref{variables}%
24595+
}).
24596+
2459424597
\LMHash{}%
2459524598
If a local variable $v$ has an initializing expression
2459624599
and does not have the modifier \FINAL,
@@ -24630,20 +24633,91 @@ \subsection{Type Promotion}
2463024633
Consider the situation
2463124634
where the declaration of a local variable $v$ is of the form
2463224635
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24633-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24636+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2463424637
where $X$ is a type variable.
2463524638
In this situation, the declared type of $v$ is \code{X}
2463624639
(\ref{localVariableDeclaration}),
24637-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24638-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24640+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24641+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2463924642

2464024643
\commentary{%
24641-
Note that \code{$X$\,\&\,$T$} is not otherwise automatically a
24644+
Note that \code{$X$\,\&\,\,$T$} is not otherwise automatically a
2464224645
type of interest just because $X$ is a type of interest.
2464324646
In particular, if $w$ is declared as
2464424647
\code{\LATE?\,\,$X$\,\,$w$ = $e$;}
24645-
where the static type of $e$ is \code{$X$\,\&\,$T$},
24646-
the type of $w$ is \emph{not} immediately promoted to \code{$X$\,\&\,$T$}.%
24648+
where the static type of $e$ is \code{$X$\,\&\,\,$T$},
24649+
the type of $w$ is \emph{not} promoted to \code{$X$\,\&\,\,$T$}.%
24650+
}
24651+
24652+
\LMHash{}%
24653+
We now mention the locations where promotion may occur
24654+
independently of the control flow.
24655+
24656+
\commentary{%
24657+
We might say that these promotions are ``atomic'',
24658+
and all other promotions are derived from these,
24659+
and from the control flow.%
24660+
}
24661+
24662+
\LMHash{}%
24663+
Let $\ell$ be a location,
24664+
and let $v$ be a local variable which is in scope at $\ell$.
24665+
Assume that $\ell$ occurs after the declaration of $v$.
24666+
The expressions that give rise to promotion or demotion
24667+
of the type of $v$ at $\ell$
24668+
are the following:
24669+
24670+
\begin{itemize}
24671+
\item A type test of the form \code{$v$\,\,\IS\,\,$T$}.
24672+
\item A type check of the form \code{$v$\,\,\AS\,\,$T$}.
24673+
\item An assignment of the form \code{$v$\,\,=\,\,$e$}
24674+
where the static type of $e$ is a type of interest for $v$ at $\ell$,
24675+
and similarly for compound assignments
24676+
(\ref{compoundAssignment})
24677+
including \lit{??=}.
24678+
\item An equality test of the form \code{$v$\,\,==\,\,$e$} or
24679+
\code{$e$\,\,==\,\,$v$}, where $e$ is \NULL,
24680+
optionally wrapped in one or more parentheses,
24681+
and similarly for \lit{!=}.
24682+
\end{itemize}
24683+
24684+
\LMHash{}%
24685+
In particular,
24686+
a check of the form \code{$v$\,\,==\,\,\NULL},
24687+
\code{\NULL\,\,==\,\,$v$},
24688+
or \code{$v$\,\,\IS\,\,Null}
24689+
where $v$ has type $T$ at $\ell$
24690+
promotes the type of $v$
24691+
to \code{Null} in the \TRUE{} continuation,
24692+
and to \NonNullType{$T$} in the \FALSE{} continuation.
24693+
24694+
\LMHash{}%
24695+
%% TODO(eernst), for review: The null safety spec says that `T?` is
24696+
%% promoted to `T`, but implementations _do_ promote `X extends int?` to
24697+
%% `X & int`. So I specify that.
24698+
A check of the form \code{$v$\,\,!=\,\,\NULL},
24699+
\code{\NULL\,\,!=\,\,$v$},
24700+
or \code{$v$\,\,\IS\,\,$T$}
24701+
where $v$ has type $T$ at $\ell$
24702+
promotes the type of $v$
24703+
to \NonNullType{$T$} in the \TRUE{} continuation,
24704+
and to \code{Null} in the \FALSE{} continuation.
24705+
24706+
\commentary{%
24707+
The resulting type of $v$ may be the obvious one, e.g.,
24708+
\code{$v$\,\,=\,\,1} may promote $v$ to \code{int},
24709+
but it may also give rise to a demotion
24710+
(changing the type of $v$ to a supertype of the type of $v$ at $\ell$),
24711+
and it may have no effect on the type of $v$
24712+
(e.g., when the static type of $e$ is not a type of interest).
24713+
These details will be specified in a future version of this specification.
24714+
24715+
Every non-trivial type of control flow determines the control flow graph,
24716+
and the control flow graph determines which of the above
24717+
may or must have occurred at $\ell$,
24718+
and hence gives rise to the stack of types of interest of $v$ at $\ell$,
24719+
which in turn determines the type of $v$ at $\ell$.
24720+
This part of the flow analysis will also be specified in the future.%
2464724721
}
2464824722

2464924723

@@ -24805,32 +24879,11 @@ \section{Null safety} %% !!!TODO!!!
2480524879
%% !!!At the end: Search Null, change to Never where appropriate
2480624880
%% !!!Search all `TODO`.*null
2480724881

24808-
\subsubsection{Null promotion}
24809-
\LMLabel{}
24810-
24811-
!!!
24812-
24813-
The machinery of type promotion is extended to promote the type of
24814-
variables based on nullability checks subject to the same set of
24815-
restrictions as normal promotion. The relevant checks and the types
24816-
they are considered to promote to are as follows.
24817-
24818-
A check of the form \code{$e$ == null} or of the form
24819-
\code{$e$\,\,\IS\,\,Null} where $e$ has static type $T$ promotes the
24820-
type of $e$ to \code{Null} in the \code{true} continuation, and to
24821-
\NonNullType{$T$} in the \code{false} continuation.
24822-
24823-
A check of the form \code{$e$ != null} or of the form
24824-
\code{$e$\,\,\IS\,\,$T$} where $e$ has static type
24825-
\code{$T$?}\ promotes the type of $e$ to $T$ in the \code{true}
24826-
continuation, and to \code{Null} in the \code{false} continuation.
24827-
24828-
The static type of an expression \code{$e$!} is \NonNullType{$T$}
24829-
where $T$ is the static type of $e$.
24830-
2483124882
\subsubsection{Null aware operator}
2483224883
\LMLabel{nullShorteningTransformation}
2483324884

24885+
!!!
24886+
2483424887
The semantics of the null aware operator \code{?.} are defined via a source to source
2483524888
translation of expressions into Dart code extended with a let binding construct.
2483624889
The translation is defined using meta-level functions over syntax. We use the

0 commit comments

Comments
 (0)