Skip to content

Guards don't promote in switch expressions #52375

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
munificent opened this issue May 12, 2023 · 2 comments
Closed

Guards don't promote in switch expressions #52375

munificent opened this issue May 12, 2023 · 2 comments
Labels
legacy-area-analyzer Use area-devexp instead.

Comments

@munificent
Copy link
Member

I'm looking into the issue reported in this tweet. I can repro it with:

main() {
  String? s = null;
  var length = switch (0) {
    _ when s != null => s.length,
    _ => 0,
  };

  switch (0) {
    case _ when s != null: s.length;
  };
}

The switch statement is fine, but the switch expression yields:

temp.dart:5:27: Error: Property 'length' cannot be accessed on 'String?' because it is potentially null.
Try accessing using ?. instead.
    _ when s != null => s.length,
                          ^^^^^^

Do we not do type promotion in switch expression guards?

@stereotype441

@stereotype441
Copy link
Member

Yeah, this is a known issue that I discovered just before 3.0 shipped, when there was no longer time to cherry-pick a fix. The fix has been cherry-picked to stable, and will ship in 3.0.1.

Tracking issue is here: #52241

@stereotype441
Copy link
Member

Duplicate of #52241

@stereotype441 stereotype441 marked this as a duplicate of #52241 May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

2 participants