From d93ea6bc79a06bcf6c8cbbd3af24f8724acd5009 Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Fri, 29 Nov 2024 20:25:57 +0100 Subject: [PATCH] simplify things by using `tcx.fn_trait_kind_from_def_id` --- .../rustc_mir_build/src/check_tail_calls.rs | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_mir_build/src/check_tail_calls.rs b/compiler/rustc_mir_build/src/check_tail_calls.rs index 911a6cb7de6..f7264341c0a 100644 --- a/compiler/rustc_mir_build/src/check_tail_calls.rs +++ b/compiler/rustc_mir_build/src/check_tail_calls.rs @@ -91,22 +91,16 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> { // Closures in thir look something akin to // `for<'a> extern "rust-call" fn(&'a [closure@...], ()) -> <[closure@...] as FnOnce<()>>::Output {<[closure@...] as Fn<()>>::call}` // So we have to check for them in this weird way... - if let &ty::FnDef(did, substs) = ty.kind() { + if let &ty::FnDef(did, args) = ty.kind() { let parent = self.tcx.parent(did); - let fn_ = self.tcx.require_lang_item(LangItem::Fn, Some(expr.span)); - let fn_once = self.tcx.require_lang_item(LangItem::FnOnce, Some(expr.span)); - let fn_mut = self.tcx.require_lang_item(LangItem::FnMut, Some(expr.span)); - if [fn_, fn_once, fn_mut].contains(&parent) { - if substs.first().and_then(|arg| arg.as_type()).is_some_and(|t| t.is_closure()) { - self.report_calling_closure( - &self.thir[fun], - substs[1].as_type().unwrap(), - expr, - ); + if self.tcx.fn_trait_kind_from_def_id(parent).is_some() + && args.first().and_then(|arg| arg.as_type()).is_some_and(Ty::is_closure) + { + self.report_calling_closure(&self.thir[fun], args[1].as_type().unwrap(), expr); - // Tail calling is likely to cause unrelated errors (ABI, argument mismatches) - return; - } + // Tail calling is likely to cause unrelated errors (ABI, argument mismatches), + // skip them, producing an error about calling a closure is enough. + return; }; }