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

Fix parsing of ranges after unary operators #134900

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add more ranges parsing tests
--- stderr -------------------------------
error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:75:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
LL |     }
LL |     expr!(!..0);
   |            ^^ expected expression

error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:76:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
...
LL |     expr!(-..0);
   |            ^^ expected expression

error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:77:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
...
LL |     expr!(*..0);
   |            ^^ expected expression

error: aborting due to 3 previous errors
------------------------------------------
  • Loading branch information
dtolnay committed Dec 29, 2024
commit adaa756b555f751201440bca4ba65fc9b5691ace
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
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved
// 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);
}