]> git.lizzy.rs Git - rust.git/commitdiff
On single local candidate, use span label
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 18 Feb 2020 09:35:18 +0000 (01:35 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 28 Feb 2020 19:37:58 +0000 (11:37 -0800)
14 files changed:
src/librustc_typeck/check/method/suggest.rs
src/test/ui/associated-const/associated-const-no-item.stderr
src/test/ui/auto-ref-slice-plus-ref.stderr
src/test/ui/impl-trait/issues/issue-21659-show-relevant-trait-impls-3.stderr
src/test/ui/impl-trait/no-method-suggested-traits.stderr
src/test/ui/issues/issue-5153.stderr
src/test/ui/issues/issue-57362-1.stderr
src/test/ui/issues/issue-57362-2.stderr
src/test/ui/never_type/issue-2149.stderr
src/test/ui/object-pointer-types.stderr
src/test/ui/self/point-at-arbitrary-self-type-trait-method.stderr
src/test/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr
src/test/ui/traits/trait-item-privacy.stderr
src/test/ui/trivial-bounds/trivial-bounds-leak.stderr

index c6c404c5c7820551a1f0705512101c46bc907912..3fa5d4baa76b4dbac37a686612fa1a4dd39d90d6 100644 (file)
@@ -846,7 +846,7 @@ fn suggest_traits_to_import<'b>(
             let message = |action| {
                 format!(
                     "the following {traits_define} an item `{name}`, perhaps you need to {action} \
-                 {one_of_them}:",
+                     {one_of_them}:",
                     traits_define =
                         if candidates.len() == 1 { "trait defines" } else { "traits define" },
                     action = action,
@@ -944,19 +944,32 @@ fn suggest_traits_to_import<'b>(
             }
 
             if !suggested {
-                let mut msg = message(if let Some(param) = param_type {
+                let action = if let Some(param) = param_type {
                     format!("restrict type parameter `{}` with", param)
                 } else {
                     "implement".to_string()
-                });
-                for (i, trait_info) in candidates.iter().enumerate() {
-                    msg.push_str(&format!(
-                        "\ncandidate #{}: `{}`",
-                        i + 1,
-                        self.tcx.def_path_str(trait_info.def_id),
-                    ));
+                };
+                let mut use_note = true;
+                if let [trait_info] = &candidates[..] {
+                    if let Some(span) = self.tcx.hir().span_if_local(trait_info.def_id) {
+                        err.span_label(
+                            self.tcx.sess.source_map().def_span(span),
+                            &format!("this trait defines an item `{}`", item_name),
+                        );
+                        use_note = false
+                    }
+                }
+                if use_note {
+                    let mut msg = message(action);
+                    for (i, trait_info) in candidates.iter().enumerate() {
+                        msg.push_str(&format!(
+                            "\ncandidate #{}: `{}`",
+                            i + 1,
+                            self.tcx.def_path_str(trait_info.def_id),
+                        ));
+                    }
+                    err.note(&msg[..]);
                 }
-                err.note(&msg[..]);
             }
         }
     }
index d96cf67b875784d5b354cdc1551595ebdf4b3934..e6765bc3dd65ed420243b55f951b8795e52969ba 100644 (file)
@@ -1,12 +1,13 @@
 error[E0599]: no associated item named `ID` found for type `i32` in the current scope
   --> $DIR/associated-const-no-item.rs:5:23
    |
+LL | trait Foo {
+   | --------- this trait defines an item `ID`
+...
 LL | const X: i32 = <i32>::ID;
    |                       ^^ associated item not found in `i32`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `ID`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error: aborting due to previous error
 
index a0739a7a90b0a2f6b13b1f9574f7f94ad71fe4d5..3e14dc801221c90590202fe343a4ed41cdcca771 100644 (file)
@@ -3,40 +3,44 @@ error[E0599]: no method named `test_mut` found for struct `std::vec::Vec<{intege
    |
 LL |     a.test_mut();
    |       ^^^^^^^^ help: there is a method with a similar name: `get_mut`
+...
+LL | trait MyIter {
+   | ------------ this trait defines an item `test_mut`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `test_mut`, perhaps you need to implement it:
-           candidate #1: `MyIter`
 
 error[E0599]: no method named `test` found for struct `std::vec::Vec<{integer}>` in the current scope
   --> $DIR/auto-ref-slice-plus-ref.rs:8:7
    |
 LL |     a.test();
    |       ^^^^ method not found in `std::vec::Vec<{integer}>`
+...
+LL | trait MyIter {
+   | ------------ this trait defines an item `test`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `test`, perhaps you need to implement it:
-           candidate #1: `MyIter`
 
 error[E0599]: no method named `test` found for array `[{integer}; 1]` in the current scope
   --> $DIR/auto-ref-slice-plus-ref.rs:10:11
    |
 LL |     ([1]).test();
    |           ^^^^ method not found in `[{integer}; 1]`
+...
+LL | trait MyIter {
+   | ------------ this trait defines an item `test`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `test`, perhaps you need to implement it:
-           candidate #1: `MyIter`
 
 error[E0599]: no method named `test` found for reference `&[{integer}; 1]` in the current scope
   --> $DIR/auto-ref-slice-plus-ref.rs:11:12
    |
 LL |     (&[1]).test();
    |            ^^^^ method not found in `&[{integer}; 1]`
+...
+LL | trait MyIter {
+   | ------------ this trait defines an item `test`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `test`, perhaps you need to implement it:
-           candidate #1: `MyIter`
 
 error: aborting due to 4 previous errors
 
index 57417975474f7fe718b76bc15f7f081ccbcb48e1..32a677a7d7fc1a2c43473b91fb04d55c0f4133c5 100644 (file)
@@ -1,6 +1,9 @@
 error[E0599]: no method named `foo` found for struct `Bar` in the current scope
   --> $DIR/issue-21659-show-relevant-trait-impls-3.rs:20:8
    |
+LL | trait Foo<A> {
+   | ------------ this trait defines an item `foo`
+...
 LL | struct Bar;
    | ----------- method `foo` not found for this
 ...
@@ -8,8 +11,6 @@ LL |     f1.foo(1usize);
    |        ^^^ method not found in `Bar`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `foo`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error: aborting due to previous error
 
index da25617e18759c08da121adaa650cc6b6ca3b605..8025c12047fefc2898134157aebd54c67578136f 100644 (file)
@@ -122,62 +122,68 @@ LL |     std::rc::Rc::new(&mut Box::new(&Foo)).method();
 error[E0599]: no method named `method2` found for type `u64` in the current scope
   --> $DIR/no-method-suggested-traits.rs:45:10
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     1u64.method2();
    |          ^^^^^^^ method not found in `u64`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method2` found for struct `std::rc::Rc<&mut std::boxed::Box<&u64>>` in the current scope
   --> $DIR/no-method-suggested-traits.rs:47:44
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     std::rc::Rc::new(&mut Box::new(&1u64)).method2();
    |                                            ^^^^^^^ method not found in `std::rc::Rc<&mut std::boxed::Box<&u64>>`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method2` found for struct `no_method_suggested_traits::Foo` in the current scope
   --> $DIR/no-method-suggested-traits.rs:50:37
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     no_method_suggested_traits::Foo.method2();
    |                                     ^^^^^^^ method not found in `no_method_suggested_traits::Foo`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method2` found for struct `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Foo>>` in the current scope
   --> $DIR/no-method-suggested-traits.rs:52:71
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method2();
    |                                                                       ^^^^^^^ method not found in `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Foo>>`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method2` found for enum `no_method_suggested_traits::Bar` in the current scope
   --> $DIR/no-method-suggested-traits.rs:54:40
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     no_method_suggested_traits::Bar::X.method2();
    |                                        ^^^^^^^ method not found in `no_method_suggested_traits::Bar`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method2` found for struct `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Bar>>` in the current scope
   --> $DIR/no-method-suggested-traits.rs:56:74
    |
+LL |     pub trait Bar {
+   |     ------------- this trait defines an item `method2`
+...
 LL |     std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method2();
    |                                                                          ^^^^^^^ method not found in `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Bar>>`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `method2`, perhaps you need to implement it:
-           candidate #1: `foo::Bar`
 
 error[E0599]: no method named `method3` found for struct `Foo` in the current scope
   --> $DIR/no-method-suggested-traits.rs:59:9
index 4680c8b131c50a77e4e46b373b2f6835e60de580..730da21ddf5fef2233f54b063f30e9bf775963d5 100644 (file)
@@ -1,12 +1,13 @@
 error[E0599]: no method named `foo` found for reference `&dyn Foo` in the current scope
   --> $DIR/issue-5153.rs:10:27
    |
+LL | trait Foo {
+   | --------- this trait defines an item `foo`
+...
 LL |     (&5isize as &dyn Foo).foo();
    |                           ^^^ method not found in `&dyn Foo`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `foo`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error: aborting due to previous error
 
index ad596db13ccfd65e43152609ce19cd99d92f955a..3a5189b132da2fefd8cdc03d86bc0129329f70e6 100644 (file)
@@ -1,13 +1,14 @@
 error[E0599]: no method named `f` found for fn pointer `fn(&u8)` in the current scope
   --> $DIR/issue-57362-1.rs:20:7
    |
+LL | trait Trait {
+   | ----------- this trait defines an item `f`
+...
 LL |     a.f();
    |       ^ method not found in `fn(&u8)`
    |
    = note: `a` is a function, perhaps you wish to call it
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `f`, perhaps you need to implement it:
-           candidate #1: `Trait`
 
 error: aborting due to previous error
 
index 3528084f6ceb4fc02d88faa5914c0bd1e046ef55..e2d80b3b4df739c041e94614b31c691f5f264540 100644 (file)
@@ -1,12 +1,13 @@
 error[E0599]: no function or associated item named `make_g` found for fn pointer `for<'r> fn(&'r ())` in the current scope
   --> $DIR/issue-57362-2.rs:22:25
    |
+LL | trait X {
+   | ------- this trait defines an item `make_g`
+...
 LL |     let x = <fn (&())>::make_g();
    |                         ^^^^^^ function or associated item not found in `for<'r> fn(&'r ())`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `make_g`, perhaps you need to implement it:
-           candidate #1: `X`
 
 error: aborting due to previous error
 
index 9645244751dabe6e8c23c2800517ae74a7c33e6f..4fadf49bd6d28465a81282130e7dac7c87aff8b8 100644 (file)
@@ -9,12 +9,13 @@ LL |         for elt in self { r = r + f(*elt); }
 error[E0599]: no method named `bind` found for array `[&str; 1]` in the current scope
   --> $DIR/issue-2149.rs:13:12
    |
+LL | trait VecMonad<A> {
+   | ----------------- this trait defines an item `bind`
+...
 LL |     ["hi"].bind(|x| [x] );
    |            ^^^^ method not found in `[&str; 1]`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `bind`, perhaps you need to implement it:
-           candidate #1: `VecMonad`
 
 error: aborting due to 2 previous errors
 
index 855894b4495c6baba701a7bece186aeb28e1ebc0..5ca326bca31c5bdbcf92665e12e4c5e21d2e1b12 100644 (file)
@@ -1,22 +1,24 @@
 error[E0599]: no method named `owned` found for reference `&dyn Foo` in the current scope
   --> $DIR/object-pointer-types.rs:11:7
    |
+LL | trait Foo {
+   | --------- this trait defines an item `owned`
+...
 LL |     x.owned();
    |       ^^^^^ method not found in `&dyn Foo`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `owned`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error[E0599]: no method named `owned` found for mutable reference `&mut dyn Foo` in the current scope
   --> $DIR/object-pointer-types.rs:17:7
    |
+LL | trait Foo {
+   | --------- this trait defines an item `owned`
+...
 LL |     x.owned();
    |       ^^^^^ method not found in `&mut dyn Foo`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `owned`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error[E0599]: no method named `managed` found for struct `std::boxed::Box<(dyn Foo + 'static)>` in the current scope
   --> $DIR/object-pointer-types.rs:23:7
index 28a7b68a6821f9c4a5f390bf2db1037b09305a3f..8ed2b8b5c95211a0dd61403d3e220e3dbe21f424 100644 (file)
@@ -2,7 +2,9 @@ error[E0599]: no method named `foo` found for struct `A` in the current scope
   --> $DIR/point-at-arbitrary-self-type-trait-method.rs:9:7
    |
 LL | trait B { fn foo(self: Box<Self>); }
-   |              --- the method is available for `std::boxed::Box<A>` here
+   | -------      --- the method is available for `std::boxed::Box<A>` here
+   | |
+   | this trait defines an item `foo`
 LL | struct A;
    | --------- method `foo` not found for this
 ...
@@ -10,8 +12,6 @@ LL |     A.foo()
    |       ^^^ method not found in `A`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `foo`, perhaps you need to implement it:
-           candidate #1: `B`
 
 error: aborting due to previous error
 
index 2d0caf1dd875599410d460f17b13cd103773dec7..ccbb2aae05d1ba2701c71cbdd7da7939aa3771e5 100644 (file)
@@ -1,6 +1,9 @@
 error[E0599]: no method named `foo_one` found for struct `MyStruct` in the current scope
   --> $DIR/specialization-trait-not-implemented.rs:22:29
    |
+LL | trait Foo {
+   | --------- this trait defines an item `foo_one`
+...
 LL | struct MyStruct;
    | ----------------
    | |
@@ -13,8 +16,6 @@ LL |     println!("{}", MyStruct.foo_one());
    = note: the method `foo_one` exists but the following trait bounds were not satisfied:
            `MyStruct: Foo`
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `foo_one`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error: aborting due to previous error
 
index 072328ab50c7068283ac0809d6e7c4c1f55bef78..5b7f0a8ce5ff15a2f574d1f3044ab3081b4d20e0 100644 (file)
@@ -4,12 +4,13 @@ error[E0599]: no method named `a` found for struct `S` in the current scope
 LL | struct S;
    | --------- method `a` not found for this
 ...
+LL |     trait A {
+   |     ------- this trait defines an item `a`
+...
 LL |     S.a();
    |       ^ method not found in `S`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `a`, perhaps you need to implement it:
-           candidate #1: `method::A`
 
 error[E0599]: no method named `b` found for struct `S` in the current scope
   --> $DIR/trait-item-privacy.rs:68:7
@@ -45,12 +46,13 @@ error[E0599]: no function or associated item named `a` found for struct `S` in t
 LL | struct S;
    | --------- function or associated item `a` not found for this
 ...
+LL |     trait A {
+   |     ------- this trait defines an item `a`
+...
 LL |     S::a(&S);
    |        ^ function or associated item not found in `S`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `a`, perhaps you need to implement it:
-           candidate #1: `method::A`
 
 error[E0599]: no function or associated item named `b` found for struct `S` in the current scope
   --> $DIR/trait-item-privacy.rs:80:8
@@ -79,12 +81,13 @@ error[E0599]: no associated item named `A` found for struct `S` in the current s
 LL | struct S;
    | --------- associated item `A` not found for this
 ...
+LL |     trait A {
+   |     ------- this trait defines an item `A`
+...
 LL |     S::A;
    |        ^ associated item not found in `S`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `A`, perhaps you need to implement it:
-           candidate #1: `assoc_const::A`
 
 error[E0599]: no associated item named `B` found for struct `S` in the current scope
   --> $DIR/trait-item-privacy.rs:98:8
index acf309ac6087221235e883bdb99467983291a40c..7ed24591e66abf3c6f24505038264a76c68a1617 100644 (file)
@@ -11,12 +11,13 @@ LL | fn cant_return_str() -> str {
 error[E0599]: no method named `test` found for type `i32` in the current scope
   --> $DIR/trivial-bounds-leak.rs:24:10
    |
+LL | pub trait Foo {
+   | ------------- this trait defines an item `test`
+...
 LL |     3i32.test();
    |          ^^^^ method not found in `i32`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `test`, perhaps you need to implement it:
-           candidate #1: `Foo`
 
 error[E0277]: the trait bound `i32: Foo` is not satisfied
   --> $DIR/trivial-bounds-leak.rs:25:15