Skip to content

Commit 397972f

Browse files
committed
Separate suggestion in a help and a note
1 parent e935acf commit 397972f

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
315315
let mut candidates = valid_out_of_scope_traits;
316316
candidates.sort();
317317
candidates.dedup();
318-
let mut msg = format!("items from traits can only be used if the trait is in scope; \
319-
the following {traits_are} implemented but not in scope, \
318+
err.help("items from traits can only be used if the trait is in scope");
319+
let mut msg = format!("the following {traits_are} implemented but not in scope, \
320320
perhaps add a `use` for {one_of_them}:",
321321
traits_are = if candidates.len() == 1 {
322322
"trait is"
@@ -338,7 +338,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
338338
if candidates.len() > limit {
339339
msg.push_str(&format!("\nand {} others", candidates.len() - limit));
340340
}
341-
err.help(&msg[..]);
341+
err.note(&msg[..]);
342342

343343
return;
344344
}
@@ -369,8 +369,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
369369
// FIXME #21673 this help message could be tuned to the case
370370
// of a type parameter: suggest adding a trait bound rather
371371
// than implementing.
372-
let mut msg = format!("items from traits can only be used if the trait is implemented \
373-
and in scope; the following {traits_define} an item `{name}`, \
372+
err.help("items from traits can only be used if the trait is implemented and in scope");
373+
let mut msg = format!("the following {traits_define} an item `{name}`, \
374374
perhaps you need to implement {one_of_them}:",
375375
traits_define = if candidates.len() == 1 {
376376
"trait defines"
@@ -389,7 +389,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
389389
i + 1,
390390
self.tcx.item_path_str(trait_info.def_id)));
391391
}
392-
err.help(&msg[..]);
392+
err.note(&msg[..]);
393393
}
394394
}
395395

src/test/compile-fail/method-call-err-msg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ fn main() {
3333
y.zero()
3434
.take() //~ ERROR no method named `take` found for type `Foo` in the current scope
3535
//~^ NOTE the method `take` exists but the following trait bounds were not satisfied
36+
//~| NOTE the following traits define an item `take`, perhaps you need to implement one of them
3637
.one(0);
3738
}

src/test/ui/impl-trait/issue-21659-show-relevant-trait-impls-3.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0599]: no method named `foo` found for type `Bar` in the current scope
44
30 | f1.foo(1usize);
55
| ^^^
66
|
7-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `foo`, perhaps you need to implement it:
7+
= help: items from traits can only be used if the trait is implemented and in scope
8+
= note: the following trait defines an item `foo`, perhaps you need to implement it:
89
candidate #1: `Foo`
910

1011
error: aborting due to previous error(s)

src/test/ui/impl-trait/method-suggestion-no-duplication.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0599]: no method named `is_empty` found for type `Foo` in the current sco
44
19 | foo(|s| s.is_empty());
55
| ^^^^^^^^
66
|
7-
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `is_empty`, perhaps you need to implement one of them:
7+
= help: items from traits can only be used if the trait is implemented and in scope
8+
= note: the following traits define an item `is_empty`, perhaps you need to implement one of them:
89
candidate #1: `std::iter::ExactSizeIterator`
910
candidate #2: `core::slice::SliceExt`
1011
candidate #3: `core::str::StrExt`

src/test/ui/impl-trait/no-method-suggested-traits.stderr

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0599]: no method named `method` found for type `u32` in the current scope
44
33 | 1u32.method();
55
| ^^^^^^
66
|
7-
= help: items from traits can only be used if the trait is in scope; the following traits are implemented but not in scope, perhaps add a `use` for one of them:
7+
= help: items from traits can only be used if the trait is in scope
8+
= note: the following traits are implemented but not in scope, perhaps add a `use` for one of them:
89
candidate #1: `use foo::Bar;`
910
candidate #2: `use no_method_suggested_traits::foo::PubPub;`
1011

@@ -14,7 +15,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
1415
38 | std::rc::Rc::new(&mut Box::new(&1u32)).method();
1516
| ^^^^^^
1617
|
17-
= help: items from traits can only be used if the trait is in scope; the following traits are implemented but not in scope, perhaps add a `use` for one of them:
18+
= help: items from traits can only be used if the trait is in scope
19+
= note: the following traits are implemented but not in scope, perhaps add a `use` for one of them:
1820
candidate #1: `use foo::Bar;`
1921
candidate #2: `use no_method_suggested_traits::foo::PubPub;`
2022

@@ -24,7 +26,8 @@ error[E0599]: no method named `method` found for type `char` in the current scop
2426
44 | 'a'.method();
2527
| ^^^^^^
2628
|
27-
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
29+
= help: items from traits can only be used if the trait is in scope
30+
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
2831
candidate #1: `use foo::Bar;`
2932

3033
error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&char>>` in the current scope
@@ -33,7 +36,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
3336
48 | std::rc::Rc::new(&mut Box::new(&'a')).method();
3437
| ^^^^^^
3538
|
36-
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
39+
= help: items from traits can only be used if the trait is in scope
40+
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
3741
candidate #1: `use foo::Bar;`
3842

3943
error[E0599]: no method named `method` found for type `i32` in the current scope
@@ -42,7 +46,8 @@ error[E0599]: no method named `method` found for type `i32` in the current scope
4246
53 | 1i32.method();
4347
| ^^^^^^
4448
|
45-
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
49+
= help: items from traits can only be used if the trait is in scope
50+
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
4651
candidate #1: `use no_method_suggested_traits::foo::PubPub;`
4752

4853
error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&i32>>` in the current scope
@@ -51,7 +56,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
5156
57 | std::rc::Rc::new(&mut Box::new(&1i32)).method();
5257
| ^^^^^^
5358
|
54-
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
59+
= help: items from traits can only be used if the trait is in scope
60+
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
5561
candidate #1: `use no_method_suggested_traits::foo::PubPub;`
5662

5763
error[E0599]: no method named `method` found for type `Foo` in the current scope
@@ -60,7 +66,8 @@ error[E0599]: no method named `method` found for type `Foo` in the current scope
6066
62 | Foo.method();
6167
| ^^^^^^
6268
|
63-
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `method`, perhaps you need to implement one of them:
69+
= help: items from traits can only be used if the trait is implemented and in scope
70+
= note: the following traits define an item `method`, perhaps you need to implement one of them:
6471
candidate #1: `foo::Bar`
6572
candidate #2: `no_method_suggested_traits::foo::PubPub`
6673
candidate #3: `no_method_suggested_traits::bar::PubPriv`
@@ -74,7 +81,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
7481
71 | std::rc::Rc::new(&mut Box::new(&Foo)).method();
7582
| ^^^^^^
7683
|
77-
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `method`, perhaps you need to implement one of them:
84+
= help: items from traits can only be used if the trait is implemented and in scope
85+
= note: the following traits define an item `method`, perhaps you need to implement one of them:
7886
candidate #1: `foo::Bar`
7987
candidate #2: `no_method_suggested_traits::foo::PubPub`
8088
candidate #3: `no_method_suggested_traits::bar::PubPriv`
@@ -88,7 +96,8 @@ error[E0599]: no method named `method2` found for type `u64` in the current scop
8896
81 | 1u64.method2();
8997
| ^^^^^^^
9098
|
91-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
99+
= help: items from traits can only be used if the trait is implemented and in scope
100+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
92101
candidate #1: `foo::Bar`
93102

94103
error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::boxed::Box<&u64>>` in the current scope
@@ -97,7 +106,8 @@ error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::bo
97106
85 | std::rc::Rc::new(&mut Box::new(&1u64)).method2();
98107
| ^^^^^^^
99108
|
100-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
109+
= help: items from traits can only be used if the trait is implemented and in scope
110+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
101111
candidate #1: `foo::Bar`
102112

103113
error[E0599]: no method named `method2` found for type `no_method_suggested_traits::Foo` in the current scope
@@ -106,7 +116,8 @@ error[E0599]: no method named `method2` found for type `no_method_suggested_trai
106116
90 | no_method_suggested_traits::Foo.method2();
107117
| ^^^^^^^
108118
|
109-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
119+
= help: items from traits can only be used if the trait is implemented and in scope
120+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
110121
candidate #1: `foo::Bar`
111122

112123
error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Foo>>` in the current scope
@@ -115,7 +126,8 @@ error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::bo
115126
94 | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method2();
116127
| ^^^^^^^
117128
|
118-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
129+
= help: items from traits can only be used if the trait is implemented and in scope
130+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
119131
candidate #1: `foo::Bar`
120132

121133
error[E0599]: no method named `method2` found for type `no_method_suggested_traits::Bar` in the current scope
@@ -124,7 +136,8 @@ error[E0599]: no method named `method2` found for type `no_method_suggested_trai
124136
98 | no_method_suggested_traits::Bar::X.method2();
125137
| ^^^^^^^
126138
|
127-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
139+
= help: items from traits can only be used if the trait is implemented and in scope
140+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
128141
candidate #1: `foo::Bar`
129142

130143
error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::boxed::Box<&no_method_suggested_traits::Bar>>` in the current scope
@@ -133,7 +146,8 @@ error[E0599]: no method named `method2` found for type `std::rc::Rc<&mut std::bo
133146
102 | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method2();
134147
| ^^^^^^^
135148
|
136-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method2`, perhaps you need to implement it:
149+
= help: items from traits can only be used if the trait is implemented and in scope
150+
= note: the following trait defines an item `method2`, perhaps you need to implement it:
137151
candidate #1: `foo::Bar`
138152

139153
error[E0599]: no method named `method3` found for type `Foo` in the current scope
@@ -142,7 +156,8 @@ error[E0599]: no method named `method3` found for type `Foo` in the current scop
142156
107 | Foo.method3();
143157
| ^^^^^^^
144158
|
145-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method3`, perhaps you need to implement it:
159+
= help: items from traits can only be used if the trait is implemented and in scope
160+
= note: the following trait defines an item `method3`, perhaps you need to implement it:
146161
candidate #1: `no_method_suggested_traits::foo::PubPub`
147162

148163
error[E0599]: no method named `method3` found for type `std::rc::Rc<&mut std::boxed::Box<&Foo>>` in the current scope
@@ -151,7 +166,8 @@ error[E0599]: no method named `method3` found for type `std::rc::Rc<&mut std::bo
151166
111 | std::rc::Rc::new(&mut Box::new(&Foo)).method3();
152167
| ^^^^^^^
153168
|
154-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method3`, perhaps you need to implement it:
169+
= help: items from traits can only be used if the trait is implemented and in scope
170+
= note: the following trait defines an item `method3`, perhaps you need to implement it:
155171
candidate #1: `no_method_suggested_traits::foo::PubPub`
156172

157173
error[E0599]: no method named `method3` found for type `Bar` in the current scope
@@ -160,7 +176,8 @@ error[E0599]: no method named `method3` found for type `Bar` in the current scop
160176
115 | Bar::X.method3();
161177
| ^^^^^^^
162178
|
163-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method3`, perhaps you need to implement it:
179+
= help: items from traits can only be used if the trait is implemented and in scope
180+
= note: the following trait defines an item `method3`, perhaps you need to implement it:
164181
candidate #1: `no_method_suggested_traits::foo::PubPub`
165182

166183
error[E0599]: no method named `method3` found for type `std::rc::Rc<&mut std::boxed::Box<&Bar>>` in the current scope
@@ -169,7 +186,8 @@ error[E0599]: no method named `method3` found for type `std::rc::Rc<&mut std::bo
169186
119 | std::rc::Rc::new(&mut Box::new(&Bar::X)).method3();
170187
| ^^^^^^^
171188
|
172-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `method3`, perhaps you need to implement it:
189+
= help: items from traits can only be used if the trait is implemented and in scope
190+
= note: the following trait defines an item `method3`, perhaps you need to implement it:
173191
candidate #1: `no_method_suggested_traits::foo::PubPub`
174192

175193
error[E0599]: no method named `method3` found for type `usize` in the current scope

src/test/ui/span/issue-7575.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ note: candidate #3 is defined in the trait `UnusedTrait`
2323
29 | fn f9(usize) -> usize; //~ NOTE candidate
2424
| ^^^^^^^^^^^^^^^^^^^^^^
2525
= help: to disambiguate the method call, write `UnusedTrait::f9(u, 342)` instead
26-
= help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `f9`, perhaps you need to implement one of them:
26+
= help: items from traits can only be used if the trait is implemented and in scope
27+
= note: the following traits define an item `f9`, perhaps you need to implement one of them:
2728
candidate #1: `CtxtFn`
2829
candidate #2: `OtherTrait`
2930
candidate #3: `UnusedTrait`
@@ -58,7 +59,8 @@ note: candidate #1 is defined in the trait `ManyImplTrait`
5859
59 | | }
5960
| |_____^
6061
= help: to disambiguate the method call, write `ManyImplTrait::is_str(t)` instead
61-
= help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `is_str`, perhaps you need to implement it:
62+
= help: items from traits can only be used if the trait is implemented and in scope
63+
= note: the following trait defines an item `is_str`, perhaps you need to implement it:
6264
candidate #1: `ManyImplTrait`
6365

6466
error: aborting due to previous error(s)

0 commit comments

Comments
 (0)