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.25k stars 1.58k forks source link

NNBD. Wrong analyzer error messages for invalid override #39882

Closed sgrekhov closed 4 years ago

sgrekhov commented 4 years ago
class A {
  int? i = 1;
}
class C extends A {
  int i = 2; // error - 'C.i=' ('void Function(int)') isn't a valid override of 'A.i=' ('void Function(int)'). - override_checking_A02_FIELD_t02.dart:118:7 - invalid_override
}
main() {
  C();
}

An expected error message is error - 'C.i=' ('void Function(int)') isn't a valid override of 'A.i=' ('void Function(int?)'). - override_checking_A02_FIELD_t02.dart:118:7 - invalid_override

And vice versa example

class A {
  int i = 1;
}
class C extends A {
  int? i = 2; // error - 'C.i' ('int Function()') isn't a valid override of 'A.i' ('int Function()'). - override_checking_A02_FIELD_t02.dart:117:8 - invalid_override
}
main() {
  C();
}

An expected error is error - 'C.i' ('int? Function()') isn't a valid override of 'A.i' ('int Function()'). - override_checking_A02_FIELD_t02.dart:117:8 - invalid_override

dartanalyzer version 2.8.0-dev.0.0

bwilkerson commented 4 years ago

I think the fix for improved display of types was landed just a day or two ago, but it's possible that these cases were not covered by that CL. @scheglov Could you check?

scheglov commented 4 years ago

I'm not sure when exactly this was fixed - some time ago, or yesterday when I did another pass over using DartType.getDisplayString(withNullability) instead of DartType.toString(). Anyway, the bleeding edge now report for the first example:

error: 'C.i=' ('void Function(int)') isn't a valid override of 'A.i=' ('void Function(int?)'). (invalid_override at [test] bin/test.dart:5)

And for the second example:

error: 'C.i' ('int? Function()') isn't a valid override of 'A.i' ('int Function()'). (invalid_override at [test] bin/test.dart:5)