Skip to content

librustc: offer suggestions for unresolved names. #5096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,40 @@ pub pure fn split_str_nonempty(s: &a/str, sep: &b/str) -> ~[~str] {
result
}

/// Levenshtein Distance between two strings
pub fn levdistance(s: &str, t: &str) -> uint {

let slen = str::len(s);
let tlen = str::len(t);

if slen == 0 { return tlen; }
if tlen == 0 { return slen; }

let mut dcol = vec::from_fn(tlen + 1, |x| x);

for str::each_chari(s) |i, sc| {

let mut current = i;
dcol[0] = current + 1;

for str::each_chari(t) |j, tc| {

let mut next = dcol[j + 1];

if sc == tc {
dcol[j + 1] = current;
} else {
dcol[j + 1] = ::cmp::min(current, next);
dcol[j + 1] = ::cmp::min(dcol[j + 1], dcol[j]) + 1;
}

current = next;
}
}

return dcol[tlen];
}

/**
* Splits a string into a vector of the substrings separated by LF ('\n')
*/
Expand Down
51 changes: 49 additions & 2 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4816,6 +4816,42 @@ pub impl Resolver {
}
}

fn find_best_match_for_name(@mut self, name: &str) -> Option<~str> {
let mut maybes: ~[~str] = ~[];
let mut values: ~[uint] = ~[];

let mut j = self.value_ribs.len();
while j != 0 {
j -= 1;
let rib = self.value_ribs.get_elt(j);
for rib.bindings.each_entry |e| {
vec::push(&mut maybes, copy *self.session.str_of(e.key));
vec::push(&mut values, uint::max_value);
}
}

let mut smallest = 0;
for vec::eachi(maybes) |i, &other| {

values[i] = str::levdistance(name, other);

if values[i] <= values[smallest] {
smallest = i;
}
}

if vec::len(values) > 0 &&
values[smallest] != uint::max_value &&
values[smallest] < str::len(name) + 2 &&
maybes[smallest] != name.to_owned() {

Some(vec::swap_remove(&mut maybes, smallest))

} else {
None
}
}

fn name_exists_in_scope_struct(@mut self, name: &str) -> bool {
let mut i = self.type_ribs.len();
while i != 0 {
Expand Down Expand Up @@ -4882,9 +4918,20 @@ pub impl Resolver {
wrong_name));
}
else {
self.session.span_err(expr.span,
fmt!("unresolved name: %s",
match self.find_best_match_for_name(wrong_name) {

Some(m) => {
self.session.span_err(expr.span,
fmt!("unresolved name: `%s`. \
Did you mean: `%s`?",
wrong_name, m));
}
None => {
self.session.span_err(expr.span,
fmt!("unresolved name: `%s`.",
wrong_name));
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/alt-join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ fn my_fail() -> ! { fail!(); }
fn main() {
match true { false => { my_fail(); } true => { } }

log(debug, x); //~ ERROR unresolved name: x
log(debug, x); //~ ERROR unresolved name: `x`.
let x: int;
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/bad-expr-path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: unresolved name: m1::a
// error-pattern: unresolved name: `m1::a`. Did you mean: `args`?

mod m1 {}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/bad-expr-path2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: unresolved name: m1::a
// error-pattern: unresolved name: `m1::a`. Did you mean: `args`?

mod m1 {
pub mod a {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/does-nothing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// error-pattern: unresolved name: this_does_nothing_what_the
// error-pattern: unresolved name: `this_does_nothing_what_the`.
fn main() { debug!("doing"); this_does_nothing_what_the; debug!("boing"); }

2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-1476.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// except according to those terms.

fn main() {
log(error, x); //~ ERROR unresolved name: x
log(error, x); //~ ERROR unresolved name: `x`.
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3021-b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn siphash(k0 : u64) {
impl siphash {
fn reset(&mut self) {
self.v0 = k0 ^ 0x736f6d6570736575; //~ ERROR attempted dynamic environment-capture
//~^ ERROR unresolved name: k0
//~^ ERROR unresolved name: `k0`.
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-3021-d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ fn siphash(k0 : u64, k1 : u64) -> siphash {
impl siphash for sipstate {
fn reset() {
self.v0 = k0 ^ 0x736f6d6570736575; //~ ERROR attempted dynamic environment-capture
//~^ ERROR unresolved name: k0
//~^ ERROR unresolved name: `k0`.
self.v1 = k1 ^ 0x646f72616e646f6d; //~ ERROR attempted dynamic environment-capture
//~^ ERROR unresolved name: k1
//~^ ERROR unresolved name: `k1`.
}
fn result() -> u64 { return mk_result(self); }
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn siphash(k0 : u64) -> siphash {
impl siphash for sipstate {
fn reset() {
self.v0 = k0 ^ 0x736f6d6570736575; //~ ERROR attempted dynamic environment-capture
//~^ ERROR unresolved name: k0
//~^ ERROR unresolved name: `k0`.
}
}
fail!();
Expand Down