From 7d2231aa22c80f47deea5b482ae03ae0cb2ae827 Mon Sep 17 00:00:00 2001 From: P1start Date: Tue, 21 Apr 2015 21:13:42 +1200 Subject: Change a few error messages to give code suggestions PR #24242 added the ability to the compiler to directly give suggestions about how to modify code to fix an error. The new errors look like this: foobar.rs:5:12: 5:25 error: expected a path on the left-hand side of `+`, not `&'static Copy` [E0178] foobar.rs:5 let x: &'static Copy + 'static; ^~~~~~~~~~~~~ foobar.rs:5:12: 5:35 help: try adding parentheses (per RFC 438): foobar.rs: let x: &'static (Copy + 'static); foobar.rs:2:13: 2:23 error: cast to unsized type: `&_` as `core::marker::Copy` foobar.rs:2 let x = &1 as Copy; ^~~~~~~~~~ foobar.rs:2:19: 2:23 help: try casting to a reference instead: foobar.rs: let x = &1 as &Copy; foobar.rs:7:24: 7:25 error: expected expression, found `;` foobar.rs:7 let x = box (1 + 1); ^ foobar.rs:7:13: 7:16 help: try using `box()` instead: foobar.rs: let x = box() (1 + 1); This also modifies compiletest to give the ability to directly test suggestions given by error messages. --- src/libsyntax/parse/parser.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 0515d1ae945..ae396d7c4e6 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -972,6 +972,9 @@ impl<'a> Parser<'a> { pub fn span_help(&self, sp: Span, m: &str) { self.sess.span_diagnostic.span_help(sp, m) } + pub fn span_suggestion(&self, sp: Span, m: &str, n: String) { + self.sess.span_diagnostic.span_suggestion(sp, m, n) + } pub fn fileline_help(&self, sp: Span, m: &str) { self.sess.span_diagnostic.fileline_help(sp, m) } @@ -2594,6 +2597,7 @@ impl<'a> Parser<'a> { } let lo = self.span.lo; + let box_hi = self.span.hi; try!(self.bump()); @@ -2610,9 +2614,10 @@ impl<'a> Parser<'a> { self.span_err(span, &format!("expected expression, found `{}`", this_token_to_string)); - let box_span = mk_sp(lo, self.last_span.hi); - self.span_help(box_span, - "perhaps you meant `box() (foo)` instead?"); + let box_span = mk_sp(lo, box_hi); + self.span_suggestion(box_span, + "try using `box()` instead:", + "box()".to_string()); self.abort_if_errors(); } let subexpression = try!(self.parse_prefix_expr()); -- cgit 1.4.1-3-g733a5