Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HosseinYousefi opened this issue Oct 31, 2024 · 4 comments
Open
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@HosseinYousefi
Copy link
Member

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?

@HosseinYousefi HosseinYousefi added the type-question A question about expected behavior or functionality label Oct 31, 2024
@dart-github-bot
Copy link
Collaborator

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.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Oct 31, 2024
@eernstg
Copy link
Member

eernstg commented Oct 31, 2024

Related to #56998?

@eernstg eernstg added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-question A question about expected behavior or functionality triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Oct 31, 2024
@johnniwinther
Copy link
Member

I don't think it's related. @chloestefantsova can you take a look?

@HosseinYousefi HosseinYousefi changed the title Generic type inference for doesn't work when extending nullable type Generic type inference doesn't work when extending nullable type Oct 31, 2024
@lrhn
Copy link
Member

lrhn commented Oct 31, 2024

Looks like a bug, or a very undeseriable property of inference.

The return Bar(foo); should infer return Bar<T>(foo); from the context type (return type of function is Bar<T>), and then foo is a valid argument.
If you write return Bar<T>(foo); it works. For some reason inference fails to do the obvious thing with a context type of Bar<T>, and infers Bar<Never>(foo) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants