@@ -14,11 +14,11 @@ use rustc::hir::map as hir_map;
1414use rustc:: hir:: print;
1515use rustc:: infer:: type_variable:: TypeVariableOrigin ;
1616use rustc:: traits:: Obligation ;
17- use rustc:: ty:: { self , Adt , Ty , TyCtxt , ToPolyTraitRef , ToPredicate , TypeFoldable } ;
17+ use rustc:: ty:: { self , Ty , TyCtxt , ToPolyTraitRef , ToPredicate , TypeFoldable } ;
1818use rustc:: ty:: print:: with_crate_prefix;
1919use syntax_pos:: { Span , FileName } ;
2020use syntax:: ast;
21- use syntax:: util:: lev_distance:: find_best_match_for_name ;
21+ use syntax:: util:: lev_distance;
2222
2323use std:: cmp:: Ordering ;
2424
@@ -188,17 +188,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
188188 let actual = self . resolve_type_vars_if_possible ( & rcvr_ty) ;
189189 let ty_str = self . ty_to_string ( actual) ;
190190 let is_method = mode == Mode :: MethodCall ;
191- let mut suggestion = None ;
192191 let item_kind = if is_method {
193192 "method"
194193 } else if actual. is_enum ( ) {
195- if let Adt ( ref adt_def, _) = actual. sty {
196- let names = adt_def. variants . iter ( ) . map ( |s| & s. ident . name ) ;
197- suggestion = find_best_match_for_name ( names,
198- & item_name. as_str ( ) ,
199- None ) ;
200- }
201- "variant"
194+ "variant or associated item"
202195 } else {
203196 match ( item_name. as_str ( ) . chars ( ) . next ( ) , actual. is_fresh_ty ( ) ) {
204197 ( Some ( name) , false ) if name. is_lowercase ( ) => {
@@ -299,25 +292,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
299292 return ;
300293 } else {
301294 span = item_name. span ;
302- let mut err = struct_span_err ! (
295+ struct_span_err ! (
303296 tcx. sess,
304297 span,
305298 E0599 ,
306299 "no {} named `{}` found for type `{}` in the current scope" ,
307300 item_kind,
308301 item_name,
309302 ty_str
310- ) ;
311- if let Some ( suggestion) = suggestion {
312- // enum variant
313- err. span_suggestion (
314- span,
315- "did you mean" ,
316- suggestion. to_string ( ) ,
317- Applicability :: MaybeIncorrect ,
318- ) ;
319- }
320- err
303+ )
321304 }
322305 } else {
323306 tcx. sess . diagnostic ( ) . struct_dummy ( )
@@ -469,14 +452,36 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
469452 out_of_scope_traits) ;
470453 }
471454
455+ if actual. is_enum ( ) {
456+ let adt_def = actual. ty_adt_def ( ) . expect ( "enum is not an ADT" ) ;
457+ if let Some ( suggestion) = lev_distance:: find_best_match_for_name (
458+ adt_def. variants . iter ( ) . map ( |s| & s. ident . name ) ,
459+ & item_name. as_str ( ) ,
460+ None ,
461+ ) {
462+ err. span_suggestion (
463+ span,
464+ "there is a variant with a similar name" ,
465+ suggestion. to_string ( ) ,
466+ Applicability :: MaybeIncorrect ,
467+ ) ;
468+ }
469+ }
470+
472471 if let Some ( lev_candidate) = lev_candidate {
472+ let def = lev_candidate. def ( ) ;
473473 err. span_suggestion (
474474 span,
475- "did you mean" ,
475+ & format ! (
476+ "there is {} {} with a similar name" ,
477+ def. article( ) ,
478+ def. kind_name( ) ,
479+ ) ,
476480 lev_candidate. ident . to_string ( ) ,
477481 Applicability :: MaybeIncorrect ,
478482 ) ;
479483 }
484+
480485 err. emit ( ) ;
481486 }
482487
0 commit comments