Skip to content

Checker:: Execute levenshtein before other context checking #39291

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

Merged
merged 1 commit into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,14 @@ impl<'a> Resolver<'a> {
}
}

let mut levenshtein_worked = false;

// Try Levenshtein.
if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
levenshtein_worked = true;
}

// Try context dependent help if relaxed lookup didn't work.
if let Some(def) = def {
match (def, source) {
Expand Down Expand Up @@ -2354,14 +2362,10 @@ impl<'a> Resolver<'a> {
}
}

// Try Levenshtein if nothing else worked.
if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
return err;
}

// Fallback label.
err.span_label(base_span, &fallback_label);
if !levenshtein_worked {
err.span_label(base_span, &fallback_label);
}
err
};
let report_errors = |this: &mut Self, def: Option<Def>| {
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/resolve/issue-39226.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
struct Handle {}

struct Something {
handle: Handle
}

fn main() {
let handle: Handle = Handle {};

let s: Something = Something {
handle: Handle
//~^ ERROR cannot find value `Handle` in this scope
//~| NOTE did you mean `handle`?
};
}
11 changes: 11 additions & 0 deletions src/test/ui/resolve/issue-39226.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0423]: expected value, found struct `Handle`
--> $DIR/issue-39226.rs:20:17
|
20 | handle: Handle
| ^^^^^^
| |
| did you mean `handle`?
| did you mean `Handle { /* fields */ }`?

error: aborting due to previous error

5 changes: 4 additions & 1 deletion src/test/ui/resolve/issue-5035.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ error[E0404]: expected trait, found type alias `K`
--> $DIR/issue-5035.rs:13:6
|
13 | impl K for isize {} //~ ERROR expected trait, found type alias `K`
| ^ type aliases cannot be used for traits
| ^
| |
| type aliases cannot be used for traits
| did you mean `I`?

error: cannot continue compilation due to previous error

1 change: 1 addition & 0 deletions src/test/ui/resolve/privacy-struct-ctor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ error[E0423]: expected value, found struct `Z`
| ^
| |
| did you mean `Z { /* fields */ }`?
| did you mean `S`?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unrevelante here. Right?

Copy link
Member

Choose a reason for hiding this comment

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

Are you asking if it's ok that your code change changes these suggestions? I'm not sure :-/ since nrc is on paternity leave right now... @jonathandturner are you the next best person to help out around error message suggestions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I'm asking if it's ok to update that part of suggestion or not. We keep both suggest but is it valid to tell user that, maybe, he misstyped it?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok, but Levenshtein suggestions should probably be turned off globally for single-letter identifiers (not necessarily in this PR).

| constructor is not visible here due to private fields
|
= help: possible better candidate is found in another module, you can import it into scope:
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ error[E0423]: expected value, found module `a::b`
--> $DIR/suggest-path-instead-of-mod-dot-item.rs:45:5
|
45 | a::b.J
| ^^^^--
| |
| ^^^---
| | |
| | did you mean `I`?
| did you mean `a::b::J`?

error[E0423]: expected value, found module `a`
Expand All @@ -50,8 +51,9 @@ error[E0423]: expected value, found module `a::b`
--> $DIR/suggest-path-instead-of-mod-dot-item.rs:61:5
|
61 | a::b.f()
| ^^^^----
| |
| ^^^-----
| | |
| | did you mean `I`?
| did you mean `a::b::f(...)`?

error[E0423]: expected value, found module `a::b`
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/resolve/tuple-struct-alias.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ error[E0423]: expected function, found type alias `A`
--> $DIR/tuple-struct-alias.rs:24:13
|
24 | let s = A(0, 1);
| ^ did you mean `A { /* fields */ }`?
| ^
| |
| did you mean `S`?
| did you mean `A { /* fields */ }`?

error[E0532]: expected tuple struct/variant, found type alias `A`
--> $DIR/tuple-struct-alias.rs:26:9
|
26 | A(..) => {}
| ^ did you mean `A { /* fields */ }`?
| ^
| |
| did you mean `S`?
| did you mean `A { /* fields */ }`?

error: aborting due to 4 previous errors