Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jan 24, 2025

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
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with or without nit

@@ -1,5 +1,5 @@
error[E0223]: ambiguous associated type
--> $DIR/issue-109195.rs:2:5
--> $DIR/issue-109195.rs:12:5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to give this test a better name as a drive-by improvement?

@compiler-errors
Copy link
Member

@bors delegate+

@bors
Copy link
Collaborator

bors commented Jan 23, 2025

✌️ @zachs18, you can now approve this pull request!

If @compiler-errors told you to "r=me" after making some further change, please make that change, then do @bors r=@compiler-errors

@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jan 23, 2025
@zachs18
Copy link
Contributor Author

zachs18 commented Jan 23, 2025

@bors r=@compiler-errors

@bors
Copy link
Collaborator

bors commented Jan 23, 2025

📌 Commit 2c58212 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 23, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 24, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#133605 (Add extensive set of drop order tests)
 - rust-lang#135489 (remove pointless allowed_through_unstable_modules on TryFromSliceError)
 - rust-lang#135757 (Add NuttX support for AArch64 and ARMv7-A targets)
 - rust-lang#135799 (rustdoc-json: Rename `Path::name` to `path`, and give it the path again.)
 - rust-lang#135865 (For E0223, suggest associated functions that are similar to the path, even if the base type has multiple inherent impl blocks.)
 - rust-lang#135890 (Implement `VecDeque::pop_front_if` & `VecDeque::pop_back_if`)
 - rust-lang#135914 (Remove usages of `QueryNormalizer` in the compiler)
 - rust-lang#135936 (fix reify-intrinsic test)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit efb8084 into rust-lang:master Jan 24, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 24, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 24, 2025
Rollup merge of rust-lang#135865 - zachs18:maybe_report_similar_assoc_fn_more, r=compiler-errors

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

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:

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

Nightly/stable output:

```rust
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:

```rust
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
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Mar 11, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#133605 (Add extensive set of drop order tests)
 - rust-lang#135489 (remove pointless allowed_through_unstable_modules on TryFromSliceError)
 - rust-lang#135757 (Add NuttX support for AArch64 and ARMv7-A targets)
 - rust-lang#135799 (rustdoc-json: Rename `Path::name` to `path`, and give it the path again.)
 - rust-lang#135865 (For E0223, suggest associated functions that are similar to the path, even if the base type has multiple inherent impl blocks.)
 - rust-lang#135890 (Implement `VecDeque::pop_front_if` & `VecDeque::pop_back_if`)
 - rust-lang#135914 (Remove usages of `QueryNormalizer` in the compiler)
 - rust-lang#135936 (fix reify-intrinsic test)

r? `@ghost`
`@rustbot` modify labels: rollup
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-tidy Area: The tidy tool S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.

5 participants