From ab5346d119b6ac3ba9e9d67df929fdf546865d0a Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 27 Mar 2013 18:15:59 +1100 Subject: [PATCH] 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`). --- src/librustc/middle/resolve.rs | 9 +++++---- src/test/compile-fail/issue-2281-part1.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/test/compile-fail/issue-2281-part1.rs diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index afc2c9f3352d3..341288b5d6cf0 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -4667,7 +4667,7 @@ pub impl Resolver { } } - 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 @@ pub impl Resolver { 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 @@ pub impl Resolver { 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 } } - diff --git a/src/test/compile-fail/issue-2281-part1.rs b/src/test/compile-fail/issue-2281-part1.rs new file mode 100644 index 0000000000000..3951eaad6d197 --- /dev/null +++ b/src/test/compile-fail/issue-2281-part1.rs @@ -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 or the MIT license +// , 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); }