Skip to content

Flow analysis. An assignment of null promotes to non-nullable type #60674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sgrekhov opened this issue May 5, 2025 · 0 comments
Open

Flow analysis. An assignment of null promotes to non-nullable type #60674

sgrekhov opened this issue May 5, 2025 · 0 comments
Labels
model-flow Implementation of flow analysis in analyzer/cfe soundness

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented May 5, 2025

The code below works without errors in either CFE and the analyzer.

test1(Object? x) {
  switch (x) {
    case null:
      x.expectStaticType<Exactly<Object?>>(); // Ok
    case _:
      x.expectStaticType<Exactly<Object>>();
  }
}

test2(Object? x) {
  switch (x) {
    case null when x = null == null:
      x.expectStaticType<Exactly<Object>>(); /// Seems that assignment `x = null` promotes `x` from `Object?` to `Object`
    case _:
      x.expectStaticType<Exactly<Object>>();
  }
}

main() {
  test1(1);
  test1(null);
  test2(1);
  test2(null);
}

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

cc @stereotype441

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

@sgrekhov sgrekhov added the model-flow Implementation of flow analysis in analyzer/cfe label May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
model-flow Implementation of flow analysis in analyzer/cfe soundness
Projects
None yet
Development

No branches or pull requests

2 participants