1
Fork 0

Rollup merge of #134900 - dtolnay:unoprange, r=compiler-errors,davidtwco

Fix parsing of ranges after unary operators

Fixes https://github.com/rust-lang/rust/issues/134899.

This PR aligns the parsing for unary `!` and `-` and `*` with how unary `&` is already parsed [here](5c0a6e68cf/compiler/rustc_parse/src/parser/expr.rs (L848-L854)).
This commit is contained in:
Matthias Krüger 2025-03-03 10:40:57 +01:00 committed by GitHub
commit 9aff9c070a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View file

@ -567,7 +567,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))
}

View file

@ -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,
@ -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));
@ -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);
}