sdk
sdk copied to clipboard
[cfe] Misleading error message
The compile-time error message on return e; seems to forget to take the special async treatment of return into account in some cases:
Future<Object>? test1() async {
Object? o = null;
return o;
}
main() {
test1();
}
With a fresh front end (using dart from 6854871069eaa5fe56f24bada4325f9aef23888b), this program causes the following error message:
tests/language/scratch2_test.dart:3:10: Error: A value of type 'Object?' can't be returned from an async function with return type 'Future<Object>?' because 'Object?' is nullable and 'Future<Object>?' isn't.
- 'Object' is from 'dart:core'.
- 'Future' is from 'dart:async'.
return o;
^
An obvious problem is that it says that Future<Object>? isn't nullable. ;-)
But the underlying problem would be that the error on the static type of e in return e; in an asynchronous non-generator function is detected using the future value type of the function, not the declared return type. So if the error message is intended to give advice about nullability it should probably say something like
Error: A value of type 'Object?' can't be returned from an async function with future value type 'Object' because 'Object?' is nullable and 'Object' isn't.
The analyzer's message for this is
A value of type 'Object?' can't be returned from the function 'test1' because it has a return type of 'Future<Object>?'.
This is also not a good message because it claims that a value of type Object? is being returned, which isn't accurate. I like the way Erik's message strips of the Future< and > but I'm not sure 'future value type' will convey the right meaning to users, so I'd want to word-smith it a bit.