@@ -203,33 +203,52 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
203
203
204
204
let encl_trait_hir_id = tcx. hir ( ) . get_parent_item ( hir_id) ;
205
205
let encl_trait = tcx. hir ( ) . expect_item ( encl_trait_hir_id) ;
206
- if [ tcx. lang_items ( ) . fn_trait ( ) , tcx. lang_items ( ) . fn_mut_trait ( ) ]
207
- . contains ( & Some ( encl_trait. def_id . to_def_id ( ) ) )
208
- && trait_item. ident . name . to_ident_string ( ) == "call"
206
+ let encl_trait_def_id = encl_trait. def_id . to_def_id ( ) ;
207
+ let fn_lang_item_name = if Some ( encl_trait_def_id) == tcx. lang_items ( ) . fn_trait ( ) {
208
+ Some ( "fn" )
209
+ } else if Some ( encl_trait_def_id) == tcx. lang_items ( ) . fn_mut_trait ( ) {
210
+ Some ( "fn_mut" )
211
+ } else {
212
+ None
213
+ } ;
214
+
215
+ if let ( Some ( fn_lang_item_name) , "call" ) =
216
+ ( fn_lang_item_name, trait_item. ident . name . to_ident_string ( ) . as_str ( ) )
209
217
{
210
218
// We are looking at the `call` function of the `fn` or `fn_mut` lang item.
211
219
// Do some rudimentary sanity checking to avoid an ICE later (issue #83471).
212
- if let Some ( method_sig @ hir:: FnSig { decl, .. } ) = method_sig {
220
+ if let Some ( hir:: FnSig { decl, span , .. } ) = method_sig {
213
221
if let & [ self_ty, _] = & decl. inputs {
214
222
if !matches ! ( self_ty. kind, hir:: TyKind :: Rptr ( _, _) ) {
215
- tcx. sess . struct_span_err (
216
- self_ty. span ,
217
- "first argument of `call` in `fn`/`fn_mut` lang item must be a reference" ,
218
- ) . emit ( ) ;
223
+ tcx. sess
224
+ . struct_span_err (
225
+ self_ty. span ,
226
+ & format ! (
227
+ "first argument of `call` in `{}` lang item must be a reference" ,
228
+ fn_lang_item_name
229
+ ) ,
230
+ )
231
+ . emit ( ) ;
219
232
}
220
233
} else {
221
234
tcx. sess
222
235
. struct_span_err (
223
- method_sig. span ,
224
- "`call` function in `fn`/`fn_mut` lang item takes exactly two arguments" ,
236
+ * span,
237
+ & format ! (
238
+ "`call` function in `{}` lang item takes exactly two arguments" ,
239
+ fn_lang_item_name
240
+ ) ,
225
241
)
226
242
. emit ( ) ;
227
243
}
228
244
} else {
229
245
tcx. sess
230
246
. struct_span_err (
231
247
trait_item. span ,
232
- "`call` trait item in `fn`/`fn_mut` lang item must be a function" ,
248
+ & format ! (
249
+ "`call` trait item in `{}` lang item must be a function" ,
250
+ fn_lang_item_name
251
+ ) ,
233
252
)
234
253
. emit ( ) ;
235
254
}
0 commit comments