fix arith_offset not taking the size of the type into account; test for offset

This commit is contained in:
Ralf Jung 2017-06-04 18:18:37 -07:00
parent 1d0e622a81
commit 70227c87bf
3 changed files with 16 additions and 3 deletions

@ -43,10 +43,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
"arith_offset" => {
// FIXME: Switch to non-checked, wrapped version of pointer_offset
let offset = self.value_to_primval(arg_vals[1], isize)?.to_i128()? as i64;
let ptr = arg_vals[0].read_ptr(&self.memory)?;
let offset = self.value_to_primval(arg_vals[1], isize)?.to_i128()?;
let new_ptr = ptr.signed_offset(offset as i64);
self.write_primval(dest, PrimVal::Ptr(new_ptr), dest_ty)?;
let result_ptr = self.pointer_offset(ptr, substs.type_at(0), offset)?;
self.write_primval(dest, PrimVal::Ptr(result_ptr), dest_ty)?;
}
"assume" => {

@ -0,0 +1,6 @@
fn main() {
let v = [1i16, 2];
let x = &v as *const i16;
let x = x.wrapping_offset(1);
assert_eq!(unsafe { *x }, 2);
}

@ -0,0 +1,6 @@
fn main() {
let v = [1i16, 2];
let x = &v as *const i16;
let x = unsafe { x.offset(1) };
assert_eq!(unsafe { *x }, 2);
}