|
1 | 1 | use rustc::middle::lang_items::PanicLocationLangItem; |
2 | | -use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar}; |
3 | 2 | use rustc::ty::subst::Subst; |
4 | | -use rustc_target::abi::{LayoutOf, Size}; |
5 | | -use syntax_pos::Symbol; |
| 3 | +use rustc_target::abi::LayoutOf; |
| 4 | +use syntax_pos::{Symbol, Span}; |
6 | 5 |
|
7 | | -use crate::interpret::{MemoryKind, MPlaceTy, intrinsics::{InterpCx, InterpResult, Machine}}; |
| 6 | +use crate::interpret::{Scalar, MemoryKind, MPlaceTy, intrinsics::{InterpCx, Machine}}; |
8 | 7 |
|
9 | 8 | impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { |
10 | | - pub fn alloc_caller_location( |
| 9 | + crate fn alloc_caller_location( |
11 | 10 | &mut self, |
12 | 11 | filename: Symbol, |
13 | 12 | line: u32, |
14 | 13 | col: u32, |
15 | | - ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { |
| 14 | + ) -> MPlaceTy<'tcx, M::PointerTag> { |
| 15 | + let file = self.allocate_str(&filename.as_str(), MemoryKind::CallerLocation); |
16 | 16 | let line = Scalar::from_u32(line); |
17 | 17 | let col = Scalar::from_u32(col); |
18 | 18 |
|
19 | | - let ptr_size = self.pointer_size(); |
20 | | - let u32_size = Size::from_bits(32); |
21 | | - |
| 19 | + // Allocate memory for `CallerLocation` struct. |
22 | 20 | let loc_ty = self.tcx.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None)) |
23 | 21 | .subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_static.into()].iter())); |
24 | | - let loc_layout = self.layout_of(loc_ty)?; |
25 | | - |
26 | | - let file_alloc = self.tcx.allocate_bytes(filename.as_str().as_bytes()); |
27 | | - let file_ptr = Pointer::new(file_alloc, Size::ZERO); |
28 | | - let file = Scalar::Ptr(self.tag_static_base_pointer(file_ptr)); |
29 | | - let file_len = Scalar::from_uint(filename.as_str().len() as u128, ptr_size); |
30 | | - |
| 22 | + let loc_layout = self.layout_of(loc_ty).unwrap(); |
31 | 23 | let location = self.allocate(loc_layout, MemoryKind::CallerLocation); |
32 | 24 |
|
33 | | - let file_out = self.mplace_field(location, 0)?; |
34 | | - let file_ptr_out = self.force_ptr(self.mplace_field(file_out, 0)?.ptr)?; |
35 | | - let file_len_out = self.force_ptr(self.mplace_field(file_out, 1)?.ptr)?; |
36 | | - let line_out = self.force_ptr(self.mplace_field(location, 1)?.ptr)?; |
37 | | - let col_out = self.force_ptr(self.mplace_field(location, 2)?.ptr)?; |
| 25 | + // Initialize fields. |
| 26 | + self.write_immediate(file.to_ref(), self.mplace_field(location, 0).unwrap().into()) |
| 27 | + .expect("writing to memory we just allocated cannot fail"); |
| 28 | + self.write_scalar(line, self.mplace_field(location, 1).unwrap().into()) |
| 29 | + .expect("writing to memory we just allocated cannot fail"); |
| 30 | + self.write_scalar(col, self.mplace_field(location, 2).unwrap().into()) |
| 31 | + .expect("writing to memory we just allocated cannot fail"); |
38 | 32 |
|
39 | | - let layout = &self.tcx.data_layout; |
40 | | - // We just allocated this, so we can skip the bounds checks. |
41 | | - let alloc = self.memory.get_raw_mut(file_ptr_out.alloc_id)?; |
42 | | - |
43 | | - alloc.write_scalar(layout, file_ptr_out, file.into(), ptr_size)?; |
44 | | - alloc.write_scalar(layout, file_len_out, file_len.into(), ptr_size)?; |
45 | | - alloc.write_scalar(layout, line_out, line.into(), u32_size)?; |
46 | | - alloc.write_scalar(layout, col_out, col.into(), u32_size)?; |
| 33 | + location |
| 34 | + } |
47 | 35 |
|
48 | | - Ok(location) |
| 36 | + pub fn alloc_caller_location_for_span( |
| 37 | + &mut self, |
| 38 | + span: Span, |
| 39 | + ) -> MPlaceTy<'tcx, M::PointerTag> { |
| 40 | + let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); |
| 41 | + let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); |
| 42 | + self.alloc_caller_location( |
| 43 | + Symbol::intern(&caller.file.name.to_string()), |
| 44 | + caller.line as u32, |
| 45 | + caller.col_display as u32 + 1, |
| 46 | + ) |
49 | 47 | } |
50 | 48 | } |
0 commit comments