Skip to content

Commit

Permalink
point to type param definition when not finding variant, method and a…
Browse files Browse the repository at this point in the history
…ssoc 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
  • Loading branch information
TaKO8Ki committed Jun 22, 2022
1 parent d40f24e commit 402dceb
Show file tree
Hide file tree
Showing 64 changed files with 168 additions and 125 deletions.
36 changes: 21 additions & 15 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}

if let Some(def) = actual.ty_adt_def() {
if let Some(full_sp) = tcx.hir().span_if_local(def.did()) {
let def_sp = tcx.sess.source_map().guess_head_span(full_sp);
err.span_label(
def_sp,
format!(
"{} `{}` not found {}",
item_kind,
item_name,
if def.is_enum() && !is_method { "here" } else { "for this" }
),
);
let ty_span = match actual.kind() {
ty::Param(param_type) => {
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
let type_param = generics.type_param(param_type, self.tcx);
Some(self.tcx.def_span(type_param.def_id))
}
ty::Adt(def, _) if def.did().is_local() => {
tcx.def_ident_span(def.did()).map(|span| span)
}
_ => None,
};

if let Some(span) = ty_span {
err.span_label(
span,
format!(
"{item_kind} `{item_name}` not found for this {}",
actual.prefix_string(self.tcx)
),
);
}

if self.is_fn_ty(rcvr_ty, span) {
Expand Down Expand Up @@ -1951,9 +1958,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
};
// Obtain the span for `param` and use it for a structured suggestion.
if let (Some(param), Some(table)) = (param_type, self.in_progress_typeck_results) {
let table_owner = table.borrow().hir_owner;
let generics = self.tcx.generics_of(table_owner.to_def_id());
if let Some(param) = param_type {
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
let type_param = generics.type_param(param, self.tcx);
let hir = self.tcx.hir();
if let Some(def_id) = type_param.def_id.as_local() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/associated-item/associated-item-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no variant or associated item named `mispellable` found for enum `
--> $DIR/associated-item-enum.rs:17:11
|
LL | enum Enum { Variant }
| --------- variant or associated item `mispellable` not found here
| ---- variant or associated item `mispellable` not found for this enum
...
LL | Enum::mispellable();
| ^^^^^^^^^^^
Expand All @@ -14,7 +14,7 @@ error[E0599]: no variant or associated item named `mispellable_trait` found for
--> $DIR/associated-item-enum.rs:18:11
|
LL | enum Enum { Variant }
| --------- variant or associated item `mispellable_trait` not found here
| ---- variant or associated item `mispellable_trait` not found for this enum
...
LL | Enum::mispellable_trait();
| ^^^^^^^^^^^^^^^^^
Expand All @@ -26,7 +26,7 @@ error[E0599]: no variant or associated item named `MISPELLABLE` found for enum `
--> $DIR/associated-item-enum.rs:19:11
|
LL | enum Enum { Variant }
| --------- variant or associated item `MISPELLABLE` not found here
| ---- variant or associated item `MISPELLABLE` not found for this enum
...
LL | Enum::MISPELLABLE;
| ^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/async-await/pin-needed-to-poll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `poll` found for struct `Sleep` in the current sco
--> $DIR/pin-needed-to-poll.rs:42:20
|
LL | struct Sleep;
| ------------- method `poll` not found for this
| ----- method `poll` not found for this struct
...
LL | self.sleep.poll(cx)
| ^^^^ method not found in `Sleep`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/bogus-tag.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no variant or associated item named `Hsl` found for enum `Color` i
--> $DIR/bogus-tag.rs:7:16
|
LL | enum Color { Rgb(isize, isize, isize), Rgba(isize, isize, isize, isize), }
| ---------- variant or associated item `Hsl` not found here
| ----- variant or associated item `Hsl` not found for this enum
...
LL | Color::Hsl(h, s, l) => { println!("hsl"); }
| ^^^ variant or associated item not found in `Color`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/confuse-field-and-method/issue-18343.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
--> $DIR/issue-18343.rs:7:7
|
LL | struct Obj<F> where F: FnMut() -> u32 {
| ------------------------------------- method `closure` not found for this
| --- method `closure` not found for this struct
...
LL | o.closure();
| ^^^^^^^ field, not a method
Expand Down
22 changes: 11 additions & 11 deletions src/test/ui/confuse-field-and-method/issue-2392.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
--> $DIR/issue-2392.rs:36:15
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `closure` not found for this
| --- method `closure` not found for this struct
...
LL | o_closure.closure();
| ^^^^^^^ field, not a method
Expand All @@ -16,7 +16,7 @@ error[E0599]: no method named `not_closure` found for struct `Obj` in the curren
--> $DIR/issue-2392.rs:38:15
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `not_closure` not found for this
| --- method `not_closure` not found for this struct
...
LL | o_closure.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
Expand All @@ -27,7 +27,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
--> $DIR/issue-2392.rs:42:12
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `closure` not found for this
| --- method `closure` not found for this struct
...
LL | o_func.closure();
| ^^^^^^^ field, not a method
Expand All @@ -41,7 +41,7 @@ error[E0599]: no method named `boxed_closure` found for struct `BoxedObj` in the
--> $DIR/issue-2392.rs:45:14
|
LL | struct BoxedObj {
| --------------- method `boxed_closure` not found for this
| -------- method `boxed_closure` not found for this struct
...
LL | boxed_fn.boxed_closure();
| ^^^^^^^^^^^^^ field, not a method
Expand All @@ -55,7 +55,7 @@ error[E0599]: no method named `boxed_closure` found for struct `BoxedObj` in the
--> $DIR/issue-2392.rs:48:19
|
LL | struct BoxedObj {
| --------------- method `boxed_closure` not found for this
| -------- method `boxed_closure` not found for this struct
...
LL | boxed_closure.boxed_closure();
| ^^^^^^^^^^^^^ field, not a method
Expand All @@ -69,7 +69,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
--> $DIR/issue-2392.rs:53:12
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `closure` not found for this
| --- method `closure` not found for this struct
...
LL | w.wrap.closure();
| ^^^^^^^ field, not a method
Expand All @@ -83,7 +83,7 @@ error[E0599]: no method named `not_closure` found for struct `Obj` in the curren
--> $DIR/issue-2392.rs:55:12
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `not_closure` not found for this
| --- method `not_closure` not found for this struct
...
LL | w.wrap.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
Expand All @@ -94,7 +94,7 @@ error[E0599]: no method named `closure` found for struct `Obj` in the current sc
--> $DIR/issue-2392.rs:58:24
|
LL | struct Obj<F> where F: FnOnce() -> u32 {
| -------------------------------------- method `closure` not found for this
| --- method `closure` not found for this struct
...
LL | check_expression().closure();
| ^^^^^^^ field, not a method
Expand All @@ -108,7 +108,7 @@ error[E0599]: no method named `f1` found for struct `FuncContainer` in the curre
--> $DIR/issue-2392.rs:64:31
|
LL | struct FuncContainer {
| -------------------- method `f1` not found for this
| ------------- method `f1` not found for this struct
...
LL | (*self.container).f1(1);
| ^^ field, not a method
Expand All @@ -122,7 +122,7 @@ error[E0599]: no method named `f2` found for struct `FuncContainer` in the curre
--> $DIR/issue-2392.rs:65:31
|
LL | struct FuncContainer {
| -------------------- method `f2` not found for this
| ------------- method `f2` not found for this struct
...
LL | (*self.container).f2(1);
| ^^ field, not a method
Expand All @@ -136,7 +136,7 @@ error[E0599]: no method named `f3` found for struct `FuncContainer` in the curre
--> $DIR/issue-2392.rs:66:31
|
LL | struct FuncContainer {
| -------------------- method `f3` not found for this
| ------------- method `f3` not found for this struct
...
LL | (*self.container).f3(1);
| ^^ field, not a method
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/confuse-field-and-method/issue-32128.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `example` found for struct `Example` in the curren
--> $DIR/issue-32128.rs:12:10
|
LL | struct Example {
| -------------- method `example` not found for this
| ------- method `example` not found for this struct
...
LL | demo.example(1);
| ^^^^^^^ field, not a method
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/confuse-field-and-method/private-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `dog_age` found for struct `Dog` in the current sc
--> $DIR/private-field.rs:16:23
|
LL | pub struct Dog {
| -------------- method `dog_age` not found for this
| --- method `dog_age` not found for this struct
...
LL | let dog_age = dog.dog_age();
| ^^^^^^^ private field, not a method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error[E0599]: the function or associated item `foo` exists for struct `Foo<{_: u
--> $DIR/issue-69654.rs:17:10
|
LL | struct Foo<const N: usize> {}
| -------------------------- function or associated item `foo` not found for this
| --- function or associated item `foo` not found for this struct
...
LL | Foo::foo();
| ^^^ function or associated item cannot be called on `Foo<{_: usize}>` due to unsatisfied trait bounds
Expand Down
18 changes: 6 additions & 12 deletions src/test/ui/const-generics/generic_const_exprs/issue-80742.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@ LL | [u8; size_of::<T>() + 1]: ,
error[E0599]: the function or associated item `new` exists for struct `Inline<dyn Debug>`, but its trait bounds were not satisfied
--> $DIR/issue-80742.rs:30:36
|
LL | / struct Inline<T>
LL | | where
LL | | [u8; size_of::<T>() + 1]: ,
LL | | {
LL | | _phantom: PhantomData<T>,
LL | | buf: [u8; size_of::<T>() + 1],
LL | | }
| |_- function or associated item `new` not found for this
LL | struct Inline<T>
| ------ function or associated item `new` not found for this struct
...
LL | let dst = Inline::<dyn Debug>::new(0);
| ^^^ function or associated item cannot be called on `Inline<dyn Debug>` due to unsatisfied trait bounds
LL | let dst = Inline::<dyn Debug>::new(0);
| ^^^ function or associated item cannot be called on `Inline<dyn Debug>` due to unsatisfied trait bounds
|
::: $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
LL | pub trait Debug {
| --------------- doesn't satisfy `dyn Debug: Sized`
LL | pub trait Debug {
| --------------- doesn't satisfy `dyn Debug: Sized`
|
= note: the following trait bounds were not satisfied:
`dyn Debug: Sized`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error[E0599]: no method named `f` found for struct `S` in the current scope
--> $DIR/invalid-const-arg-for-type-param.rs:9:7
|
LL | struct S;
| --------- method `f` not found for this
| - method `f` not found for this struct
...
LL | S.f::<0>();
| ^ method not found in `S`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-needs_drop-monomorphic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `assert` found for struct `Bo
--> $DIR/const-needs_drop-monomorphic.rs:11:46
|
LL | struct Bool<const B: bool> {}
| -------------------------- function or associated item `assert` not found for this
| ---- function or associated item `assert` not found for this struct
...
LL | Bool::<{ std::mem::needs_drop::<T>() }>::assert();
| ^^^^^^ function or associated item cannot be called on `Bool<{ std::mem::needs_drop::<T>() }>` due to unsatisfied trait bounds
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/copy-a-resource.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `clone` found for struct `Foo` in the current scop
--> $DIR/copy-a-resource.rs:18:16
|
LL | struct Foo {
| ---------- method `clone` not found for this
| --- method `clone` not found for this struct
...
LL | let _y = x.clone();
| ^^^^^ method not found in `Foo`
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/derives/derive-assoc-type-not-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ error[E0599]: the method `clone` exists for struct `Bar<NotClone>`, but its trai
|
LL | struct Bar<T: Foo> {
| ------------------
| |
| method `clone` not found for this
| | |
| | method `clone` not found for this struct
| doesn't satisfy `Bar<NotClone>: Clone`
...
LL | struct NotClone;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/derives/issue-91492.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LL | pub struct NoDerives;
| --------------------- doesn't satisfy `NoDerives: Clone`
...
LL | struct Object<T, A>(T, A);
| -------------------------- method `use_clone` not found for this
| ------ method `use_clone` not found for this struct
...
LL | foo.use_clone();
| ^^^^^^^^^ method cannot be called on `Object<NoDerives, SomeDerives>` due to unsatisfied trait bounds
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/derives/issue-91550.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL | pub struct NoDerives;
| --------------------- doesn't satisfy `NoDerives: Eq`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_eq` not found for this
| ------ method `use_eq` not found for this struct
...
LL | foo.use_eq();
| ^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
Expand All @@ -44,7 +44,7 @@ LL | pub struct NoDerives;
| --------------------- doesn't satisfy `NoDerives: Ord`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_ord` not found for this
| ------ method `use_ord` not found for this struct
...
LL | foo.use_ord();
| ^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
Expand All @@ -66,7 +66,7 @@ LL | pub struct NoDerives;
| doesn't satisfy `NoDerives: PartialOrd`
LL |
LL | struct Object<T>(T);
| -------------------- method `use_ord_and_partial_ord` not found for this
| ------ method `use_ord_and_partial_ord` not found for this struct
...
LL | foo.use_ord_and_partial_ord();
| ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-40006.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ error[E0599]: no method named `hello_method` found for struct `S` in the current
--> $DIR/issue-40006.rs:38:7
|
LL | struct S;
| --------- method `hello_method` not found for this
| - method `hello_method` not found for this struct
...
LL | S.hello_method();
| ^^^^^^^^^^^^ method not found in `S`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/dont-suggest-private-trait-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no function or associated item named `new` found for struct `T` in
--> $DIR/dont-suggest-private-trait-method.rs:4:8
|
LL | struct T;
| --------- function or associated item `new` not found for this
| - function or associated item `new` not found for this struct
...
LL | T::new();
| ^^^ function or associated item not found in `T`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0599.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no associated item named `NotEvenReal` found for struct `Foo` in t
--> $DIR/E0599.rs:4:20
|
LL | struct Foo;
| ----------- associated item `NotEvenReal` not found for this
| --- associated item `NotEvenReal` not found for this struct
...
LL | || if let Foo::NotEvenReal() = Foo {};
| ^^^^^^^^^^^ associated item not found in `Foo`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ error[E0599]: the method `f` exists for struct `S`, but its trait bounds were no
|
LL | struct S;
| ---------
| |
| method `f` not found for this
| | |
| | method `f` not found for this struct
| doesn't satisfy `<S as X>::Y<i32> = i32`
| doesn't satisfy `S: M`
...
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/hrtb/issue-30786.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ error[E0599]: the method `filterx` exists for struct `Map<Repeat, [closure@$DIR/
|
LL | pub struct Map<S, F> {
| --------------------
| |
| method `filterx` not found for this
| | |
| | method `filterx` not found for this struct
| doesn't satisfy `_: StreamExt`
...
LL | let filter = map.filterx(|x: &_| true);
Expand All @@ -28,8 +28,8 @@ error[E0599]: the method `countx` exists for struct `Filter<Map<Repeat, for<'r>
|
LL | pub struct Filter<S, F> {
| -----------------------
| |
| method `countx` not found for this
| | |
| | method `countx` not found for this struct
| doesn't satisfy `_: StreamExt`
...
LL | let count = filter.countx();
Expand Down
Loading

0 comments on commit 402dceb

Please sign in to comment.