Skip to content

Flow analysis. Reachability in case of null-aware method invocation. #60610

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

Closed
sgrekhov opened this issue Apr 24, 2025 · 2 comments
Closed

Flow analysis. Reachability in case of null-aware method invocation. #60610

sgrekhov opened this issue Apr 24, 2025 · 2 comments
Labels
model-flow Implementation of flow analysis in analyzer/cfe type-question A question about expected behavior or functionality

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Apr 24, 2025

Now we have the following.

class C {
  void foo(int x) {}
}

test1(C? c) {
  late int i;
  if (c == null) {
    c?.foo(i = 42); // No dead code warning
  }
  i; // No error
}

test2(C? c) {
  int i;
  if (c != null) {
    c?.foo(i = 42); // Warning. The receiver can't be null, so the null-aware operator '?.' is unnecessary.
  }
  i; // Error. Not definitely assigned.
}

main() {
  test1(C());
  test2(C());
}

I think it's ok for now because sound-flow-analysis feature is not implemented yet. @stereotype441 what should we expect with this feature? I'd expect dead code warning and "Definitely unassigned" error in test1().

I'm not sure about what expect in case of test2(). How flow analysis should treat if (c != null) { c?.something;}?

Dart SDK version: 3.9.0-43.0.dev (dev) (Tue Apr 22 13:03:05 2025 -0700) on "windows_x64"

@sgrekhov sgrekhov added model-flow Implementation of flow analysis in analyzer/cfe type-question A question about expected behavior or functionality labels Apr 24, 2025
@stereotype441
Copy link
Member

With sound-flow-analysis enabled, I would expect no change in the behavior of test1. The reason is that flow analysis never promotes to Null. It could do so soundly, but to do so would have negative consequences. For details see dart-lang/language#1505 (comment).

With sound-flow-analysis enabled, I would expect the "not definitely assigned" error in test2 to go away, because if (c != null) promotes c to type C, and ?. applied to a non-nullable type is guaranteed to execute its right hand side.

@sgrekhov
Copy link
Contributor Author

@stereotype441 thank you for the explanation! The question is answered so I'm closing this issue.

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 type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

2 participants