Skip to content

Commit 537f2a6

Browse files
author
Julian Kulesh
committed
move comparator into +find_best_match_name+ function
1 parent 6506f9c commit 537f2a6

File tree

5 files changed

+55
-30
lines changed

5 files changed

+55
-30
lines changed

src/librustc_resolve/lib.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,17 +3213,6 @@ impl<'a> Resolver<'a> {
32133213
// Make sure error reporting is deterministic.
32143214
names.sort_by_key(|name| name.as_str());
32153215

3216-
3217-
// Ugly code, just to see if using case insensitive comparison will help
3218-
let exact_match = names.iter().find(|x| x.as_str().to_uppercase() == name.as_str().to_uppercase());
3219-
// do not use Levenstein, just return the value we found (if any)
3220-
if exact_match.is_some() {
3221-
return match exact_match {
3222-
Some(found) => Some(found.clone()),
3223-
_ => None,
3224-
}
3225-
}
3226-
32273216
match find_best_match_for_name(names.iter(), &name.as_str(), None) {
32283217
Some(found) if found != name => Some(found),
32293218
_ => None,

src/libsyntax/util/lev_distance.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,45 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
4444
/// To find the best match for a given string from an iterator of names
4545
/// As a loose rule to avoid the obviously incorrect suggestions, it takes
4646
/// an optional limit for the maximum allowable edit distance, which defaults
47-
/// to one-third of the given word
47+
/// to one-third of the given word.
48+
/// Besides Levenshtein, we use case insensitive comparison to improve accuracy on an edge case with
49+
/// a lower(upper)case letters mismatch.
4850
pub fn find_best_match_for_name<'a, T>(iter_names: T,
4951
lookup: &str,
5052
dist: Option<usize>) -> Option<Symbol>
5153
where T: Iterator<Item = &'a Symbol> {
5254
let max_dist = dist.map_or_else(|| cmp::max(lookup.len(), 3) / 3, |d| d);
53-
iter_names
55+
56+
let (case_insensitive_match, levenstein_match) = iter_names
5457
.filter_map(|&name| {
5558
let dist = lev_distance(lookup, &name.as_str());
56-
if dist <= max_dist { // filter the unwanted cases
59+
if dist <= max_dist {
5760
Some((name, dist))
5861
} else {
5962
None
6063
}
6164
})
62-
.min_by_key(|&(_, val)| val) // extract the tuple containing the minimum edit distance
63-
.map(|(s, _)| s) // and return only the string
65+
// Here we are collecting the next structure:
66+
// (case_insensitive_match, (levenstein_match, levenstein_distance))
67+
.fold((None, None), |result, (candidate, dist)| {
68+
(
69+
if candidate.as_str().to_uppercase() == lookup.to_uppercase() {
70+
Some(candidate)
71+
} else {
72+
result.0
73+
},
74+
match result.1 {
75+
None => Some((candidate, dist)),
76+
Some((c, d)) => Some(if dist < d { (candidate, dist) } else { (c, d) })
77+
}
78+
)
79+
});
80+
81+
if let Some(candidate) = case_insensitive_match {
82+
Some(candidate) // exact case insensitive match has a higher priority
83+
} else {
84+
if let Some((candidate, _)) = levenstein_match { Some(candidate) } else { None }
85+
}
6486
}
6587

6688
#[test]

src/test/ui/issue-46332.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Original Levenshtein distance for both of this is 1. We improved accuracy with
12+
// additional case insensitive comparison.
13+
14+
struct TyUint {}
15+
16+
struct TyInt {}
17+
18+
fn main() {
19+
TyUInt {};
20+
}

src/test/ui/issue-46332.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0422]: cannot find struct, variant or union type `TyUInt` in this scope
2+
--> $DIR/issue-46332.rs:19:5
3+
|
4+
19 | TyUInt {};
5+
| ^^^^^^ did you mean `TyUint`?
6+
7+
error: aborting due to previous error
8+

src/test/ui/issue46332.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)