Skip to content

Commit

Permalink
expose span to M::assert_panic, and provide helper to turn that into …
Browse files Browse the repository at this point in the history
…CallerLocation
  • Loading branch information
RalfJung committed Dec 2, 2019
1 parent b1aa3ca commit a8eea62
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/librustc_mir/const_eval.rs
Expand Up @@ -397,6 +397,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,

fn assert_panic(
ecx: &mut InterpCx<'mir, 'tcx, Self>,
_span: Span,
msg: &AssertMessage<'tcx>,
_unwind: Option<mir::BasicBlock>,
) -> InterpResult<'tcx> {
Expand Down
8 changes: 1 addition & 7 deletions src/librustc_mir/interpret/intrinsics.rs
Expand Up @@ -110,13 +110,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

match intrinsic_name {
"caller_location" => {
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
let location = self.alloc_caller_location(
Symbol::intern(&caller.file.name.to_string()),
caller.line as u32,
caller.col_display as u32 + 1,
)?;
let location = self.alloc_caller_location_for_span(span)?;
self.write_scalar(location.ptr, dest)?;
}

Expand Down
17 changes: 15 additions & 2 deletions src/librustc_mir/interpret/intrinsics/caller_location.rs
Expand Up @@ -2,12 +2,12 @@ use rustc::middle::lang_items::PanicLocationLangItem;
use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar};
use rustc::ty::subst::Subst;
use rustc_target::abi::{LayoutOf, Size};
use syntax_pos::Symbol;
use syntax_pos::{Symbol, Span};

use crate::interpret::{MemoryKind, MPlaceTy, intrinsics::{InterpCx, InterpResult, Machine}};

impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
pub fn alloc_caller_location(
crate fn alloc_caller_location(
&mut self,
filename: Symbol,
line: u32,
Expand Down Expand Up @@ -47,4 +47,17 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

Ok(location)
}

pub fn alloc_caller_location_for_span(
&mut self,
span: Span,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
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,
)
}
}
1 change: 1 addition & 0 deletions src/librustc_mir/interpret/machine.rs
Expand Up @@ -178,6 +178,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
/// Called to evaluate `Assert` MIR terminators that trigger a panic.
fn assert_panic(
ecx: &mut InterpCx<'mir, 'tcx, Self>,
span: Span,
msg: &AssertMessage<'tcx>,
unwind: Option<mir::BasicBlock>,
) -> InterpResult<'tcx>;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/terminator.rs
Expand Up @@ -122,7 +122,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
if expected == cond_val {
self.go_to_block(target);
} else {
M::assert_panic(self, msg, cleanup)?;
M::assert_panic(self, terminator.source_info.span, msg, cleanup)?;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -158,6 +158,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {

fn assert_panic(
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
_span: Span,
_msg: &rustc::mir::interpret::AssertMessage<'tcx>,
_unwind: Option<rustc::mir::BasicBlock>,
) -> InterpResult<'tcx> {
Expand Down

0 comments on commit a8eea62

Please sign in to comment.