Skip to content

Commit 9d871b0

Browse files
committed
Auto merge of #111731 - MU001999:fix/issue-111727, r=cjgillot
Keep only the trait when emitting the error for `MyTrait + 'a` Fixes #111727
2 parents 965cf5c + d573838 commit 9d871b0

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
288288
let mode = no_match_data.mode;
289289
let tcx = self.tcx;
290290
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
291-
let (ty_str, ty_file) = tcx.short_ty_string(rcvr_ty);
292-
let short_ty_str = with_forced_trimmed_paths!(rcvr_ty.to_string());
291+
let ((mut ty_str, ty_file), short_ty_str) = if trait_missing_method
292+
&& let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
293+
((predicates.to_string(), None), with_forced_trimmed_paths!(predicates.to_string()))
294+
} else {
295+
(tcx.short_ty_string(rcvr_ty), with_forced_trimmed_paths!(rcvr_ty.to_string()))
296+
};
293297
let is_method = mode == Mode::MethodCall;
294298
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
295299
let similar_candidate = no_match_data.similar_candidate;
@@ -329,12 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
329333
span = item_name.span;
330334

331335
// Don't show generic arguments when the method can't be found in any implementation (#81576).
332-
let mut ty_str_reported = if trait_missing_method {
333-
ty_str.strip_prefix("dyn ").expect("Failed to remove the prefix dyn").to_owned()
334-
} else {
335-
ty_str.clone()
336-
};
337-
336+
let mut ty_str_reported = ty_str.clone();
338337
if let ty::Adt(_, generics) = rcvr_ty.kind() {
339338
if generics.len() > 0 {
340339
let mut autoderef = self.autoderef(span, rcvr_ty);
@@ -383,14 +382,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
383382
if tcx.sess.source_map().is_multiline(sugg_span) {
384383
err.span_label(sugg_span.with_hi(span.lo()), "");
385384
}
386-
let mut ty_str = if short_ty_str.len() < ty_str.len() && ty_str.len() > 10 {
387-
short_ty_str
388-
} else {
389-
ty_str
390-
};
391-
if trait_missing_method {
392-
ty_str =
393-
ty_str.strip_prefix("dyn ").expect("Failed to remove the prefix dyn").to_owned();
385+
386+
if short_ty_str.len() < ty_str.len() && ty_str.len() > 10 {
387+
ty_str = short_ty_str;
394388
}
395389

396390
if let Some(file) = ty_file {

tests/ui/resolve/issue-111727.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// edition: 2021
2+
3+
fn main() {
4+
std::any::Any::create(); //~ ERROR
5+
}

tests/ui/resolve/issue-111727.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0599]: no function or associated item named `create` found for trait `Any`
2+
--> $DIR/issue-111727.rs:4:20
3+
|
4+
LL | std::any::Any::create();
5+
| ^^^^^^ function or associated item not found in `Any`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)