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

For E0223, suggest associated functions that are similar to the path, even if the base type has multiple inherent impl blocks. #135865

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

zachs18
Copy link
Contributor

@zachs18 zachs18 commented Jan 22, 2025

Currently, the "help: there is an associated function with a similar name from_utf8" suggestion for String::from::utf8 is only given if String has exactly one inherent impl item. This PR makes the suggestion be emitted even if the base type has multiple inherent impl items.

Example:

struct Foo;
impl Foo {
    fn bar_baz() {}
}
impl Foo {} // load-bearing
fn main() {
    Foo::bar::baz;
}

Nightly/stable output:

error[E0223]: ambiguous associated type
 --> f.rs:7:5
  |
7 |     Foo::bar::baz;
  |     ^^^^^^^^
  |
help: if there were a trait named `Example` with associated type `bar` implemented for `Foo`, you could use the fully-qualified path
  |
7 |     <Foo as Example>::bar::baz;
  |     ~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0223`.

Output with this PR, or without the load-bearing empty impl on nightly/stable:

error[E0223]: ambiguous associated type
 --> f.rs:7:5
  |
7 |     Foo::bar::baz;
  |     ^^^^^^^^
  |
help: there is an associated function with a similar name: `bar_baz`
  |
7 |     Foo::bar_baz;
  |          ~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0223`.

Ideally, this suggestion would also work for non-ADT types like str::char::indices (edit: latest commit makes this work with primitives) or <dyn Any>::downcast::mut_unchecked, but that seemed to be a harder change.

@rustbot label +A-diagnostics

@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2025

r? @compiler-errors

rustbot has assigned @compiler-errors.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2025

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 22, 2025
@zachs18
Copy link
Contributor Author

zachs18 commented Jan 22, 2025

(Latest commit also works for primitives, but not <dyn Any>::downcast::mut_unchecked)

@rust-log-analyzer

This comment has been minimized.

@zachs18 zachs18 force-pushed the maybe_report_similar_assoc_fn_more branch from 9f23011 to 7e1a8bd Compare January 22, 2025 08:13
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants