-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
cfe-dysfunctionalitiesIssues for the CFE not behaving as intendedIssues for the CFE not behaving as intendedtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)Incorrect behavior (everything from a crash to more subtle misbehavior)
Description
I should preface by saying: this is super niche, and it's not a big deal to me if/when it gets fixed.
But I noticed this problem and thought it'd be nice to report it.
Summary
If a value's type is defined by a type argument, a switch expression can be missing the null case, and it isn't noticed until runtime.
Example
int foo<T extends int?>(T value) => switch (value) { int() => value };
void main() {
final int value = foo<int?>(null);
print(value); // prints "null"
print(value.isEven); // throws an error
}The same issue happens when there aren't any type constraints:
int foo<T>(T value) => switch (value) { Object() => value };
void main() {
final Object value = foo(null);
print(value == null);
}According to static analysis, The operand can't be 'null', so the condition is always 'false'.
But the program prints "true".
I've only seen this situation once in the "real world", so perhaps it's not a huge deal :)
AbdeMohlbi
Metadata
Metadata
Assignees
Labels
cfe-dysfunctionalitiesIssues for the CFE not behaving as intendedIssues for the CFE not behaving as intendedtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)Incorrect behavior (everything from a crash to more subtle misbehavior)