Skip to content
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

Flow analysis. Assignment is not detected in case ... when ... part #60269

Open
sgrekhov opened this issue Mar 7, 2025 · 3 comments
Open

Flow analysis. Assignment is not detected in case ... when ... part #60269

sgrekhov opened this issue Mar 7, 2025 · 3 comments
Labels
area-dart-model Use area-dart-model for issues related to packages analyzer, front_end, and kernel. fe-analyzer-shared-flow-analysis model-flow Implementation of flow analysis in analyzer/cfe

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Mar 7, 2025

The following program fails in both CFE and the analyzer.

main() {
  int i;
  if (true case == true when (i = 42) > 0) {
    print("Ok");
  }
  i; // Error: Non-nullable variable 'i' must be assigned before it can be used.
}

Flow analysis for if-case statements is not yet specified in the specification but intuitively I'd expect that it should detect that i is definitely assigned here.

cc @stereotype441

Dart SDK version: 3.8.0-166.0.dev (dev) (Tue Mar 4 20:02:17 2025 -0800) on "windows_x64"

@eernstg
Copy link
Member

eernstg commented Mar 7, 2025

Hmmmmmm, perhaps the flow analysis doesn't conclude that == true is guaranteed to match when the scrutinee is true, so i = 42 remains possibly rather than definitely executed when i; is reached?

@stereotype441
Copy link
Member

Hmmmmmm, perhaps the flow analysis doesn't conclude that == true is guaranteed to match when the scrutinee is true, so i = 42 remains possibly rather than definitely executed when i; is reached?

Yes, that is correct. Flow analysis only distinguishes true from false when they're used in a conditional context; otherwise, it considers them both just to be arbitrary values of type bool. So when analyzing if (true case == true when ...), flow analysis just sees if (<some boolean> case == true when ...).

@lrhn
Copy link
Member

lrhn commented Mar 10, 2025

I'd not expect (or want) flow analysis to guess the result of non-constant == checks.

If you write a seemingly trivial if (1 == 1), that's still not the same as if (true). If you meant the latter, write that!

We "know" that it'll be true because we know the behavior of int.==, but I don't want the inference to have to assume anything about any non-constant invocation of a function, other than that it returns its static type.
We could, but the author could also just write true if they meant it.

That is: If it's not a constant, it's not compile-time true.

if (true case == true when (i = 42) > 0) {

is the same as

if (true == true && (i = 42) > 0) {}

and that also doesn't initialize i. And that's fine with me.

@lrhn lrhn added the area-dart-model Use area-dart-model for issues related to packages analyzer, front_end, and kernel. label Mar 10, 2025
@johnniwinther johnniwinther added the model-flow Implementation of flow analysis in analyzer/cfe label Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart-model Use area-dart-model for issues related to packages analyzer, front_end, and kernel. fe-analyzer-shared-flow-analysis model-flow Implementation of flow analysis in analyzer/cfe
Projects
None yet
Development

No branches or pull requests

5 participants