language icon indicating copy to clipboard operation
language copied to clipboard

[Dart 3] Can't infer types in some cases

Open MohiuddinM opened this issue 1 year ago • 6 comments

The compiler and analyzer are unable to infer types in some pretty clear cases. For example:

class A<T> { }

class C extends A<int> {}

class B<T extends A<S>, S> {
  B(A<S> x);
}

void main() {
  B(C());
}

This code results in the following error:

lib/main.dart:10:3:
Error: Inferred type argument 'A<Object?>' doesn't conform to the bound 'A<S>' of the type variable 'T' on 'B'.
 - 'A' is from 'package:dartpad_sample/main.dart' ('lib/main.dart').
 - 'Object' is from 'dart:core'.
  B(C());
  ^
lib/main.dart:5:9:
Info: This is the type variable whose bound isn't conformed to.
class B<T extends A<S>, S> {
        ^
Error: Compilation failed.

Analyzer error:

Couldn't infer type parameter 'T'.

Tried to infer 'A<Object?>' for 'T' which doesn't work:
  Type parameter 'T' is declared to extend 'A<S>' producing 'A<int>'.

Consider passing explicit type argument(s) to the generic.

Version: Dart SDK version: 3.0.0 (stable) (Thu May 4 01:11:00 2023 -0700) on "windows_x64"

MohiuddinM avatar May 14 '23 21:05 MohiuddinM