Skip to content

[flow analysis] promote variable in a function expression if potentially final #2753

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
asashour opened this issue Dec 30, 2022 · 4 comments
Closed

Comments

@asashour
Copy link

asashour commented Dec 30, 2022

void g(void Function() x) {}

void f(int? i) {
  int? x;
  if (i == null) return;
  x = i; // here x is not-nullable
  g(() {
    x.isEven; // 🔥 x is nullable
  });
}

Current behavior: on entering the function, x promotion is discarded, so it is not promoted to be not-nullable, because x is assigned (written).

The current proposed implementation is that on entering the closure, variables which are:

  1. not written/assigned in any closure, and
  2. not written after "entering" this closure

are considered final and so promotion is preserved.

@asashour
Copy link
Author

asashour commented Dec 30, 2022

@lrhn
Copy link
Member

lrhn commented Dec 30, 2022

The example above seems sound because there is no demoting assignment to x down-flow from the creation of the closure.
If there were, it would not be sound to assume that x still has the same type when the closure function is eventually called.

@asashour asashour changed the title [flow analysis] promote variable in a function expression if not assigned inside it [flow analysis] promote variable in a function expression if potentially final Jan 2, 2023
@lrhn lrhn transferred this issue from dart-lang/sdk Jan 2, 2023
@lrhn lrhn added the inference label Jan 2, 2023
@lrhn
Copy link
Member

lrhn commented Jan 2, 2023

Moving this to the language repo, since any change to inference is a language change.

@stereotype441
Copy link
Member

Duplicate of #1536.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants