diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 37bda15ac2c41..50686162ebc47 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2584,10 +2584,17 @@ impl<'a> Parser<'a> { if !self.eat(&token::RPAREN) { let place = self.parse_expr(); self.expect(&token::RPAREN); - let subexpression = self.parse_prefix_expr(); - hi = subexpression.span.hi; - ex = ExprBox(place, subexpression); - return self.mk_expr(lo, hi, ex); + if can_begin_expr(&self.token) { + let subexpression = self.parse_prefix_expr(); + hi = subexpression.span.hi; + ex = ExprBox(place, subexpression); + return self.mk_expr(lo, hi, ex); + } else { // Support `box(EXPR)` == `box EXPR`. + let subexpression = place; + hi = subexpression.span.hi; + ex = self.mk_unary(UnUniq, subexpression); + return self.mk_expr(lo, hi, ex); + } } } diff --git a/src/test/run-pass/parenthesized-box.rs b/src/test/run-pass/parenthesized-box.rs new file mode 100644 index 0000000000000..b22588b86b2cd --- /dev/null +++ b/src/test/run-pass/parenthesized-box.rs @@ -0,0 +1,14 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x: Box = box (1 + 2); + assert_eq!(x, box 3); +}