Skip to content

Commit d697d63

Browse files
committed
Do not suggest path::to<impl Trait for Type>::method
In the pretty-printer, we have a weird way to display fully-qualified for non-local impls, where we show a regular path, but the section corresponding to the `<Type as Trait>` we use non-syntax for it like `path::to<impl Trait for Type>`. It should be `<Type for path::to::Trait>`, but this is only better when we are printing code to be suggested, not to find where the `impl` actually is, so we add a new flag to the printer for this.
1 parent c40d63c commit d697d63

File tree

4 files changed

+59
-49
lines changed

4 files changed

+59
-49
lines changed

compiler/rustc_middle/src/ty/print/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub trait Printer<'tcx>: Sized {
227227
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == parent_def_id,
228228
};
229229

230-
if !in_self_mod && !in_trait_mod {
230+
if !in_self_mod && !in_trait_mod && !self.for_suggestion() {
231231
// If the impl is not co-located with either self-type or
232232
// trait-type, then fallback to a format that identifies
233233
// the module more clearly.
@@ -243,6 +243,10 @@ pub trait Printer<'tcx>: Sized {
243243
self.path_qualified(self_ty, impl_trait_ref)
244244
}
245245
}
246+
247+
fn for_suggestion(&self) -> bool {
248+
false
249+
}
246250
}
247251

248252
/// As a heuristic, when we see an impl, if we see that the

compiler/rustc_middle/src/ty/print/pretty.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,7 @@ pub struct FmtPrinterData<'a, 'tcx> {
20102010

20112011
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<Symbol> + 'a>>,
20122012
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid) -> Option<Symbol> + 'a>>,
2013+
pub for_suggestion: bool,
20132014
}
20142015

20152016
impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
@@ -2060,6 +2061,7 @@ impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
20602061
region_highlight_mode: RegionHighlightMode::default(),
20612062
ty_infer_name_resolver: None,
20622063
const_infer_name_resolver: None,
2064+
for_suggestion: false,
20632065
}))
20642066
}
20652067

@@ -2311,6 +2313,9 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
23112313
Ok(())
23122314
}
23132315
}
2316+
fn for_suggestion(&self) -> bool {
2317+
self.0.for_suggestion
2318+
}
23142319
}
23152320

23162321
impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
205205
}
206206
};
207207
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
208+
printer.for_suggestion = true;
208209
let const_getter =
209210
move |ct_vid| Some(infcx.tcx.item_name(infcx.const_var_origin(ct_vid)?.param_def_id?));
210211
printer.const_infer_name_resolver = Some(Box::new(const_getter));

tests/ui/inference/issue-72690.stderr

+48-48
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ LL | String::from("x".as_ref());
2121
- impl AsRef<str> for str;
2222
help: try using a fully qualified path to specify the expected types
2323
|
24-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
25-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
24+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
25+
| ++++++++++++++++++++++++++++++ ~
2626
help: try using a fully qualified path to specify the expected types
2727
|
28-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
29-
| +++++++++++++++++++++++++++++++++++++++++ ~
28+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
29+
| +++++++++++++++++++++++++++++ ~
3030
help: try using a fully qualified path to specify the expected types
3131
|
3232
LL | String::from(<str as AsRef<str>>::as_ref("x"));
3333
| ++++++++++++++++++++++++++++ ~
3434
help: try using a fully qualified path to specify the expected types
3535
|
36-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
37-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
36+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
37+
| +++++++++++++++++++++++++++++ ~
3838

3939
error[E0283]: type annotations needed
4040
--> $DIR/issue-72690.rs:12:9
@@ -59,20 +59,20 @@ LL | |x| String::from("x".as_ref());
5959
- impl AsRef<str> for str;
6060
help: try using a fully qualified path to specify the expected types
6161
|
62-
LL | |x| String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
63-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
62+
LL | |x| String::from(<str as AsRef<OsStr>>::as_ref("x"));
63+
| ++++++++++++++++++++++++++++++ ~
6464
help: try using a fully qualified path to specify the expected types
6565
|
66-
LL | |x| String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
67-
| +++++++++++++++++++++++++++++++++++++++++ ~
66+
LL | |x| String::from(<str as AsRef<Path>>::as_ref("x"));
67+
| +++++++++++++++++++++++++++++ ~
6868
help: try using a fully qualified path to specify the expected types
6969
|
7070
LL | |x| String::from(<str as AsRef<str>>::as_ref("x"));
7171
| ++++++++++++++++++++++++++++ ~
7272
help: try using a fully qualified path to specify the expected types
7373
|
74-
LL | |x| String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
75-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
74+
LL | |x| String::from(<str as AsRef<[u8]>>::as_ref("x"));
75+
| +++++++++++++++++++++++++++++ ~
7676

7777
error[E0283]: type annotations needed for `&_`
7878
--> $DIR/issue-72690.rs:17:9
@@ -113,20 +113,20 @@ LL | String::from("x".as_ref());
113113
- impl AsRef<str> for str;
114114
help: try using a fully qualified path to specify the expected types
115115
|
116-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
117-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
116+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
117+
| ++++++++++++++++++++++++++++++ ~
118118
help: try using a fully qualified path to specify the expected types
119119
|
120-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
121-
| +++++++++++++++++++++++++++++++++++++++++ ~
120+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
121+
| +++++++++++++++++++++++++++++ ~
122122
help: try using a fully qualified path to specify the expected types
123123
|
124124
LL | String::from(<str as AsRef<str>>::as_ref("x"));
125125
| ++++++++++++++++++++++++++++ ~
126126
help: try using a fully qualified path to specify the expected types
127127
|
128-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
129-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
128+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
129+
| +++++++++++++++++++++++++++++ ~
130130

131131
error[E0283]: type annotations needed
132132
--> $DIR/issue-72690.rs:28:5
@@ -151,20 +151,20 @@ LL | String::from("x".as_ref());
151151
- impl AsRef<str> for str;
152152
help: try using a fully qualified path to specify the expected types
153153
|
154-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
155-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
154+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
155+
| ++++++++++++++++++++++++++++++ ~
156156
help: try using a fully qualified path to specify the expected types
157157
|
158-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
159-
| +++++++++++++++++++++++++++++++++++++++++ ~
158+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
159+
| +++++++++++++++++++++++++++++ ~
160160
help: try using a fully qualified path to specify the expected types
161161
|
162162
LL | String::from(<str as AsRef<str>>::as_ref("x"));
163163
| ++++++++++++++++++++++++++++ ~
164164
help: try using a fully qualified path to specify the expected types
165165
|
166-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
167-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
166+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
167+
| +++++++++++++++++++++++++++++ ~
168168

169169
error[E0283]: type annotations needed
170170
--> $DIR/issue-72690.rs:37:5
@@ -189,20 +189,20 @@ LL | String::from("x".as_ref());
189189
- impl AsRef<str> for str;
190190
help: try using a fully qualified path to specify the expected types
191191
|
192-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
193-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
192+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
193+
| ++++++++++++++++++++++++++++++ ~
194194
help: try using a fully qualified path to specify the expected types
195195
|
196-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
197-
| +++++++++++++++++++++++++++++++++++++++++ ~
196+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
197+
| +++++++++++++++++++++++++++++ ~
198198
help: try using a fully qualified path to specify the expected types
199199
|
200200
LL | String::from(<str as AsRef<str>>::as_ref("x"));
201201
| ++++++++++++++++++++++++++++ ~
202202
help: try using a fully qualified path to specify the expected types
203203
|
204-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
205-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
204+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
205+
| +++++++++++++++++++++++++++++ ~
206206

207207
error[E0283]: type annotations needed
208208
--> $DIR/issue-72690.rs:46:5
@@ -227,20 +227,20 @@ LL | String::from("x".as_ref());
227227
- impl AsRef<str> for str;
228228
help: try using a fully qualified path to specify the expected types
229229
|
230-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
231-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
230+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
231+
| ++++++++++++++++++++++++++++++ ~
232232
help: try using a fully qualified path to specify the expected types
233233
|
234-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
235-
| +++++++++++++++++++++++++++++++++++++++++ ~
234+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
235+
| +++++++++++++++++++++++++++++ ~
236236
help: try using a fully qualified path to specify the expected types
237237
|
238238
LL | String::from(<str as AsRef<str>>::as_ref("x"));
239239
| ++++++++++++++++++++++++++++ ~
240240
help: try using a fully qualified path to specify the expected types
241241
|
242-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
243-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
242+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
243+
| +++++++++++++++++++++++++++++ ~
244244

245245
error[E0283]: type annotations needed
246246
--> $DIR/issue-72690.rs:53:5
@@ -265,20 +265,20 @@ LL | String::from("x".as_ref());
265265
- impl AsRef<str> for str;
266266
help: try using a fully qualified path to specify the expected types
267267
|
268-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
269-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
268+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
269+
| ++++++++++++++++++++++++++++++ ~
270270
help: try using a fully qualified path to specify the expected types
271271
|
272-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
273-
| +++++++++++++++++++++++++++++++++++++++++ ~
272+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
273+
| +++++++++++++++++++++++++++++ ~
274274
help: try using a fully qualified path to specify the expected types
275275
|
276276
LL | String::from(<str as AsRef<str>>::as_ref("x"));
277277
| ++++++++++++++++++++++++++++ ~
278278
help: try using a fully qualified path to specify the expected types
279279
|
280-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
281-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
280+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
281+
| +++++++++++++++++++++++++++++ ~
282282

283283
error[E0283]: type annotations needed
284284
--> $DIR/issue-72690.rs:62:5
@@ -303,20 +303,20 @@ LL | String::from("x".as_ref());
303303
- impl AsRef<str> for str;
304304
help: try using a fully qualified path to specify the expected types
305305
|
306-
LL | String::from(os_str::<impl AsRef<OsStr> for str>::as_ref("x"));
307-
| ++++++++++++++++++++++++++++++++++++++++++++ ~
306+
LL | String::from(<str as AsRef<OsStr>>::as_ref("x"));
307+
| ++++++++++++++++++++++++++++++ ~
308308
help: try using a fully qualified path to specify the expected types
309309
|
310-
LL | String::from(path::<impl AsRef<Path> for str>::as_ref("x"));
311-
| +++++++++++++++++++++++++++++++++++++++++ ~
310+
LL | String::from(<str as AsRef<Path>>::as_ref("x"));
311+
| +++++++++++++++++++++++++++++ ~
312312
help: try using a fully qualified path to specify the expected types
313313
|
314314
LL | String::from(<str as AsRef<str>>::as_ref("x"));
315315
| ++++++++++++++++++++++++++++ ~
316316
help: try using a fully qualified path to specify the expected types
317317
|
318-
LL | String::from(core::str::<impl AsRef<[u8]> for str>::as_ref("x"));
319-
| ++++++++++++++++++++++++++++++++++++++++++++++ ~
318+
LL | String::from(<str as AsRef<[u8]>>::as_ref("x"));
319+
| +++++++++++++++++++++++++++++ ~
320320

321321
error: aborting due to 17 previous errors
322322

0 commit comments

Comments
 (0)