Skip to content

Commit 612c4c6

Browse files
committed
Update ABI in const impls of panic_fn/begin_panic_fn.
1 parent e218da4 commit 612c4c6

File tree

6 files changed

+22
-39
lines changed

6 files changed

+22
-39
lines changed

src/librustc_mir/const_eval/machine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
177177

178178
fn find_mir_or_eval_fn(
179179
ecx: &mut InterpCx<'mir, 'tcx, Self>,
180+
span: Span,
180181
instance: ty::Instance<'tcx>,
181182
args: &[OpTy<'tcx>],
182183
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
@@ -199,7 +200,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
199200
// Some functions we support even if they are non-const -- but avoid testing
200201
// that for const fn! We certainly do *not* want to actually call the fn
201202
// though, so be sure we return here.
202-
return if ecx.hook_panic_fn(instance, args, ret)? {
203+
return if ecx.hook_panic_fn(span, instance, args)? {
203204
Ok(None)
204205
} else {
205206
throw_unsup_format!("calling non-const function `{}`", instance)

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -366,47 +366,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
366366
/// Returns `true` if an intercept happened.
367367
pub fn hook_panic_fn(
368368
&mut self,
369+
span: Span,
369370
instance: ty::Instance<'tcx>,
370371
args: &[OpTy<'tcx, M::PointerTag>],
371-
_ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
372372
) -> InterpResult<'tcx, bool> {
373373
let def_id = instance.def_id();
374-
if Some(def_id) == self.tcx.lang_items().panic_fn() {
375-
// &'static str, &core::panic::Location { &'static str, u32, u32 }
376-
assert!(args.len() == 2);
374+
if Some(def_id) == self.tcx.lang_items().panic_fn()
375+
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
376+
{
377+
// &'static str
378+
assert!(args.len() == 1);
377379

378380
let msg_place = self.deref_operand(args[0])?;
379381
let msg = Symbol::intern(self.read_str(msg_place)?);
380-
381-
let location = self.deref_operand(args[1])?;
382-
let (file, line, col) = (
383-
self.mplace_field(location, 0)?,
384-
self.mplace_field(location, 1)?,
385-
self.mplace_field(location, 2)?,
386-
);
387-
388-
let file_place = self.deref_operand(file.into())?;
389-
let file = Symbol::intern(self.read_str(file_place)?);
390-
let line = self.read_scalar(line.into())?.to_u32()?;
391-
let col = self.read_scalar(col.into())?.to_u32()?;
392-
throw_panic!(Panic { msg, file, line, col })
393-
} else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() {
394-
assert!(args.len() == 2);
395-
// &'static str, &core::panic::Location { &'static str, u32, u32 }
396-
let msg = args[0];
397-
let place = self.deref_operand(args[1])?;
398-
let (file, line, col) = (
399-
self.mplace_field(place, 0)?,
400-
self.mplace_field(place, 1)?,
401-
self.mplace_field(place, 2)?,
402-
);
403-
404-
let msg_place = self.deref_operand(msg.into())?;
405-
let msg = Symbol::intern(self.read_str(msg_place)?);
406-
let file_place = self.deref_operand(file.into())?;
407-
let file = Symbol::intern(self.read_str(file_place)?);
408-
let line = self.read_scalar(line.into())?.to_u32()?;
409-
let col = self.read_scalar(col.into())?.to_u32()?;
382+
let span = self.find_closest_untracked_caller_location().unwrap_or(span);
383+
let (file, line, col) = self.location_triple_for_span(span);
410384
throw_panic!(Panic { msg, file, line, col })
411385
} else {
412386
return Ok(false);

src/librustc_mir/interpret/intrinsics/caller_location.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::interpret::{
99
};
1010

1111
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12-
/// Walks up the callstack from the intrinsic's callsite, searching for the first frame which is
13-
/// not `#[track_caller]`.
12+
/// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a
13+
/// frame which is not `#[track_caller]`. If the first frame found lacks `#[track_caller]`, then
14+
/// `None` is returned and the callsite of the function invocation itself should be used.
1415
crate fn find_closest_untracked_caller_location(&self) -> Option<Span> {
1516
let mut caller_span = None;
1617
for next_caller in self.stack.iter().rev() {
@@ -54,9 +55,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5455
}
5556

5657
pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> {
58+
let (file, line, column) = self.location_triple_for_span(span);
59+
self.alloc_caller_location(file, line, column)
60+
}
61+
62+
pub fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
5763
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
5864
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
59-
self.alloc_caller_location(
65+
(
6066
Symbol::intern(&caller.file.name.to_string()),
6167
caller.line as u32,
6268
caller.col_display as u32 + 1,

src/librustc_mir/interpret/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
139139
/// was used.
140140
fn find_mir_or_eval_fn(
141141
ecx: &mut InterpCx<'mir, 'tcx, Self>,
142+
span: Span,
142143
instance: ty::Instance<'tcx>,
143144
args: &[OpTy<'tcx, Self::PointerTag>],
144145
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,

src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
238238
| ty::InstanceDef::CloneShim(..)
239239
| ty::InstanceDef::Item(_) => {
240240
// We need MIR for this fn
241-
let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
241+
let body = match M::find_mir_or_eval_fn(self, span, instance, args, ret, unwind)? {
242242
Some(body) => body,
243243
None => return Ok(()),
244244
};

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
125125

126126
fn find_mir_or_eval_fn(
127127
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
128+
_span: Span,
128129
_instance: ty::Instance<'tcx>,
129130
_args: &[OpTy<'tcx>],
130131
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,

0 commit comments

Comments
 (0)