Skip to content

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

@HosseinYousefi

Description

@HosseinYousefi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-inferenceImplementation of type inferencetype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions