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

Add levenshtein distance based suggestions everywhere #30197

Open
Manishearth opened this issue Dec 4, 2015 · 29 comments
Open

Add levenshtein distance based suggestions everywhere #30197

Manishearth opened this issue Dec 4, 2015 · 29 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Manishearth
Copy link
Member

Currently, we suggest typo fixes in case of function calls, locals, and field accesses. So, both the marked lines below will suggest the correct fix:

struct A {foo: u8}
fn main() {
    let x = A {foo: 1};
    x.fob; // here
    let aaaaaaa=1;
    let z = aaaaaab; // here
}

However, this is not the case for imports, crates, and inline paths. We should fix this.

This is probably not an easy bug, but should be fun. Basically you need to look for the source of the "could not find X" error, and do something similar to

fn find_best_match_for_name(&mut self, name: &str) -> SuggestionType {

@apasel422 @wafflespeanut want to work on this?

@Manishearth Manishearth added A-diagnostics Area: Messages for errors, warnings, and lints E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Dec 4, 2015
@wafflespeanut
Copy link
Contributor

@Manishearth Thanks, I'll try :)

@Manishearth Manishearth self-assigned this Dec 8, 2015
@Manishearth
Copy link
Member Author

Go ahead!

@mitaa
Copy link
Contributor

mitaa commented Feb 27, 2016

I was surprised that I didn't get a suggestion here

fn main() {
    "".is_emtpy();
}

@wafflespeanut
Copy link
Contributor

What could one possibly suggest there? :)

@mitaa
Copy link
Contributor

mitaa commented Feb 27, 2016

is_empty? (p and t are flipped)

@wafflespeanut
Copy link
Contributor

Hmm, looks like we currently show suggestions only for the stuff that exist in the source file.

@Manishearth
Copy link
Member Author

No, we just don't handle methods.

@cramertj
Copy link
Member

Did crate and inline path support ever get added for this? If not, I'd be interested in giving it a shot.

@Manishearth
Copy link
Member Author

I don't think so. Go ahead!

@nikhilshagri
Copy link
Contributor

@Manishearth can I pick this up if nobody else is working on it?

@Manishearth
Copy link
Member Author

Sure!

@nagisa
Copy link
Member

nagisa commented Dec 2, 2016

We do not make suggestions for fields behind a reference:

struct Ty { field: () }

fn test1(t: Ty) { t.fied }
fn test2(t: &mut Ty) { t.fied }
error: attempted access of field `fied` on type `Ty`, but no field with that name was found
 --> <anon>:3:19
  |
3 | fn test1(t: Ty) { t.fied }
  |                   ^^^^^^
  |
help: did you mean `field`?
 --> <anon>:3:21
  |
3 | fn test1(t: Ty) { t.fied }
  |                     ^^^^

error: attempted access of field `fied` on type `&mut Ty`, but no field with that name was found
 --> <anon>:4:24
  |
4 | fn test2(t: &mut Ty) { t.fied }
  |                        ^^^^^^

@leonardo-m
Copy link

leonardo-m commented Dec 5, 2016

From Issue #38166. Some ideas from: https://gcc.gnu.org/gcc-7/changes.html

const MAX_ITEM: usize = 10;
fn foo_bar() {}
fn foo(c: esize) {} // Misspelled type name.
enum Bar { First, Second }
fn main() {
    let v = [0u32; MAXITEM]; // Misspelled constant name.
    foobar(); // Misspelled function name.
    let b: Bar = Bar::second; // Misspelled enum variant.
}

In all four cases I'd like the Rust compiler to suggest the closest names (usize/isize, MAX_ITEM, foo_bar, Bar::Second).

@petrochenkov
Copy link
Contributor

petrochenkov commented Dec 5, 2016

#38154 uses Levenshtein-based suggestions in all path contexts, but there are two more serious problems:

bors added a commit that referenced this issue Jan 14, 2017
resolve: Levenshtein-based suggestions for non-import paths

This patch addresses both items from #30197 (comment) and therefore implements the largest part of #30197.

r? @jseyfried
@petrochenkov
Copy link
Contributor

petrochenkov commented Jan 14, 2017

Suggestions for last segments of non-import paths are implemented in #38927

Remaining items:

bors added a commit that referenced this issue Feb 16, 2017
Refactor macro resolution errors + add derive macro suggestions

Move legacy macro resolution error reporting to `finalize_current_module_macro_resolutions`, and provide suggestions for derive macros.

Fixes #39323

cc #30197

r? @jseyfried
bors added a commit that referenced this issue Feb 17, 2017
Refactor macro resolution errors + add derive macro suggestions

Move legacy macro resolution error reporting to `finalize_current_module_macro_resolutions`, and provide suggestions for derive macros.

Fixes #39323

cc #30197

r? @jseyfried
@laumann
Copy link
Contributor

laumann commented Aug 19, 2017

Hi! Is anyone working on adding suggestions for method names (re @mitaa's is_emtpy() example)? I have a patch that I wrote some time ago that implements this, but if someone else is working on it I don't want to get in the way.

laumann pushed a commit to laumann/rust that referenced this issue Sep 21, 2017
Use the syntax::util::lev_distance module to provide suggestions when a
named method cannot be found.

Part of rust-lang#30197
bors added a commit that referenced this issue Sep 25, 2017
Add suggestions for misspelled method names

Use the syntax::util::lev_distance module to provide suggestions when a
named method cannot be found.

Part of #30197
@laumann
Copy link
Contributor

laumann commented Sep 26, 2017

If @petrochenkov's list is being kept up-to-date, I think the "method calls" point can be crossed off.

@petrochenkov
Copy link
Contributor

@laumann
Updated the list.
Thanks for working on this.

@laumann
Copy link
Contributor

laumann commented Sep 26, 2017

@petrochenkov Happy to help :-)

I considered looking into suggestions for "Lifetime and label names" next - that sounds like it should be more or less straight-forward...

@petrochenkov
Copy link
Contributor

@laumann
It should be straightforward, but note that with #44524 typos in lifetimes will be impossible by definition (every typo becomes a new lifetime, modulo some corner cases).

laumann pushed a commit to laumann/rust that referenced this issue Oct 10, 2017
kennytm added a commit to kennytm/rust that referenced this issue Oct 13, 2017
…r=petrochenkov

Add suggestions for misspelled labels

Another part of rust-lang#30197
@steveklabnik
Copy link
Member

Triage: do we plan on doing more of this? I'm not sure.

@estebank estebank added the P-low Low priority label May 25, 2019
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 22, 2020
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-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests