From a63b51728c7631394df4f7ba218aeb9234c2a716 Mon Sep 17 00:00:00 2001 From: P1start Date: Sat, 30 Aug 2014 11:22:55 +1200 Subject: [PATCH] Parse bracketed `box` expressions This lets the parser parse expressions like `box (1i + 2)` as `box() (1i + 2)`. Expressions are parsed greedily in `if`, `while`, and `for` expressions, so that `if box(foo) {} {}` is parsed as `if (box(foo) {}) {}`. This is the same behaviour as before this change. Closes #15386. --- src/libsyntax/parse/parser.rs | 15 +++++++++++---- src/test/run-pass/parenthesized-box.rs | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/test/run-pass/parenthesized-box.rs 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); +}