Skip to content

Commit 7b6b200

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 37854f7 commit 7b6b200

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

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

24320+
\LMHash{}%
24321+
The initial stack of types of interest for $v$ has exactly one element,
24322+
namely the declared type
24323+
(\ref{localVariableDeclaration}).
24324+
This is the stack of types of interest
24325+
for the declaring occurrence of the name of $v$
24326+
(\commentary{%
24327+
i.e., the very first time the variable is mentioned, \ref{variables}%
24328+
}).
24329+
2432724330
\LMHash{}%
2432824331
If a local variable $v$ has an initializing expression
2432924332
and does not have the modifier \FINAL,
@@ -24363,20 +24366,91 @@ \subsection{Type Promotion}
2436324366
Consider the situation
2436424367
where the declaration of a local variable $v$ is of the form
2436524368
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24366-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24369+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2436724370
where $X$ is a type variable.
2436824371
In this situation, the declared type of $v$ is \code{X}
2436924372
(\ref{localVariableDeclaration}),
24370-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24371-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24373+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24374+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2437224375

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

2438224456

@@ -24537,32 +24611,11 @@ \section{Null safety} %% !!!TODO!!!
2453724611
%% !!!At the end: Search Null, change to Never where appropriate
2453824612
%% !!!Search all `TODO`.*null
2453924613

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

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

0 commit comments

Comments
 (0)