Skip to content

Commit 5cf72ff

Browse files
committed
Parse arbitrary operators after expr-like macro invocations in statement position
Closes #20093.
1 parent fea5aa6 commit 5cf72ff

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/libsyntax/parse/parser.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2995,14 +2995,17 @@ impl<'a> Parser<'a> {
29952995
/// actually, this seems to be the main entry point for
29962996
/// parsing an arbitrary expression.
29972997
pub fn parse_assign_expr(&mut self) -> P<Expr> {
2998-
let lo = self.span.lo;
29992998
let lhs = self.parse_binops();
2999+
self.parse_assign_expr_with(lhs)
3000+
}
3001+
3002+
pub fn parse_assign_expr_with(&mut self, lhs: P<Expr>) -> P<Expr> {
30003003
let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL;
30013004
match self.token {
30023005
token::Eq => {
30033006
self.bump();
30043007
let rhs = self.parse_expr_res(restrictions);
3005-
self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
3008+
self.mk_expr(lhs.span.lo, rhs.span.hi, ExprAssign(lhs, rhs))
30063009
}
30073010
token::BinOpEq(op) => {
30083011
self.bump();
@@ -3020,8 +3023,9 @@ impl<'a> Parser<'a> {
30203023
token::Shr => BiShr
30213024
};
30223025
let rhs_span = rhs.span;
3026+
let span = lhs.span;
30233027
let assign_op = self.mk_assign_op(aop, lhs, rhs);
3024-
self.mk_expr(lo, rhs_span.hi, assign_op)
3028+
self.mk_expr(span.lo, rhs_span.hi, assign_op)
30253029
}
30263030
_ => {
30273031
lhs
@@ -3919,8 +3923,9 @@ impl<'a> Parser<'a> {
39193923
let e = self.mk_mac_expr(span.lo,
39203924
span.hi,
39213925
macro.and_then(|m| m.node));
3922-
let e =
3923-
self.parse_dot_or_call_expr_with(e);
3926+
let e = self.parse_dot_or_call_expr_with(e);
3927+
let e = self.parse_more_binops(e, 0);
3928+
let e = self.parse_assign_expr_with(e);
39243929
self.handle_expression_like_statement(
39253930
e,
39263931
ast::DUMMY_NODE_ID,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test parsing binary operators after macro invocations.
12+
13+
#![feature(macro_rules)]
14+
15+
macro_rules! id {
16+
($e: expr) => { $e }
17+
}
18+
19+
fn foo() {
20+
id!(1i) + 1;
21+
id![1i] - 1;
22+
id!(1i) * 1;
23+
id![1i] / 1;
24+
id!(1i) % 1;
25+
26+
id!(1i) & 1;
27+
id![1i] | 1;
28+
id!(1i) ^ 1;
29+
30+
let mut x = 1i;
31+
id![x] = 2;
32+
id!(x) += 1;
33+
34+
id!(1f64).clone();
35+
36+
id!([1i, 2, 3])[1];
37+
id![drop](1i);
38+
39+
id!(true) && true;
40+
id![true] || true;
41+
}
42+
43+
fn main() {}

0 commit comments

Comments
 (0)