Skip to content

Commit 8cc1788

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 2894d27 commit 8cc1788

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
@@ -24753,15 +24753,6 @@ \subsection{Type Promotion}
2475324753
then $T$ is also a type of interest.%
2475424754
}
2475524755

24756-
\LMHash{}%
24757-
The initial stack of types of interest for $v$ has exactly one element,
24758-
namely the declared type.
24759-
This is the stack of types of interest
24760-
for the declaring occurrence of the name of $v$
24761-
(\commentary{%
24762-
that is, the very first time the variable is mentioned, \ref{variables}%
24763-
}).
24764-
2476524756
\commentary{%
2476624757
The flow analysis will be specified in
2476724758
a future version of this specification.
@@ -24771,9 +24762,21 @@ \subsection{Type Promotion}
2477124762
based on the same properties of locations
2477224763
that are immediate predecessors of $\ell$
2477324764
in the control flow graph,
24774-
ending in the declaration of $v$.%
24765+
ending in the declaration of $v$.
24766+
At this point we just specify the initial step,
24767+
and the situations where promotion may occur.%
2477524768
}
2477624769

24770+
\LMHash{}%
24771+
The initial stack of types of interest for $v$ has exactly one element,
24772+
namely the declared type
24773+
(\ref{localVariableDeclaration}).
24774+
This is the stack of types of interest
24775+
for the declaring occurrence of the name of $v$
24776+
(\commentary{%
24777+
i.e., the very first time the variable is mentioned, \ref{variables}%
24778+
}).
24779+
2477724780
\LMHash{}%
2477824781
If a local variable $v$ has an initializing expression
2477924782
and does not have the modifier \FINAL,
@@ -24813,20 +24816,91 @@ \subsection{Type Promotion}
2481324816
Consider the situation
2481424817
where the declaration of a local variable $v$ is of the form
2481524818
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24816-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24819+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2481724820
where $X$ is a type variable.
2481824821
In this situation, the declared type of $v$ is \code{X}
2481924822
(\ref{localVariableDeclaration}),
24820-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24821-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24823+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24824+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2482224825

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

2483224906

@@ -24988,32 +25062,11 @@ \section{Null safety} %% !!!TODO!!!
2498825062
%% !!!At the end: Search Null, change to Never where appropriate
2498925063
%% !!!Search all `TODO`.*null
2499025064

24991-
\subsubsection{Null promotion}
24992-
\LMLabel{}
24993-
24994-
!!!
24995-
24996-
The machinery of type promotion is extended to promote the type of
24997-
variables based on nullability checks subject to the same set of
24998-
restrictions as normal promotion. The relevant checks and the types
24999-
they are considered to promote to are as follows.
25000-
25001-
A check of the form \code{$e$ == null} or of the form
25002-
\code{$e$\,\,\IS\,\,Null} where $e$ has static type $T$ promotes the
25003-
type of $e$ to \code{Null} in the \code{true} continuation, and to
25004-
\NonNullType{$T$} in the \code{false} continuation.
25005-
25006-
A check of the form \code{$e$ != null} or of the form
25007-
\code{$e$\,\,\IS\,\,$T$} where $e$ has static type
25008-
\code{$T$?}\ promotes the type of $e$ to $T$ in the \code{true}
25009-
continuation, and to \code{Null} in the \code{false} continuation.
25010-
25011-
The static type of an expression \code{$e$!} is \NonNullType{$T$}
25012-
where $T$ is the static type of $e$.
25013-
2501425065
\subsubsection{Null aware operator}
2501525066
\LMLabel{nullShorteningTransformation}
2501625067

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

0 commit comments

Comments
 (0)