Skip to content

Type arguments don't have null safetyΒ #56998

@nate-thegrate

Description

@nate-thegrate

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 :)

Metadata

Metadata

Assignees

Labels

cfe-dysfunctionalitiesIssues for the CFE not behaving as intendedtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions