Skip to content

Commit 0698d90

Browse files
authored
Unrolled build for rust-lang#134900
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).
2 parents e16a049 + 462604d commit 0698d90

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

compiler/rustc_parse/src/parser/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,11 @@ impl<'a> Parser<'a> {
567567
fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
568568
self.bump();
569569
let attrs = self.parse_outer_attributes()?;
570-
let expr = self.parse_expr_prefix(attrs)?;
570+
let expr = if self.token.is_range_separator() {
571+
self.parse_expr_prefix_range(attrs)
572+
} else {
573+
self.parse_expr_prefix(attrs)
574+
}?;
571575
let span = self.interpolated_or_expr_span(&expr);
572576
Ok((lo.to(span), expr))
573577
}

tests/ui/parser/ranges-precedence.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//@ run-pass
2+
//@ edition: 2021
23
// Test that the precedence of ranges is correct
34

4-
5+
use core::ops::{Add, RangeTo};
56

67
struct Foo {
78
foo: usize,
@@ -11,6 +12,13 @@ impl Foo {
1112
fn bar(&self) -> usize { 5 }
1213
}
1314

15+
impl Add<RangeTo<usize>> for Foo {
16+
type Output = usize;
17+
fn add(self, range: RangeTo<usize>) -> Self::Output {
18+
self.foo + range.end
19+
}
20+
}
21+
1422
fn main() {
1523
let x = 1+3..4+5;
1624
assert_eq!(x, (4..9));
@@ -49,4 +57,22 @@ fn main() {
4957

5058
let y = ..;
5159
assert_eq!(y, (..));
60+
61+
let reference = &..0;
62+
assert_eq!(*reference, ..0);
63+
let reference2 = &&..0;
64+
assert_eq!(**reference2, ..0);
65+
66+
let closure = || ..0;
67+
assert_eq!(closure(), ..0);
68+
69+
let sum = Foo { foo: 3 } + ..4;
70+
assert_eq!(sum, 7);
71+
72+
macro_rules! expr {
73+
($e:expr) => {};
74+
}
75+
expr!(!..0);
76+
expr!(-..0);
77+
expr!(*..0);
5278
}

0 commit comments

Comments
 (0)