@@ -883,40 +883,50 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
883
883
} )
884
884
}
885
885
886
+ fn extract_type_name ( & self , ty : & ' a Ty < ' tcx > ) -> String {
887
+ if let ty:: TyInfer ( ty:: TyVar ( ty_vid) ) = ( * ty) . sty {
888
+ let ty_vars = self . type_variables . borrow ( ) ;
889
+ if let TypeVariableOrigin :: TypeParameterDefinition ( _, name) =
890
+ * ty_vars. var_origin ( ty_vid) {
891
+ name. to_string ( )
892
+ } else {
893
+ ty. to_string ( )
894
+ }
895
+ } else {
896
+ ty. to_string ( )
897
+ }
898
+ }
886
899
887
- fn need_type_info ( & self ,
888
- obligation : & PredicateObligation < ' tcx > ,
889
- ty : Ty < ' tcx > ) {
890
-
900
+ fn need_type_info ( & self , obligation : & PredicateObligation < ' tcx > , ty : Ty < ' tcx > ) {
891
901
let ty = self . resolve_type_vars_if_possible ( & ty) ;
902
+ let name = self . extract_type_name ( & ty) ;
892
903
let ref cause = obligation. cause ;
893
904
894
905
let mut err = struct_span_err ! ( self . tcx. sess,
895
906
cause. span,
896
907
E0282 ,
897
- "unable to fully infer type(s) " ) ;
908
+ "type annotations needed " ) ;
898
909
899
- err. note ( "type annotations or generic parameter binding required" ) ;
900
- err. span_label ( cause. span , & format ! ( "cannot infer type" ) ) ;
910
+ err. span_label ( cause. span , & format ! ( "cannot infer type for `{}`" , name) ) ;
901
911
902
912
let expr = self . tcx . hir . expect_expr ( cause. body_id ) ;
903
913
904
914
let mut local_visitor = FindLocalByTypeVisitor {
905
915
infcx : & self ,
906
916
target_ty : & ty,
907
- found_pattern : None
917
+ found_pattern : None ,
908
918
} ;
909
919
910
920
local_visitor. visit_expr ( expr) ;
911
921
912
922
if let Some ( pattern) = local_visitor. found_pattern {
913
923
let pattern_span = pattern. span ;
914
- if let Some ( n ) = pattern. simple_name ( ) {
924
+ if let Some ( simple_name ) = pattern. simple_name ( ) {
915
925
err. span_label ( pattern_span,
916
- & format ! ( "annotating the type for the variable `{}` would help" , n) ) ;
926
+ & format ! ( "consider giving `{}` a type" ,
927
+ simple_name) ) ;
917
928
} else {
918
- err. span_label ( pattern_span,
919
- & format ! ( "annotating the type of pattern would help" ) ) ;
929
+ err. span_label ( pattern_span, & format ! ( "consider giving a type to pattern" ) ) ;
920
930
}
921
931
}
922
932
0 commit comments