Skip to content

Commit 402dceb

Browse files
committedJun 22, 2022
point to type param definition when not finding variant, method and assoc type
use `def_ident_span` , `body_owner_def_id` instead of `in_progress_typeck_results`, `guess_head_span` use `body_id.owner` directly add description to label
1 parent d40f24e commit 402dceb

File tree

64 files changed

+168
-125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+168
-125
lines changed
 

‎compiler/rustc_typeck/src/check/method/suggest.rs‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
346346
);
347347
}
348348

349-
if let Some(def) = actual.ty_adt_def() {
350-
if let Some(full_sp) = tcx.hir().span_if_local(def.did()) {
351-
let def_sp = tcx.sess.source_map().guess_head_span(full_sp);
352-
err.span_label(
353-
def_sp,
354-
format!(
355-
"{} `{}` not found {}",
356-
item_kind,
357-
item_name,
358-
if def.is_enum() && !is_method { "here" } else { "for this" }
359-
),
360-
);
349+
let ty_span = match actual.kind() {
350+
ty::Param(param_type) => {
351+
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
352+
let type_param = generics.type_param(param_type, self.tcx);
353+
Some(self.tcx.def_span(type_param.def_id))
354+
}
355+
ty::Adt(def, _) if def.did().is_local() => {
356+
tcx.def_ident_span(def.did()).map(|span| span)
361357
}
358+
_ => None,
359+
};
360+
361+
if let Some(span) = ty_span {
362+
err.span_label(
363+
span,
364+
format!(
365+
"{item_kind} `{item_name}` not found for this {}",
366+
actual.prefix_string(self.tcx)
367+
),
368+
);
362369
}
363370

364371
if self.is_fn_ty(rcvr_ty, span) {
@@ -1951,9 +1958,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19511958
)
19521959
};
19531960
// Obtain the span for `param` and use it for a structured suggestion.
1954-
if let (Some(param), Some(table)) = (param_type, self.in_progress_typeck_results) {
1955-
let table_owner = table.borrow().hir_owner;
1956-
let generics = self.tcx.generics_of(table_owner.to_def_id());
1961+
if let Some(param) = param_type {
1962+
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
19571963
let type_param = generics.type_param(param, self.tcx);
19581964
let hir = self.tcx.hir();
19591965
if let Some(def_id) = type_param.def_id.as_local() {

‎src/test/ui/associated-item/associated-item-enum.stderr‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no variant or associated item named `mispellable` found for enum `
22
--> $DIR/associated-item-enum.rs:17:11
33
|
44
LL | enum Enum { Variant }
5-
| --------- variant or associated item `mispellable` not found here
5+
| ---- variant or associated item `mispellable` not found for this enum
66
...
77
LL | Enum::mispellable();
88
| ^^^^^^^^^^^
@@ -14,7 +14,7 @@ error[E0599]: no variant or associated item named `mispellable_trait` found for
1414
--> $DIR/associated-item-enum.rs:18:11
1515
|
1616
LL | enum Enum { Variant }
17-
| --------- variant or associated item `mispellable_trait` not found here
17+
| ---- variant or associated item `mispellable_trait` not found for this enum
1818
...
1919
LL | Enum::mispellable_trait();
2020
| ^^^^^^^^^^^^^^^^^
@@ -26,7 +26,7 @@ error[E0599]: no variant or associated item named `MISPELLABLE` found for enum `
2626
--> $DIR/associated-item-enum.rs:19:11
2727
|
2828
LL | enum Enum { Variant }
29-
| --------- variant or associated item `MISPELLABLE` not found here
29+
| ---- variant or associated item `MISPELLABLE` not found for this enum
3030
...
3131
LL | Enum::MISPELLABLE;
3232
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.