]> git.lizzy.rs Git - rust.git/commitdiff
Improve E0178 suggestion placement
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 28 Mar 2017 12:10:16 +0000 (14:10 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 25 Apr 2017 09:07:42 +0000 (11:07 +0200)
src/librustc_errors/emitter.rs
src/librustc_typeck/diagnostics.rs
src/libsyntax/parse/parser.rs
src/test/compile-fail/E0178.rs [deleted file]
src/test/compile-fail/trait-object-reference-without-parens-suggestion.rs [deleted file]
src/test/ui/did_you_mean/E0178.rs [new file with mode: 0644]
src/test/ui/did_you_mean/E0178.stderr [new file with mode: 0644]
src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.rs [new file with mode: 0644]
src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.stderr [new file with mode: 0644]

index 085424ef7e676a299d8a593b0e3d804fa1b7abc6..68e58e230f82693d64148d56f2734d6a237c9b67 100644 (file)
@@ -37,9 +37,12 @@ fn emit(&mut self, db: &DiagnosticBuilder) {
 
         if let Some(sugg) = db.suggestion.clone() {
             assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len());
-            if sugg.substitutes.len() == 1 && // don't display multispans as labels
-               sugg.msg.split_whitespace().count() < 10 && // don't display long messages as labels
-               sugg.substitutes[0].find('\n').is_none() { // don't display multiline suggestions as labels
+            // don't display multispans as labels
+            if sugg.substitutes.len() == 1 &&
+               // don't display long messages as labels
+               sugg.msg.split_whitespace().count() < 10 &&
+               // don't display multiline suggestions as labels
+               sugg.substitutes[0].find('\n').is_none() {
                 let msg = format!("{} `{}`", sugg.msg, sugg.substitutes[0]);
                 primary_span.push_span_label(sugg.msp.primary_spans()[0], msg);
             } else {
index 54637269bc02c58ddf6c271bc681e06f3f7cb247..0f42ee15ecf6fea1110144fa55135cd10dc15f78 100644 (file)
@@ -3185,7 +3185,7 @@ fn main() {
 String concatenation appends the string on the right to the string on the
 left and may require reallocation. This requires ownership of the string
 on the left. If something should be added to a string literal, move the
-literal to the heap by allocating it with `to_owned()` like in 
+literal to the heap by allocating it with `to_owned()` like in
 `"Your text".to_owned()`.
 
 "##,
index e1fe8f1b2eb3fa5b5ba4572f3f61f7f4ba51167e..55098d774723437128eca6b541c6b4a4427d1b16 100644 (file)
@@ -1490,9 +1490,8 @@ fn maybe_recover_from_bad_type_plus(&mut self, allow_plus: bool, ty: &Ty) -> PRe
         let bounds = self.parse_ty_param_bounds()?;
         let sum_span = ty.span.to(self.prev_span);
 
-        let mut err = struct_span_err!(self.sess.span_diagnostic, ty.span, E0178,
+        let mut err = struct_span_err!(self.sess.span_diagnostic, sum_span, E0178,
             "expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(&ty));
-        err.span_label(ty.span, &format!("expected a path"));
 
         match ty.node {
             TyKind::Rptr(ref lifetime, ref mut_ty) => {
@@ -1511,9 +1510,11 @@ fn maybe_recover_from_bad_type_plus(&mut self, allow_plus: bool, ty: &Ty) -> PRe
                 err.span_suggestion(sum_span, "try adding parentheses:", sum_with_parens);
             }
             TyKind::Ptr(..) | TyKind::BareFn(..) => {
-                help!(&mut err, "perhaps you forgot parentheses?");
+                err.span_label(sum_span, &"perhaps you forgot parentheses?");
             }
-            _ => {}
+            _ => {
+                err.span_label(sum_span, &"expected a path");
+            },
         }
         err.emit();
         Ok(())
diff --git a/src/test/compile-fail/E0178.rs b/src/test/compile-fail/E0178.rs
deleted file mode 100644 (file)
index 6527465..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2016 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.
-
-trait Foo {}
-
-struct Bar<'a> {
-    w: &'a Foo + Copy,
-    //~^ ERROR E0178
-    //~| NOTE expected a path
-    x: &'a Foo + 'a,
-    //~^ ERROR E0178
-    //~| NOTE expected a path
-    y: &'a mut Foo + 'a,
-    //~^ ERROR E0178
-    //~| NOTE expected a path
-    z: fn() -> Foo + 'a,
-    //~^ ERROR E0178
-    //~| NOTE expected a path
-}
-
-fn main() {
-}
diff --git a/src/test/compile-fail/trait-object-reference-without-parens-suggestion.rs b/src/test/compile-fail/trait-object-reference-without-parens-suggestion.rs
deleted file mode 100644 (file)
index f9f887b..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2015 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() {
-    let _: &Copy + 'static;
-    //~^ ERROR expected a path
-    //~| HELP try adding parentheses
-    //~| SUGGESTION let _: &(Copy + 'static);
-    //~| ERROR the trait `std::marker::Copy` cannot be made into an object
-    let _: &'static Copy + 'static;
-    //~^ ERROR expected a path
-    //~| HELP try adding parentheses
-    //~| SUGGESTION let _: &'static (Copy + 'static);
-}
diff --git a/src/test/ui/did_you_mean/E0178.rs b/src/test/ui/did_you_mean/E0178.rs
new file mode 100644 (file)
index 0000000..8fb6c98
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2016 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.
+
+trait Foo {}
+
+struct Bar<'a> {
+    w: &'a Foo + Copy,
+    x: &'a Foo + 'a,
+    y: &'a mut Foo + 'a,
+    z: fn() -> Foo + 'a,
+}
+
+fn main() {
+}
diff --git a/src/test/ui/did_you_mean/E0178.stderr b/src/test/ui/did_you_mean/E0178.stderr
new file mode 100644 (file)
index 0000000..86f51a2
--- /dev/null
@@ -0,0 +1,26 @@
+error[E0178]: expected a path on the left-hand side of `+`, not `&'a Foo`
+  --> $DIR/E0178.rs:14:8
+   |
+14 |     w: &'a Foo + Copy,
+   |        ^^^^^^^^^^^^^^ try adding parentheses: `&'a (Foo + Copy)`
+
+error[E0178]: expected a path on the left-hand side of `+`, not `&'a Foo`
+  --> $DIR/E0178.rs:15:8
+   |
+15 |     x: &'a Foo + 'a,
+   |        ^^^^^^^^^^^^ try adding parentheses: `&'a (Foo + 'a)`
+
+error[E0178]: expected a path on the left-hand side of `+`, not `&'a mut Foo`
+  --> $DIR/E0178.rs:16:8
+   |
+16 |     y: &'a mut Foo + 'a,
+   |        ^^^^^^^^^^^^^^^^ try adding parentheses: `&'a mut (Foo + 'a)`
+
+error[E0178]: expected a path on the left-hand side of `+`, not `fn() -> Foo`
+  --> $DIR/E0178.rs:17:8
+   |
+17 |     z: fn() -> Foo + 'a,
+   |        ^^^^^^^^^^^^^^^^ perhaps you forgot parentheses?
+
+error: aborting due to 4 previous errors
+
diff --git a/src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.rs b/src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.rs
new file mode 100644 (file)
index 0000000..11757ab
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2015 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() {
+    let _: &Copy + 'static;
+    let _: &'static Copy + 'static;
+}
diff --git a/src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.stderr b/src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.stderr
new file mode 100644 (file)
index 0000000..cffa047
--- /dev/null
@@ -0,0 +1,22 @@
+error[E0178]: expected a path on the left-hand side of `+`, not `&Copy`
+  --> $DIR/trait-object-reference-without-parens-suggestion.rs:12:12
+   |
+12 |     let _: &Copy + 'static;
+   |            ^^^^^^^^^^^^^^^ try adding parentheses: `&(Copy + 'static)`
+
+error[E0178]: expected a path on the left-hand side of `+`, not `&'static Copy`
+  --> $DIR/trait-object-reference-without-parens-suggestion.rs:13:12
+   |
+13 |     let _: &'static Copy + 'static;
+   |            ^^^^^^^^^^^^^^^^^^^^^^^ try adding parentheses: `&'static (Copy + 'static)`
+
+error[E0038]: the trait `std::marker::Copy` cannot be made into an object
+  --> $DIR/trait-object-reference-without-parens-suggestion.rs:12:12
+   |
+12 |     let _: &Copy + 'static;
+   |            ^^^^^ the trait `std::marker::Copy` cannot be made into an object
+   |
+   = note: the trait cannot require that `Self : Sized`
+
+error: aborting due to previous error
+