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

+20-7
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+6-6
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

+1-1
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

+1-1
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

+1-1
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() {

tests/ui/imports/unnamed_pub_trait.stderr

+1-1
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 unnamed_pub_trait_source::prelude::*; // trait Tr
1616
|

tests/ui/issues/issue-10465.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | b.foo();
55
| ^^^ method not found in `&B`
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 `A` which provides `foo` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use a::A;
1111
|

tests/ui/issues/issue-39175.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | Command::new("echo").arg("hello").exec();
77
= help: items from traits can only be used if the trait is in scope
88
help: there is a method `pre_exec` with a similar name, but with different arguments
99
--> $SRC_DIR/std/src/os/unix/process.rs:LL:COL
10-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
10+
help: trait `CommandExt` which provides `exec` is implemented but not in scope; perhaps you want to import it
1111
|
1212
LL + use std::os::unix::process::CommandExt;
1313
|

tests/ui/issues/issue-56175.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn trait_method(&self) {
1010
| ------------ the method is available for `FooStruct` 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 `Trait` which provides `trait_method` is implemented but not in scope; perhaps you want to import it
1414
|
1515
LL + use reexported_trait::Trait;
1616
|
@@ -31,7 +31,7 @@ LL | fn trait_method_b(&self) {
3131
| -------------- the method is available for `FooStruct` here
3232
|
3333
= help: items from traits can only be used if the trait is in scope
34-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
34+
help: trait `TraitB` which provides `trait_method_b` is implemented but not in scope; perhaps you want to import it
3535
|
3636
LL + use reexported_trait::TraitBRename;
3737
|

tests/ui/rust-2018/trait-import-suggestions.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | x.foobar();
88
| ^^^^^^
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `Foobar` which provides `foobar` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use crate::foo::foobar::Foobar;
1414
|
@@ -27,7 +27,7 @@ LL | x.bar();
2727
| ^^^
2828
|
2929
= help: items from traits can only be used if the trait is in scope
30-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
30+
help: trait `Bar` which provides `bar` is implemented but not in scope; perhaps you want to import it
3131
|
3232
LL + use crate::foo::Bar;
3333
|
@@ -54,7 +54,7 @@ LL | let y = u32::from_str("33");
5454
| ^^^^^^^^ function or associated item not found in `u32`
5555
|
5656
= help: items from traits can only be used if the trait is in scope
57-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
57+
help: trait `FromStr` which provides `from_str` is implemented but not in scope; perhaps you want to import it
5858
|
5959
LL + use std::str::FromStr;
6060
|

tests/ui/rust-2018/uniform-paths/issue-87932.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | A::deserialize();
88
| ^^^^^^^^^^^ function or associated item not found in `A`
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `Deserialize` which provides `deserialize` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use <crate::A as issue_87932_a::Deserialize>::deserialize::_a::Deserialize;
1414
|

tests/ui/rust-2021/future-prelude-collision-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | let _: u32 = 3u8.try_into().unwrap();
66
|
77
= help: items from traits can only be used if the trait is in scope
88
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
9-
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
9+
help: the following traits which provide `try_into` are implemented but not in scope; perhaps you want to import one of them
1010
|
1111
LL + use crate::m::TryIntoU32;
1212
|

tests/ui/shadowed/shadowed-trait-methods.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | ().f()
88
| ^ method not found in `()`
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use foo::T;
1414
|

tests/ui/suggestions/dont-wrap-ambiguous-receivers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod banana {
2-
//~^ HELP the following traits are implemented but not in scope
2+
//~^ HELP the following traits which provide `pick` are implemented but not in scope
33
pub struct Chaenomeles;
44

55
pub trait Apple {

tests/ui/suggestions/dont-wrap-ambiguous-receivers.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | banana::Chaenomeles.pick()
88
| ^^^^ method not found in `Chaenomeles`
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
11+
help: the following traits which provide `pick` are implemented but not in scope; perhaps you want to import one of them
1212
|
1313
LL + use banana::Apple;
1414
|

tests/ui/suggestions/import-trait-for-method-call.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | h.finish()
88
= note: the method is available for `DefaultHasher` here
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `Hasher` which provides `finish` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use std::hash::Hasher;
1414
|

tests/ui/suggestions/suggest-tryinto-edition-change.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ LL | let _i: i16 = 0_i32.try_into().unwrap();
5858
|
5959
= help: items from traits can only be used if the trait is in scope
6060
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
61-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
61+
help: trait `TryInto` which provides `try_into` is implemented but not in scope; perhaps you want to import it
6262
|
6363
LL + use std::convert::TryInto;
6464
|

tests/ui/suggestions/use-placement-typeck.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | pub struct S;
1111
| ------------ method `abc` not found for this struct
1212
|
1313
= help: items from traits can only be used if the trait is in scope
14-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
14+
help: trait `Foo` which provides `abc` is implemented but not in scope; perhaps you want to import it
1515
|
1616
LL + use m::Foo;
1717
|

tests/ui/traits/item-privacy.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | S.a();
88
| ^
99
|
1010
= help: items from traits can only be used if the trait is implemented and in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `A` which provides `a` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use method::A;
1414
|
@@ -30,7 +30,7 @@ LL | S.b();
3030
| ^
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 `B` which provides `b` is implemented but not in scope; perhaps you want to import it
3434
|
3535
LL + use method::B;
3636
|
@@ -63,7 +63,7 @@ help: there is an associated constant `B` with a similar name
6363
|
6464
LL | const B: u8 = 0;
6565
| ^^^^^^^^^^^
66-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
66+
help: trait `A` which provides `a` is implemented but not in scope; perhaps you want to import it
6767
|
6868
LL + use method::A;
6969
|
@@ -83,7 +83,7 @@ help: there is an associated constant `B` with a similar name
8383
|
8484
LL | const B: u8 = 0;
8585
| ^^^^^^^^^^^
86-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
86+
help: trait `B` which provides `b` is implemented but not in scope; perhaps you want to import it
8787
|
8888
LL + use method::B;
8989
|
@@ -107,7 +107,7 @@ LL | S::A;
107107
| ^ associated item not found in `S`
108108
|
109109
= help: items from traits can only be used if the trait is implemented and in scope
110-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
110+
help: trait `A` which provides `A` is implemented but not in scope; perhaps you want to import it
111111
|
112112
LL + use assoc_const::A;
113113
|
@@ -126,7 +126,7 @@ LL | S::B;
126126
| ^ associated item not found in `S`
127127
|
128128
= help: items from traits can only be used if the trait is in scope
129-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
129+
help: trait `B` which provides `B` is implemented but not in scope; perhaps you want to import it
130130
|
131131
LL + use assoc_const::B;
132132
|

tests/ui/traits/method-private.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | foo.method();
88
| ^^^^^^ private method
99
|
1010
= help: items from traits can only be used if the trait is in scope
11-
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
11+
help: trait `Bar` which provides `method` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use inner::Bar;
1414
|

0 commit comments

Comments
 (0)