Skip to content

Flow analysis. No promotion in case of shared case scope and assignment in when part #60708

Closed
@sgrekhov

Description

@sgrekhov

If switch statement variable is assigned in when part then no promotion occurs if case scope is shared. Why there is no promotion in test2() below?

test1(Object? x) {
  switch (x) {
    case int v when (x = 42) > 0:
      x.expectStaticType<Exactly<int>>(); // Ok, promoted
    case _:
      x.expectStaticType<Exactly<Object?>>();
  }
}

test2(Object? x) {
  switch (x) {
    case int v when (x = 42) > 0:
    case int v:
      x.expectStaticType<Exactly<Object?>>(); // No promotion
    case _:
      x.expectStaticType<Exactly<Object?>>();
  }
}

T expectStaticType<R extends Exactly<T>>() {
  return this;
}

typedef Exactly<T> = T Function(T);

cc @stereotype441

Dart SDK version: 3.9.0-95.0.dev (dev) (Sun May 4 21:03:05 2025 -0700) on "windows_x64"

Metadata

Metadata

Assignees

No one assigned

    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