Skip to content
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

Cannot parse two ifs #36377

Closed
aartbik opened this issue Mar 28, 2019 · 4 comments
Closed

Cannot parse two ifs #36377

aartbik opened this issue Mar 28, 2019 · 4 comments
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.) P1 A high priority bug; for example, a single project is unusable or has many test failures
Milestone

Comments

@aartbik
Copy link
Contributor

aartbik commented Mar 28, 2019

Fuzzing found a few more missing cases, having two if's does not seem to work yet.

Set<int> globx = {};

@NeverInline
int bar() {
  globx = { if (true) 3,           4 };  // OK
  globx = {           3, if (true) 4 };  // OK
  globx = { if (true) 3, if (true) 4 };  // NOT OK
}
hello.dart:40:13: Error: Expected ':' after this.
  globx = { if (true) 3, if (true) 4 };  // NOT OK
            ^
hello.dart:40:26: Error: Expected ':' after this.
  globx = { if (true) 3, if (true) 4 };  // NOT OK
                         ^

@a-siva

@aartbik aartbik added area-front-end Use area-front-end for front end / CFE / kernel format related issues. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.) labels Mar 28, 2019
@aartbik
Copy link
Contributor Author

aartbik commented Mar 28, 2019

For what it is worth, fuzzing also found this issue for for-loops.
Also, putting a value in between resolves the issue.

  globx = { for (int i = 0; i < 10; i++) i,  100 };  // OK
  globx = { 0, for (int i = 0; i < 10; i++) 100 + i };  // OK
  globx = { for (int i = 0; i < 10; i++) i,  for (int i = 0; i < 10; i++) 100 + i };  // NOT OK
  globx = { for (int i = 0; i < 10; i++) i,  50, for (int i = 0; i < 10; i++) 100 + i };  // OK AGAIN
hello.dart:42:13: Error: Expected ':' after this.
  globx = { for (int i = 0; i < 10; i++) i,  for (int i = 0; i < 10; i++) 100 + i };  // NOT OK
            ^
hello.dart:42:46: Error: Expected ':' after this.
  globx = { for (int i = 0; i < 10; i++) i,  for (int i = 0; i < 10; i++) 100 + i };  // NOT OK
                                             ^

@aartbik
Copy link
Contributor Author

aartbik commented Mar 28, 2019

Also, not really a bug, but the new syntax gives us a nice classic dangling else problem :-)

globx = [ if (b1) if (b2) 1 else 2 ];

false false : []
false true  : []
true  false : [2]
true  true  : [1]

@aartbik aartbik modified the milestones: dart 2.3, Dart2.3 Mar 28, 2019
@aartbik aartbik added the P1 A high priority bug; for example, a single project is unusable or has many test failures label Mar 28, 2019
@lrhn
Copy link
Member

lrhn commented Mar 29, 2019

This looks like a bug in the inference of whether the literal is a map or a set, as if only top-level elements are counted when trying to see if it's a map or set. The "expected ':' here" message suggests that it is being treated as a map literal.

Since the if branches contain a single value element expression, they should count as a vote for being a set, and the literal should become a set literal.

The "dangling else" issue is a classical parsing ambiguity with C-style if/else. We solve it the same way here as in if statements: Always bind the then to the nearest if without a then. The results you print are as expected.

@chloestefantsova
Copy link
Contributor

Thanks for spotting this! Proposed fix: https://dart-review.googlesource.com/c/sdk/+/98341.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.) P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

4 participants