@@ -44,8 +44,8 @@ struct InherentCollect<'tcx> {
4444
4545impl ItemLikeVisitor < ' v > for InherentCollect < ' tcx > {
4646 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 ) ,
4949 _ => return ,
5050 } ;
5151
@@ -70,6 +70,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
7070 "bool" ,
7171 "bool" ,
7272 item. span ,
73+ assoc_items,
7374 ) ;
7475 }
7576 ty:: Char => {
@@ -80,6 +81,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
8081 "char" ,
8182 "char" ,
8283 item. span ,
84+ assoc_items,
8385 ) ;
8486 }
8587 ty:: Str => {
@@ -90,6 +92,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
9092 "str" ,
9193 "str" ,
9294 item. span ,
95+ assoc_items,
9396 ) ;
9497 }
9598 ty:: Slice ( slice_item) if slice_item == self . tcx . types . u8 => {
@@ -100,6 +103,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
100103 "slice_u8" ,
101104 "[u8]" ,
102105 item. span ,
106+ assoc_items,
103107 ) ;
104108 }
105109 ty:: Slice ( _) => {
@@ -110,6 +114,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
110114 "slice" ,
111115 "[T]" ,
112116 item. span ,
117+ assoc_items,
113118 ) ;
114119 }
115120 ty:: Array ( _, _) => {
@@ -120,6 +125,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
120125 "array" ,
121126 "[T; N]" ,
122127 item. span ,
128+ assoc_items,
123129 ) ;
124130 }
125131 ty:: RawPtr ( ty:: TypeAndMut { ty : inner, mutbl : hir:: Mutability :: Not } )
@@ -132,6 +138,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
132138 "const_slice_ptr" ,
133139 "*const [T]" ,
134140 item. span ,
141+ assoc_items,
135142 ) ;
136143 }
137144 ty:: RawPtr ( ty:: TypeAndMut { ty : inner, mutbl : hir:: Mutability :: Mut } )
@@ -144,6 +151,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
144151 "mut_slice_ptr" ,
145152 "*mut [T]" ,
146153 item. span ,
154+ assoc_items,
147155 ) ;
148156 }
149157 ty:: RawPtr ( ty:: TypeAndMut { ty : _, mutbl : hir:: Mutability :: Not } ) => {
@@ -154,6 +162,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
154162 "const_ptr" ,
155163 "*const T" ,
156164 item. span ,
165+ assoc_items,
157166 ) ;
158167 }
159168 ty:: RawPtr ( ty:: TypeAndMut { ty : _, mutbl : hir:: Mutability :: Mut } ) => {
@@ -164,6 +173,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
164173 "mut_ptr" ,
165174 "*mut T" ,
166175 item. span ,
176+ assoc_items,
167177 ) ;
168178 }
169179 ty:: Int ( ast:: IntTy :: I8 ) => {
@@ -174,6 +184,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
174184 "i8" ,
175185 "i8" ,
176186 item. span ,
187+ assoc_items,
177188 ) ;
178189 }
179190 ty:: Int ( ast:: IntTy :: I16 ) => {
@@ -184,6 +195,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
184195 "i16" ,
185196 "i16" ,
186197 item. span ,
198+ assoc_items,
187199 ) ;
188200 }
189201 ty:: Int ( ast:: IntTy :: I32 ) => {
@@ -194,6 +206,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
194206 "i32" ,
195207 "i32" ,
196208 item. span ,
209+ assoc_items,
197210 ) ;
198211 }
199212 ty:: Int ( ast:: IntTy :: I64 ) => {
@@ -204,6 +217,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
204217 "i64" ,
205218 "i64" ,
206219 item. span ,
220+ assoc_items,
207221 ) ;
208222 }
209223 ty:: Int ( ast:: IntTy :: I128 ) => {
@@ -214,6 +228,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
214228 "i128" ,
215229 "i128" ,
216230 item. span ,
231+ assoc_items,
217232 ) ;
218233 }
219234 ty:: Int ( ast:: IntTy :: Isize ) => {
@@ -224,6 +239,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
224239 "isize" ,
225240 "isize" ,
226241 item. span ,
242+ assoc_items,
227243 ) ;
228244 }
229245 ty:: Uint ( ast:: UintTy :: U8 ) => {
@@ -234,6 +250,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
234250 "u8" ,
235251 "u8" ,
236252 item. span ,
253+ assoc_items,
237254 ) ;
238255 }
239256 ty:: Uint ( ast:: UintTy :: U16 ) => {
@@ -244,6 +261,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
244261 "u16" ,
245262 "u16" ,
246263 item. span ,
264+ assoc_items,
247265 ) ;
248266 }
249267 ty:: Uint ( ast:: UintTy :: U32 ) => {
@@ -254,6 +272,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
254272 "u32" ,
255273 "u32" ,
256274 item. span ,
275+ assoc_items,
257276 ) ;
258277 }
259278 ty:: Uint ( ast:: UintTy :: U64 ) => {
@@ -264,6 +283,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
264283 "u64" ,
265284 "u64" ,
266285 item. span ,
286+ assoc_items,
267287 ) ;
268288 }
269289 ty:: Uint ( ast:: UintTy :: U128 ) => {
@@ -274,6 +294,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
274294 "u128" ,
275295 "u128" ,
276296 item. span ,
297+ assoc_items,
277298 ) ;
278299 }
279300 ty:: Uint ( ast:: UintTy :: Usize ) => {
@@ -284,6 +305,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
284305 "usize" ,
285306 "usize" ,
286307 item. span ,
308+ assoc_items,
287309 ) ;
288310 }
289311 ty:: Float ( ast:: FloatTy :: F32 ) => {
@@ -294,6 +316,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
294316 "f32" ,
295317 "f32" ,
296318 item. span ,
319+ assoc_items,
297320 ) ;
298321 }
299322 ty:: Float ( ast:: FloatTy :: F64 ) => {
@@ -304,6 +327,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
304327 "f64" ,
305328 "f64" ,
306329 item. span ,
330+ assoc_items,
307331 ) ;
308332 }
309333 ty:: Error ( _) => { }
@@ -369,6 +393,7 @@ impl InherentCollect<'tcx> {
369393 lang : & str ,
370394 ty : & str ,
371395 span : Span ,
396+ assoc_items : & [ hir:: ImplItemRef < ' _ > ] ,
372397 ) {
373398 match ( lang_def_id, lang_def_id2) {
374399 ( Some ( lang_def_id) , _) if lang_def_id == impl_def_id. to_def_id ( ) => {
@@ -378,6 +403,32 @@ impl InherentCollect<'tcx> {
378403 // OK
379404 }
380405 _ => {
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+
381432 struct_span_err ! (
382433 self . tcx. sess,
383434 span,
@@ -387,7 +438,7 @@ impl InherentCollect<'tcx> {
387438 lang,
388439 ty
389440 )
390- . span_help ( span , "consider using a trait to implement these methods" )
441+ . help ( & format ! ( "consider using a trait{}" , to_implement ) )
391442 . emit ( ) ;
392443 }
393444 }
0 commit comments