Skip to content

Inconsistent type demoting of a template #55021

@seha-bot

Description

@seha-bot

Dart 3.3.0

Consider this snippet:

final class Foo<T> {
  const Foo(this.bar);
  final T bar;
}

List<Foo> get foos1 => const [Foo(1), Foo("2")];
List get foos2 => const [Foo(1), Foo("2")];

void main() {
  final List<Foo> wrong = foos1;
  print(wrong[0].runtimeType);
  
  final List<Foo> right = foos2.cast<Foo>().toList();
  print(right[0].runtimeType);

  final List alsoRight = foos2;
  print(alsoRight[0].runtimeType);
}

Output:

Foo<dynamic>
Foo<int>
Foo<int>

I don't think there is a valid reason for foos1's elements losing their type.

Second example:

final class Foo<T> {
  const Foo(this.bar);
  final T bar;
}

void main() {
  Foo<dynamic> a = Foo(1);
  print(a.runtimeType);
  print(a.bar.runtimeType);
  
  dynamic b = Foo(1);
  print(b.runtimeType);
  print(b.bar.runtimeType);
}

In the above example bar's type is never forgotten, but a is demoted to Foo<dynamic> even tho the instance is of type Foo<int>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions