@@ -214,36 +214,21 @@ pub fn implements_trait<'tcx>(
214214 trait_id : DefId ,
215215 args : & [ GenericArg < ' tcx > ] ,
216216) -> bool {
217- let callee_id = cx
218- . enclosing_body
219- . map ( |body| cx. tcx . hir ( ) . body_owner ( body) . owner . to_def_id ( ) ) ;
220- implements_trait_with_env_from_iter (
221- cx. tcx ,
222- cx. param_env ,
223- ty,
224- trait_id,
225- callee_id,
226- args. iter ( ) . map ( |& x| Some ( x) ) ,
227- )
217+ implements_trait_with_env_from_iter ( cx. tcx , cx. param_env , ty, trait_id, None , args. iter ( ) . map ( |& x| Some ( x) ) )
228218}
229219
230220/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
221+ ///
222+ /// The `callee_id` argument is used to determine whether this is a function call in a `const fn` environment, used for checking const traits.
231223pub fn implements_trait_with_env < ' tcx > (
232224 tcx : TyCtxt < ' tcx > ,
233225 param_env : ParamEnv < ' tcx > ,
234226 ty : Ty < ' tcx > ,
235227 trait_id : DefId ,
236- callee_id : DefId ,
228+ callee_id : Option < DefId > ,
237229 args : & [ GenericArg < ' tcx > ] ,
238230) -> bool {
239- implements_trait_with_env_from_iter (
240- tcx,
241- param_env,
242- ty,
243- trait_id,
244- Some ( callee_id) ,
245- args. iter ( ) . map ( |& x| Some ( x) ) ,
246- )
231+ implements_trait_with_env_from_iter ( tcx, param_env, ty, trait_id, callee_id, args. iter ( ) . map ( |& x| Some ( x) ) )
247232}
248233
249234/// Same as `implements_trait_from_env` but takes the arguments as an iterator.
@@ -258,6 +243,13 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
258243 // Clippy shouldn't have infer types
259244 assert ! ( !ty. has_infer( ) ) ;
260245
246+ // If a `callee_id` is passed, then we assert that it is a body owner
247+ // through calling `body_owner_kind`, which would panic if the callee
248+ // does not have a body.
249+ if let Some ( callee_id) = callee_id {
250+ let _ = tcx. hir ( ) . body_owner_kind ( callee_id) ;
251+ }
252+
261253 let ty = tcx. erase_regions ( ty) ;
262254 if ty. has_escaping_bound_vars ( ) {
263255 return false ;
0 commit comments