Skip to content

Fix parsing of ranges after unary operators #134900

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

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,11 @@ impl<'a> Parser<'a> {
fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
self.bump();
let attrs = self.parse_outer_attributes()?;
let expr = self.parse_expr_prefix(attrs)?;
let expr = if self.token.is_range_separator() {
self.parse_expr_prefix_range(attrs)
} else {
self.parse_expr_prefix(attrs)
}?;
let span = self.interpolated_or_expr_span(&expr);
Ok((lo.to(span), expr))
}
Expand Down
28 changes: 27 additions & 1 deletion tests/ui/parser/ranges-precedence.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//@ run-pass
//@ edition: 2021
// Test that the precedence of ranges is correct


use core::ops::{Add, RangeTo};

struct Foo {
foo: usize,
Expand All @@ -11,6 +12,13 @@ impl Foo {
fn bar(&self) -> usize { 5 }
}

impl Add<RangeTo<usize>> for Foo {
type Output = usize;
fn add(self, range: RangeTo<usize>) -> Self::Output {
self.foo + range.end
}
}

fn main() {
let x = 1+3..4+5;
assert_eq!(x, (4..9));
Expand Down Expand Up @@ -49,4 +57,22 @@ fn main() {

let y = ..;
assert_eq!(y, (..));

let reference = &..0;
assert_eq!(*reference, ..0);
let reference2 = &&..0;
assert_eq!(**reference2, ..0);

let closure = || ..0;
assert_eq!(closure(), ..0);

let sum = Foo { foo: 3 } + ..4;
assert_eq!(sum, 7);

macro_rules! expr {
($e:expr) => {};
}
expr!(!..0);
expr!(-..0);
expr!(*..0);
}
Loading