From 54d35e71a6a0053641e5813f74fbcf1c7379503a Mon Sep 17 00:00:00 2001 From: Yiming Lei Date: Fri, 8 Jul 2022 12:42:29 -0700 Subject: [PATCH] =?UTF-8?q?distinguish=20the=20method=20and=20associated?= =?UTF-8?q?=20function=20diagnostic=20information=20Methods=20are=20define?= =?UTF-8?q?d=20within=20the=20context=20of=20a=20struct=20and=20their=20fi?= =?UTF-8?q?rst=20parameter=20is=20always=20self=20Associated=20functions?= =?UTF-8?q?=20don=E2=80=99t=20take=20self=20as=20a=20parameter=20=09modifi?= =?UTF-8?q?ed:=20=20=20compiler/rustc=5Ftypeck/src/check/method/suggest.rs?= =?UTF-8?q?=20=09modified:=20=20=20src/test/ui/auto-ref-slice-plus-ref.std?= =?UTF-8?q?err=20=09modified:=20=20=20src/test/ui/block-result/issue-3563.?= =?UTF-8?q?stderr=20=09modified:=20=20=20src/test/ui/issues/issue-28344.st?= =?UTF-8?q?derr=20=09modified:=20=20=20src/test/ui/suggestions/dont-sugges?= =?UTF-8?q?t-pin-array-dot-set.stderr=20=09modified:=20=20=20src/test/ui/s?= =?UTF-8?q?uggestions/suggest-methods.stderr=20=09modified:=20=20=20src/te?= =?UTF-8?q?st/ui/traits/trait-upcasting/subtrait-method.stderr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rustc_typeck/src/check/method/suggest.rs | 33 +++++++++++++------ src/test/ui/auto-ref-slice-plus-ref.stderr | 2 +- src/test/ui/block-result/issue-3563.stderr | 2 +- src/test/ui/issues/issue-28344.stderr | 4 +-- .../dont-suggest-pin-array-dot-set.stderr | 2 +- .../ui/suggestions/suggest-methods.stderr | 8 ++--- .../trait-upcasting/subtrait-method.stderr | 10 +++--- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index 4e1a105fc71cc..4b91049355804 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1035,16 +1035,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // that had unsatisfied trait bounds if unsatisfied_predicates.is_empty() { let def_kind = lev_candidate.kind.as_def_kind(); - err.span_suggestion( - span, - &format!( - "there is {} {} with a similar name", - def_kind.article(), - def_kind.descr(lev_candidate.def_id), - ), - lev_candidate.name, - Applicability::MaybeIncorrect, - ); + // Methods are defined within the context of a struct and their first parameter is always self, + // which represents the instance of the struct the method is being called on + // Associated functions don’t take self as a parameter and + // they are not methods because they don’t have an instance of the struct to work with. + if def_kind == DefKind::AssocFn && lev_candidate.fn_has_self_parameter { + err.span_suggestion( + span, + &format!("there is a method with a similar name",), + lev_candidate.name, + Applicability::MaybeIncorrect, + ); + } else { + err.span_suggestion( + span, + &format!( + "there is {} {} with a similar name", + def_kind.article(), + def_kind.descr(lev_candidate.def_id), + ), + lev_candidate.name, + Applicability::MaybeIncorrect, + ); + } } } diff --git a/src/test/ui/auto-ref-slice-plus-ref.stderr b/src/test/ui/auto-ref-slice-plus-ref.stderr index eb8447ff0f3e7..e2883050720d6 100644 --- a/src/test/ui/auto-ref-slice-plus-ref.stderr +++ b/src/test/ui/auto-ref-slice-plus-ref.stderr @@ -2,7 +2,7 @@ error[E0599]: no method named `test_mut` found for struct `Vec<{integer}>` in th --> $DIR/auto-ref-slice-plus-ref.rs:7:7 | LL | a.test_mut(); - | ^^^^^^^^ help: there is an associated function with a similar name: `get_mut` + | ^^^^^^^^ help: there is a method with a similar name: `get_mut` | = help: items from traits can only be used if the trait is implemented and in scope note: `MyIter` defines an item `test_mut`, perhaps you need to implement it diff --git a/src/test/ui/block-result/issue-3563.stderr b/src/test/ui/block-result/issue-3563.stderr index 5255e48bee13b..be551f6e889fc 100644 --- a/src/test/ui/block-result/issue-3563.stderr +++ b/src/test/ui/block-result/issue-3563.stderr @@ -2,7 +2,7 @@ error[E0599]: no method named `b` found for reference `&Self` in the current sco --> $DIR/issue-3563.rs:3:17 | LL | || self.b() - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-28344.stderr b/src/test/ui/issues/issue-28344.stderr index b1d1c01b27a3c..85a8698afc5a7 100644 --- a/src/test/ui/issues/issue-28344.stderr +++ b/src/test/ui/issues/issue-28344.stderr @@ -25,7 +25,7 @@ LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8); | ^^^^^ | | | function or associated item not found in `dyn BitXor<_>` - | help: there is an associated function with a similar name: `bitxor` + | help: there is a method with a similar name: `bitxor` warning: trait objects without an explicit `dyn` are deprecated --> $DIR/issue-28344.rs:10:13 @@ -53,7 +53,7 @@ LL | let g = BitXor::bitor; | ^^^^^ | | | function or associated item not found in `dyn BitXor<_>` - | help: there is an associated function with a similar name: `bitxor` + | help: there is a method with a similar name: `bitxor` error: aborting due to 4 previous errors; 2 warnings emitted diff --git a/src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr b/src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr index 677aa031bf7d6..c66da3ea6d976 100644 --- a/src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr +++ b/src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr @@ -2,7 +2,7 @@ error[E0599]: no method named `set` found for array `[u8; 1]` in the current sco --> $DIR/dont-suggest-pin-array-dot-set.rs:14:7 | LL | a.set(0, 3); - | ^^^ help: there is an associated function with a similar name: `get` + | ^^^ help: there is a method with a similar name: `get` error: aborting due to previous error diff --git a/src/test/ui/suggestions/suggest-methods.stderr b/src/test/ui/suggestions/suggest-methods.stderr index dd9010e32955e..4277b23fdc127 100644 --- a/src/test/ui/suggestions/suggest-methods.stderr +++ b/src/test/ui/suggestions/suggest-methods.stderr @@ -5,25 +5,25 @@ LL | struct Foo; | --- method `bat` not found for this struct ... LL | f.bat(1.0); - | ^^^ help: there is an associated function with a similar name: `bar` + | ^^^ help: there is a method with a similar name: `bar` error[E0599]: no method named `is_emtpy` found for struct `String` in the current scope --> $DIR/suggest-methods.rs:21:15 | LL | let _ = s.is_emtpy(); - | ^^^^^^^^ help: there is an associated function with a similar name: `is_empty` + | ^^^^^^^^ help: there is a method with a similar name: `is_empty` error[E0599]: no method named `count_eos` found for type `u32` in the current scope --> $DIR/suggest-methods.rs:25:19 | LL | let _ = 63u32.count_eos(); - | ^^^^^^^^^ help: there is an associated function with a similar name: `count_zeros` + | ^^^^^^^^^ help: there is a method with a similar name: `count_zeros` error[E0599]: no method named `count_o` found for type `u32` in the current scope --> $DIR/suggest-methods.rs:28:19 | LL | let _ = 63u32.count_o(); - | ^^^^^^^ help: there is an associated function with a similar name: `count_ones` + | ^^^^^^^ help: there is a method with a similar name: `count_ones` error: aborting due to 4 previous errors diff --git a/src/test/ui/traits/trait-upcasting/subtrait-method.stderr b/src/test/ui/traits/trait-upcasting/subtrait-method.stderr index 8c69011800b47..af7a410f6d92c 100644 --- a/src/test/ui/traits/trait-upcasting/subtrait-method.stderr +++ b/src/test/ui/traits/trait-upcasting/subtrait-method.stderr @@ -2,7 +2,7 @@ error[E0599]: no method named `c` found for reference `&dyn Bar` in the current --> $DIR/subtrait-method.rs:56:9 | LL | bar.c(); - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` | = help: items from traits can only be used if the trait is implemented and in scope note: `Baz` defines an item `c`, perhaps you need to implement it @@ -15,7 +15,7 @@ error[E0599]: no method named `b` found for reference `&dyn Foo` in the current --> $DIR/subtrait-method.rs:60:9 | LL | foo.b(); - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` | = help: items from traits can only be used if the trait is implemented and in scope note: `Bar` defines an item `b`, perhaps you need to implement it @@ -28,7 +28,7 @@ error[E0599]: no method named `c` found for reference `&dyn Foo` in the current --> $DIR/subtrait-method.rs:62:9 | LL | foo.c(); - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` | = help: items from traits can only be used if the trait is implemented and in scope note: `Baz` defines an item `c`, perhaps you need to implement it @@ -41,7 +41,7 @@ error[E0599]: no method named `b` found for reference `&dyn Foo` in the current --> $DIR/subtrait-method.rs:66:9 | LL | foo.b(); - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` | = help: items from traits can only be used if the trait is implemented and in scope note: `Bar` defines an item `b`, perhaps you need to implement it @@ -54,7 +54,7 @@ error[E0599]: no method named `c` found for reference `&dyn Foo` in the current --> $DIR/subtrait-method.rs:68:9 | LL | foo.c(); - | ^ help: there is an associated function with a similar name: `a` + | ^ help: there is a method with a similar name: `a` | = help: items from traits can only be used if the trait is implemented and in scope note: `Baz` defines an item `c`, perhaps you need to implement it