Rollup merge of #118502 - Young-Flash:fix, r=compiler-errors

fix: correct the arg for 'suggest to use associated function syntax' diagnostic

close https://github.com/rust-lang/rust/issues/118469
This commit is contained in:
Matthias Krüger 2023-12-08 23:15:12 +01:00 committed by GitHub
commit 9041d1cec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 3 deletions

View File

@ -1591,10 +1591,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
sig.inputs().skip_binder().get(0).and_then(|first| {
if first.peel_refs() == rcvr_ty.peel_refs() {
None
} else {
let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
// if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
if first.peel_refs() == impl_ty {
Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
} else {
None
}
})
} else {

View File

@ -0,0 +1,16 @@
// run-rustfix
struct A {}
impl A {
fn hello(_a: i32) {}
fn test(_a: Self, _b: i32) {}
}
fn main() {
let _a = A {};
A::hello(1);
//~^ ERROR no method named `hello` found
A::test(_a, 1);
//~^ ERROR no method named `test` found
}

View File

@ -0,0 +1,16 @@
// run-rustfix
struct A {}
impl A {
fn hello(_a: i32) {}
fn test(_a: Self, _b: i32) {}
}
fn main() {
let _a = A {};
_a.hello(1);
//~^ ERROR no method named `hello` found
_a.test(1);
//~^ ERROR no method named `test` found
}

View File

@ -0,0 +1,41 @@
error[E0599]: no method named `hello` found for struct `A` in the current scope
--> $DIR/suggest-assoc-fn-call-without-receiver.rs:12:8
|
LL | struct A {}
| -------- method `hello` not found for this struct
...
LL | _a.hello(1);
| ---^^^^^---
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `A::hello(1)`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `A`
--> $DIR/suggest-assoc-fn-call-without-receiver.rs:6:5
|
LL | fn hello(_a: i32) {}
| ^^^^^^^^^^^^^^^^^
error[E0599]: no method named `test` found for struct `A` in the current scope
--> $DIR/suggest-assoc-fn-call-without-receiver.rs:14:8
|
LL | struct A {}
| -------- method `test` not found for this struct
...
LL | _a.test(1);
| ---^^^^---
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `A::test(_a, 1)`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `A`
--> $DIR/suggest-assoc-fn-call-without-receiver.rs:7:5
|
LL | fn test(_a: Self, _b: i32) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0599`.