Skip to content

Commit ef33295

Browse files
committed
Reintroduce zst-slice reading read_bytes method on Memory
1 parent ebf0336 commit ef33295

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,22 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
567567
}
568568
}
569569

570+
/// Byte Accessors
571+
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
572+
pub fn read_bytes(
573+
&self,
574+
ptr: Scalar<M::PointerTag>,
575+
size: Size,
576+
) -> EvalResult<'tcx, &[u8]> {
577+
if size.bytes() == 0 {
578+
Ok(&[])
579+
} else {
580+
let ptr = ptr.to_ptr()?;
581+
self.get(ptr.alloc_id)?.read_bytes(self, ptr, size)
582+
}
583+
}
584+
}
585+
570586
/// Interning (for CTFE)
571587
impl<'a, 'mir, 'tcx, M> Memory<'a, 'mir, 'tcx, M>
572588
where

src/librustc_mir/interpret/operand.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
351351
mplace: MPlaceTy<'tcx, M::PointerTag>,
352352
) -> EvalResult<'tcx, &str> {
353353
let len = mplace.len(self)?;
354-
let ptr = mplace.ptr.to_ptr()?;
355-
let bytes = self.memory
356-
.get(ptr.alloc_id)?
357-
.read_bytes(self, ptr, Size::from_bytes(len as u64))?;
354+
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
358355
let str = ::std::str::from_utf8(bytes)
359356
.map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
360357
Ok(str)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// compile-pass
2+
13
#![feature(const_raw_ptr_deref)]
24

35
const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const str) };
4-
//~^ ERROR it is undefined behaviour to use this value
56

67
fn main() {}

0 commit comments

Comments
 (0)