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

Account for unicode confusables on typoed identifiers #66849

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3871,6 +3871,7 @@ dependencies = [
"syntax",
"syntax_expand",
"syntax_pos",
"unicode_skeleton",
]

[[package]]
Expand Down Expand Up @@ -4471,6 +4472,7 @@ dependencies = [
"serialize",
"smallvec 1.0.0",
"syntax_pos",
"unicode_skeleton",
]

[[package]]
Expand Down Expand Up @@ -5045,6 +5047,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"

[[package]]
name = "unicode_skeleton"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66bd74db2c088d393d1fbf83db2cd5663137640f072d128287dd53c882a0f412"
dependencies = [
"unicode-normalization",
]

[[package]]
name = "unstable-book-gen"
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions src/librustc_resolve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ rustc_data_structures = { path = "../librustc_data_structures" }
rustc_metadata = { path = "../librustc_metadata" }
rustc_error_codes = { path = "../librustc_error_codes" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
unicode_skeleton = "0.1.1"
8 changes: 5 additions & 3 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,11 @@ impl<'a> Resolver<'a> {
&ident.as_str(),
None,
) {
Some(found) if found != ident.name => suggestions
.into_iter()
.find(|suggestion| suggestion.candidate == found),
Some(found) if found != ident.name => suggestions.into_iter()
.find(|suggestion| unicode_skeleton::confusable(
suggestion.candidate.as_str().chars(),
found.as_str().chars(),
)),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ rustc_lexer = { path = "../librustc_lexer" }
rustc_macros = { path = "../librustc_macros" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
rustc_error_codes = { path = "../librustc_error_codes" }
unicode_skeleton = "0.1.1"
57 changes: 34 additions & 23 deletions src/libsyntax/util/lev_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,45 @@ pub fn find_best_match_for_name<'a, T>(iter_names: T,
where T: Iterator<Item = &'a Symbol> {
let max_dist = dist.map_or_else(|| cmp::max(lookup.len(), 3) / 3, |d| d);

let (case_insensitive_match, levenstein_match) = iter_names
.filter_map(|&name| {
let dist = lev_distance(lookup, &name.as_str());
if dist <= max_dist {
Some((name, dist))
} else {
None
}
})
// Here we are collecting the next structure:
// (case_insensitive_match, (levenstein_match, levenstein_distance))
.fold((None, None), |result, (candidate, dist)| {
(
if candidate.as_str().to_uppercase() == lookup.to_uppercase() {
Some(candidate)
let (case_insensitive_match, levenstein_match, is_confusable) = iter_names
.filter_map(|&name| {
let dist = lev_distance(lookup, &name.as_str());
let is_confusable = unicode_skeleton::confusable(lookup, name.as_str().chars());
if dist <= max_dist || is_confusable {
Some((name, dist, is_confusable))
} else {
result.0
},
match result.1 {
None => Some((candidate, dist)),
Some((c, d)) => Some(if dist < d { (candidate, dist) } else { (c, d) })
None
}
)
});
})
// Here we are collecting the next structure:
// (case_insensitive_match, (levenstein_match, levenstein_distance))
.fold((None, None, None), |result, (candidate, dist, is_confusable)| {
(
if candidate.as_str().to_uppercase() == lookup.to_uppercase() {
Some(candidate)
} else {
result.0
},
match result.1 {
None => Some((candidate, dist)),
Some((c, d)) => Some(if dist < d { (candidate, dist) } else { (c, d) }),
},
match (is_confusable, result.2) {
(false, Some((c, d))) => Some((c, d)),
(false, None) => None,
(true, None) => Some((candidate, dist)),
(true, Some((c, d))) => Some(if dist < d { (candidate, dist) } else { (c, d) }),
}
)
});

if let Some(candidate) = case_insensitive_match {
Some(candidate) // exact case insensitive match has a higher priority
} else if let Some((candidate, _)) = is_confusable {
Some(candidate)
} else if let Some((candidate, _)) = levenstein_match {
Some(candidate)
} else {
if let Some((candidate, _)) = levenstein_match { Some(candidate) } else { None }
None
}
}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/unicode-confusable-typo.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
#![feature(non_ascii_idents)]

struct ℝ𝓊𝓈𝓉;

fn main() {
let ü = ℝ𝓊𝓈𝓉;
//~^ ERROR cannot find value `Rust` in this scope
let _ = ü;
//~^ ERROR cannot find value `u` in this scope
}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/unicode-confusable-typo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
#![feature(non_ascii_idents)]

struct ℝ𝓊𝓈𝓉;

fn main() {
let ü = Rust;
//~^ ERROR cannot find value `Rust` in this scope
let _ = u;
//~^ ERROR cannot find value `u` in this scope
}
18 changes: 18 additions & 0 deletions src/test/ui/suggestions/unicode-confusable-typo.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0425]: cannot find value `Rust` in this scope
--> $DIR/unicode-confusable-typo.rs:7:13
|
LL | struct ℝ𝓊𝓈𝓉;
| ------------ similarly named unit struct `ℝ𝓊𝓈𝓉` defined here
...
LL | let ü = Rust;
| ^^^^ help: a unit struct with a similar name exists: `ℝ𝓊𝓈𝓉`

error[E0425]: cannot find value `u` in this scope
--> $DIR/unicode-confusable-typo.rs:9:13
|
LL | let _ = u;
| ^ help: a local variable with a similar name exists: `ü`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.