Skip to content

Unary operator applied to range fails to parse #134899

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
dtolnay opened this issue Dec 29, 2024 · 0 comments · Fixed by #134900
Closed

Unary operator applied to range fails to parse #134899

dtolnay opened this issue Dec 29, 2024 · 0 comments · Fixed by #134900
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 29, 2024

struct S;
impl<T> std::ops::Add<T> for S {
    type Output = ();
    fn add(self, _: T) {}
}

fn main() {
    let _ = ..0;
    let _ = S + ..0;
    let _ = &..0;
    let _ = || ..0;
    let _ = *..0;
}

With the exception of *..0, all of the above expressions parse successfully, so .. is definitely allowed to appear at the beginning of an expression — including an expression with higher precedence such as binary + and unary &. I think it is a bug that *..0 does not parse. Same for -..0 and !..0.

error: expected expression, found `..`
  --> src/main.rs:12:14
   |
12 |     let _ = *..0;
   |              ^^ expected expression
@dtolnay dtolnay added A-parser Area: The lexing & parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 29, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 29, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 6, 2025
Fix parsing of ranges after unary operators

Fixes rust-lang#134899.

This PR aligns the parsing for unary `!` and `-` and `*` with how unary `&` is already parsed [here](https://github.com/rust-lang/rust/blob/5c0a6e68cfdad859615c2888de76505f13e6f01b/compiler/rustc_parse/src/parser/expr.rs#L848-L854).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 3, 2025
…rs,davidtwco

Fix parsing of ranges after unary operators

Fixes rust-lang#134899.

This PR aligns the parsing for unary `!` and `-` and `*` with how unary `&` is already parsed [here](https://github.com/rust-lang/rust/blob/5c0a6e68cfdad859615c2888de76505f13e6f01b/compiler/rustc_parse/src/parser/expr.rs#L848-L854).
@bors bors closed this as completed in 9aff9c0 Mar 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 4, 2025
Rollup merge of rust-lang#134900 - dtolnay:unoprange, r=compiler-errors,davidtwco

Fix parsing of ranges after unary operators

Fixes rust-lang#134899.

This PR aligns the parsing for unary `!` and `-` and `*` with how unary `&` is already parsed [here](https://github.com/rust-lang/rust/blob/5c0a6e68cfdad859615c2888de76505f13e6f01b/compiler/rustc_parse/src/parser/expr.rs#L848-L854).
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants