@@ -44,8 +44,8 @@ struct InherentCollect<'tcx> {
44
44
45
45
impl ItemLikeVisitor < ' v > for InherentCollect < ' tcx > {
46
46
fn visit_item ( & mut self , item : & hir:: Item < ' _ > ) {
47
- let ty = match item. kind {
48
- hir:: ItemKind :: Impl { of_trait : None , ref self_ty, .. } => self_ty,
47
+ let ( ty , assoc_items ) = match item. kind {
48
+ hir:: ItemKind :: Impl { of_trait : None , ref self_ty, items , .. } => ( self_ty, items ) ,
49
49
_ => return ,
50
50
} ;
51
51
@@ -70,6 +70,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
70
70
"bool" ,
71
71
"bool" ,
72
72
item. span ,
73
+ assoc_items,
73
74
) ;
74
75
}
75
76
ty:: Char => {
@@ -80,6 +81,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
80
81
"char" ,
81
82
"char" ,
82
83
item. span ,
84
+ assoc_items,
83
85
) ;
84
86
}
85
87
ty:: Str => {
@@ -90,6 +92,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
90
92
"str" ,
91
93
"str" ,
92
94
item. span ,
95
+ assoc_items,
93
96
) ;
94
97
}
95
98
ty:: Slice ( slice_item) if slice_item == self . tcx . types . u8 => {
@@ -100,6 +103,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
100
103
"slice_u8" ,
101
104
"[u8]" ,
102
105
item. span ,
106
+ assoc_items,
103
107
) ;
104
108
}
105
109
ty:: Slice ( _) => {
@@ -110,6 +114,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
110
114
"slice" ,
111
115
"[T]" ,
112
116
item. span ,
117
+ assoc_items,
113
118
) ;
114
119
}
115
120
ty:: Array ( _, _) => {
@@ -120,6 +125,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
120
125
"array" ,
121
126
"[T; N]" ,
122
127
item. span ,
128
+ assoc_items,
123
129
) ;
124
130
}
125
131
ty:: RawPtr ( ty:: TypeAndMut { ty : inner, mutbl : hir:: Mutability :: Not } )
@@ -132,6 +138,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
132
138
"const_slice_ptr" ,
133
139
"*const [T]" ,
134
140
item. span ,
141
+ assoc_items,
135
142
) ;
136
143
}
137
144
ty:: RawPtr ( ty:: TypeAndMut { ty : inner, mutbl : hir:: Mutability :: Mut } )
@@ -144,6 +151,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
144
151
"mut_slice_ptr" ,
145
152
"*mut [T]" ,
146
153
item. span ,
154
+ assoc_items,
147
155
) ;
148
156
}
149
157
ty:: RawPtr ( ty:: TypeAndMut { ty : _, mutbl : hir:: Mutability :: Not } ) => {
@@ -154,6 +162,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
154
162
"const_ptr" ,
155
163
"*const T" ,
156
164
item. span ,
165
+ assoc_items,
157
166
) ;
158
167
}
159
168
ty:: RawPtr ( ty:: TypeAndMut { ty : _, mutbl : hir:: Mutability :: Mut } ) => {
@@ -164,6 +173,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
164
173
"mut_ptr" ,
165
174
"*mut T" ,
166
175
item. span ,
176
+ assoc_items,
167
177
) ;
168
178
}
169
179
ty:: Int ( ast:: IntTy :: I8 ) => {
@@ -174,6 +184,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
174
184
"i8" ,
175
185
"i8" ,
176
186
item. span ,
187
+ assoc_items,
177
188
) ;
178
189
}
179
190
ty:: Int ( ast:: IntTy :: I16 ) => {
@@ -184,6 +195,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
184
195
"i16" ,
185
196
"i16" ,
186
197
item. span ,
198
+ assoc_items,
187
199
) ;
188
200
}
189
201
ty:: Int ( ast:: IntTy :: I32 ) => {
@@ -194,6 +206,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
194
206
"i32" ,
195
207
"i32" ,
196
208
item. span ,
209
+ assoc_items,
197
210
) ;
198
211
}
199
212
ty:: Int ( ast:: IntTy :: I64 ) => {
@@ -204,6 +217,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
204
217
"i64" ,
205
218
"i64" ,
206
219
item. span ,
220
+ assoc_items,
207
221
) ;
208
222
}
209
223
ty:: Int ( ast:: IntTy :: I128 ) => {
@@ -214,6 +228,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
214
228
"i128" ,
215
229
"i128" ,
216
230
item. span ,
231
+ assoc_items,
217
232
) ;
218
233
}
219
234
ty:: Int ( ast:: IntTy :: Isize ) => {
@@ -224,6 +239,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
224
239
"isize" ,
225
240
"isize" ,
226
241
item. span ,
242
+ assoc_items,
227
243
) ;
228
244
}
229
245
ty:: Uint ( ast:: UintTy :: U8 ) => {
@@ -234,6 +250,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
234
250
"u8" ,
235
251
"u8" ,
236
252
item. span ,
253
+ assoc_items,
237
254
) ;
238
255
}
239
256
ty:: Uint ( ast:: UintTy :: U16 ) => {
@@ -244,6 +261,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
244
261
"u16" ,
245
262
"u16" ,
246
263
item. span ,
264
+ assoc_items,
247
265
) ;
248
266
}
249
267
ty:: Uint ( ast:: UintTy :: U32 ) => {
@@ -254,6 +272,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
254
272
"u32" ,
255
273
"u32" ,
256
274
item. span ,
275
+ assoc_items,
257
276
) ;
258
277
}
259
278
ty:: Uint ( ast:: UintTy :: U64 ) => {
@@ -264,6 +283,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
264
283
"u64" ,
265
284
"u64" ,
266
285
item. span ,
286
+ assoc_items,
267
287
) ;
268
288
}
269
289
ty:: Uint ( ast:: UintTy :: U128 ) => {
@@ -274,6 +294,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
274
294
"u128" ,
275
295
"u128" ,
276
296
item. span ,
297
+ assoc_items,
277
298
) ;
278
299
}
279
300
ty:: Uint ( ast:: UintTy :: Usize ) => {
@@ -284,6 +305,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
284
305
"usize" ,
285
306
"usize" ,
286
307
item. span ,
308
+ assoc_items,
287
309
) ;
288
310
}
289
311
ty:: Float ( ast:: FloatTy :: F32 ) => {
@@ -294,6 +316,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
294
316
"f32" ,
295
317
"f32" ,
296
318
item. span ,
319
+ assoc_items,
297
320
) ;
298
321
}
299
322
ty:: Float ( ast:: FloatTy :: F64 ) => {
@@ -304,6 +327,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
304
327
"f64" ,
305
328
"f64" ,
306
329
item. span ,
330
+ assoc_items,
307
331
) ;
308
332
}
309
333
ty:: Error ( _) => { }
@@ -369,6 +393,7 @@ impl InherentCollect<'tcx> {
369
393
lang : & str ,
370
394
ty : & str ,
371
395
span : Span ,
396
+ assoc_items : & [ hir:: ImplItemRef < ' _ > ] ,
372
397
) {
373
398
match ( lang_def_id, lang_def_id2) {
374
399
( Some ( lang_def_id) , _) if lang_def_id == impl_def_id. to_def_id ( ) => {
@@ -378,6 +403,32 @@ impl InherentCollect<'tcx> {
378
403
// OK
379
404
}
380
405
_ => {
406
+ let to_implement = if assoc_items. len ( ) == 0 {
407
+ String :: new ( )
408
+ } else {
409
+ let plural = assoc_items. len ( ) > 1 ;
410
+ let assoc_items_kind = {
411
+ let item_types = assoc_items. iter ( ) . map ( |x| x. kind ) ;
412
+ if item_types. clone ( ) . all ( |x| x == hir:: AssocItemKind :: Const ) {
413
+ "constant"
414
+ } else if item_types
415
+ . clone ( )
416
+ . all ( |x| matches ! { x, hir:: AssocItemKind :: Fn { .. } } )
417
+ {
418
+ "method"
419
+ } else {
420
+ "associated item"
421
+ }
422
+ } ;
423
+
424
+ format ! (
425
+ " to implement {} {}{}" ,
426
+ if plural { "these" } else { "this" } ,
427
+ assoc_items_kind,
428
+ if plural { "s" } else { "" }
429
+ )
430
+ } ;
431
+
381
432
struct_span_err ! (
382
433
self . tcx. sess,
383
434
span,
@@ -387,7 +438,7 @@ impl InherentCollect<'tcx> {
387
438
lang,
388
439
ty
389
440
)
390
- . span_help ( span , "consider using a trait to implement these methods" )
441
+ . help ( & format ! ( "consider using a trait{}" , to_implement ) )
391
442
. emit ( ) ;
392
443
}
393
444
}
0 commit comments