dart-lang / language

Design of the Dart language
Other
2.66k stars 205 forks source link

Spec and implementations disagree about context for return/yield in function expressions #3664

Open stereotype441 opened 7 months ago

stereotype441 commented 7 months ago

The analyzer and front end disagree on how to convert the context of a function expression into the context for the operands of return and yield inside the function expression, and they both disagree with the spec.

Paraphrasing from here, and ignoring legacy logic, the spec says to do this:

The analyzer behavior differs from the spec in the following ways:

The front end behavior differs from the spec in the following ways:

That's a lot of behavioral differences! We should pick a behavior to standardize on, and update spec, CFE, and analyzer to all match.

My gut feeling is that the behavior we want is probably a mixture of all three. Perhaps something like this:

But I think that before deciding for sure, it would be worth doing some investigation to see how breaking this would be.

@dart-lang/language-team any thoughts?

lrhn commented 7 months ago

Likely related to https://github.com/dart-lang/language/issues/3148, or at least in the same ballpark. Also https://github.com/dart-lang/language/pull/3151, which I really need to get landed RSN. It might, I hope, specify the correct behavior, and maybe even some of what compilers have implemented.

The paraphrasing (surely accidentally) omits one crucial detail: If the context type scheme of the function literal is C, and C is a function type scheme, then the K used in the following steps is the return type (scheme) of that function type (scheme). It's called the imposed return type schema. (If C is not a function type, the imposed return type schema is probably _, which also has its problems with inference.)

That also means that K cannot be an intersection type. If C is an intersection type, like X & int Function(int), we could use the function part of that to derive K, but I don't know if we do. But K itself can never be an intersection type. Phew! One less tricky case to worry about. Well, unless we do something special for x = () { return e; }(); to give () { ... } a context type of typeof<x> Function(), which we may end up doing with vertical inference.

Generally, a function expression with a context type scheme of R Function(...) should behave mostly similar to a function declaration with a return type of R (modulo not all schemes being types, so "greatest closure of" where appropriate). If we infer the return type of the function expression during downward inference, it should be exactly that, but I don't know if we allow ourselves to refine the type during upward inference. In either case, the imposed return type scheme is then used to derive the type needed at return or yield statements, which becomes the context types for those (except in async functions where the implicit await in returns may add an extra FutureOr).

For sync* and async*, the #3151 PR defines functions similar to futureValueTypeSchema, imaginatively named streamElementTypeSchema and iterableElementTypeScheme. They are similar to what you describe, without using a unionFree helper. (Because I just don't trust helper functions when it comes to structural recursion, it's far too easy to miss something. I want every case handled explicitly, where I can see them!) The biggest difference is that a top-type of dynamic or void also becomes the element type. Just because it makes some kind of sense to me that a dynamic foo() async* { ... } should have a context type of dynamic for elements, and void foo() async* { ... } should use void. Not because it makes any real difference in a contravariant context.

The rule for async functions should probably incorporate the context rules for await, as mentioned in another issue. I wasn't aware of that rule when I specified async return, or wrote #3571, otherwise it should have done the same to the return context type as await does. (Or I should move on #870 and get rid of the FutureOr entirely.)