@@ -15,7 +15,7 @@ struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
1515 hir_map : & ' a hir:: map:: Map < ' gcx > ,
1616 found_local_pattern : Option < & ' gcx Pat > ,
1717 found_arg_pattern : Option < & ' gcx Pat > ,
18- found_ty : Option < Ty < ' tcx > > ,
18+ found_ty : Option < String > ,
1919}
2020
2121impl < ' a , ' gcx , ' tcx > FindLocalByTypeVisitor < ' a , ' gcx , ' tcx > {
@@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
5555 fn visit_local ( & mut self , local : & ' gcx Local ) {
5656 if let ( None , Some ( ty) ) = ( self . found_local_pattern , self . node_matches_type ( local. hir_id ) ) {
5757 self . found_local_pattern = Some ( & * local. pat ) ;
58- self . found_ty = Some ( ty) ;
58+ self . found_ty = Some ( ty. to_string ( ) ) ;
5959 }
6060 intravisit:: walk_local ( self , local) ;
6161 }
@@ -67,7 +67,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
6767 self . node_matches_type ( argument. hir_id ) ,
6868 ) {
6969 self . found_arg_pattern = Some ( & * argument. pat ) ;
70- self . found_ty = Some ( ty) ;
70+ self . found_ty = Some ( ty. to_string ( ) ) ;
7171 }
7272 }
7373 intravisit:: walk_body ( self , body) ;
@@ -108,7 +108,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
108108 let name = self . extract_type_name ( & ty, None ) ;
109109
110110 let mut err_span = span;
111- let mut labels = vec ! [ ( span, InferCtxt :: missing_type_msg( & name) ) ] ;
112111
113112 let mut local_visitor = FindLocalByTypeVisitor {
114113 infcx : & self ,
@@ -124,10 +123,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
124123 local_visitor. visit_expr ( expr) ;
125124 }
126125
127- let ty_msg = match local_visitor. found_ty {
128- Some ( ty) if & ty. to_string ( ) != "_" = > format ! ( " for `{}`" , ty) ,
126+ let ty_msg = match & local_visitor. found_ty {
127+ Some ( ty) if & ty[ .. ] != "_" && ty != & name = > format ! ( " in `{}`" , ty) ,
129128 _ => String :: new ( ) ,
130129 } ;
130+ let mut labels = vec ! [ ( span, InferCtxt :: missing_type_msg( & name, & ty_msg) ) ] ;
131+
131132 if let Some ( pattern) = local_visitor. found_arg_pattern {
132133 err_span = pattern. span ;
133134 // We don't want to show the default label for closures.
@@ -148,8 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
148149 labels. clear ( ) ;
149150 labels. push ( ( pattern. span , format ! (
150151 "consider giving this closure parameter {}" ,
151- match local_visitor. found_ty {
152- Some ( ty) if & ty. to_string ( ) != "_" => format!(
152+ match & local_visitor. found_ty {
153+ Some ( ty) if & ty[ .. ] != "_" && ty != & name => format!(
153154 "the type `{}` with the type parameter `{}` specified" ,
154155 ty,
155156 name,
@@ -162,18 +163,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
162163 match pattern. span . compiler_desugaring_kind ( ) {
163164 None => labels. push ( (
164165 pattern. span ,
165- format ! (
166- "consider giving `{}` {}" ,
167- simple_ident,
168- match local_visitor. found_ty {
169- Some ( ty) if & ty. to_string( ) != "_" => format!(
170- "the type `{}` with the type parameter `{}` specified" ,
171- ty,
172- name,
173- ) ,
174- _ => "a type" . to_owned( ) ,
175- } ,
176- ) ,
166+ format ! ( "consider giving `{}` a type" , simple_ident) ,
177167 ) ) ,
178168 Some ( CompilerDesugaringKind :: ForLoop ) => labels. push ( (
179169 pattern. span ,
@@ -213,15 +203,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
213203 span,
214204 E0698 ,
215205 "type inside generator must be known in this context" ) ;
216- err. span_label ( span, InferCtxt :: missing_type_msg ( & name) ) ;
206+ err. span_label ( span, InferCtxt :: missing_type_msg ( & name, "" ) ) ;
217207 err
218208 }
219209
220- fn missing_type_msg ( type_name : & str ) -> String {
210+ fn missing_type_msg ( type_name : & str , postfix : & str ) -> String {
221211 if type_name == "_" {
222212 "cannot infer type" . to_owned ( )
223213 } else {
224- format ! ( "cannot infer type for `{}`" , type_name)
214+ format ! ( "cannot infer type for `{}`{} " , type_name, postfix )
225215 }
226216 }
227217}
0 commit comments