]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_typeck/check/method/suggest.rs
On single local candidate, use span label
[rust.git] / src / librustc_typeck / check / method / suggest.rs
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[..]);
             }
         }
     }