Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostic for CandidateSource::Trait take receiver as first arg mistakely #119775

Closed
Young-Flash opened this issue Jan 9, 2024 · 1 comment · Fixed by #119957
Closed

Diagnostic for CandidateSource::Trait take receiver as first arg mistakely #119775

Young-Flash opened this issue Jan 9, 2024 · 1 comment · Fixed by #119957
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Young-Flash
Copy link
Member

Young-Flash commented Jan 9, 2024

Code

struct A {}

trait M {
    fn new(a: Self);
}
impl M for A {
    fn new(a: Self) {
        todo!()
    }
}

fn main() {
    let a = A {};
    a.new();
}

Current output

error[E0599]: no method named `new` found for struct `A` in the current scope
  --> src/main.rs:28:7
   |
15 | struct A {}
   | -------- method `new` not found for this struct
...
28 |     a.new();
   |     --^^^--
   |     | |
   |     | this is an associated function, not a method
   |     help: use associated function syntax instead: `A::new()`
   |
   = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `M`
  --> src/main.rs:18:5
   |
18 |     fn new(a: Self);
   |     ^^^^^^^^^^^^^^^^
   = help: items from traits can only be used if the trait is implemented and in scope

Desired output

A::new(a)

Rationale and extra context

We had solved CandidateSource::Impl case in #118502, could do the same for CandidateSource::Trait

let first_arg = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0)
&& let Some(assoc) = self.associated_value(*impl_did, item_name)
&& assoc.kind == ty::AssocKind::Fn
{
let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
sig.inputs().skip_binder().get(0).and_then(|first| {
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 {
None
};

Other cases

No response

Rust Version

rustc 1.76.0-nightly (ba7c7a301 2023-11-13)
binary: rustc
commit-hash: ba7c7a301984967c8c13adb580ef9b86ba706a83
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.4

Anything else?

No response

@Young-Flash Young-Flash added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 9, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 9, 2024
@Young-Flash
Copy link
Member Author

@rustbot claim

@saethlin saethlin added C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 10, 2024
Nadrieril added a commit to Nadrieril/rust that referenced this issue Jan 27, 2024
fix: correct suggestion arg for impl trait

follow up rust-lang#118502, close rust-lang#119775
@bors bors closed this as completed in c6f0a5c Jan 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 27, 2024
Rollup merge of rust-lang#119957 - Young-Flash:fix, r=fmease

fix: correct suggestion arg for impl trait

follow up rust-lang#118502, close rust-lang#119775
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants