Skip to content

Commit a9528a2

Browse files
committed
Keep only the trait when emitting the error for MyTrait + 'a
1 parent ba6f5e3 commit a9528a2

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ 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) = tcx.short_ty_string(rcvr_ty);
292+
let mut short_ty_str = with_forced_trimmed_paths!(rcvr_ty.to_string());
293293
let is_method = mode == Mode::MethodCall;
294294
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
295295
let similar_candidate = no_match_data.similar_candidate;
@@ -328,13 +328,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
328328
}
329329
span = item_name.span;
330330

331-
// 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-
};
331+
if trait_missing_method && let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
332+
ty_str = predicates.to_string();
333+
short_ty_str = with_forced_trimmed_paths!(predicates.to_string());
334+
}
337335

336+
// Don't show generic arguments when the method can't be found in any implementation (#81576).
337+
let mut ty_str_reported = ty_str.clone();
338338
if let ty::Adt(_, generics) = rcvr_ty.kind() {
339339
if generics.len() > 0 {
340340
let mut autoderef = self.autoderef(span, rcvr_ty);
@@ -383,14 +383,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
383383
if tcx.sess.source_map().is_multiline(sugg_span) {
384384
err.span_label(sugg_span.with_hi(span.lo()), "");
385385
}
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();
386+
387+
if short_ty_str.len() < ty_str.len() && ty_str.len() > 10 {
388+
ty_str = short_ty_str;
394389
}
395390

396391
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)