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

range syntax doesn't work with non-literal endpoints in for statements #20241

Closed
blaenk opened this issue Dec 26, 2014 · 5 comments · Fixed by #21374
Closed

range syntax doesn't work with non-literal endpoints in for statements #20241

blaenk opened this issue Dec 26, 2014 · 5 comments · Fixed by #21374
Labels
A-grammar Area: The grammar of Rust A-parser Area: The parsing of Rust source code to an AST
Milestone

Comments

@blaenk
Copy link
Contributor

blaenk commented Dec 26, 2014

Range syntax doesn't work with non-literal endpoints. It seems that the parser tries to parse a structure definition or instantiation.

I don't know if this is known and just not completely implemented yet, but I figured I'd give the heads up.

fn main() {
  let x = 10;

  for z in 0u..x {
    println!("{}", z);
  }
}

:5:12: 5:13 error: expected :, found !
:5 println!("{}", z);

playpen

@huonw
Copy link
Member

huonw commented Dec 26, 2014

It should work with explicit disambiguation via parens for i in (0..n) { ....

This seems like it may be something that crops up often so it's probably worth at least improving the diagnostics.

@bstrie
Copy link
Contributor

bstrie commented Dec 26, 2014

@huonw I don't understand why the parens are necessary, surely that's not our plan for 1.0. Why can't the parser unambiguously detect that .. denotes a range in that context?

@blaenk
Copy link
Contributor Author

blaenk commented Dec 26, 2014

Especially since we've already gone out of our way to prevent other ambiguities.

@huonw
Copy link
Member

huonw commented Dec 26, 2014

@bstrie it is detecting that .. denotes a range. This isn't an ambiguity with .. specifically, it's a problem with the <ident> { token sequence at the end of the .. expression. It looks it just needs to have the same special case in for position as exists for for i in x { ... and for i in x+y { ....

@huonw huonw added A-grammar Area: The grammar of Rust A-parser Area: The parsing of Rust source code to an AST labels Dec 26, 2014
@nrc nrc changed the title range syntax doesn't work with non-literal endpoints range syntax doesn't work with non-literal endpoints in for statements Dec 29, 2014
@huonw
Copy link
Member

huonw commented Jan 10, 2015

#20830 (and #20858) has some more analysis.

dgrunwald added a commit to dgrunwald/rust that referenced this issue Jan 11, 2015
Fixes rust-lang#20811 and rust-lang#20241.

Note: this changes the semantics of parse_more_binops() to parse binops with
precedence >= min_prec.
Previously, it would parse binops with precedence > min_prec.
@brson brson added this to the 1.0 beta milestone Jan 15, 2015
dgrunwald added a commit to dgrunwald/rust that referenced this issue Jan 22, 2015
Grammar changes:
* allow 'for _ in 1..i {}' (fixes rust-lang#20241)
* allow 'for _ in 1.. {}' as infinite loop
* prevent use of range notation in contexts where only operators of high
  precedence are expected (fixes rust-lang#20811)

Parser code cleanup:
* remove RESTRICTION_NO_DOTS
* make AS_PREC const and follow naming convention
* make min_prec inclusive
bors added a commit that referenced this issue Jan 23, 2015
This PR is intended as alternative to #20958. It fixes the same grammar inconsistencies, but does not increase the operator precedence of `..`, leaving it at the same level as the assignment operator.
For previous discussion, see #20811 and #20958.

Grammar changes:
* allow `for _ in 1..i {}` (fixes #20241)
* allow `for _ in 1.. {}` as infinite loop
* prevent use of range notation in contexts where only operators of high precedence are expected (fixes #20811)

Parser code cleanup:
* remove `RESTRICTION_NO_DOTS`
* make `AS_PREC` const and follow naming convention
* make `min_prec` inclusive

r? nikomatsakis
dlrobertson pushed a commit to dlrobertson/rust that referenced this issue Nov 29, 2018
Grammar changes:
* allow 'for _ in 1..i {}' (fixes rust-lang#20241)
* allow 'for _ in 1.. {}' as infinite loop
* prevent use of range notation in contexts where only operators of high
  precedence are expected (fixes rust-lang#20811)

Parser code cleanup:
* remove RESTRICTION_NO_DOTS
* make AS_PREC const and follow naming convention
* make min_prec inclusive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-grammar Area: The grammar of Rust A-parser Area: The parsing of Rust source code to an AST
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants