Skip to content

Commit

Permalink
Adjust const eval code to reflect offset_from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 18, 2019
1 parent 4a51801 commit 94a6d4b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 36 deletions.
7 changes: 1 addition & 6 deletions src/librustc_mir/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
9 changes: 0 additions & 9 deletions src/test/ui/consts/offset_from_ub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
22 changes: 1 addition & 21 deletions src/test/ui/consts/offset_from_ub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 94a6d4b

Please sign in to comment.