Skip to content

Commit f7f80c2

Browse files
authored
Rollup merge of #99064 - lyming2007:issue-97687-fix, r=estebank
distinguish the method and associated function diagnostic information Methods are defined within the context of a struct and their first parameter is always self Associated functions don’t take self as a parameter ``` modified: compiler/rustc_typeck/src/check/method/suggest.rs modified: src/test/ui/auto-ref-slice-plus-ref.stderr modified: src/test/ui/block-result/issue-3563.stderr modified: src/test/ui/issues/issue-28344.stderr modified: src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr modified: src/test/ui/suggestions/suggest-methods.stderr modified: src/test/ui/traits/trait-upcasting/subtrait-method.stderr ```
2 parents cfb5ae2 + 54d35e7 commit f7f80c2

File tree

7 files changed

+37
-24
lines changed

7 files changed

+37
-24
lines changed

Diff for: compiler/rustc_typeck/src/check/method/suggest.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -1066,16 +1066,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10661066
// that had unsatisfied trait bounds
10671067
if unsatisfied_predicates.is_empty() {
10681068
let def_kind = lev_candidate.kind.as_def_kind();
1069-
err.span_suggestion(
1070-
span,
1071-
&format!(
1072-
"there is {} {} with a similar name",
1073-
def_kind.article(),
1074-
def_kind.descr(lev_candidate.def_id),
1075-
),
1076-
lev_candidate.name,
1077-
Applicability::MaybeIncorrect,
1078-
);
1069+
// Methods are defined within the context of a struct and their first parameter is always self,
1070+
// which represents the instance of the struct the method is being called on
1071+
// Associated functions don’t take self as a parameter and
1072+
// they are not methods because they don’t have an instance of the struct to work with.
1073+
if def_kind == DefKind::AssocFn && lev_candidate.fn_has_self_parameter {
1074+
err.span_suggestion(
1075+
span,
1076+
&format!("there is a method with a similar name",),
1077+
lev_candidate.name,
1078+
Applicability::MaybeIncorrect,
1079+
);
1080+
} else {
1081+
err.span_suggestion(
1082+
span,
1083+
&format!(
1084+
"there is {} {} with a similar name",
1085+
def_kind.article(),
1086+
def_kind.descr(lev_candidate.def_id),
1087+
),
1088+
lev_candidate.name,
1089+
Applicability::MaybeIncorrect,
1090+
);
1091+
}
10791092
}
10801093
}
10811094

Diff for: src/test/ui/auto-ref-slice-plus-ref.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `test_mut` found for struct `Vec<{integer}>` in th
22
--> $DIR/auto-ref-slice-plus-ref.rs:7:7
33
|
44
LL | a.test_mut();
5-
| ^^^^^^^^ help: there is an associated function with a similar name: `get_mut`
5+
| ^^^^^^^^ help: there is a method with a similar name: `get_mut`
66
|
77
= help: items from traits can only be used if the trait is implemented and in scope
88
note: `MyIter` defines an item `test_mut`, perhaps you need to implement it

Diff for: src/test/ui/block-result/issue-3563.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `b` found for reference `&Self` in the current sco
22
--> $DIR/issue-3563.rs:3:17
33
|
44
LL | || self.b()
5-
| ^ help: there is an associated function with a similar name: `a`
5+
| ^ help: there is a method with a similar name: `a`
66

77
error: aborting due to previous error
88

Diff for: src/test/ui/issues/issue-28344.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
2525
| ^^^^^
2626
| |
2727
| function or associated item not found in `dyn BitXor<_>`
28-
| help: there is an associated function with a similar name: `bitxor`
28+
| help: there is a method with a similar name: `bitxor`
2929

3030
warning: trait objects without an explicit `dyn` are deprecated
3131
--> $DIR/issue-28344.rs:10:13
@@ -53,7 +53,7 @@ LL | let g = BitXor::bitor;
5353
| ^^^^^
5454
| |
5555
| function or associated item not found in `dyn BitXor<_>`
56-
| help: there is an associated function with a similar name: `bitxor`
56+
| help: there is a method with a similar name: `bitxor`
5757

5858
error: aborting due to 4 previous errors; 2 warnings emitted
5959

Diff for: src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `set` found for array `[u8; 1]` in the current sco
22
--> $DIR/dont-suggest-pin-array-dot-set.rs:14:7
33
|
44
LL | a.set(0, 3);
5-
| ^^^ help: there is an associated function with a similar name: `get`
5+
| ^^^ help: there is a method with a similar name: `get`
66

77
error: aborting due to previous error
88

Diff for: src/test/ui/suggestions/suggest-methods.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ LL | struct Foo;
55
| ---------- method `bat` not found for this struct
66
...
77
LL | f.bat(1.0);
8-
| ^^^ help: there is an associated function with a similar name: `bar`
8+
| ^^^ help: there is a method with a similar name: `bar`
99

1010
error[E0599]: no method named `is_emtpy` found for struct `String` in the current scope
1111
--> $DIR/suggest-methods.rs:21:15
1212
|
1313
LL | let _ = s.is_emtpy();
14-
| ^^^^^^^^ help: there is an associated function with a similar name: `is_empty`
14+
| ^^^^^^^^ help: there is a method with a similar name: `is_empty`
1515

1616
error[E0599]: no method named `count_eos` found for type `u32` in the current scope
1717
--> $DIR/suggest-methods.rs:25:19
1818
|
1919
LL | let _ = 63u32.count_eos();
20-
| ^^^^^^^^^ help: there is an associated function with a similar name: `count_zeros`
20+
| ^^^^^^^^^ help: there is a method with a similar name: `count_zeros`
2121

2222
error[E0599]: no method named `count_o` found for type `u32` in the current scope
2323
--> $DIR/suggest-methods.rs:28:19
2424
|
2525
LL | let _ = 63u32.count_o();
26-
| ^^^^^^^ help: there is an associated function with a similar name: `count_ones`
26+
| ^^^^^^^ help: there is a method with a similar name: `count_ones`
2727

2828
error: aborting due to 4 previous errors
2929

Diff for: src/test/ui/traits/trait-upcasting/subtrait-method.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `c` found for reference `&dyn Bar` in the current
22
--> $DIR/subtrait-method.rs:56:9
33
|
44
LL | bar.c();
5-
| ^ help: there is an associated function with a similar name: `a`
5+
| ^ help: there is a method with a similar name: `a`
66
|
77
= help: items from traits can only be used if the trait is implemented and in scope
88
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
1515
--> $DIR/subtrait-method.rs:60:9
1616
|
1717
LL | foo.b();
18-
| ^ help: there is an associated function with a similar name: `a`
18+
| ^ help: there is a method with a similar name: `a`
1919
|
2020
= help: items from traits can only be used if the trait is implemented and in scope
2121
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
2828
--> $DIR/subtrait-method.rs:62:9
2929
|
3030
LL | foo.c();
31-
| ^ help: there is an associated function with a similar name: `a`
31+
| ^ help: there is a method with a similar name: `a`
3232
|
3333
= help: items from traits can only be used if the trait is implemented and in scope
3434
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
4141
--> $DIR/subtrait-method.rs:66:9
4242
|
4343
LL | foo.b();
44-
| ^ help: there is an associated function with a similar name: `a`
44+
| ^ help: there is a method with a similar name: `a`
4545
|
4646
= help: items from traits can only be used if the trait is implemented and in scope
4747
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
5454
--> $DIR/subtrait-method.rs:68:9
5555
|
5656
LL | foo.c();
57-
| ^ help: there is an associated function with a similar name: `a`
57+
| ^ help: there is a method with a similar name: `a`
5858
|
5959
= help: items from traits can only be used if the trait is implemented and in scope
6060
note: `Baz` defines an item `c`, perhaps you need to implement it

0 commit comments

Comments
 (0)