Skip to content

Commit caa216d

Browse files
committed
Tweak wording of "implemented trait isn't imported" suggestion
1 parent 14277ef commit caa216d

27 files changed

+59
-46
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
275275
.span_if_local(def_id)
276276
.unwrap_or_else(|| self.tcx.def_span(def_id));
277277
err.span_label(sp, format!("private {kind} defined here"));
278-
self.suggest_valid_traits(&mut err, out_of_scope_traits, true);
278+
self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
279279
err.emit();
280280
}
281281

@@ -2890,6 +2890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28902890
fn suggest_valid_traits(
28912891
&self,
28922892
err: &mut DiagnosticBuilder<'_>,
2893+
item_name: Ident,
28932894
valid_out_of_scope_traits: Vec<DefId>,
28942895
explain: bool,
28952896
) -> bool {
@@ -2908,9 +2909,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29082909
err.help("items from traits can only be used if the trait is in scope");
29092910
}
29102911
let msg = format!(
2911-
"the following {traits_are} implemented but not in scope; \
2912-
perhaps add a `use` for {one_of_them}:",
2913-
traits_are = if candidates.len() == 1 { "trait is" } else { "traits are" },
2912+
"{this_trait_is} implemented but not in scope; perhaps you want to import \
2913+
{one_of_them}",
2914+
this_trait_is = if candidates.len() == 1 {
2915+
format!(
2916+
"trait `{}` which provides `{item_name}` is",
2917+
self.tcx.item_name(candidates[0]),
2918+
)
2919+
} else {
2920+
format!("the following traits which provide `{item_name}` are")
2921+
},
29142922
one_of_them = if candidates.len() == 1 { "it" } else { "one of them" },
29152923
);
29162924

@@ -3118,7 +3126,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31183126
}
31193127
}
31203128
}
3121-
if self.suggest_valid_traits(err, valid_out_of_scope_traits, true) {
3129+
if self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
31223130
return;
31233131
}
31243132

@@ -3404,7 +3412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34043412
[] => {}
34053413
[trait_info] if trait_info.def_id.is_local() => {
34063414
if impls_trait(trait_info.def_id) {
3407-
self.suggest_valid_traits(err, vec![trait_info.def_id], false);
3415+
self.suggest_valid_traits(err, item_name, vec![trait_info.def_id], false);
34083416
} else {
34093417
err.subdiagnostic(
34103418
self.dcx(),
@@ -3431,7 +3439,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34313439
));
34323440
for (i, trait_info) in trait_infos.iter().enumerate() {
34333441
if impls_trait(trait_info.def_id) {
3434-
self.suggest_valid_traits(err, vec![trait_info.def_id], false);
3442+
self.suggest_valid_traits(
3443+
err,
3444+
item_name,
3445+
vec![trait_info.def_id],
3446+
false,
3447+
);
34353448
}
34363449
msg.push_str(&format!(
34373450
"\ncandidate #{}: `{}`",

tests/ui/async-await/in-trait/dont-project-to-specializable-projection.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | match fut.as_mut().poll(ctx) {
3030
= note: the method is available for `Pin<&mut impl Future<Output = ()>>` here
3131
|
3232
= help: items from traits can only be used if the trait is in scope
33-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
33+
help: trait `Future` which provides `poll` is implemented but not in scope; perhaps you want to import it
3434
|
3535
LL + use std::future::Future;
3636
|

tests/ui/coherence/coherence_inherent.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
77
= help: items from traits can only be used if the trait is in scope
8-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
8+
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use Lib::TheTrait;
1111
|

tests/ui/coherence/coherence_inherent_cc.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
77
= help: items from traits can only be used if the trait is in scope
8-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
8+
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use coherence_inherent_cc_lib::TheTrait;
1111
|

tests/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LL | ().clone()
2626
help: there is a method `clone_from` with a similar name, but with different arguments
2727
--> $SRC_DIR/core/src/clone.rs:LL:COL
2828
= note: this error originates in the macro `::bar::m` (in Nightly builds, run with -Z macro-backtrace for more info)
29-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
29+
help: trait `Clone` which provides `clone` is implemented but not in scope; perhaps you want to import it
3030
|
3131
LL + use std::clone::Clone;
3232
|

tests/ui/hygiene/trait_items.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | pub macro m() { ().f() }
1212
|
1313
= help: items from traits can only be used if the trait is in scope
1414
= note: this error originates in the macro `::baz::m` (in Nightly builds, run with -Z macro-backtrace for more info)
15-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
15+
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1616
|
1717
LL + use foo::T;
1818
|

tests/ui/impl-trait/no-method-suggested-traits.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | 1u32.method();
55
| ^^^^^^
66
|
77
= help: items from traits can only be used if the trait is in scope
8-
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
8+
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
99
|
1010
LL + use foo::Bar;
1111
|
@@ -27,7 +27,7 @@ LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
2727
| ^^^^^^
2828
|
2929
= help: items from traits can only be used if the trait is in scope
30-
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
30+
help: the following traits which provide `method` are implemented but not in scope; perhaps you want to import one of them
3131
|
3232
LL + use foo::Bar;
3333
|
@@ -52,7 +52,7 @@ LL | 'a'.method();
5252
| ^^^^^^
5353
|
5454
= help: items from traits can only be used if the trait is in scope
55-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
55+
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
5656
|
5757
LL + use foo::Bar;
5858
|
@@ -68,7 +68,7 @@ LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
6868
| ^^^^^^
6969
|
7070
= help: items from traits can only be used if the trait is in scope
71-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
71+
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
7272
|
7373
LL + use foo::Bar;
7474
|
@@ -89,7 +89,7 @@ LL | fn method(&self) {}
8989
| ------ the method is available for `i32` here
9090
|
9191
= help: items from traits can only be used if the trait is in scope
92-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
92+
help: trait `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
9393
|
9494
LL + use no_method_suggested_traits::foo::PubPub;
9595
|
@@ -105,7 +105,7 @@ LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
105105
| ^^^^^^
106106
|
107107
= help: items from traits can only be used if the trait is in scope
108-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
108+
help: trait `PubPub` which provides `method` is implemented but not in scope; perhaps you want to import it
109109
|
110110
LL + use no_method_suggested_traits::foo::PubPub;
111111
|

tests/ui/imports/overlapping_pub_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This crate declares two public paths, `m::Tr` and `prelude::_`. Make sure we prefer the former.
55
*/
66
extern crate overlapping_pub_trait_source;
7-
//~^ HELP the following trait is implemented but not in scope; perhaps add a `use` for it:
7+
//~^ HELP trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
88
//~| SUGGESTION overlapping_pub_trait_source::m::Tr
99

1010
fn main() {

tests/ui/imports/overlapping_pub_trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | pub trait Tr { fn method(&self); }
1010
| ------ the method is available for `S` here
1111
|
1212
= help: items from traits can only be used if the trait is in scope
13-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
13+
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
1414
|
1515
LL + use overlapping_pub_trait_source::m::Tr;
1616
|

tests/ui/imports/unnamed_pub_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* importing it by name, and instead we suggest importing it by glob.
66
*/
77
extern crate unnamed_pub_trait_source;
8-
//~^ HELP the following trait is implemented but not in scope; perhaps add a `use` for it:
8+
//~^ HELP trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
99
//~| SUGGESTION unnamed_pub_trait_source::prelude::*; // trait Tr
1010

1111
fn main() {

0 commit comments

Comments
 (0)