librustc: Limit the typo suggestions to reasonable suggests.

Impose a limit so that the typo suggester only shows reasonable
suggestions (i.e. don't suggest `args` when the error is `foobar`).
This commit is contained in:
Huon Wilson 2013-03-27 18:15:59 +11:00
parent 2888563510
commit ab5346d119
2 changed files with 18 additions and 4 deletions

View file

@ -4667,7 +4667,7 @@ fn resolve_item_by_identifier_in_lexical_scope(@mut self,
}
}
fn find_best_match_for_name(@mut self, name: &str) -> Option<~str> {
fn find_best_match_for_name(@mut self, name: &str, max_distance: uint) -> Option<~str> {
let this = &mut *self;
let mut maybes: ~[~str] = ~[];
@ -4695,6 +4695,7 @@ fn find_best_match_for_name(@mut self, name: &str) -> Option<~str> {
if vec::len(values) > 0 &&
values[smallest] != uint::max_value &&
values[smallest] < str::len(name) + 2 &&
values[smallest] <= max_distance &&
maybes[smallest] != name.to_owned() {
Some(vec::swap_remove(&mut maybes, smallest))
@ -4771,8 +4772,9 @@ fn resolve_expr(@mut self, expr: @expr, visitor: ResolveVisitor) {
wrong_name));
}
else {
match self.find_best_match_for_name(wrong_name) {
// limit search to 5 to reduce the number
// of stupid suggestions
match self.find_best_match_for_name(wrong_name, 5) {
Some(m) => {
self.session.span_err(expr.span,
fmt!("unresolved name: `%s`. \
@ -5293,4 +5295,3 @@ pub fn resolve_crate(session: Session,
trait_map: trait_map
}
}

View file

@ -0,0 +1,13 @@
// Copyright 2012 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.
// error-pattern: unresolved name: `foobar`.
fn main(args: ~[str]) { debug!(foobar); }