Improve the error message for parenthesised box expressions

Closes #15386.
This commit is contained in:
P1start 2014-10-26 12:07:54 +13:00
parent 2a7be1b209
commit fb00015246
2 changed files with 28 additions and 0 deletions

View File

@ -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`.
@ -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);

View File

@ -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 `;`
}