Skip to content

Better subclass handling for argument_type_not_assignable #56505

Open
@FMorschel

Description

When we have a subclass (different type parameter) being used instead of the expected class on a parameter we have a bad error message compared to when we use the expected class.

Also, the current good error message is a bit redundant in the following example. I'm having the declaration path for Iterable twice on the error message (shortened it here to make the message more easy to see in the issue but in my case it is really big) and the actual problem is simply the type parameter for the elements.

Repro:

a.dart:

class A {
  const A();
}

main.dart:

import 'a.dart' as a;

void main() {
  final list = <a.A>{};
  list.addAll(A.list);
  list.addAll(A.iterable);
}

class A {
  const A();

  static List<A> get list => [
    for (int i = 0; i < 10; i++) const A(),
  ];

  static Iterable<A> get iterable => [
    for (int i = 0; i < 10; i++) const A(),
  ];
}

On main.dart line 5 list.addAll(A.list), we get:

The argument type 'List<A>' can't be assigned to the parameter type 'Iterable<A>'. dart(argument_type_not_assignable)

On line 6 list.addAll(A.iterable) we get a way better error message:

The argument type 'Iterable<A> (where A is defined in .\bin\bug.dart)' can't be assigned to the parameter type 'Iterable<A> (where A is defined in .\bin\a.dart)'. dart(argument_type_not_assignable)

iterable.dart(92, 22): Iterable is defined in ...\dart-sdk\lib\core\iterable.dart
bug.dart(9, 7): A is defined in .\bin\bug.dart

iterable.dart(92, 22): Iterable is defined in ...\dart-sdk\lib\core\iterable.dart
a.dart(1, 7): A is defined in .\bin\a.dart

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestanalyzer-uxarea-analyzerUse area-analyzer for Dart analyzer issues, including the analysis server and code completion.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions