Skip to content

[extension types] Analyzer incorrectly says that patterns involving extension types will never match #55406

@Quijx

Description

@Quijx

The analyzer shows a warning when a pattern matches an extension type saying that the value will never match the required type, when it clearly will.

The following dart code contains some examples and the corresponding analyzer warnings that are shown.

extension type const Id(int i) {}

class Foo {
  const Foo(this.id, this.ids);

  final Id id;
  final List<Id> ids;
}

void main() {
  const foo = Foo(Id(0), [Id(1), Id(2)]);
  switch (const Id(0)) {
    case Id():
      // ^^ The matched value type 'Id' can never match the required type 'Id'.
      print('id');
  }
  switch (foo) {
    case Foo(:final id):
      //      ^^^^^^^^ The matched value type 'Id' can never match the required type 'Id'.
      print(id);
  }
  switch (foo) {
    case Foo(:final ids):
      //      ^^^^^^^^^ The matched value type 'List<Id>' can never match the required type 'List<Id>'.
      print(ids);
  }
}

When switching the extension type with a corresponding class, the warnings disappear.
The program executes as expected and the cases are matched.

This happens in Dart SDK version: 3.3.0 (stable) as well as on the current master channel on dartpad.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions