diff options
| author | P1start <rewi-github@whanau.org> | 2014-10-26 12:07:54 +1300 |
|---|---|---|
| committer | P1start <rewi-github@whanau.org> | 2014-10-30 17:12:22 +1300 |
| commit | fb00015246824313e8882935c9ba175ea6daf9d4 (patch) | |
| tree | 83cc1b80299b18f8b2d9381eb31de19c0118a423 | |
| parent | 2a7be1b209a4ceea42418f55baf68774571ed9a2 (diff) | |
| download | rust-fb00015246824313e8882935c9ba175ea6daf9d4.tar.gz rust-fb00015246824313e8882935c9ba175ea6daf9d4.zip | |
Improve the error message for parenthesised box expressions
Closes #15386.
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 14 | ||||
| -rw-r--r-- | src/test/compile-fail/parenthesized-box-expr-message.rs | 14 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8ef3a559bf4..d9dd37fcb4f 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 00000000000..05bbaec37af --- /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 <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 `;` +} |
