Skip to content

Commit 5ce95b1

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 8062ab0 commit 5ce95b1

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
@@ -24732,15 +24732,6 @@ \subsection{Type Promotion}
2473224732
then $T$ is also a type of interest.%
2473324733
}
2473424734

24735-
\LMHash{}%
24736-
The initial stack of types of interest for $v$ has exactly one element,
24737-
namely the declared type.
24738-
This is the stack of types of interest
24739-
for the declaring occurrence of the name of $v$
24740-
(\commentary{%
24741-
that is, the very first time the variable is mentioned, \ref{variables}%
24742-
}).
24743-
2474424735
\commentary{%
2474524736
The flow analysis will be specified in
2474624737
a future version of this specification.
@@ -24750,9 +24741,21 @@ \subsection{Type Promotion}
2475024741
based on the same properties of locations
2475124742
that are immediate predecessors of $\ell$
2475224743
in the control flow graph,
24753-
ending in the declaration of $v$.%
24744+
ending in the declaration of $v$.
24745+
At this point we just specify the initial step,
24746+
and the situations where promotion may occur.%
2475424747
}
2475524748

24749+
\LMHash{}%
24750+
The initial stack of types of interest for $v$ has exactly one element,
24751+
namely the declared type
24752+
(\ref{localVariableDeclaration}).
24753+
This is the stack of types of interest
24754+
for the declaring occurrence of the name of $v$
24755+
(\commentary{%
24756+
i.e., the very first time the variable is mentioned, \ref{variables}%
24757+
}).
24758+
2475624759
\LMHash{}%
2475724760
If a local variable $v$ has an initializing expression
2475824761
and does not have the modifier \FINAL,
@@ -24792,20 +24795,91 @@ \subsection{Type Promotion}
2479224795
Consider the situation
2479324796
where the declaration of a local variable $v$ is of the form
2479424797
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24795-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24798+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2479624799
where $X$ is a type variable.
2479724800
In this situation, the declared type of $v$ is \code{X}
2479824801
(\ref{localVariableDeclaration}),
24799-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24800-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24802+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24803+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2480124804

2480224805
\commentary{%
24803-
Note that \code{$X$\,\&\,$T$} is not otherwise automatically a
24806+
Note that \code{$X$\,\&\,\,$T$} is not otherwise automatically a
2480424807
type of interest just because $X$ is a type of interest.
2480524808
In particular, if $w$ is declared as
2480624809
\code{\LATE?\,\,$X$\,\,$w$ = $e$;}
24807-
where the static type of $e$ is \code{$X$\,\&\,$T$},
24808-
the type of $w$ is \emph{not} immediately promoted to \code{$X$\,\&\,$T$}.%
24810+
where the static type of $e$ is \code{$X$\,\&\,\,$T$},
24811+
the type of $w$ is \emph{not} promoted to \code{$X$\,\&\,\,$T$}.%
24812+
}
24813+
24814+
\LMHash{}%
24815+
We now mention the locations where promotion may occur
24816+
independently of the control flow.
24817+
24818+
\commentary{%
24819+
We might say that these promotions are ``atomic'',
24820+
and all other promotions are derived from these,
24821+
and from the control flow.%
24822+
}
24823+
24824+
\LMHash{}%
24825+
Let $\ell$ be a location,
24826+
and let $v$ be a local variable which is in scope at $\ell$.
24827+
Assume that $\ell$ occurs after the declaration of $v$.
24828+
The expressions that give rise to promotion or demotion
24829+
of the type of $v$ at $\ell$
24830+
are the following:
24831+
24832+
\begin{itemize}
24833+
\item A type test of the form \code{$v$\,\,\IS\,\,$T$}.
24834+
\item A type check of the form \code{$v$\,\,\AS\,\,$T$}.
24835+
\item An assignment of the form \code{$v$\,\,=\,\,$e$}
24836+
where the static type of $e$ is a type of interest for $v$ at $\ell$,
24837+
and similarly for compound assignments
24838+
(\ref{compoundAssignment})
24839+
including \lit{??=}.
24840+
\item An equality test of the form \code{$v$\,\,==\,\,$e$} or
24841+
\code{$e$\,\,==\,\,$v$}, where $e$ is \NULL,
24842+
optionally wrapped in one or more parentheses,
24843+
and similarly for \lit{!=}.
24844+
\end{itemize}
24845+
24846+
\LMHash{}%
24847+
In particular,
24848+
a check of the form \code{$v$\,\,==\,\,\NULL},
24849+
\code{\NULL\,\,==\,\,$v$},
24850+
or \code{$v$\,\,\IS\,\,Null}
24851+
where $v$ has type $T$ at $\ell$
24852+
promotes the type of $v$
24853+
to \code{Null} in the \TRUE{} continuation,
24854+
and to \NonNullType{$T$} in the \FALSE{} continuation.
24855+
24856+
\LMHash{}%
24857+
%% TODO(eernst), for review: The null safety spec says that `T?` is
24858+
%% promoted to `T`, but implementations _do_ promote `X extends int?` to
24859+
%% `X & int`. So I specify that.
24860+
A check of the form \code{$v$\,\,!=\,\,\NULL},
24861+
\code{\NULL\,\,!=\,\,$v$},
24862+
or \code{$v$\,\,\IS\,\,$T$}
24863+
where $v$ has type $T$ at $\ell$
24864+
promotes the type of $v$
24865+
to \NonNullType{$T$} in the \TRUE{} continuation,
24866+
and to \code{Null} in the \FALSE{} continuation.
24867+
24868+
\commentary{%
24869+
The resulting type of $v$ may be the obvious one, e.g.,
24870+
\code{$v$\,\,=\,\,1} may promote $v$ to \code{int},
24871+
but it may also give rise to a demotion
24872+
(changing the type of $v$ to a supertype of the type of $v$ at $\ell$),
24873+
and it may have no effect on the type of $v$
24874+
(e.g., when the static type of $e$ is not a type of interest).
24875+
These details will be specified in a future version of this specification.
24876+
24877+
Every non-trivial type of control flow determines the control flow graph,
24878+
and the control flow graph determines which of the above
24879+
may or must have occurred at $\ell$,
24880+
and hence gives rise to the stack of types of interest of $v$ at $\ell$,
24881+
which in turn determines the type of $v$ at $\ell$.
24882+
This part of the flow analysis will also be specified in the future.%
2480924883
}
2481024884

2481124885

@@ -24967,32 +25041,11 @@ \section{Null safety} %% !!!TODO!!!
2496725041
%% !!!At the end: Search Null, change to Never where appropriate
2496825042
%% !!!Search all `TODO`.*null
2496925043

24970-
\subsubsection{Null promotion}
24971-
\LMLabel{}
24972-
24973-
!!!
24974-
24975-
The machinery of type promotion is extended to promote the type of
24976-
variables based on nullability checks subject to the same set of
24977-
restrictions as normal promotion. The relevant checks and the types
24978-
they are considered to promote to are as follows.
24979-
24980-
A check of the form \code{$e$ == null} or of the form
24981-
\code{$e$\,\,\IS\,\,Null} where $e$ has static type $T$ promotes the
24982-
type of $e$ to \code{Null} in the \code{true} continuation, and to
24983-
\NonNullType{$T$} in the \code{false} continuation.
24984-
24985-
A check of the form \code{$e$ != null} or of the form
24986-
\code{$e$\,\,\IS\,\,$T$} where $e$ has static type
24987-
\code{$T$?}\ promotes the type of $e$ to $T$ in the \code{true}
24988-
continuation, and to \code{Null} in the \code{false} continuation.
24989-
24990-
The static type of an expression \code{$e$!} is \NonNullType{$T$}
24991-
where $T$ is the static type of $e$.
24992-
2499325044
\subsubsection{Null aware operator}
2499425045
\LMLabel{nullShorteningTransformation}
2499525046

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

0 commit comments

Comments
 (0)