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.11k stars 1.57k forks source link

Analyzer: Improperly failed inference with promoted type parameters #35799

Open MichaelRFairhurst opened 5 years ago

MichaelRFairhurst commented 5 years ago

Here, the call to f should be valid and inferrable as Cloneable<T>.

class Clonable<T> {}                                                                             
class SubClonable<T> extends Clonable<T> {
  T m(T t) => t;  
} 

void f<T extends Clonable<T>>(T object) {
  if (object is SubClonable<T>) {
    f(object);
  }     
}

However it fails in analyzer and CFE. This is the analyzer ticket for this issue. (CFE is #35800)

It's critical to note that object is promoted to type T' extends SubCloneable<T>. So if the analyzer attempts to infer SubClonable<T> for T in f(object), then that will fail. However, it should be possible to infer Clonable, which in fact works when typed explicitly.

May be related to #35100

stereotype441 commented 5 years ago

Note: there is a test in the analyzer codebase that should be re-enabled when this bug is fixed: https://dart-review.googlesource.com/c/sdk/+/90880/4/pkg/analyzer/test/src/task/strong/checker_test.dart#3989

eernstg commented 1 year ago

I'm not convinced that this issue should be considered as a bug in the analyzer, it is probably about the type inference algorithm. I created https://github.com/dart-lang/language/issues/3009 for that.

FMorschel commented 1 year ago

So, just to help out a bit, as for what I found:

52077

https://github.com/dart-lang/sdk/issues/52117 https://github.com/dart-lang/sdk/issues/56028 (this is potentially the solution for the other two)

Are all possibly related?

eernstg commented 1 year ago

@FMorschel, thanks for looking into those potential connections!

I do think there is a connection between dart-lang/sdk#52117 and #52077 and dart-lang/sdk#56028, they are all about extensions and intersection types.

However, there is probably no connection to this issue, where the topic is F-bounded type variables and inference (with no references to intersection types or extensions at all).

FMorschel commented 1 year ago

Sorry about my ignorance. I just thought they were connected because in https://github.com/dart-lang/sdk/issues/52117 there is also a suggestion in https://github.com/dart-lang/sdk/issues/52117 that sounded to me similar to this issue.

eernstg commented 1 year ago

No problem, it is not always easy to see whether or not there is a connection!