@@ -4,12 +4,13 @@ use rustc_errors::Applicability;
44use rustc_hir:: LangItem ;
55use rustc_hir:: def:: DefKind ;
66use rustc_hir:: def_id:: CRATE_DEF_ID ;
7+ use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
78use rustc_middle:: span_bug;
89use rustc_middle:: thir:: visit:: { self , Visitor } ;
910use rustc_middle:: thir:: { BodyTy , Expr , ExprId , ExprKind , Thir } ;
1011use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
1112use rustc_span:: def_id:: { DefId , LocalDefId } ;
12- use rustc_span:: { DUMMY_SP , ErrorGuaranteed , Span } ;
13+ use rustc_span:: { ErrorGuaranteed , Span } ;
1314
1415pub ( crate ) fn check_tail_calls ( tcx : TyCtxt < ' _ > , def : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
1516 let ( thir, expr) = tcx. thir_body ( def) ?;
@@ -21,7 +22,6 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E
2122 }
2223
2324 let is_closure = matches ! ( tcx. def_kind( def) , DefKind :: Closure ) ;
24- let caller_ty = tcx. type_of ( def) . skip_binder ( ) ;
2525
2626 let mut visitor = TailCallCkVisitor {
2727 tcx,
@@ -30,7 +30,7 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E
3030 // FIXME(#132279): we're clearly in a body here.
3131 typing_env : ty:: TypingEnv :: non_body_analysis ( tcx, def) ,
3232 is_closure,
33- caller_ty ,
33+ caller_def_id : def ,
3434 } ;
3535
3636 visitor. visit_expr ( & thir[ expr] ) ;
@@ -47,8 +47,8 @@ struct TailCallCkVisitor<'a, 'tcx> {
4747 /// The result of the checks, `Err(_)` if there was a problem with some
4848 /// tail call, `Ok(())` if all of them were fine.
4949 found_errors : Result < ( ) , ErrorGuaranteed > ,
50- /// Type of the caller function.
51- caller_ty : Ty < ' tcx > ,
50+ /// `LocalDefId` of the caller function.
51+ caller_def_id : LocalDefId ,
5252}
5353
5454impl < ' tcx > TailCallCkVisitor < ' _ , ' tcx > {
@@ -148,11 +148,13 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
148148 // we should think what is the expected behavior here.
149149 // (we should probably just accept this by revealing opaques?)
150150 if caller_sig. inputs_and_output != callee_sig. inputs_and_output {
151+ let caller_ty = self . tcx . type_of ( self . caller_def_id ) . skip_binder ( ) ;
152+
151153 self . report_signature_mismatch (
152154 expr. span ,
153155 self . tcx . liberate_late_bound_regions (
154156 CRATE_DEF_ID . to_def_id ( ) ,
155- self . caller_ty . fn_sig ( self . tcx ) ,
157+ caller_ty. fn_sig ( self . tcx ) ,
156158 ) ,
157159 self . tcx . liberate_late_bound_regions ( CRATE_DEF_ID . to_def_id ( ) , ty. fn_sig ( self . tcx ) ) ,
158160 ) ;
@@ -173,7 +175,7 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
173175 // coercing the function to an `fn()` pointer. (although in that case the tailcall is
174176 // basically useless -- the shim calls the actual function, so tailcalling the shim is
175177 // equivalent to calling the function)
176- let caller_needs_location = self . needs_location ( self . caller_ty ) ;
178+ let caller_needs_location = self . caller_needs_location ( ) ;
177179
178180 if caller_needs_location {
179181 self . report_track_caller_caller ( expr. span ) ;
@@ -189,19 +191,11 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
189191 }
190192 }
191193
192- /// Returns true if function of type `ty` needs location argument
193- /// (i.e. if a function is marked as `#[track_caller]`).
194- ///
195- /// Panics if the function's instance can't be immediately resolved.
196- fn needs_location ( & self , ty : Ty < ' tcx > ) -> bool {
197- if let & ty:: FnDef ( did, substs) = ty. kind ( ) {
198- let instance =
199- ty:: Instance :: expect_resolve ( self . tcx , self . typing_env , did, substs, DUMMY_SP ) ;
200-
201- instance. def . requires_caller_location ( self . tcx )
202- } else {
203- false
204- }
194+ /// Returns true if the caller function needs a location argument
195+ /// (i.e. if a function is marked as `#[track_caller]`)
196+ fn caller_needs_location ( & self ) -> bool {
197+ let flags = self . tcx . codegen_fn_attrs ( self . caller_def_id ) . flags ;
198+ flags. contains ( CodegenFnAttrFlags :: TRACK_CALLER )
205199 }
206200
207201 fn report_in_closure ( & mut self , expr : & Expr < ' _ > ) {
0 commit comments