@@ -15,7 +15,7 @@ struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
15
15
hir_map : & ' a hir:: map:: Map < ' gcx > ,
16
16
found_local_pattern : Option < & ' gcx Pat > ,
17
17
found_arg_pattern : Option < & ' gcx Pat > ,
18
- found_ty : Option < Ty < ' tcx > > ,
18
+ found_ty : Option < String > ,
19
19
}
20
20
21
21
impl < ' a , ' gcx , ' tcx > FindLocalByTypeVisitor < ' a , ' gcx , ' tcx > {
@@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
55
55
fn visit_local ( & mut self , local : & ' gcx Local ) {
56
56
if let ( None , Some ( ty) ) = ( self . found_local_pattern , self . node_matches_type ( local. hir_id ) ) {
57
57
self . found_local_pattern = Some ( & * local. pat ) ;
58
- self . found_ty = Some ( ty) ;
58
+ self . found_ty = Some ( ty. to_string ( ) ) ;
59
59
}
60
60
intravisit:: walk_local ( self , local) ;
61
61
}
@@ -67,7 +67,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
67
67
self . node_matches_type ( argument. hir_id ) ,
68
68
) {
69
69
self . found_arg_pattern = Some ( & * argument. pat ) ;
70
- self . found_ty = Some ( ty) ;
70
+ self . found_ty = Some ( ty. to_string ( ) ) ;
71
71
}
72
72
}
73
73
intravisit:: walk_body ( self , body) ;
@@ -108,7 +108,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
108
108
let name = self . extract_type_name ( & ty, None ) ;
109
109
110
110
let mut err_span = span;
111
- let mut labels = vec ! [ ( span, InferCtxt :: missing_type_msg( & name) ) ] ;
112
111
113
112
let mut local_visitor = FindLocalByTypeVisitor {
114
113
infcx : & self ,
@@ -124,10 +123,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
124
123
local_visitor. visit_expr ( expr) ;
125
124
}
126
125
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) ,
129
128
_ => String :: new ( ) ,
130
129
} ;
130
+ let mut labels = vec ! [ ( span, InferCtxt :: missing_type_msg( & name, & ty_msg) ) ] ;
131
+
131
132
if let Some ( pattern) = local_visitor. found_arg_pattern {
132
133
err_span = pattern. span ;
133
134
// We don't want to show the default label for closures.
@@ -148,8 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
148
149
labels. clear ( ) ;
149
150
labels. push ( ( pattern. span , format ! (
150
151
"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!(
153
154
"the type `{}` with the type parameter `{}` specified" ,
154
155
ty,
155
156
name,
@@ -162,18 +163,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
162
163
match pattern. span . compiler_desugaring_kind ( ) {
163
164
None => labels. push ( (
164
165
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) ,
177
167
) ) ,
178
168
Some ( CompilerDesugaringKind :: ForLoop ) => labels. push ( (
179
169
pattern. span ,
@@ -213,15 +203,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
213
203
span,
214
204
E0698 ,
215
205
"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, "" ) ) ;
217
207
err
218
208
}
219
209
220
- fn missing_type_msg ( type_name : & str ) -> String {
210
+ fn missing_type_msg ( type_name : & str , postfix : & str ) -> String {
221
211
if type_name == "_" {
222
212
"cannot infer type" . to_owned ( )
223
213
} else {
224
- format ! ( "cannot infer type for `{}`" , type_name)
214
+ format ! ( "cannot infer type for `{}`{} " , type_name, postfix )
225
215
}
226
216
}
227
217
}
0 commit comments