dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

Generic type inference for doesn't work when extending nullable type #57001

Open HosseinYousefi opened 3 hours ago

HosseinYousefi commented 3 hours ago

This works fine:

class Foo {}

class Bar<T extends Foo?> {
  final T foo;
  Bar(this.foo);

  static Bar<T> bar<T extends Foo>(T foo) {
    return Bar(foo); // <-- No need to write Bar<T>(foo)
  }
}

but if T extends Foo? instead, it doesn't:

class Foo {}

class Bar<T extends Foo?> {
  final T foo;
  Bar(this.foo);

  static Bar<T> bar<T extends Foo?>(T foo) {
    return Bar(foo);
    //         ^^^
    // The argument type 'T' can't be assigned to the parameter type 'Never'.
  }
}

Is this expected behavior?

dart-github-bot commented 2 hours ago

Summary: The issue reports that generic type inference fails when a type parameter T extends a nullable type (Foo?) in a static method. The code compiles when T extends a non-nullable type (Foo), but throws an error when T extends a nullable type.

eernstg commented 1 hour ago

Related to https://github.com/dart-lang/sdk/issues/56998?