Skip to content

Commit caeaf9b

Browse files
committed
Fix an ICE when using become in a default trait method.
The logic determining whether the relevant function is marked as `#[track_caller]` only worked with functions that could be resolved to a concrete instance, which default trait methods cannot. We need to check the codegen attribute on the default method itself.
1 parent 6355cd3 commit caeaf9b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

compiler/rustc_mir_build/src/check_tail_calls.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
33
use rustc_errors::Applicability;
44
use rustc_hir::LangItem;
55
use rustc_hir::def::DefKind;
6+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
67
use rustc_middle::span_bug;
78
use rustc_middle::thir::visit::{self, Visitor};
89
use rustc_middle::thir::{BodyTy, Expr, ExprId, ExprKind, Thir};
@@ -184,15 +185,23 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
184185
}
185186

186187
/// Returns true if function of type `ty` needs location argument
187-
/// (i.e. if a function is marked as `#[track_caller]`).
188-
///
189-
/// Panics if the function's instance can't be immediately resolved.
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]`)
190191
fn needs_location(&self, ty: Ty<'tcx>) -> bool {
191192
if let &ty::FnDef(did, substs) = ty.kind() {
192-
let instance =
193-
ty::Instance::expect_resolve(self.tcx, self.typing_env, did, substs, DUMMY_SP);
194-
195-
instance.def.requires_caller_location(self.tcx)
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+
}
196205
} else {
197206
false
198207
}

0 commit comments

Comments
 (0)