@@ -33,7 +33,6 @@ extern crate syntax;
3333#[ no_link]
3434extern crate rustc_bitflags;
3535extern crate rustc_front;
36-
3736extern crate rustc;
3837
3938use self :: PatternBindingMode :: * ;
@@ -69,7 +68,7 @@ use syntax::ast::{TyUs, TyU8, TyU16, TyU32, TyU64, TyF64, TyF32};
6968use syntax:: attr:: AttrMetaMethods ;
7069use syntax:: parse:: token:: { self , special_names, special_idents} ;
7170use syntax:: codemap:: { self , Span , Pos } ;
72- use syntax:: util:: lev_distance:: { lev_distance , max_suggestion_distance } ;
71+ use syntax:: util:: lev_distance:: find_best_match_for_name ;
7372
7473use rustc_front:: intravisit:: { self , FnKind , Visitor } ;
7574use rustc_front:: hir;
@@ -94,7 +93,6 @@ use std::cell::{Cell, RefCell};
9493use std:: fmt;
9594use std:: mem:: replace;
9695use std:: rc:: { Rc , Weak } ;
97- use std:: usize;
9896
9997use resolve_imports:: { Target , ImportDirective , ImportResolutionPerNamespace } ;
10098use resolve_imports:: Shadowable ;
@@ -121,7 +119,7 @@ macro_rules! execute_callback {
121119
122120enum SuggestionType {
123121 Macro ( String ) ,
124- Function ( String ) ,
122+ Function ( token :: InternedString ) ,
125123 NotFound ,
126124}
127125
@@ -3352,39 +3350,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33523350 NoSuggestion
33533351 }
33543352
3355- fn find_best_match_for_name ( & mut self , name : & str ) -> SuggestionType {
3356- let mut maybes: Vec < token:: InternedString > = Vec :: new ( ) ;
3357- let mut values: Vec < usize > = Vec :: new ( ) ;
3358-
3353+ fn find_best_match ( & mut self , name : & str ) -> SuggestionType {
33593354 if let Some ( macro_name) = self . session . available_macros
3360- . borrow ( ) . iter ( ) . find ( |n| n. as_str ( ) == name) {
3355+ . borrow ( ) . iter ( ) . find ( |n| n. as_str ( ) == name) {
33613356 return SuggestionType :: Macro ( format ! ( "{}!" , macro_name) ) ;
33623357 }
33633358
3364- for rib in self . value_ribs . iter ( ) . rev ( ) {
3365- for ( & k, _) in & rib. bindings {
3366- maybes. push ( k. as_str ( ) ) ;
3367- values. push ( usize:: MAX ) ;
3368- }
3369- }
3370-
3371- let mut smallest = 0 ;
3372- for ( i, other) in maybes. iter ( ) . enumerate ( ) {
3373- values[ i] = lev_distance ( name, & other) ;
3359+ let names = self . value_ribs
3360+ . iter ( )
3361+ . rev ( )
3362+ . flat_map ( |rib| rib. bindings . keys ( ) ) ;
33743363
3375- if values[ i] <= values[ smallest] {
3376- smallest = i;
3364+ if let Some ( found) = find_best_match_for_name ( names, name, None ) {
3365+ if name != & * found {
3366+ return SuggestionType :: Function ( found) ;
33773367 }
3378- }
3379-
3380- let max_distance = max_suggestion_distance ( name) ;
3381- if !values. is_empty ( ) && values[ smallest] <= max_distance && name != & maybes[ smallest] [ ..] {
3382-
3383- SuggestionType :: Function ( maybes[ smallest] . to_string ( ) )
3384-
3385- } else {
3386- SuggestionType :: NotFound
3387- }
3368+ } SuggestionType :: NotFound
33883369 }
33893370
33903371 fn resolve_expr ( & mut self , expr : & Expr ) {
@@ -3495,7 +3476,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34953476 NoSuggestion => {
34963477 // limit search to 5 to reduce the number
34973478 // of stupid suggestions
3498- match self . find_best_match_for_name ( & path_name) {
3479+ match self . find_best_match ( & path_name) {
34993480 SuggestionType :: Macro ( s) => {
35003481 format ! ( "the macro `{}`" , s)
35013482 }
0 commit comments