Description
[Edit: A summary of the issues brought up can be found here.]
Cf. dart-lang/sdk#42568, the analyzer and the CFE disagree on some programs with respect to inference of the return type of a function literal.
The difference in behavior fits the following understanding: The analyzer takes normal completion into account (which means that the 'actual returned type' is considered to be nullable when the body of the function can complete normally), but the CFE (and the current rules) do not include that step.
[Correction, July 6th: The current rules do include a step where normal completion includes Null
in the return type.]
It may seem reasonable to include this step, because it allows function literals to complete normally when that's convenient, and the result is simply that the return type will be nullable, which is sound.
However, we do not otherwise equip normal completion with such powers:
void main() {
var b = true;
f() {
if (b) return 1;
}
g() {
if (b) return 1;
if (!b) return null;
}
}
Here, f
is an error because the inferred return type is int
, and this makes normal completion of the body an error. However, g
is accepted, because return null;
makes the inferred return type nullable, in which case it is not an error to reach the end.
So I'd recommend that we keep the current rules and ask the analyzer to report this error. [Correction July 6th: The current rules are actually a bit confusing, because the function literal gets a nullable return type if the body can complete normally, but a local function seems to omit that step.]
(We have otherwise considered new errors from the analyzer to be a breaking change, but it seems likely that the CFE is used in so many contexts today that it is very difficult to write Dart software which is being used for anything and which avoids seeing an error from the CFE. In other words, it seems reasonable to add the rule that it is also not a breaking change to report a new error in the analyzer if the CFE already reports that error.)
@lrhn, @leafpetersen, @natebosch, @stereotype441, @jakemac53, @munificent, WDYT?