Skip to content

Commit

Permalink
Rollup merge of #118381 - Enselic:edit-dist-len, r=WaffleLapkin
Browse files Browse the repository at this point in the history
rustc_span: Use correct edit distance start length for suggestions

Otherwise the suggestions can be off-base for non-ASCII identifiers. For example suggesting that `Ok` is a name similar to `读文`.

Closes #72553.
  • Loading branch information
compiler-errors authored Nov 28, 2023
2 parents 7851d98 + 45fc842 commit 344459e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_span/src/edit_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ fn find_best_match_for_name_impl(
return Some(*c);
}

let mut dist = dist.unwrap_or_else(|| cmp::max(lookup.len(), 3) / 3);
// `fn edit_distance()` use `chars()` to calculate edit distance, so we must
// also use `chars()` (and not `str::len()`) to calculate length here.
let lookup_len = lookup.chars().count();

let mut dist = dist.unwrap_or_else(|| cmp::max(lookup_len, 3) / 3);
let mut best = None;
// store the candidates with the same distance, only for `use_substring_score` current.
let mut next_candidates = vec![];
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/suggestions/non_ascii_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
// There shall be no suggestions here. In particular not `Ok`.
let _ = 读文; //~ ERROR cannot find value `读文` in this scope
}
9 changes: 9 additions & 0 deletions tests/ui/suggestions/non_ascii_ident.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0425]: cannot find value `读文` in this scope
--> $DIR/non_ascii_ident.rs:3:13
|
LL | let _ = 读文;
| ^^^^ not found in this scope

error: aborting due to 1 previous error

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

0 comments on commit 344459e

Please sign in to comment.