Adjust const eval code to reflect offset_from docs

This commit is contained in:
Oliver Scherer 2019-10-18 12:24:05 +02:00
parent 4a51801c39
commit 94a6d4b1b8
4 changed files with 10 additions and 36 deletions

View File

@ -249,14 +249,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let usize_layout = self.layout_of(self.tcx.types.usize)?;
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
let (val, overflowed, _) = self.overflowing_binary_op(
let (val, _overflowed, _) = self.overflowing_binary_op(
BinOp::Sub, a_offset, b_offset,
)?;
if overflowed {
throw_ub_format!(
"second argument to `ptr_offset_from` must be smaller than first",
);
}
let pointee_layout = self.layout_of(substs.type_at(0))?;
let isize_layout = self.layout_of(self.tcx.types.isize)?;
let val = ImmTy::from_scalar(val, isize_layout);

View File

@ -33,7 +33,15 @@ pub const OFFSET_2: usize = {
offset as usize
};
pub const OVERFLOW: isize = {
let uninit = std::mem::MaybeUninit::<Struct2>::uninit();
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2;
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
unsafe { (base_ptr as *const u8).offset_from(field_ptr) }
};
fn main() {
assert_eq!(OFFSET, 0);
assert_eq!(OFFSET_2, 1);
assert_eq!(OVERFLOW, -1);
}

View File

@ -32,13 +32,4 @@ pub const NOT_MULTIPLE_OF_SIZE: usize = {
offset as usize
};
pub const OVERFLOW: usize = {
//~^ NOTE
let uninit = std::mem::MaybeUninit::<Struct>::uninit();
let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
let offset = unsafe { (base_ptr as *const u8).offset_from(field_ptr) };
offset as usize
};
fn main() {}

View File

@ -57,25 +57,5 @@ LL | | offset as usize
LL | | };
| |__-
error: any use of this value will cause an error
--> $SRC_DIR/libcore/ptr/mod.rs:LL:COL
|
LL | intrinsics::ptr_offset_from(self, origin)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| second argument to `ptr_offset_from` must be smaller than first
| inside call to `std::ptr::<impl *const u8>::offset_from` at $DIR/offset_from_ub.rs:40:27
|
::: $DIR/offset_from_ub.rs:35:1
|
LL | / pub const OVERFLOW: usize = {
LL | |
LL | | let uninit = std::mem::MaybeUninit::<Struct>::uninit();
LL | | let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
... |
LL | | offset as usize
LL | | };
| |__-
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors