Open
Description
Consider the following declaration:
void f<X>(X x) {
if (x is int?) {
// `x` has been promoted to `X & int?`.
X x1 = x;
x?.isEven;
if (x != null) {
// `x` has been promoted to `X & int`.
X x2 = x;
x.isEven;
}
}
}
This program is accepted by the analyzer as well as the CFE (DartPad, 'Based on Flutter 3.0.1 Dart SDK 2.17.1'), and it illustrates that the test x != null
can promote a variable from X & int?
to X & int
.
The specification language for this case would be the following:
A check of the form
e != null
or of the forme is T
wheree
has static typeT?
promotes the type of e toT
in thetrue
continuation, and toNull
in thefalse
continuation.
However, that rule only handles the case where the variable has a type of the form T?
, and X & int?
is not such a type. Do we need to generalize the specification a little bit in order to describe what the tools are currently doing?
@leafpetersen, WDYT?