Skip to content

Commit f41a618

Browse files
committed
Address review feedback.
1 parent 9cff6c7 commit f41a618

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

compiler/rustc_mir_build/src/check_tail_calls.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::thir::visit::{self, Visitor};
99
use rustc_middle::thir::{BodyTy, Expr, ExprId, ExprKind, Thir};
1010
use rustc_middle::ty::{self, Ty, TyCtxt};
1111
use rustc_span::def_id::{DefId, LocalDefId};
12-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
12+
use rustc_span::{ErrorGuaranteed, Span};
1313

1414
pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), ErrorGuaranteed> {
1515
let (thir, expr) = tcx.thir_body(def)?;
@@ -21,7 +21,6 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E
2121
}
2222

2323
let is_closure = matches!(tcx.def_kind(def), DefKind::Closure);
24-
let caller_ty = tcx.type_of(def).skip_binder();
2524

2625
let mut visitor = TailCallCkVisitor {
2726
tcx,
@@ -30,7 +29,7 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E
3029
// FIXME(#132279): we're clearly in a body here.
3130
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
3231
is_closure,
33-
caller_ty,
32+
caller_def_id: def,
3433
};
3534

3635
visitor.visit_expr(&thir[expr]);
@@ -47,8 +46,8 @@ struct TailCallCkVisitor<'a, 'tcx> {
4746
/// The result of the checks, `Err(_)` if there was a problem with some
4847
/// tail call, `Ok(())` if all of them were fine.
4948
found_errors: Result<(), ErrorGuaranteed>,
50-
/// Type of the caller function.
51-
caller_ty: Ty<'tcx>,
49+
/// `LocalDefId` of the caller function.
50+
caller_def_id: LocalDefId,
5251
}
5352

5453
impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
@@ -168,7 +167,7 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
168167
// coercing the function to an `fn()` pointer. (although in that case the tailcall is
169168
// basically useless -- the shim calls the actual function, so tailcalling the shim is
170169
// equivalent to calling the function)
171-
let caller_needs_location = self.needs_location(self.caller_ty);
170+
let caller_needs_location = self.caller_needs_location();
172171

173172
if caller_needs_location {
174173
self.report_track_caller_caller(expr.span);
@@ -184,27 +183,13 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
184183
}
185184
}
186185

187-
/// Returns true if function of type `ty` needs location argument
188-
/// (i.e. if a function is marked as `#[track_caller]`,
189-
/// is an override of a default trait method marked as `#[track_caller]`
190-
/// or an implementation of a prototype method marked as `#[track_caller]`)
191-
fn needs_location(&self, ty: Ty<'tcx>) -> bool {
192-
if let &ty::FnDef(did, substs) = ty.kind() {
193-
// If this is a default trait method, it won't be resolved to a concrete instance.
194-
if self
195-
.tcx
196-
.opt_associated_item(did)
197-
.is_some_and(|impl_item| impl_item.container == ty::AssocItemContainer::Trait)
198-
{
199-
self.tcx.codegen_fn_attrs(did).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
200-
} else {
201-
let instance =
202-
ty::Instance::expect_resolve(self.tcx, self.typing_env, did, substs, DUMMY_SP);
203-
instance.def.requires_caller_location(self.tcx)
204-
}
205-
} else {
206-
false
207-
}
186+
/// Returns true if the caller function needs location argument
187+
/// (i.e. if a function is marked as `#[track_caller]`)
188+
fn caller_needs_location(&self) -> bool {
189+
self.tcx
190+
.codegen_fn_attrs(self.caller_def_id)
191+
.flags
192+
.contains(CodegenFnAttrFlags::TRACK_CALLER)
208193
}
209194

210195
fn report_in_closure(&mut self, expr: &Expr<'_>) {

0 commit comments

Comments
 (0)