Skip to content

Flow analysis doesn't recognize that null check patterns always match a non-nullable type #52084

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
stereotype441 opened this issue Apr 18, 2023 · 0 comments
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-flow Implementation of flow analysis in analyzer/cfe P2 A bug or feature request we're likely to work on

Comments

@stereotype441
Copy link
Member

Flow analysis currently (as of cb4c9c7) fails to recognize that if the matched value type is non-nullable, then a null check pattern (e.g. _?) or the equivalent relational pattern (!= null), will always match.

As a result, flow analysis can't recognize that a switch statement like this one is trivially exhaustive:

bool f(int i) { // (1)
  switch (i) {
    case var _?:
      return i.isEven;
  }
}

main() {
  print(f(0));
  print(f(1));
}

And so an error is issued at (1), saying "The body might complete normally, causing 'null' to be returned, but the return type, 'bool', is a potentially non-nullable type."

If flow analysis were slightly smarter, it would see that no error needs to be issued at (1), because var _? always matches the non-nullable type int, so there would be no error.

@stereotype441 stereotype441 self-assigned this Apr 18, 2023
@stereotype441 stereotype441 added P2 A bug or feature request we're likely to work on area-fe-analyzer-shared labels Apr 18, 2023
@stereotype441 stereotype441 removed their assignment Apr 18, 2023
@johnniwinther johnniwinther added area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-flow Implementation of flow analysis in analyzer/cfe and removed legacy-area-fe-analyzer-shared labels Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-flow Implementation of flow analysis in analyzer/cfe P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

2 participants