Skip to content

Commit 56356a0

Browse files
committed
Miri: add helper fn to allocate string; simplify alloc_caller_location
1 parent cde17d9 commit 56356a0

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ pub fn const_caller_location<'tcx>(
552552
tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
553553
.subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
554554
);
555-
let loc_place = ecx.alloc_caller_location(file, line, col).unwrap();
555+
let loc_place = ecx.alloc_caller_location(file, line, col);
556556
intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap();
557557
let loc_const = ty::Const {
558558
ty: loc_ty,

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
110110

111111
match intrinsic_name {
112112
"caller_location" => {
113-
let location = self.alloc_caller_location_for_span(span)?;
113+
let location = self.alloc_caller_location_for_span(span);
114114
self.write_scalar(location.ptr, dest)?;
115115
}
116116

src/librustc_mir/interpret/intrinsics/caller_location.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
11
use rustc::middle::lang_items::PanicLocationLangItem;
2-
use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar};
32
use rustc::ty::subst::Subst;
4-
use rustc_target::abi::{LayoutOf, Size};
3+
use rustc_target::abi::LayoutOf;
54
use syntax_pos::{Symbol, Span};
65

7-
use crate::interpret::{MemoryKind, MPlaceTy, intrinsics::{InterpCx, InterpResult, Machine}};
6+
use crate::interpret::{Scalar, MemoryKind, MPlaceTy, intrinsics::{InterpCx, Machine}};
87

98
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
109
crate fn alloc_caller_location(
1110
&mut self,
1211
filename: Symbol,
1312
line: u32,
1413
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);
1616
let line = Scalar::from_u32(line);
1717
let col = Scalar::from_u32(col);
1818

19-
let ptr_size = self.pointer_size();
20-
let u32_size = Size::from_bits(32);
21-
19+
// Allocate memory for `CallerLocation` struct.
2220
let loc_ty = self.tcx.type_of(self.tcx.require_lang_item(PanicLocationLangItem, None))
2321
.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();
3123
let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
3224

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)?;
38-
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)?;
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");
4732

48-
Ok(location)
33+
location
4934
}
5035

5136
pub fn alloc_caller_location_for_span(
5237
&mut self,
5338
span: Span,
54-
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
39+
) -> MPlaceTy<'tcx, M::PointerTag> {
5540
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
5641
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
5742
self.alloc_caller_location(

src/librustc_mir/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
331331
Ok(self.read_immediate(op)?.to_scalar_or_undef())
332332
}
333333

334-
// Turn the MPlace into a string (must already be dereferenced!)
334+
// Turn the fat MPlace into a string (must already be dereferenced!)
335335
pub fn read_str(
336336
&self,
337337
mplace: MPlaceTy<'tcx, M::PointerTag>,

src/librustc_mir/interpret/place.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,24 @@ where
10341034
MPlaceTy::from_aligned_ptr(ptr, layout)
10351035
}
10361036

1037+
/// Returns a fat MPlace.
1038+
pub fn allocate_str(
1039+
&mut self,
1040+
str: &str,
1041+
kind: MemoryKind<M::MemoryKinds>,
1042+
) -> MPlaceTy<'tcx, M::PointerTag> {
1043+
let ptr = self.memory.allocate_static_bytes(str.as_bytes(), kind);
1044+
let meta = Scalar::from_uint(str.len() as u128, self.pointer_size());
1045+
let mplace = MemPlace {
1046+
ptr: ptr.into(),
1047+
align: Align::from_bytes(1).unwrap(),
1048+
meta: Some(meta),
1049+
};
1050+
1051+
let layout = self.layout_of(self.tcx.mk_static_str()).unwrap();
1052+
MPlaceTy { mplace, layout }
1053+
}
1054+
10371055
pub fn write_discriminant_index(
10381056
&mut self,
10391057
variant_index: VariantIdx,

0 commit comments

Comments
 (0)