@@ -30,7 +30,6 @@ extern crate syntax;
3030#[ no_link]
3131extern crate rustc_bitflags;
3232extern crate rustc_front;
33-
3433extern crate rustc;
3534
3635use self :: PatternBindingMode :: * ;
@@ -66,7 +65,7 @@ use syntax::ast::{TyUs, TyU8, TyU16, TyU32, TyU64, TyF64, TyF32};
6665use syntax:: attr:: AttrMetaMethods ;
6766use syntax:: parse:: token:: { self , special_names, special_idents} ;
6867use syntax:: codemap:: { self , Span , Pos } ;
69- use syntax:: util:: lev_distance:: { lev_distance , max_suggestion_distance } ;
68+ use syntax:: util:: lev_distance:: find_best_match_for_name ;
7069
7170use rustc_front:: intravisit:: { self , FnKind , Visitor } ;
7271use rustc_front:: hir;
@@ -91,7 +90,6 @@ use std::cell::{Cell, RefCell};
9190use std:: fmt;
9291use std:: mem:: replace;
9392use std:: rc:: { Rc , Weak } ;
94- use std:: usize;
9593
9694use resolve_imports:: { Target , ImportDirective , ImportResolutionPerNamespace } ;
9795use resolve_imports:: Shadowable ;
@@ -118,7 +116,7 @@ macro_rules! execute_callback {
118116
119117enum SuggestionType {
120118 Macro ( String ) ,
121- Function ( String ) ,
119+ Function ( token :: InternedString ) ,
122120 NotFound ,
123121}
124122
@@ -3422,39 +3420,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34223420 NoSuggestion
34233421 }
34243422
3425- fn find_best_match_for_name ( & mut self , name : & str ) -> SuggestionType {
3426- let mut maybes: Vec < token:: InternedString > = Vec :: new ( ) ;
3427- let mut values: Vec < usize > = Vec :: new ( ) ;
3428-
3423+ fn find_best_match ( & mut self , name : & str ) -> SuggestionType {
34293424 if let Some ( macro_name) = self . session . available_macros
3430- . borrow ( ) . iter ( ) . find ( |n| n. as_str ( ) == name) {
3425+ . borrow ( ) . iter ( ) . find ( |n| n. as_str ( ) == name) {
34313426 return SuggestionType :: Macro ( format ! ( "{}!" , macro_name) ) ;
34323427 }
34333428
3434- for rib in self . value_ribs . iter ( ) . rev ( ) {
3435- for ( & k, _) in & rib. bindings {
3436- maybes. push ( k. as_str ( ) ) ;
3437- values. push ( usize:: MAX ) ;
3438- }
3439- }
3440-
3441- let mut smallest = 0 ;
3442- for ( i, other) in maybes. iter ( ) . enumerate ( ) {
3443- values[ i] = lev_distance ( name, & other) ;
3429+ let names = self . value_ribs
3430+ . iter ( )
3431+ . rev ( )
3432+ . flat_map ( |rib| rib. bindings . keys ( ) ) ;
34443433
3445- if values[ i] <= values[ smallest] {
3446- smallest = i;
3434+ if let Some ( found) = find_best_match_for_name ( names, name, None ) {
3435+ if name != & * found {
3436+ return SuggestionType :: Function ( found) ;
34473437 }
3448- }
3449-
3450- let max_distance = max_suggestion_distance ( name) ;
3451- if !values. is_empty ( ) && values[ smallest] <= max_distance && name != & maybes[ smallest] [ ..] {
3452-
3453- SuggestionType :: Function ( maybes[ smallest] . to_string ( ) )
3454-
3455- } else {
3456- SuggestionType :: NotFound
3457- }
3438+ } SuggestionType :: NotFound
34583439 }
34593440
34603441 fn resolve_expr ( & mut self , expr : & Expr ) {
@@ -3568,7 +3549,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35683549 NoSuggestion => {
35693550 // limit search to 5 to reduce the number
35703551 // of stupid suggestions
3571- match self . find_best_match_for_name ( & path_name) {
3552+ match self . find_best_match ( & path_name) {
35723553 SuggestionType :: Macro ( s) => {
35733554 format ! ( "the macro `{}`" , s)
35743555 }
0 commit comments