diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index a04bf7c0f1a..a2b901029ec 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 7b5b4a77d9a..4d680068188 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 07e1be0480c..fedc1af7705 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 12185ab5ccd..ad9695f2fc2 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