Skip to content

Commit

Permalink
Improve the error message for parenthesised box expressions
Browse files Browse the repository at this point in the history
Closes #15386.
  • Loading branch information
ftxqxd committed Oct 30, 2014
1 parent 2a7be1b commit fb00015
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -2730,6 +2730,8 @@ impl<'a> Parser<'a> {
return self.parse_dot_or_call_expr();
}

let lo = self.span.lo;

self.bump();

// Check for a place: `box(PLACE) EXPR`.
Expand All @@ -2738,6 +2740,18 @@ impl<'a> Parser<'a> {
if !self.eat(&token::RParen) {
let place = self.parse_expr();
self.expect(&token::RParen);
// Give a suggestion to use `box()` when a parenthesised expression is used
if !self.token.can_begin_expr() {
let span = self.span;
let this_token_to_string = self.this_token_to_string();
self.span_err(span,
format!("expected expression, found `{}`",
this_token_to_string).as_slice());
let box_span = mk_sp(lo, self.last_span.hi);
self.span_help(box_span,
"perhaps you meant `box() (foo)` instead?");
self.abort_if_errors();
}
let subexpression = self.parse_prefix_expr();
hi = subexpression.span.hi;
ex = ExprBox(place, subexpression);
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/parenthesized-box-expr-message.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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
box(1 + 1) //~ HELP perhaps you meant `box() (foo)` instead?
; //~ ERROR expected expression, found `;`
}

0 comments on commit fb00015

Please sign in to comment.