Skip to content

Commit 2370d46

Browse files
committed
Auto merge of #30375 - aaronkeen:issue_28777, r=eddyb
RESTRICTION_STMT_EXPR restriction to allow subsequent expressions to contain braces. #28777
2 parents 0bd29f8 + cedd794 commit 2370d46

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/libsyntax/parse/parser.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2809,16 +2809,25 @@ impl<'a> Parser<'a> {
28092809
}
28102810

28112811
let rhs = try!(match op.fixity() {
2812-
Fixity::Right => self.with_res(restrictions, |this|{
2813-
this.parse_assoc_expr_with(op.precedence(), LhsExpr::NotYetParsed)
2812+
Fixity::Right => self.with_res(
2813+
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2814+
|this| {
2815+
this.parse_assoc_expr_with(op.precedence(),
2816+
LhsExpr::NotYetParsed)
28142817
}),
2815-
Fixity::Left => self.with_res(restrictions, |this|{
2816-
this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed)
2818+
Fixity::Left => self.with_res(
2819+
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2820+
|this| {
2821+
this.parse_assoc_expr_with(op.precedence() + 1,
2822+
LhsExpr::NotYetParsed)
28172823
}),
28182824
// We currently have no non-associative operators that are not handled above by
28192825
// the special cases. The code is here only for future convenience.
2820-
Fixity::None => self.with_res(restrictions, |this|{
2821-
this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed)
2826+
Fixity::None => self.with_res(
2827+
restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2828+
|this| {
2829+
this.parse_assoc_expr_with(op.precedence() + 1,
2830+
LhsExpr::NotYetParsed)
28222831
}),
28232832
});
28242833

src/test/parse-fail/assoc-oddities-3.rs renamed to src/test/run-pass/assoc-oddities-3.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
11+
fn that_odd_parse(c: bool, n: usize) -> u32 {
12+
let x = 2;
13+
let a = [1, 2, 3, 4];
14+
let b = [5, 6, 7, 7];
15+
x + if c { a } else { b }[n]
16+
}
1217

13-
fn that_odd_parse() {
14-
// see assoc-oddities-1 for explanation
15-
x + if c { a } else { b }[n]; //~ ERROR expected one of
18+
fn main() {
19+
assert_eq!(4, that_odd_parse(true, 1));
20+
assert_eq!(8, that_odd_parse(false, 1));
1621
}

src/test/run-pass/issue-28777.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 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+
fn main() {
12+
let v1 = { 1 + {2} * {3} };
13+
let v2 = 1 + {2} * {3} ;
14+
15+
assert_eq!(7, v1);
16+
assert_eq!(7, v2);
17+
18+
let v3;
19+
v3 = { 1 + {2} * {3} };
20+
let v4;
21+
v4 = 1 + {2} * {3};
22+
assert_eq!(7, v3);
23+
assert_eq!(7, v4);
24+
25+
let v5 = { 1 + {2} * 3 };
26+
assert_eq!(7, v5);
27+
28+
let v9 = { 1 + if 1 > 2 {1} else {2} * {3} };
29+
assert_eq!(7, v9);
30+
}

0 commit comments

Comments
 (0)