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

strict-inference doesn't complain about example given in its doc #45371

Open goderbauer opened 3 years ago

goderbauer commented 3 years ago

According to [1] the following should report an inference failure when strict-inference is turned on:

void main() {
  var a = [1, 2, 3].fold(true, (s, x) => s + x);
}

However, in an example project with the analysis_options.yaml file posted below no infernece issue is reported on Dart 2.13.0 (build 2.13.0-143.0.dev). (The only issue reported is an unused variable a.)

analyzer:
  language:
    strict-inference: true

[1] https://github.com/dart-lang/language/blob/master/resources/type-system/strict-inference.md#example-under-constrained-generic-method-invocations

/cc @srawlins

goderbauer commented 3 years ago

This example given in the same doc also behaves differently then documented (differences noted in all-caps):

void f1(a) => print(a);           // Inference failure
void f2(var a) => print(a);       // Inference failure
void f3(final a) => print(a);     // Inference failure
void f4(int a) => print(a);       // OK
void f5<T>(T a) => print(a);      // OK
void f6([var a = 7]) => print(a); // Inference failure
void f7([int a = 7]) => print(a); // OK
void f8({var a}) => print(a);     // Inference failure
void f9({int a}) => print(a);     // OK

class C {
  C.x(var a) {}               // Inference failure <--- NO HINT PRODUCED
  C.y(int a) {}               // OK
  void f1(var a) => print(a); // Inference failure
  void f2(int a) => print(a);
}

class D extends C {
  @override
  void f2(a) => print(a); // OK <--- PRODUCES HINT
}

class E extends C {
  @override
  void f2(var a) => print(a); // OK <--- PRODUCES HINT
}

void fA(String cb(var a)) => print(cb(7)); // Inference failure
void fB(String cb(int x)) => print(cb(7)); // OK

// Typedef parameters cannot be specified with `var`.
typedef Callback = void Function(int); // OK

void main() {
  var f = (var a) {};      // Inference failure <--- NO HINT PRODUCED
  var g = (_, __, ___) {}; // OK
  fA((a) => a * a);        // OK
  fB((a) => a * a);        // OK
  Callback h = (var a) {}; // OK
}
srawlins commented 3 years ago

I think the first issue mentioned is an issue with the impl. Most of the missing Hints in the doc you mention I think are issues with the doc, as these are lines where inference does not take place.