Skip to content

Commit 3863b68

Browse files
committed
Propagate restrictions against struct literals to the RHS of assignments
This prevents confusing errors when accidentally using an assignment in an `if` expression. For example: ```rust fn main() { let x = 1u; if x = x { println!("{}", x); } } ``` Previously, this yielded: ``` test.rs:4:16: 4:17 error: expected `:`, found `!` test.rs:4 println!("{}", x); ^ ``` With this change, it now yields: ``` test.rs:3:8: 3:13 error: mismatched types: expected `bool`, found `()` (expected bool, found ()) test.rs:3 if x = x { ^~~~~ ``` Closes issue #17283
1 parent 99293b1 commit 3863b68

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,15 +2692,16 @@ impl<'a> Parser<'a> {
26922692
pub fn parse_assign_expr(&mut self) -> P<Expr> {
26932693
let lo = self.span.lo;
26942694
let lhs = self.parse_binops();
2695+
let restrictions = self.restrictions & RestrictionNoStructLiteral;
26952696
match self.token {
26962697
token::EQ => {
26972698
self.bump();
2698-
let rhs = self.parse_expr();
2699+
let rhs = self.parse_expr_res(restrictions);
26992700
self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
27002701
}
27012702
token::BINOPEQ(op) => {
27022703
self.bump();
2703-
let rhs = self.parse_expr();
2704+
let rhs = self.parse_expr_res(restrictions);
27042705
let aop = match op {
27052706
token::PLUS => BiAdd,
27062707
token::MINUS => BiSub,

0 commit comments

Comments
 (0)