Skip to content

Commit 01503d0

Browse files
committed
Avoid extra path trimming in method not found error
Method errors have an extra check that force trim paths whenever the normal string is longer than 10 characters, which can be quite unhelpful when multiple items have the same name (for example an `Error`). A user reported this force trimming as being quite unhelpful when they had a method error where the precise path of the `Error` mattered. The code uses `tcx.short_string` already to get the normal path, which tries to be clever around trimming paths if necessary, so there is no reason for this extra force trimming.
1 parent e88e854 commit 01503d0

26 files changed

+63
-47
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
599599
let tcx = self.tcx;
600600
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
601601
let mut ty_file = None;
602-
let (mut ty_str, short_ty_str) =
602+
let (ty_str, short_ty_str) =
603603
if trait_missing_method && let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
604604
(predicates.to_string(), with_forced_trimmed_paths!(predicates.to_string()))
605605
} else {
@@ -738,10 +738,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
738738
err.span_label(within_macro_span, "due to this macro variable");
739739
}
740740

741-
if short_ty_str.len() < ty_str.len() && ty_str.len() > 10 {
742-
ty_str = short_ty_str;
743-
}
744-
745741
if rcvr_ty.references_error() {
746742
err.downgrade_to_delayed_bug();
747743
}

tests/run-make/crate-loading/multiple-dep-versions.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ error[E0599]: no method named `foo` found for struct `dep_2_reexport::Type` in t
3939
--> replaced
4040
|
4141
LL | Type.foo();
42-
| ^^^ method not found in `Type`
42+
| ^^^ method not found in `dep_2_reexport::Type`
4343
|
4444
note: there are multiple different versions of crate `dependency` in the dependency graph
4545
--> replaced
@@ -63,7 +63,7 @@ error[E0599]: no function or associated item named `bar` found for struct `dep_2
6363
--> replaced
6464
|
6565
LL | Type::bar();
66-
| ^^^ function or associated item not found in `Type`
66+
| ^^^ function or associated item not found in `dep_2_reexport::Type`
6767
|
6868
note: there are multiple different versions of crate `dependency` in the dependency graph
6969
--> replaced

tests/ui/associated-types/issue-43924.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0599]: no function or associated item named `default` found for trait obj
1414
--> $DIR/issue-43924.rs:14:39
1515
|
1616
LL | assert_eq!(<() as Foo<u32>>::Out::default().to_string(), "false");
17-
| ^^^^^^^ function or associated item not found in `dyn ToString`
17+
| ^^^^^^^ function or associated item not found in `(dyn ToString + 'static)`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/attributes/rustc_confusables.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ error[E0599]: no method named `foo` found for struct `rustc_confusables_across_c
4242
--> $DIR/rustc_confusables.rs:15:7
4343
|
4444
LL | x.foo();
45-
| ^^^ method not found in `BTreeSet`
45+
| ^^^ method not found in `rustc_confusables_across_crate::BTreeSet`
4646

4747
error[E0599]: no method named `push` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope
4848
--> $DIR/rustc_confusables.rs:17:7
4949
|
5050
LL | x.push();
51-
| ^^^^ method not found in `BTreeSet`
51+
| ^^^^ method not found in `rustc_confusables_across_crate::BTreeSet`
5252
|
5353
help: you might have meant to use `insert`
5454
|
@@ -60,7 +60,7 @@ error[E0599]: no method named `test` found for struct `rustc_confusables_across_
6060
--> $DIR/rustc_confusables.rs:20:7
6161
|
6262
LL | x.test();
63-
| ^^^^ method not found in `BTreeSet`
63+
| ^^^^ method not found in `rustc_confusables_across_crate::BTreeSet`
6464

6565
error[E0599]: no method named `pulled` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope
6666
--> $DIR/rustc_confusables.rs:22:7

tests/ui/empty/empty-struct-braces-expr.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ error[E0599]: no variant or associated item named `Empty3` found for enum `empty
121121
--> $DIR/empty-struct-braces-expr.rs:25:19
122122
|
123123
LL | let xe3 = XE::Empty3;
124-
| ^^^^^^ variant or associated item not found in `XE`
124+
| ^^^^^^ variant or associated item not found in `empty_struct::XE`
125125
|
126126
help: there is a variant with a similar name
127127
|
@@ -132,7 +132,7 @@ error[E0599]: no variant or associated item named `Empty3` found for enum `empty
132132
--> $DIR/empty-struct-braces-expr.rs:26:19
133133
|
134134
LL | let xe3 = XE::Empty3();
135-
| ^^^^^^ variant or associated item not found in `XE`
135+
| ^^^^^^ variant or associated item not found in `empty_struct::XE`
136136
|
137137
help: there is a variant with a similar name
138138
|

tests/ui/error-emitter/multiline-removal-suggestion.svg

Lines changed: 5 additions & 5 deletions
Loading

tests/ui/functions-closures/fn-help-with-err.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0599]: no method named `bar` found for struct `Arc<{closure@$DIR/fn-help-
88
--> $DIR/fn-help-with-err.rs:19:10
99
|
1010
LL | arc2.bar();
11-
| ^^^ method not found in `Arc<{closure@fn-help-with-err.rs:18:36}>`
11+
| ^^^ method not found in `Arc<{closure@$DIR/fn-help-with-err.rs:18:36: 18:38}>`
1212
|
1313
= help: items from traits can only be used if the trait is implemented and in scope
1414
note: `Bar` defines an item `bar`, perhaps you need to implement it

tests/ui/higher-ranked/trait-bounds/hrtb-doesnt-borrow-self-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0599]: the method `filterx` exists for struct `Map<Repeat, {closure@hrtb-doesnt-borrow-self-1.rs:113:27}>`, but its trait bounds were not satisfied
1+
error[E0599]: the method `filterx` exists for struct `Map<Repeat, {closure@$DIR/hrtb-doesnt-borrow-self-1.rs:113:27: 113:34}>`, but its trait bounds were not satisfied
22
--> $DIR/hrtb-doesnt-borrow-self-1.rs:114:22
33
|
44
LL | pub struct Map<S, F> {

tests/ui/higher-ranked/trait-bounds/hrtb-doesnt-borrow-self-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0599]: the method `countx` exists for struct `Filter<Map<Repeat, fn(&u64) -> &u64 {identity::<u64>}>, {closure@hrtb-doesnt-borrow-self-2.rs:109:30}>`, but its trait bounds were not satisfied
1+
error[E0599]: the method `countx` exists for struct `Filter<Map<Repeat, for<'a> fn(&'a u64) -> &'a u64 {identity::<u64>}>, {closure@$DIR/hrtb-doesnt-borrow-self-2.rs:109:30: 109:37}>`, but its trait bounds were not satisfied
22
--> $DIR/hrtb-doesnt-borrow-self-2.rs:110:24
33
|
44
LL | pub struct Filter<S, F> {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ error[E0599]: no method named `method2` found for struct `no_method_suggested_tr
173173
--> $DIR/no-method-suggested-traits.rs:52:37
174174
|
175175
LL | no_method_suggested_traits::Foo.method2();
176-
| ^^^^^^^ method not found in `Foo`
176+
| ^^^^^^^ method not found in `no_method_suggested_traits::Foo`
177177
|
178178
= help: items from traits can only be used if the trait is implemented and in scope
179179
note: `foo::Bar` defines an item `method2`, perhaps you need to implement it
@@ -186,7 +186,7 @@ error[E0599]: no method named `method2` found for struct `Rc<&mut Box<&no_method
186186
--> $DIR/no-method-suggested-traits.rs:54:71
187187
|
188188
LL | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method2();
189-
| ^^^^^^^ method not found in `Rc<&mut Box<&Foo>>`
189+
| ^^^^^^^ method not found in `Rc<&mut Box<&no_method_suggested_traits::Foo>>`
190190
|
191191
= help: items from traits can only be used if the trait is implemented and in scope
192192
note: `foo::Bar` defines an item `method2`, perhaps you need to implement it
@@ -199,7 +199,7 @@ error[E0599]: no method named `method2` found for enum `no_method_suggested_trai
199199
--> $DIR/no-method-suggested-traits.rs:56:40
200200
|
201201
LL | no_method_suggested_traits::Bar::X.method2();
202-
| ^^^^^^^ method not found in `Bar`
202+
| ^^^^^^^ method not found in `no_method_suggested_traits::Bar`
203203
|
204204
= help: items from traits can only be used if the trait is implemented and in scope
205205
note: `foo::Bar` defines an item `method2`, perhaps you need to implement it
@@ -212,7 +212,7 @@ error[E0599]: no method named `method2` found for struct `Rc<&mut Box<&no_method
212212
--> $DIR/no-method-suggested-traits.rs:58:74
213213
|
214214
LL | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method2();
215-
| ^^^^^^^ method not found in `Rc<&mut Box<&Bar>>`
215+
| ^^^^^^^ method not found in `Rc<&mut Box<&no_method_suggested_traits::Bar>>`
216216
|
217217
= help: items from traits can only be used if the trait is implemented and in scope
218218
note: `foo::Bar` defines an item `method2`, perhaps you need to implement it
@@ -283,25 +283,25 @@ error[E0599]: no method named `method3` found for struct `no_method_suggested_tr
283283
--> $DIR/no-method-suggested-traits.rs:73:37
284284
|
285285
LL | no_method_suggested_traits::Foo.method3();
286-
| ^^^^^^^ method not found in `Foo`
286+
| ^^^^^^^ method not found in `no_method_suggested_traits::Foo`
287287

288288
error[E0599]: no method named `method3` found for struct `Rc<&mut Box<&no_method_suggested_traits::Foo>>` in the current scope
289289
--> $DIR/no-method-suggested-traits.rs:74:71
290290
|
291291
LL | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method3();
292-
| ^^^^^^^ method not found in `Rc<&mut Box<&Foo>>`
292+
| ^^^^^^^ method not found in `Rc<&mut Box<&no_method_suggested_traits::Foo>>`
293293

294294
error[E0599]: no method named `method3` found for enum `no_method_suggested_traits::Bar` in the current scope
295295
--> $DIR/no-method-suggested-traits.rs:76:40
296296
|
297297
LL | no_method_suggested_traits::Bar::X.method3();
298-
| ^^^^^^^ method not found in `Bar`
298+
| ^^^^^^^ method not found in `no_method_suggested_traits::Bar`
299299

300300
error[E0599]: no method named `method3` found for struct `Rc<&mut Box<&no_method_suggested_traits::Bar>>` in the current scope
301301
--> $DIR/no-method-suggested-traits.rs:77:74
302302
|
303303
LL | std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method3();
304-
| ^^^^^^^ method not found in `Rc<&mut Box<&Bar>>`
304+
| ^^^^^^^ method not found in `Rc<&mut Box<&no_method_suggested_traits::Bar>>`
305305

306306
error: aborting due to 24 previous errors
307307

0 commit comments

Comments
 (0)