Skip to content

Commit 55dd0dd

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 1470431 commit 55dd0dd

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
@@ -24432,15 +24432,6 @@ \subsection{Type Promotion}
2443224432
then $T$ is also a type of interest.%
2443324433
}
2443424434

24435-
\LMHash{}%
24436-
The initial stack of types of interest for $v$ has exactly one element,
24437-
namely the declared type.
24438-
This is the stack of types of interest
24439-
for the declaring occurrence of the name of $v$
24440-
(\commentary{%
24441-
that is, the very first time the variable is mentioned, \ref{variables}%
24442-
}).
24443-
2444424435
\commentary{%
2444524436
The flow analysis will be specified in
2444624437
a future version of this specification.
@@ -24450,9 +24441,21 @@ \subsection{Type Promotion}
2445024441
based on the same properties of locations
2445124442
that are immediate predecessors of $\ell$
2445224443
in the control flow graph,
24453-
ending in the declaration of $v$.%
24444+
ending in the declaration of $v$.
24445+
At this point we just specify the initial step,
24446+
and the situations where promotion may occur.%
2445424447
}
2445524448

24449+
\LMHash{}%
24450+
The initial stack of types of interest for $v$ has exactly one element,
24451+
namely the declared type
24452+
(\ref{localVariableDeclaration}).
24453+
This is the stack of types of interest
24454+
for the declaring occurrence of the name of $v$
24455+
(\commentary{%
24456+
i.e., the very first time the variable is mentioned, \ref{variables}%
24457+
}).
24458+
2445624459
\LMHash{}%
2445724460
If a local variable $v$ has an initializing expression
2445824461
and does not have the modifier \FINAL,
@@ -24492,20 +24495,91 @@ \subsection{Type Promotion}
2449224495
Consider the situation
2449324496
where the declaration of a local variable $v$ is of the form
2449424497
\code{\LATE?\,\,\VAR\,\,$v$ = $e$;}
24495-
where the static type of $e$ is of the form \code{$X$\,\&\,$T$}
24498+
where the static type of $e$ is of the form \code{$X$\,\&\,\,$T$}
2449624499
where $X$ is a type variable.
2449724500
In this situation, the declared type of $v$ is \code{X}
2449824501
(\ref{localVariableDeclaration}),
24499-
but the type \code{$X$\,\&\,$T$} is added to the stack of types of interest,
24500-
and the type of $v$ is immediately promoted to \code{$X$\,\&\,$T$}.
24502+
but the type \code{$X$\,\&\,\,$T$} is added to the stack of types of interest,
24503+
and the type of $v$ is immediately promoted to \code{$X$\,\&\,\,$T$}.
2450124504

2450224505
\commentary{%
24503-
Note that \code{$X$\,\&\,$T$} is not otherwise automatically a
24506+
Note that \code{$X$\,\&\,\,$T$} is not otherwise automatically a
2450424507
type of interest just because $X$ is a type of interest.
2450524508
In particular, if $w$ is declared as
2450624509
\code{\LATE?\,\,$X$\,\,$w$ = $e$;}
24507-
where the static type of $e$ is \code{$X$\,\&\,$T$},
24508-
the type of $w$ is \emph{not} immediately promoted to \code{$X$\,\&\,$T$}.%
24510+
where the static type of $e$ is \code{$X$\,\&\,\,$T$},
24511+
the type of $w$ is \emph{not} promoted to \code{$X$\,\&\,\,$T$}.%
24512+
}
24513+
24514+
\LMHash{}%
24515+
We now mention the locations where promotion may occur
24516+
independently of the control flow.
24517+
24518+
\commentary{%
24519+
We might say that these promotions are ``atomic'',
24520+
and all other promotions are derived from these,
24521+
and from the control flow.%
24522+
}
24523+
24524+
\LMHash{}%
24525+
Let $\ell$ be a location,
24526+
and let $v$ be a local variable which is in scope at $\ell$.
24527+
Assume that $\ell$ occurs after the declaration of $v$.
24528+
The expressions that give rise to promotion or demotion
24529+
of the type of $v$ at $\ell$
24530+
are the following:
24531+
24532+
\begin{itemize}
24533+
\item A type test of the form \code{$v$\,\,\IS\,\,$T$}.
24534+
\item A type check of the form \code{$v$\,\,\AS\,\,$T$}.
24535+
\item An assignment of the form \code{$v$\,\,=\,\,$e$}
24536+
where the static type of $e$ is a type of interest for $v$ at $\ell$,
24537+
and similarly for compound assignments
24538+
(\ref{compoundAssignment})
24539+
including \lit{??=}.
24540+
\item An equality test of the form \code{$v$\,\,==\,\,$e$} or
24541+
\code{$e$\,\,==\,\,$v$}, where $e$ is \NULL,
24542+
optionally wrapped in one or more parentheses,
24543+
and similarly for \lit{!=}.
24544+
\end{itemize}
24545+
24546+
\LMHash{}%
24547+
In particular,
24548+
a check of the form \code{$v$\,\,==\,\,\NULL},
24549+
\code{\NULL\,\,==\,\,$v$},
24550+
or \code{$v$\,\,\IS\,\,Null}
24551+
where $v$ has type $T$ at $\ell$
24552+
promotes the type of $v$
24553+
to \code{Null} in the \TRUE{} continuation,
24554+
and to \NonNullType{$T$} in the \FALSE{} continuation.
24555+
24556+
\LMHash{}%
24557+
%% TODO(eernst), for review: The null safety spec says that `T?` is
24558+
%% promoted to `T`, but implementations _do_ promote `X extends int?` to
24559+
%% `X & int`. So I specify that.
24560+
A check of the form \code{$v$\,\,!=\,\,\NULL},
24561+
\code{\NULL\,\,!=\,\,$v$},
24562+
or \code{$v$\,\,\IS\,\,$T$}
24563+
where $v$ has type $T$ at $\ell$
24564+
promotes the type of $v$
24565+
to \NonNullType{$T$} in the \TRUE{} continuation,
24566+
and to \code{Null} in the \FALSE{} continuation.
24567+
24568+
\commentary{%
24569+
The resulting type of $v$ may be the obvious one, e.g.,
24570+
\code{$v$\,\,=\,\,1} may promote $v$ to \code{int},
24571+
but it may also give rise to a demotion
24572+
(changing the type of $v$ to a supertype of the type of $v$ at $\ell$),
24573+
and it may have no effect on the type of $v$
24574+
(e.g., when the static type of $e$ is not a type of interest).
24575+
These details will be specified in a future version of this specification.
24576+
24577+
Every non-trivial type of control flow determines the control flow graph,
24578+
and the control flow graph determines which of the above
24579+
may or must have occurred at $\ell$,
24580+
and hence gives rise to the stack of types of interest of $v$ at $\ell$,
24581+
which in turn determines the type of $v$ at $\ell$.
24582+
This part of the flow analysis will also be specified in the future.%
2450924583
}
2451024584

2451124585

@@ -24666,32 +24740,11 @@ \section{Null safety} %% !!!TODO!!!
2466624740
%% !!!At the end: Search Null, change to Never where appropriate
2466724741
%% !!!Search all `TODO`.*null
2466824742

24669-
\subsubsection{Null promotion}
24670-
\LMLabel{}
24671-
24672-
!!!
24673-
24674-
The machinery of type promotion is extended to promote the type of
24675-
variables based on nullability checks subject to the same set of
24676-
restrictions as normal promotion. The relevant checks and the types
24677-
they are considered to promote to are as follows.
24678-
24679-
A check of the form \code{$e$ == null} or of the form
24680-
\code{$e$\,\,\IS\,\,Null} where $e$ has static type $T$ promotes the
24681-
type of $e$ to \code{Null} in the \code{true} continuation, and to
24682-
\NonNullType{$T$} in the \code{false} continuation.
24683-
24684-
A check of the form \code{$e$ != null} or of the form
24685-
\code{$e$\,\,\IS\,\,$T$} where $e$ has static type
24686-
\code{$T$?}\ promotes the type of $e$ to $T$ in the \code{true}
24687-
continuation, and to \code{Null} in the \code{false} continuation.
24688-
24689-
The static type of an expression \code{$e$!} is \NonNullType{$T$}
24690-
where $T$ is the static type of $e$.
24691-
2469224743
\subsubsection{Null aware operator}
2469324744
\LMLabel{nullShorteningTransformation}
2469424745

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

0 commit comments

Comments
 (0)