From fb00015246824313e8882935c9ba175ea6daf9d4 Mon Sep 17 00:00:00 2001 From: P1start Date: Sun, 26 Oct 2014 12:07:54 +1300 Subject: [PATCH] Improve the error message for parenthesised box expressions Closes #15386. --- src/libsyntax/parse/parser.rs | 14 ++++++++++++++ .../compile-fail/parenthesized-box-expr-message.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/test/compile-fail/parenthesized-box-expr-message.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8ef3a559bf41a..d9dd37fcb4f4e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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); diff --git a/src/test/compile-fail/parenthesized-box-expr-message.rs b/src/test/compile-fail/parenthesized-box-expr-message.rs new file mode 100644 index 0000000000000..05bbaec37af02 --- /dev/null +++ b/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 or the MIT license +// , 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 `;` +}