-
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
[fe-analyzer-shared] Glitch in initializerExpression
parse
#54284
Comments
One observation that is relevant to this issue: Consider: class Foo {
Foo() : a = () {}, b = 0;
} While that program is rejected by analyzer, but not by DSP, (and it seems like analyzer exhibits intended behavior), the following is accepted by both, analyzer and DSP: class Foo {
Foo() : a = <T>() {}, b = 0;
} Also, consider: class Foo {
Foo() : a = <T>() => 0;
} Without |
When parsing initializers ( Said more directly, perhaps, it (tried to) parse void foo() {
var x;
print(x = throw 0..isEven);
} (In both cases parenthesizing the throw is a workaround). Whereas for the field initializer it calls As for disallowing function literals there's a variable Wrt allowing it --- it seems a little weird that I'll try to make that change and run the bots to see what happens. |
…ePrecedenceExpressionLoop instead of just false For now, see what happens. Bug: #54284 Change-Id: I8e9f081ae3aa5caff7e155ef628e90547cc8afe6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392561 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Erik Ernst <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
Is there anything more to do here? |
I still see this behavior (DartPad based on Dart SDK 3.7.0-123.0.dev and Flutter SDK 3.27.0-1.0.pre.438): var x = throw 0..isEven; // OK.
class A {
var x;
A() : x = throw 0..isEven; // Syntax error.
}
void main() {} The initializing <expression> ->
<throwExpression> ->
'throw' <expression> ->
'throw' <cascade> ->
'throw' <conditionalExpression> '..' <cascadeSection> ->
'throw' '0' '..' <cascadeSelector> <cascadeSectionTail> ->
'throw' '0' '..' 'isEven' However, we have the following in the constructor, where an <initializerExpression> ->
<throwExpression> -> /*... same as above ...*/ ->
'throw' '0' '..' 'isEven' There is nothing in this derivation that brings up the special rule that "an In particular, the following differs so much from the grammar that it will inevitably cause some parsing outcomes that don't match the grammar:
|
From the tags on 0512c58 it is first included in 3.7.0-130.0.dev. |
OK, so I was checking the situation with a tool whose version was too low, sorry about that. However, @modulovalue mention a few other cases: class Foo1 {
final Function a;
final int b;
Foo1()
: a = <T>() {},
b = 0;
}
class Foo2 {
final Function a;
Foo2() : a = <T>() => 0;
} Both of these declarations allow the This behavior is different from the specified behavior, but it could be argued that it is benign. See dart-lang/language#4163. |
Thanks to @modulovalue who brought up this behavior here. Consider the following program:
This program causes the analyzer and the CFE to report a syntax error for the
<initializerExpression>
in the constructor, whereas the initializing expression in the top-level variable declaration is accepted.The non-terminal
<initializerExpression>
has for a long time been somewhat vaguely defined because we have a rule that it cannot be a function literal. That rule was never defined precisely. (However, a step in that direction is being taken here.)It is possible that the error above is caused by the use of
<throwExpressionWithoutCascade>
rather than<throwExpression>
, but unless there is a specific reason to make that choice, I'd recommend that the throw expression is parsed using the "normal" non-terminal for that purpose, that is,<throwExpression>
.@jensjoha, perhaps you could say something about some of the questions related to this topic? For example:
<initializerExpression>
and/or an<initializerList>
performed, and how is it enforced that an<initializerExpression>
cannot be a function literal?<throwExpressionWithoutCascade>
rather than<throwExpression>
in an initializer list?<throwExpression>
?The text was updated successfully, but these errors were encountered: