Reintroduce zst-slice reading `read_bytes` method on `Memory`

This commit is contained in:
Oliver Scherer 2018-11-13 16:13:51 +01:00
parent ebf03363f2
commit ef332959dc
3 changed files with 19 additions and 5 deletions

View File

@ -567,6 +567,22 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
}
}
/// Byte Accessors
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
pub fn read_bytes(
&self,
ptr: Scalar<M::PointerTag>,
size: Size,
) -> EvalResult<'tcx, &[u8]> {
if size.bytes() == 0 {
Ok(&[])
} else {
let ptr = ptr.to_ptr()?;
self.get(ptr.alloc_id)?.read_bytes(self, ptr, size)
}
}
}
/// Interning (for CTFE)
impl<'a, 'mir, 'tcx, M> Memory<'a, 'mir, 'tcx, M>
where

View File

@ -351,10 +351,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
mplace: MPlaceTy<'tcx, M::PointerTag>,
) -> EvalResult<'tcx, &str> {
let len = mplace.len(self)?;
let ptr = mplace.ptr.to_ptr()?;
let bytes = self.memory
.get(ptr.alloc_id)?
.read_bytes(self, ptr, Size::from_bytes(len as u64))?;
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
let str = ::std::str::from_utf8(bytes)
.map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
Ok(str)

View File

@ -1,6 +1,7 @@
// compile-pass
#![feature(const_raw_ptr_deref)]
const FOO: &str = unsafe { &*(1_usize as *const [u8; 0] as *const [u8] as *const str) };
//~^ ERROR it is undefined behaviour to use this value
fn main() {}