-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spec allows function literals in initializer list #11509
Comments
This comment was originally written by @mhausner The grammar does allow it, you are right. Two years ago we discussed the same issue because having function literals in field initializer expressions leads to ambiguous code. Consider this: class C { Is field x initialized to a function literal and the constructor has no body, or is field x initialized to (a) and => a; is the body of the constructor? The old ANTLR grammar used to have a tie-braker rule that made it impossible to have function literals in initializer expressions (unless nested in parenthesis). I don't see such a rule in the spec now. Gilad, have we made the conscious decision to allow function literals in initializer expressions, or is this an oversight in the current spec? Set owner to @gbracha. |
The grammar needs to be reviewed for such issues. This is a bug in the grammar. |
Is this "Area-Language"? |
Issue #11508 has been merged into this issue. |
This comment was originally written by @mhausner Maybe more Area-Spec it that existed. Removed Area-VM label. |
Marked this as blocking #11828. |
Issue #15626 has been merged into this issue. |
If we disallow '=>' for the body of generative constructors, I think the grammar would no longer be ambiguous. |
No milestone now: This correction will be considered as part of a larger update of grammar related parts of the specification, based on Dart.g. |
Cf. also #35674, which has just been closed as a duplicate of this one. |
Since it's been a while since the last comment, I just wanted to mention that this behavior can still be observed: class C {
final int x;
C() : x = (){ return 42; }();
} Analyzer and ANTLR parse trees:
|
Note: once this is resolved, it would probably make sense to also update the switch expression grammar since there's a similar ambiguity there, too. See: https://github.com/dart-lang/language/blob/main/accepted/3.0/patterns/feature-specification.md#function-expression-in-guard-ambiguity |
Thanks for the heads-up! The error which is reported for the original example today (DartPad, master) is a type error:
This implies that the text This is all in line with the expected behavior today. There is a rule that prevents function literals from being present in initializer elements. It has never been specified precisely, but that is being clarified now in #54284. In any case, that rule was (or at least could have been) introduced in response to this issue. The initial message of this issue indicates that the grammar allowed this kind of construct, and it doesn't mention any special exceptions for initializer list elements. In other words, this issue was relevant for the language before that rule was introduced, which means Dart as of 2014 or so. So we can close this one now, and rely on #54284 to clarify the rule that makes this issue obsolete. |
Example code:
class C {
final int x;
C() : x = (){ return 42; }();
}
main() {
print(new C().x);
}
This errs with the message:
'file:..../initcall.dart': Error: line 3 pos 13: unexpected token ')'
C() : x = (){ return 42; }();
The grammar allows the syntax.
The text was updated successfully, but these errors were encountered: