Skip to content

Take doc(alias) into account when providing typo suggestions #83968

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

Closed
jyn514 opened this issue Apr 7, 2021 · 5 comments · Fixed by #107108
Closed

Take doc(alias) into account when providing typo suggestions #83968

jyn514 opened this issue Apr 7, 2021 · 5 comments · Fixed by #107108
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Apr 7, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a5d74dbaa80fd7833c2b937fc94f58fa

fn main() {
    let x = [1, 2, 3].length();
}

The current output is:

error[E0599]: no method named `length` found for array `[{integer}; 3]` in the current scope
 --> src/main.rs:2:23
  |
2 |     let x = [1, 2, 3].length();
  |                       ^^^^^^ method not found in `[{integer}; 3]`

Ideally the output should look like:

error[E0599]: no method named `length` found for array `[{integer}; 3]` in the current scope
 --> src/main.rs:2:23
  |
2 |     let x = [1, 2, 3].length();
  |                       ^^^^^^ help: a function with an alias to `length` exists: `len`

Searching for length will already bring up len in the docs: https://doc.rust-lang.org/std/?search=length
It would be nice if the CLI errors had feature parity.

Meta

rustc --version: 1.51.0

@jyn514 jyn514 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. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Apr 7, 2021
@Badel2
Copy link
Contributor

Badel2 commented Apr 9, 2022

Searching for length will already bring up len in the docs: https://doc.rust-lang.org/std/?search=length

Not anymore?

@amab8901
Copy link
Contributor

amab8901 commented Jan 16, 2023

I think this issue should be closed. The solution has already been implemented.

fn main() {
    let x = [1, 2, 3].length();
}

in playground (https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a5d74dbaa80fd7833c2b937fc94f58fa)
yields the following output:

Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `length` found for array `[{integer}; 3]` in the current scope
 --> src/main.rs:2:23
  |
2 |     let x = [1, 2, 3].length();
  |                       ^^^^^^ help: there is a method with a similar name: `len`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

@jyn514
Copy link
Member Author

jyn514 commented Jan 16, 2023

@Badel2 @amab8901 this specific issue with len has been fixed, but the underlying problem that doc(alias) is ignored has not been fixed. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2aecfff005336e11136e0d063fd93ff8

error[E0425]: cannot find function `memcpy` in this scope
 --> src/main.rs:5:5
  |
5 |     memcpy(&dst, &src[2..]);
  |     ^^^^^^ not found in this scope
  |
help: consider importing one of these items
  |
1 | use libc::memcpy;
  |
1 | use openssl_sys::memcpy;
  |

error[E0599]: no method named `memcpy` found for array `[{integer}; 2]` in the current scope
 --> src/main.rs:6:9
  |
6 |     dst.memcpy(&src[2..]);
  |         ^^^^^^ method not found in `[{integer}; 2]`

@jyn514 jyn514 added the E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Jan 16, 2023
@jyn514
Copy link
Member Author

jyn514 commented Jan 16, 2023

Mentoring instructions: change

/// Finds the method with the appropriate name (or return type, as the case may be). If
/// `allow_similar_names` is set, find methods with close-matching names.
// The length of the returned iterator is nearly always 0 or 1 and this
// method is fairly hot.
fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
if let Some(name) = self.method_name {
if self.allow_similar_names {
let max_dist = max(name.as_str().len(), 3) / 3;
self.tcx
.associated_items(def_id)
.in_definition_order()
.filter(|x| {
if !self.is_relevant_kind_for_mode(x.kind) {
return false;
}
match lev_distance_with_substrings(name.as_str(), x.name.as_str(), max_dist)
to also consider doc(alias), not just the names themselves.

@jyn514 jyn514 added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Jan 16, 2023
@sulami
Copy link
Contributor

sulami commented Jan 18, 2023

This looks doable.

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jan 23, 2023
…suggestions, r=compiler-errors

Consider doc(alias) when providing typo suggestions

This means that

```rust
impl Foo {
    #[doc(alias = "quux")]
    fn bar(&self) {}
}

fn main() {
    (Foo {}).quux();
}
```

will suggest `bar`. This currently uses the "there is a method with a similar name" help text, because the point where we choose and emit a suggestion is different from where we gather the suggestions. Changes have mainly been made to the latter.

The selection code will now fall back to aliased candidates, but generally only if there is no candidate that matches based on the existing Levenshtein methodology.

Fixes rust-lang#83968.
@bors bors closed this as completed in f908f0b Jan 23, 2023
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-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. 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.

4 participants