diff options
| author | Adam Perry <adam.n.perry@gmail.com> | 2019-12-23 19:25:09 -0800 |
|---|---|---|
| committer | Adam Perry <adam.n.perry@gmail.com> | 2020-01-04 10:02:17 -0800 |
| commit | 612c4c6c900a6e2d39df1019a794a8aa3ddf6e17 (patch) | |
| tree | 654262669e2c99acec282bed49d98d36faa34078 | |
| parent | e218da425160d4babaa46d7da1720a11ac6c02fa (diff) | |
Update ABI in const impls of panic_fn/begin_panic_fn.
| -rw-r--r-- | src/librustc_mir/const_eval/machine.rs | 3 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/intrinsics.rs | 42 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/intrinsics/caller_location.rs | 12 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/machine.rs | 1 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/terminator.rs | 2 | ||||
| -rw-r--r-- | src/librustc_mir/transform/const_prop.rs | 1 |
6 files changed, 22 insertions, 39 deletions
diff --git a/src/librustc_mir/const_eval/machine.rs b/src/librustc_mir/const_eval/machine.rs index a76153c19ec..aa9c718c3eb 100644 --- a/src/librustc_mir/const_eval/machine.rs +++ b/src/librustc_mir/const_eval/machine.rs @@ -177,6 +177,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, fn find_mir_or_eval_fn( ecx: &mut InterpCx<'mir, 'tcx, Self>, + span: Span, instance: ty::Instance<'tcx>, args: &[OpTy<'tcx>], ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, @@ -199,7 +200,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, // Some functions we support even if they are non-const -- but avoid testing // that for const fn! We certainly do *not* want to actually call the fn // though, so be sure we return here. - return if ecx.hook_panic_fn(instance, args, ret)? { + return if ecx.hook_panic_fn(span, instance, args)? { Ok(None) } else { throw_unsup_format!("calling non-const function `{}`", instance) diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index 256f7f2ab38..b075e8ea383 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -366,47 +366,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { /// Returns `true` if an intercept happened. pub fn hook_panic_fn( &mut self, + span: Span, instance: ty::Instance<'tcx>, args: &[OpTy<'tcx, M::PointerTag>], - _ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>, ) -> InterpResult<'tcx, bool> { let def_id = instance.def_id(); - if Some(def_id) == self.tcx.lang_items().panic_fn() { - // &'static str, &core::panic::Location { &'static str, u32, u32 } - assert!(args.len() == 2); + if Some(def_id) == self.tcx.lang_items().panic_fn() + || Some(def_id) == self.tcx.lang_items().begin_panic_fn() + { + // &'static str + assert!(args.len() == 1); let msg_place = self.deref_operand(args[0])?; let msg = Symbol::intern(self.read_str(msg_place)?); - - let location = self.deref_operand(args[1])?; - let (file, line, col) = ( - self.mplace_field(location, 0)?, - self.mplace_field(location, 1)?, - self.mplace_field(location, 2)?, - ); - - let file_place = self.deref_operand(file.into())?; - let file = Symbol::intern(self.read_str(file_place)?); - let line = self.read_scalar(line.into())?.to_u32()?; - let col = self.read_scalar(col.into())?.to_u32()?; - throw_panic!(Panic { msg, file, line, col }) - } else if Some(def_id) == self.tcx.lang_items().begin_panic_fn() { - assert!(args.len() == 2); - // &'static str, &core::panic::Location { &'static str, u32, u32 } - let msg = args[0]; - let place = self.deref_operand(args[1])?; - let (file, line, col) = ( - self.mplace_field(place, 0)?, - self.mplace_field(place, 1)?, - self.mplace_field(place, 2)?, - ); - - let msg_place = self.deref_operand(msg.into())?; - let msg = Symbol::intern(self.read_str(msg_place)?); - let file_place = self.deref_operand(file.into())?; - let file = Symbol::intern(self.read_str(file_place)?); - let line = self.read_scalar(line.into())?.to_u32()?; - let col = self.read_scalar(col.into())?.to_u32()?; + let span = self.find_closest_untracked_caller_location().unwrap_or(span); + let (file, line, col) = self.location_triple_for_span(span); throw_panic!(Panic { msg, file, line, col }) } else { return Ok(false); diff --git a/src/librustc_mir/interpret/intrinsics/caller_location.rs b/src/librustc_mir/interpret/intrinsics/caller_location.rs index da0a9fb4f1b..0b07cb2f654 100644 --- a/src/librustc_mir/interpret/intrinsics/caller_location.rs +++ b/src/librustc_mir/interpret/intrinsics/caller_location.rs @@ -9,8 +9,9 @@ use crate::interpret::{ }; impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { - /// Walks up the callstack from the intrinsic's callsite, searching for the first frame which is - /// not `#[track_caller]`. + /// Walks up the callstack from the intrinsic's callsite, searching for the first callsite in a + /// frame which is not `#[track_caller]`. If the first frame found lacks `#[track_caller]`, then + /// `None` is returned and the callsite of the function invocation itself should be used. crate fn find_closest_untracked_caller_location(&self) -> Option<Span> { let mut caller_span = None; for next_caller in self.stack.iter().rev() { @@ -54,9 +55,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } pub fn alloc_caller_location_for_span(&mut self, span: Span) -> MPlaceTy<'tcx, M::PointerTag> { + let (file, line, column) = self.location_triple_for_span(span); + self.alloc_caller_location(file, line, column) + } + + pub fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); - self.alloc_caller_location( + ( Symbol::intern(&caller.file.name.to_string()), caller.line as u32, caller.col_display as u32 + 1, diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 3b444ac46ef..3dc572d256d 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -139,6 +139,7 @@ pub trait Machine<'mir, 'tcx>: Sized { /// was used. fn find_mir_or_eval_fn( ecx: &mut InterpCx<'mir, 'tcx, Self>, + span: Span, instance: ty::Instance<'tcx>, args: &[OpTy<'tcx, Self::PointerTag>], ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>, diff --git a/src/librustc_mir/interpret/terminator.rs b/src/librustc_mir/interpret/terminator.rs index 1cc22c03a05..7b0efd21778 100644 --- a/src/librustc_mir/interpret/terminator.rs +++ b/src/librustc_mir/interpret/terminator.rs @@ -238,7 +238,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | ty::InstanceDef::CloneShim(..) | ty::InstanceDef::Item(_) => { // We need MIR for this fn - let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? { + let body = match M::find_mir_or_eval_fn(self, span, instance, args, ret, unwind)? { Some(body) => body, None => return Ok(()), }; diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index e1c2b494337..4580ae631c2 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -125,6 +125,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine { fn find_mir_or_eval_fn( _ecx: &mut InterpCx<'mir, 'tcx, Self>, + _span: Span, _instance: ty::Instance<'tcx>, _args: &[OpTy<'tcx>], _ret: Option<(PlaceTy<'tcx>, BasicBlock)>, |
