Skip to content

Exhaustive check on generic sealed class is considered not exhaustive #4199

Open
@rrousselGit

Description

@rrousselGit

Consider the following sealed hierarchy:

sealed class Base<A> {}

sealed class Mid<A, B> extends Base<A> {}

final class Final<A, B> extends Mid<A, B> {}

We can then use it with pattern matching like so:

void main() {
  Base<int> base = Final<int, Object?>();

  switch (base) {
    case Final<int, Object?>():
      print('42');
  }
}

The problem is, this switch is considered non-exhaustive.

To have the compiler consider the switch as exhaustive, we incorrectly have to change Final<int, Object?>() into Final<Object?, Object?>().

This is incorrect, because given Base<int>, it is guaranteed that all instances will implement Final<int, ...>

Removing the intermediary Mid class fixes the issue, so it probably has to do with the added level of indirection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    exhaustivenessrequestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions