Skip to content

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

@jyn514

Description

@jyn514
Member

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
on Apr 7, 2021
Badel2

Badel2 commented on Apr 9, 2022

@Badel2
Contributor

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

Not anymore?

amab8901

amab8901 commented on Jan 16, 2023

@amab8901
Contributor

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

jyn514 commented on Jan 16, 2023

@jyn514
MemberAuthor

@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]`
added
E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
on Jan 16, 2023
jyn514

jyn514 commented on Jan 16, 2023

@jyn514
MemberAuthor

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.

added
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
on Jan 16, 2023
sulami

sulami commented on Jan 18, 2023

@sulami
Contributor

This looks doable.

@rustbot claim

added a commit that references this issue on Jan 23, 2023
f4f3335
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @sulami@Badel2@jyn514@amab8901

    Issue actions

      Take doc(alias) into account when providing typo suggestions · Issue #83968 · rust-lang/rust