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.29k stars 1.59k forks source link

Shadowing class generics with method generics is confusing #33956

Open matanlurey opened 6 years ago

matanlurey commented 6 years ago

Reproduction case:

class Foo<T> {
  void addValues<T>(Iterable<T> values) {}
}

This causes a compile-time error:

[error] The argument type 'Iterable<T> (dart:core/iterable.dart)' can't be assigned to the parameter type 'Iterable<T> (dart:core/iterable.dart)'.

That's because the T's are different...

I do not know where to assign this (language? cfe? analyzer?)

jonahwilliams commented 6 years ago

There is a similar issue in typedefs, I'm not sure if it has already been brought up:

// The T in Function shadows the T in the name.
MyCallback<T> = Function<T>(T);
// Correct version.
MyCallback2<T> = Function(T);

For example, in 2.0.0-dev.68.0 the following code produces the analyzer error: A value of type '(String) → void' can't be assigned to a variable of type '<T>(T) → void' at main.dart:7:23 • invalid_assignment

MyCallback<T> = Function<T>(T);

void main() {
  MyCallback<String> bar = foo; 
}

void foo(String value) {}
vsmenon commented 6 years ago

@matanlurey I'm not getting a compile error on your snippet of code (2.0.0-dev.69.2).

srawlins commented 6 years ago

Lol see https://github.com/dart-lang/sdk/issues/57713 also

matanlurey commented 6 years ago

@vsmenon Really? I see it internally, in DDC. Unless this was just fixed.