Skip to content

CFE error message for non-exhaustion of promoted-type-variable is misleading #54652

Open
@lrhn

Description

@lrhn

If I do:

sealed class Sealed {}
class Sub extends Sealed {}
void test<T extends Object?>(T v) {
  if (v is Sealed) {
    switch (v) {
      case _ when false: // Non-exhaustive switch.
    }
  }
}

I get an error (correctly) saying that the switch statement is not exhaustive, because the type T&Sealed is always-exhaust.
The message is:

compile: lib/main.dart:6:13:
Error: The type 'T' is not exhaustively matched by the switch cases since it doesn't match 'Sub()'.
    switch (v) {
            ^

The problem here is that the type that needs to be exhausted is Sealed, not T.

The analyzer, in comparison, reports:

error
line 6 • The type 'T & Sealed' is not exhaustively matched by the switch cases since it doesn't match 'Sub()'

That's technically correct, but arguably leaking the internal intersection type.
What the user knows it that they promoted to Sealed, so they expect that they are working on Sealed.
Still, it's better than T.

(Found while testing extension types, but also applies to plain classes.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    legacy-area-front-endLegacy: Use area-dart-model instead.model-messagesPoor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions