Skip to content

Promotion based on a non-null test: Implementation more flexible than specification #2258

Open
@eernstg

Description

@eernstg

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 form e is T where e has static type T? promotes the type of e to T in the true continuation, and to Null in the false 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions