Skip to content

Flow analysis. No promotion to Never via type test #60657

@sgrekhov

Description

@sgrekhov

Promotion via type test is specified as:

  • We say that a variable x is promotable via type test with type T given variable model VM if
    • VM = VariableModel(declared, promoted, tested, assigned, unassigned, captured)
    • and captured is false
    • and S is the current type of x in VM
    • and not S <: T
    • and T <: S or (S is X extends R and T <: R) or (S is X & R and T <: R)

According to the above we should be able to promote any type to type Never. But we cannot.

main() {
  String s = "a";
  if (s is Never) {
    s.expectStaticType<Exactly<Never>>(); // Error: 'Exactly<Never>' doesn't conform to the bound 'Exactly<String>' of the type parameter 'R'. 
  }
}

extension StaticType<T> on T {
  T expectStaticType<R extends Exactly<T>>() {
    return this;
  }
}
typedef Exactly<T> = T Function(T);

Yes, s.expectStaticType<...>(...) is dead code, but according to the spec it should work.

Dart SDK version: 3.9.0-74.0.dev (dev) (Tue Apr 29 21:02:55 2025 -0700) on "windows_x64"

Metadata

Metadata

Assignees

Labels

area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-flowImplementation of flow analysis in analyzer/cfe

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions