Skip to content

Commit

Permalink
librustc: Limit the typo suggestions to reasonable suggests.
Browse files Browse the repository at this point in the history
Impose a limit so that the typo suggester only shows reasonable
suggestions (i.e. don't suggest `args` when the error is `foobar`).
  • Loading branch information
huonw committed Mar 27, 2013
1 parent 2888563 commit ab5346d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ~[];
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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`. \
Expand Down Expand Up @@ -5293,4 +5295,3 @@ pub fn resolve_crate(session: Session,
trait_map: trait_map
}
}

13 changes: 13 additions & 0 deletions src/test/compile-fail/issue-2281-part1.rs
Original file line number Diff line number Diff line change
@@ -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); }

5 comments on commit ab5346d

@bors
Copy link
Contributor

@bors bors commented on ab5346d Mar 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on ab5346d Mar 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dbaupp/rust/rustc-typo-limit = ab5346d into auto

@bors
Copy link
Contributor

@bors bors commented on ab5346d Mar 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbaupp/rust/rustc-typo-limit = ab5346d merged ok, testing candidate = d7ba0ac

@bors
Copy link
Contributor

@bors bors commented on ab5346d Mar 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on ab5346d Mar 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = d7ba0ac

Please sign in to comment.