dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

NNBD. 'switch' error for nullable enum type #40394

Closed sgrekhov closed 4 years ago

sgrekhov commented 4 years ago

Consider the following code

enum E {
  one,
  two,
  three
}

main() {
  E? e = E.three;
  switch (e) { // error - Type 'E?' of the switch expression isn't assignable to the type 'E' of case expressions
    case E.one:
      true;
      break;
    case E.two:
      false;
      break;
    default:
      42;
  }
}

Tested on dartanalyzer version 2.8.0-dev.6.0

lrhn commented 4 years ago

Error suggests what is wrong. It's the run-time type of the case expression constants which must be subtypes of the static type of e, not the other way around.