Skip to content

Flow analysis. Type of interest and the for loop increment part #60557

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 17, 2025 · 2 comments
Closed

Flow analysis. Type of interest and the for loop increment part #60557

sgrekhov opened this issue Apr 17, 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

@stereotype441 is the following expected? Why if type T is made a type of interest in the increment part of the for(;;) loop then a variable of type S cannot be promoted to T in the body of the loop but can be after it?

class S {}

class T extends S {
  int answer() => 42;
}

test1() {
  S s = S();
  for (int i = 0; i < 1; s is T ? true : false, i++) {
    s = T();
    s.answer(); // Error. The method 'answer' isn't defined for the type 'S'. 
  }
}

test2() {
  S s = S();
  for (int i = 0; i < 1; s is T ? true : false, i++) {
  }
  s = T();
  s.answer(); // Ok
}

Dart SDK version: 3.9.0-23.0.dev (dev) (Tue Apr 15 21:06:42 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 17, 2025
@stereotype441
Copy link
Member

Yes, this is working as intended. The reason is because the order in which flow analysis visits the code corresponds to the order in which it executes. Even though the "updater" part of a for(;;) loop appears textually before the loop body, it is always executed after the body. So flow analysis visits it after visiting the body. This means that if T is made a type of interest in the updater, that doesn't affect the body. But if T is made a type of interest in the body, that will affect the updater.

Closing as "not planned" since this is working as intended.

@stereotype441 stereotype441 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2025
@sgrekhov
Copy link
Contributor Author

Thank you!

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