From 94a6d4b1b88135bf0dc7b24f23b47eaad50d5ea5 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 18 Oct 2019 12:24:05 +0200 Subject: [PATCH] Adjust const eval code to reflect `offset_from` docs --- src/librustc_mir/interpret/intrinsics.rs | 7 +----- .../consts/{offset_of.rs => offset_from.rs} | 8 +++++++ src/test/ui/consts/offset_from_ub.rs | 9 -------- src/test/ui/consts/offset_from_ub.stderr | 22 +------------------ 4 files changed, 10 insertions(+), 36 deletions(-) rename src/test/ui/consts/{offset_of.rs => offset_from.rs} (77%) diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index a04bf7c0f1a18..a2b901029ec54 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -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); diff --git a/src/test/ui/consts/offset_of.rs b/src/test/ui/consts/offset_from.rs similarity index 77% rename from src/test/ui/consts/offset_of.rs rename to src/test/ui/consts/offset_from.rs index 7b5b4a77d9ae3..4d6800681889c 100644 --- a/src/test/ui/consts/offset_of.rs +++ b/src/test/ui/consts/offset_from.rs @@ -33,7 +33,15 @@ pub const OFFSET_2: usize = { offset as usize }; +pub const OVERFLOW: isize = { + let uninit = std::mem::MaybeUninit::::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); } diff --git a/src/test/ui/consts/offset_from_ub.rs b/src/test/ui/consts/offset_from_ub.rs index 07e1be0480cde..fedc1af7705a5 100644 --- a/src/test/ui/consts/offset_from_ub.rs +++ b/src/test/ui/consts/offset_from_ub.rs @@ -32,13 +32,4 @@ pub const NOT_MULTIPLE_OF_SIZE: usize = { offset as usize }; -pub const OVERFLOW: usize = { - //~^ NOTE - let uninit = std::mem::MaybeUninit::::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() {} diff --git a/src/test/ui/consts/offset_from_ub.stderr b/src/test/ui/consts/offset_from_ub.stderr index 12185ab5ccda9..ad9695f2fc220 100644 --- a/src/test/ui/consts/offset_from_ub.stderr +++ b/src/test/ui/consts/offset_from_ub.stderr @@ -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::::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::::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