Skip to content

Commit 9460f64

Browse files
Rollup merge of #141512 - Noratrieb:stop-trimming-this-much, r=compiler-errors
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.
2 parents c747b7d + 01503d0 commit 9460f64

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<{[email protected]: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, {[email protected]: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>}>, {[email protected]: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

tests/ui/issues/issue-30123.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `new_undirected` found for st
22
--> $DIR/issue-30123.rs:7:33
33
|
44
LL | let ug = Graph::<i32, i32>::new_undirected();
5-
| ^^^^^^^^^^^^^^ function or associated item not found in `Graph<i32, i32>`
5+
| ^^^^^^^^^^^^^^ function or associated item not found in `issue_30123_aux::Graph<i32, i32>`
66
|
77
note: if you're trying to build a new `issue_30123_aux::Graph<i32, i32>`, consider using `issue_30123_aux::Graph::<N, E>::new` which returns `issue_30123_aux::Graph<_, _>`
88
--> $DIR/auxiliary/issue-30123-aux.rs:14:5

tests/ui/issues/issue-41880.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | pub struct Iterate<T, F> {
55
| ------------------------ method `iter` not found for this struct
66
...
77
LL | println!("{:?}", a.iter().take(10).collect::<Vec<usize>>());
8-
| ^^^^ method not found in `Iterate<{integer}, {[email protected]:26:24}>`
8+
| ^^^^ method not found in `Iterate<{integer}, {closure@$DIR/issue-41880.rs:26:24: 26:27}>`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/macros/missing-writer.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ error[E0599]: cannot write into `&'static str`
2424
--> $DIR/missing-writer.rs:5:12
2525
|
2626
LL | write!("{}_{}", x, y);
27-
| -------^^^^^^^------- method not found in `&str`
27+
| -------^^^^^^^------- method not found in `&'static str`
2828
|
2929
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
3030
--> $DIR/missing-writer.rs:5:12
@@ -41,7 +41,7 @@ error[E0599]: cannot write into `&'static str`
4141
--> $DIR/missing-writer.rs:11:14
4242
|
4343
LL | writeln!("{}_{}", x, y);
44-
| ---------^^^^^^^------- method not found in `&str`
44+
| ---------^^^^^^^------- method not found in `&'static str`
4545
|
4646
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
4747
--> $DIR/missing-writer.rs:11:14

tests/ui/methods/issue-19521.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `homura` found for reference `&'static str` in the
22
--> $DIR/issue-19521.rs:2:8
33
|
44
LL | "".homura()();
5-
| ^^^^^^ method not found in `&str`
5+
| ^^^^^^ method not found in `&'static str`
66

77
error: aborting due to 1 previous error
88

tests/ui/methods/method-not-found-generic-arg-elision.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error[E0599]: no method named `extend` found for struct `Map` in the current sco
2323
--> $DIR/method-not-found-generic-arg-elision.rs:87:67
2424
|
2525
LL | v.iter().map(Box::new(|x| x * x) as Box<dyn Fn(&i32) -> i32>).extend(std::iter::once(100));
26-
| ^^^^^^ method not found in `Map<Iter<'_, i32>, Box<dyn Fn(&i32) -> i32>>`
26+
| ^^^^^^ method not found in `Map<std::slice::Iter<'_, i32>, Box<dyn for<'a> Fn(&'a i32) -> i32>>`
2727

2828
error[E0599]: no method named `method` found for struct `Wrapper<bool>` in the current scope
2929
--> $DIR/method-not-found-generic-arg-elision.rs:90:13

tests/ui/methods/receiver-equality.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct B<T>(T);
55
| ----------- function or associated item `method` not found for this struct
66
...
77
LL | B::<for<'a> fn(&'a ())>::method(y);
8-
| ^^^^^^ function or associated item not found in `B<fn(&())>`
8+
| ^^^^^^ function or associated item not found in `B<for<'a> fn(&'a ())>`
99

1010
error: aborting due to 1 previous error
1111

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Ensures that the path of the `Error` type is not trimmed
2+
// to make it clear which Error type is meant.
3+
4+
fn main() {
5+
meow().unknown(); //~ ERROR no method named `unknown` found
6+
//~^ NOTE method not found in `Result<(), std::io::Error>`
7+
}
8+
9+
fn meow() -> Result<(), std::io::Error> {
10+
Ok(())
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0599]: no method named `unknown` found for enum `Result` in the current scope
2+
--> $DIR/untrimmed-path-type.rs:5:11
3+
|
4+
LL | meow().unknown();
5+
| ^^^^^^^ method not found in `Result<(), std::io::Error>`
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0599`.

tests/ui/mismatched_types/issue-36053-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ help: consider adjusting the signature so it borrows its argument
1515
LL | once::<&str>("str").fuse().filter(|a: &&str| true).count();
1616
| +
1717

18-
error[E0599]: the method `count` exists for struct `Filter<Fuse<Once<&str>>, {[email protected]:7:39}>`, but its trait bounds were not satisfied
18+
error[E0599]: the method `count` exists for struct `Filter<Fuse<std::iter::Once<&str>>, {closure@$DIR/issue-36053-2.rs:7:39: 7:48}>`, but its trait bounds were not satisfied
1919
--> $DIR/issue-36053-2.rs:7:55
2020
|
2121
LL | once::<&str>("str").fuse().filter(|a: &str| true).count();

tests/ui/nll/issue-57362-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `make_g` found for fn pointer
22
--> $DIR/issue-57362-2.rs:23:25
33
|
44
LL | let x = <fn (&())>::make_g();
5-
| ^^^^^^ function or associated item not found in `fn(&())`
5+
| ^^^^^^ function or associated item not found in `for<'a> fn(&'a ())`
66
|
77
= help: items from traits can only be used if the trait is implemented and in scope
88
note: `X` defines an item `make_g`, perhaps you need to implement it

tests/ui/nll/issue-57642-higher-ranked-subtype.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `make_g` found for fn pointer
22
--> $DIR/issue-57642-higher-ranked-subtype.rs:31:25
33
|
44
LL | let x = <fn (&())>::make_g();
5-
| ^^^^^^ function or associated item not found in `fn(&())`
5+
| ^^^^^^ function or associated item not found in `for<'a> fn(&'a ())`
66
|
77
= help: items from traits can only be used if the trait is implemented and in scope
88
note: `X` defines an item `make_g`, perhaps you need to implement it
@@ -15,7 +15,7 @@ error[E0599]: no function or associated item named `make_f` found for fn pointer
1515
--> $DIR/issue-57642-higher-ranked-subtype.rs:36:25
1616
|
1717
LL | let x = <fn (&())>::make_f();
18-
| ^^^^^^ function or associated item not found in `fn(&())`
18+
| ^^^^^^ function or associated item not found in `for<'a> fn(&'a ())`
1919
|
2020
= help: items from traits can only be used if the trait is implemented and in scope
2121
note: `Y` defines an item `make_f`, perhaps you need to implement it

0 commit comments

Comments
 (0)