From 3863b68df490dab6bcaccef39039382d47ffb226 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Mon, 15 Sep 2014 19:06:27 -0700 Subject: [PATCH] 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 --- src/libsyntax/parse/parser.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 44576f7c166..c5efb6e5571 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2692,15 +2692,16 @@ impl<'a> Parser<'a> { pub fn parse_assign_expr(&mut self) -> P { let lo = self.span.lo; let lhs = self.parse_binops(); + let restrictions = self.restrictions & RestrictionNoStructLiteral; match self.token { token::EQ => { self.bump(); - let rhs = self.parse_expr(); + let rhs = self.parse_expr_res(restrictions); self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs)) } token::BINOPEQ(op) => { self.bump(); - let rhs = self.parse_expr(); + let rhs = self.parse_expr_res(restrictions); let aop = match op { token::PLUS => BiAdd, token::MINUS => BiSub,