Skip to content

Commit 7a64987

Browse files
committed
Auto merge of #46347 - raventid:did-you-mean-increase-accuracy, r=estebank
Add case insensitive comparison, besides Levenstein for DYM Closes #46332 Draft version. The idea is that Levenstein does not work for some cases when we have multiple equal weights for strings. I didn't understand the case with `if found != name => Some(found)` so it means that new code does not work correctly yet. At least now I think that we might return all maximal weights from levenstein and think about next cases in priority order: 1) There is exact match -> None 2) There is exact match, but case insensitive -> Some(match) 3) There is some match from levenstein -> Some(matches.take_any) 4) There is no match -> None @estebank WDYT?
2 parents e0d11f3 + f18446e commit 7a64987

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/libsyntax/util/lev_distance.rs

+27-5
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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
//~^ ERROR cannot find struct, variant or union type `TyUInt` in this scope
21+
}

src/test/ui/issue-46332.stderr

+8
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+

0 commit comments

Comments
 (0)