Skip to content

Commit 9097f9f

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 1408243 commit 9097f9f

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
@@ -24305,15 +24305,6 @@ \subsection{Type Promotion}
2430524305
then $T$ is also a type of interest.%
2430624306
}
2430724307

24308-
\LMHash{}%
24309-
The initial stack of types of interest for $v$ has exactly one element,
24310-
namely the declared type.
24311-
This is the stack of types of interest
24312-
for the declaring occurrence of the name of $v$
24313-
(\commentary{%
24314-
that is, the very first time the variable is mentioned, \ref{variables}%
24315-
}).
24316-
2431724308
\commentary{%
2431824309
The flow analysis will be specified in
2431924310
a future version of this specification.
@@ -24323,9 +24314,21 @@ \subsection{Type Promotion}
2432324314
based on the same properties of locations
2432424315
that are immediate predecessors of $\ell$
2432524316
in the control flow graph,
24326-
ending in the declaration of $v$.%
24317+
ending in the declaration of $v$.
24318+
At this point we just specify the initial step,
24319+
and the situations where promotion may occur.%
2432724320
}
2432824321

24322+
\LMHash{}%
24323+
The initial stack of types of interest for $v$ has exactly one element,
24324+
namely the declared type
24325+
(\ref{localVariableDeclaration}).
24326+
This is the stack of types of interest
24327+
for the declaring occurrence of the name of $v$
24328+
(\commentary{%
24329+
i.e., the very first time the variable is mentioned, \ref{variables}%
24330+
}).
24331+
2432924332
\LMHash{}%
2433024333
If a local variable $v$ has an initializing expression
2433124334
and does not have the modifier \FINAL,
@@ -24365,20 +24368,91 @@ \subsection{Type Promotion}
2436524368
Consider the situation
2436624369
where the declaration of a local variable $v$ is of the form
2436724370
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24368-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24371+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2436924372
where $X$ is a type variable.
2437024373
In this situation, the declared type of $v$ is \code{X}
2437124374
(\ref{localVariableDeclaration}),
24372-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24373-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24375+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24376+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2437424377

2437524378
\commentary{%
24376-
Note that \code{$X$\,\&\,$T$} is not otherwise automatically a
24379+
Note that \code{$X$\,\&\,\,$T$} is not otherwise automatically a
2437724380
type of interest just because $X$ is a type of interest.
2437824381
In particular, if $w$ is declared as
2437924382
\code{\LATE?\,\,$X$\,\,$w$ = $e$;}
24380-
where the static type of $e$ is \code{$X$\,\&\,$T$},
24381-
the type of $w$ is \emph{not} immediately promoted to \code{$X$\,\&\,$T$}.%
24383+
where the static type of $e$ is \code{$X$\,\&\,\,$T$},
24384+
the type of $w$ is \emph{not} promoted to \code{$X$\,\&\,\,$T$}.%
24385+
}
24386+
24387+
\LMHash{}%
24388+
We now mention the locations where promotion may occur
24389+
independently of the control flow.
24390+
24391+
\commentary{%
24392+
We might say that these promotions are ``atomic'',
24393+
and all other promotions are derived from these,
24394+
and from the control flow.%
24395+
}
24396+
24397+
\LMHash{}%
24398+
Let $\ell$ be a location,
24399+
and let $v$ be a local variable which is in scope at $\ell$.
24400+
Assume that $\ell$ occurs after the declaration of $v$.
24401+
The expressions that give rise to promotion or demotion
24402+
of the type of $v$ at $\ell$
24403+
are the following:
24404+
24405+
\begin{itemize}
24406+
\item A type test of the form \code{$v$\,\,\IS\,\,$T$}.
24407+
\item A type check of the form \code{$v$\,\,\AS\,\,$T$}.
24408+
\item An assignment of the form \code{$v$\,\,=\,\,$e$}
24409+
where the static type of $e$ is a type of interest for $v$ at $\ell$,
24410+
and similarly for compound assignments
24411+
(\ref{compoundAssignment})
24412+
including \lit{??=}.
24413+
\item An equality test of the form \code{$v$\,\,==\,\,$e$} or
24414+
\code{$e$\,\,==\,\,$v$}, where $e$ is \NULL,
24415+
optionally wrapped in one or more parentheses,
24416+
and similarly for \lit{!=}.
24417+
\end{itemize}
24418+
24419+
\LMHash{}%
24420+
In particular,
24421+
a check of the form \code{$v$\,\,==\,\,\NULL},
24422+
\code{\NULL\,\,==\,\,$v$},
24423+
or \code{$v$\,\,\IS\,\,Null}
24424+
where $v$ has type $T$ at $\ell$
24425+
promotes the type of $v$
24426+
to \code{Null} in the \TRUE{} continuation,
24427+
and to \NonNullType{$T$} in the \FALSE{} continuation.
24428+
24429+
\LMHash{}%
24430+
%% TODO(eernst), for review: The null safety spec says that `T?` is
24431+
%% promoted to `T`, but implementations _do_ promote `X extends int?` to
24432+
%% `X & int`. So I specify that.
24433+
A check of the form \code{$v$\,\,!=\,\,\NULL},
24434+
\code{\NULL\,\,!=\,\,$v$},
24435+
or \code{$v$\,\,\IS\,\,$T$}
24436+
where $v$ has type $T$ at $\ell$
24437+
promotes the type of $v$
24438+
to \NonNullType{$T$} in the \TRUE{} continuation,
24439+
and to \code{Null} in the \FALSE{} continuation.
24440+
24441+
\commentary{%
24442+
The resulting type of $v$ may be the obvious one, e.g.,
24443+
\code{$v$\,\,=\,\,1} may promote $v$ to \code{int},
24444+
but it may also give rise to a demotion
24445+
(changing the type of $v$ to a supertype of the type of $v$ at $\ell$),
24446+
and it may have no effect on the type of $v$
24447+
(e.g., when the static type of $e$ is not a type of interest).
24448+
These details will be specified in a future version of this specification.
24449+
24450+
Every non-trivial type of control flow determines the control flow graph,
24451+
and the control flow graph determines which of the above
24452+
may or must have occurred at $\ell$,
24453+
and hence gives rise to the stack of types of interest of $v$ at $\ell$,
24454+
which in turn determines the type of $v$ at $\ell$.
24455+
This part of the flow analysis will also be specified in the future.%
2438224456
}
2438324457

2438424458

@@ -24539,32 +24613,11 @@ \section{Null safety} %% !!!TODO!!!
2453924613
%% !!!At the end: Search Null, change to Never where appropriate
2454024614
%% !!!Search all `TODO`.*null
2454124615

24542-
\subsubsection{Null promotion}
24543-
\LMLabel{}
24544-
24545-
!!!
24546-
24547-
The machinery of type promotion is extended to promote the type of
24548-
variables based on nullability checks subject to the same set of
24549-
restrictions as normal promotion. The relevant checks and the types
24550-
they are considered to promote to are as follows.
24551-
24552-
A check of the form \code{$e$ == null} or of the form
24553-
\code{$e$\,\,\IS\,\,Null} where $e$ has static type $T$ promotes the
24554-
type of $e$ to \code{Null} in the \code{true} continuation, and to
24555-
\NonNullType{$T$} in the \code{false} continuation.
24556-
24557-
A check of the form \code{$e$ != null} or of the form
24558-
\code{$e$\,\,\IS\,\,$T$} where $e$ has static type
24559-
\code{$T$?}\ promotes the type of $e$ to $T$ in the \code{true}
24560-
continuation, and to \code{Null} in the \code{false} continuation.
24561-
24562-
The static type of an expression \code{$e$!} is \NonNullType{$T$}
24563-
where $T$ is the static type of $e$.
24564-
2456524616
\subsubsection{Null aware operator}
2456624617
\LMLabel{nullShorteningTransformation}
2456724618

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

0 commit comments

Comments
 (0)