-
Notifications
You must be signed in to change notification settings - Fork 209
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
Code after switch statements consider variable not initialized, but using a switch expression works #4272
Comments
Sounds like a difference between inference guessing at exhaustiveness and the exhaustiveness checking algorithm doing better. The first example is a switch expression. It must be exhaustive, so the inference algorithm just assumes that it is, and lets the exhaustiveness checking algorithm check it, which is successful. The second example is a switch statement which is allowed to not be exhaustive. The inference algorithm does a best-effort check to see if it looks exhaustive. It fails, so it assumes the switch is not exhaustive. |
My guess is that it is (simply?) not considered in flow analysis. The cases are clearly exhaustive, or else you would have a different error, and it would be emitted in both cases. I can't test now, but do statement switches ever be considered in flow analysis? For instance, if we were dealing with a boolean or an enum instead of a list. |
Sealed patterns work: sealed class Foo {}
class Bar extends Foo {}
class Baz extends Foo {}
void main() {
Foo value = Bar();
final String result;
switch (value) {
case Bar():
result = 'bar';
case Baz():
result = 'baz';
}
print(result.length); // OK
} Other random patterns work too: int value = 42;
final String result;
switch (value) {
case 42:
result = 'bar';
case _:
result = 'baz';
}
print(result.length); // Valid too Same with bools/enums. The issue seems to be with collections. |
Some simpler List patterns work too: List<String> value = [];
final String result;
switch (value) {
case ['foo']:
result = 'bar';
case [...]:
result = 'baz';
} So something with the OP's example uniquely breaks this. |
See also #2977 |
Consider those two switches:
Both
switch
have the exact same cases. but in the second case,value
is considered not initialized, with the following error:My guess is that this is related to Collection pattern match. But considering one
switch
works, could we make other kinds ofswitch
work too?The text was updated successfully, but these errors were encountered: