Skip to content

Commit 207f520

Browse files
committed
Pass a location to #[track_caller] functions in codegen_call_terminator.
1 parent fa6bb39 commit 207f520

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
770770
&fn_abi.args[first_args.len()..])
771771
}
772772

773+
let needs_location =
774+
instance.map(|i| i.def.requires_caller_location(self.cx.tcx())).unwrap_or_default();
775+
if needs_location {
776+
assert_eq!(
777+
fn_abi.args.len(), args.len() + 1,
778+
"#[track_caller] fn's must have 1 more argument in their ABI than in their MIR",
779+
);
780+
let location = self.get_caller_location(&mut bx, span);
781+
let last_arg = &fn_abi.args.last().unwrap();
782+
self.codegen_argument(&mut bx, location, &mut llargs, last_arg);
783+
}
784+
773785
let fn_ptr = match (llfn, instance) {
774786
(Some(llfn), _) => llfn,
775787
(None, Some(instance)) => bx.get_fn_addr(instance),

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
182182

183183
// Allocate variable and temp allocas
184184
fx.locals = {
185-
let args = arg_local_refs(&mut bx, &fx, &memory_locals);
185+
let args = arg_local_refs(&mut bx, &mut fx, &memory_locals);
186186

187187
let mut allocate_local = |local| {
188188
let decl = &mir_body.local_decls[local];
@@ -324,14 +324,14 @@ fn create_funclets<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
324324
/// indirect.
325325
fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
326326
bx: &mut Bx,
327-
fx: &FunctionCx<'a, 'tcx, Bx>,
327+
fx: &mut FunctionCx<'a, 'tcx, Bx>,
328328
memory_locals: &BitSet<mir::Local>,
329329
) -> Vec<LocalRef<'tcx, Bx::Value>> {
330330
let mir = fx.mir;
331331
let mut idx = 0;
332332
let mut llarg_idx = fx.fn_abi.ret.is_indirect() as usize;
333333

334-
mir.args_iter().enumerate().map(|(arg_index, local)| {
334+
let args = mir.args_iter().enumerate().map(|(arg_index, local)| {
335335
let arg_decl = &mir.local_decls[local];
336336

337337
if Some(local) == mir.spread_arg {
@@ -427,7 +427,20 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
427427
bx.store_fn_arg(arg, &mut llarg_idx, tmp);
428428
LocalRef::Place(tmp)
429429
}
430-
}).collect()
430+
}).collect::<Vec<_>>();
431+
432+
if fx.instance.def.requires_caller_location(bx.tcx()) {
433+
assert_eq!(
434+
fx.fn_abi.args.len(), args.len() + 1,
435+
"#[track_caller] fn's must have 1 more argument in their ABI than in their MIR",
436+
);
437+
let arg = &fx.fn_abi.args.last().unwrap();
438+
let place = PlaceRef::alloca(bx, arg.layout);
439+
bx.store_fn_arg(arg, &mut llarg_idx, place);
440+
fx.caller_location = Some(place);
441+
}
442+
443+
args
431444
}
432445

433446
mod analyze;

0 commit comments

Comments
 (0)