From 89a18cb60053a6c1b3b5f817246ddfd917be1e3e Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 9 Apr 2022 01:27:47 -0700 Subject: [PATCH 1/4] Add `unsigned_offset_from` on pointers Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order. --- .../src/intrinsics/mod.rs | 13 +++- .../rustc_codegen_ssa/src/mir/intrinsic.rs | 21 ++++--- .../src/interpret/intrinsics.rs | 24 ++++++-- compiler/rustc_span/src/symbol.rs | 1 + compiler/rustc_typeck/src/check/intrinsic.rs | 3 + library/alloc/src/lib.rs | 1 + library/alloc/src/slice.rs | 2 +- library/alloc/src/vec/drain.rs | 2 +- library/alloc/src/vec/in_place_collect.rs | 2 +- library/alloc/src/vec/in_place_drop.rs | 2 +- library/alloc/src/vec/into_iter.rs | 2 +- library/core/src/intrinsics.rs | 13 ++++ library/core/src/ptr/const_ptr.rs | 61 +++++++++++++++++++ library/core/src/ptr/mut_ptr.rs | 60 ++++++++++++++++++ library/core/src/slice/raw.rs | 4 +- src/test/codegen/intrinsics/offset_from.rs | 36 +++++++++++ src/test/ui/consts/offset_from.rs | 8 +++ src/test/ui/consts/offset_from_ub.rs | 21 ++++++- src/test/ui/consts/offset_from_ub.stderr | 14 ++++- 19 files changed, 265 insertions(+), 25 deletions(-) create mode 100644 src/test/codegen/intrinsics/offset_from.rs diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index d76dfca7960c4..b254ca3bec843 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -713,14 +713,21 @@ fn codegen_regular_intrinsic_call<'tcx>( ret.write_cvalue(fx, val); }; - ptr_offset_from, (v ptr, v base) { + ptr_offset_from | ptr_offset_from_unsigned, (v ptr, v base) { let ty = substs.type_at(0); let isize_layout = fx.layout_of(fx.tcx.types.isize); let pointee_size: u64 = fx.layout_of(ty).size.bytes(); - let diff = fx.bcx.ins().isub(ptr, base); + let diff_bytes = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. - let val = CValue::by_val(fx.bcx.ins().sdiv_imm(diff, pointee_size as i64), isize_layout); + let diff = if intrinsic == sym::ptr_offset_from_unsigned { + // Because diff_bytes ULT isize::MAX, this would be fine as signed, + // but unsigned is slightly easier to codegen, so might as well. + fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64) + } else { + fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64) + }; + let val = CValue::by_val(diff, isize_layout); ret.write_cvalue(fx, val); }; diff --git a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs index f15c469ae5741..6d6d3ae01f4a3 100644 --- a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs +++ b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs @@ -555,21 +555,28 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } } - sym::ptr_offset_from => { + sym::ptr_offset_from | sym::ptr_offset_from_unsigned => { let ty = substs.type_at(0); let pointee_size = bx.layout_of(ty).size; - // This is the same sequence that Clang emits for pointer subtraction. - // It can be neither `nsw` nor `nuw` because the input is treated as - // unsigned but then the output is treated as signed, so neither works. let a = args[0].immediate(); let b = args[1].immediate(); let a = bx.ptrtoint(a, bx.type_isize()); let b = bx.ptrtoint(b, bx.type_isize()); - let d = bx.sub(a, b); let pointee_size = bx.const_usize(pointee_size.bytes()); - // this is where the signed magic happens (notice the `s` in `exactsdiv`) - bx.exactsdiv(d, pointee_size) + if name == sym::ptr_offset_from { + // This is the same sequence that Clang emits for pointer subtraction. + // It can be neither `nsw` nor `nuw` because the input is treated as + // unsigned but then the output is treated as signed, so neither works. + let d = bx.sub(a, b); + // this is where the signed magic happens (notice the `s` in `exactsdiv`) + bx.exactsdiv(d, pointee_size) + } else { + // The `_unsigned` version knows the relative ordering of the pointers, + // so can use `sub nuw` and `udiv exact` instead of dealing in signed. + let d = bx.unchecked_usub(a, b); + bx.exactudiv(d, pointee_size) + } } _ => { diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 3cc237faf695c..f6dd02a9abacf 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -308,7 +308,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self); self.write_pointer(offset_ptr, dest)?; } - sym::ptr_offset_from => { + sym::ptr_offset_from | sym::ptr_offset_from_unsigned => { let a = self.read_pointer(&args[0])?; let b = self.read_pointer(&args[1])?; @@ -330,8 +330,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // Both are pointers. They must be into the same allocation. if a_alloc_id != b_alloc_id { throw_ub_format!( - "ptr_offset_from cannot compute offset of pointers into different \ - allocations.", + "{} cannot compute offset of pointers into different allocations.", + intrinsic_name, ); } // And they must both be valid for zero-sized accesses ("in-bounds or one past the end"). @@ -348,16 +348,30 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { CheckInAllocMsg::OffsetFromTest, )?; + if intrinsic_name == sym::ptr_offset_from_unsigned && a_offset < b_offset { + throw_ub_format!( + "{} cannot compute a negative offset, but {} < {}", + intrinsic_name, + a_offset.bytes(), + b_offset.bytes(), + ); + } + // Compute offset. let usize_layout = self.layout_of(self.tcx.types.usize)?; let isize_layout = self.layout_of(self.tcx.types.isize)?; + let ret_layout = if intrinsic_name == sym::ptr_offset_from { + isize_layout + } else { + usize_layout + }; 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, _ty) = self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?; let pointee_layout = self.layout_of(substs.type_at(0))?; - let val = ImmTy::from_scalar(val, isize_layout); - let size = ImmTy::from_int(pointee_layout.size.bytes(), isize_layout); + let val = ImmTy::from_scalar(val, ret_layout); + let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout); self.exact_div(&val, &size, dest)?; } } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d6885bebc320e..2cc6eb0358567 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1079,6 +1079,7 @@ symbols! { ptr_null, ptr_null_mut, ptr_offset_from, + ptr_offset_from_unsigned, pub_macro_rules, pub_restricted, pure, diff --git a/compiler/rustc_typeck/src/check/intrinsic.rs b/compiler/rustc_typeck/src/check/intrinsic.rs index 0dd8ee88ca2ad..b67185e52116d 100644 --- a/compiler/rustc_typeck/src/check/intrinsic.rs +++ b/compiler/rustc_typeck/src/check/intrinsic.rs @@ -305,6 +305,9 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) { sym::ptr_offset_from => { (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.isize) } + sym::ptr_offset_from_unsigned => { + (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.usize) + } sym::unchecked_div | sym::unchecked_rem | sym::exact_div => { (1, vec![param(0), param(0)], param(0)) } diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index ecebc7ed9ac85..1c569e156077b 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -127,6 +127,7 @@ #![feature(pattern)] #![feature(ptr_internals)] #![feature(ptr_metadata)] +#![feature(ptr_unsigned_offset_from)] #![feature(receiver_trait)] #![feature(set_ptr_value)] #![feature(slice_group_by)] diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 5bdf36a63a225..376aa4812bd7a 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -1056,7 +1056,7 @@ where fn drop(&mut self) { // `T` is not a zero-sized type, and these are pointers into a slice's elements. unsafe { - let len = self.end.offset_from(self.start) as usize; + let len = self.end.unsigned_offset_from(self.start); ptr::copy_nonoverlapping(self.start, self.dest, len); } } diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 1bff19d05c10d..1e4d911e7e6d7 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -163,7 +163,7 @@ impl Drop for Drain<'_, T, A> { // it from the original vec but also avoid creating a &mut to the front since that could // invalidate raw pointers to it which some unsafe code might rely on. let vec_ptr = vec.as_mut().as_mut_ptr(); - let drop_offset = drop_ptr.offset_from(vec_ptr) as usize; + let drop_offset = drop_ptr.unsigned_offset_from(vec_ptr); let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); ptr::drop_in_place(to_drop); } diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index 282af8cc33fdd..a393732567bba 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -250,7 +250,7 @@ where let sink = self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap(); // iteration succeeded, don't drop head - unsafe { ManuallyDrop::new(sink).dst.offset_from(dst_buf) as usize } + unsafe { ManuallyDrop::new(sink).dst.unsigned_offset_from(dst_buf) } } } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 354d25c2389fb..62334f6daba65 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop { impl InPlaceDrop { fn len(&self) -> usize { - unsafe { self.dst.offset_from(self.inner) as usize } + unsafe { self.dst.unsigned_offset_from(self.inner) } } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index a7df6f59b5989..b4797f3781305 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -169,7 +169,7 @@ impl Iterator for IntoIter { let exact = if mem::size_of::() == 0 { self.end.addr().wrapping_sub(self.ptr.addr()) } else { - unsafe { self.end.offset_from(self.ptr) as usize } + unsafe { self.end.unsigned_offset_from(self.ptr) } }; (exact, Some(exact)) } diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index d1647c388594c..10de5ff2d1d19 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -1903,6 +1903,11 @@ extern "rust-intrinsic" { #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] pub fn ptr_offset_from(ptr: *const T, base: *const T) -> isize; + /// See documentation of `<*const T>::unsigned_offset_from` for details. + #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] + #[cfg(not(bootstrap))] + pub fn ptr_offset_from_unsigned(ptr: *const T, base: *const T) -> usize; + /// See documentation of `<*const T>::guaranteed_eq` for details. /// /// Note that, unlike most intrinsics, this is safe to call; @@ -2385,3 +2390,11 @@ where { called_in_const.call_once(arg) } + +/// Bootstrap polyfill +#[cfg(bootstrap)] +pub const unsafe fn ptr_offset_from_unsigned(ptr: *const T, base: *const T) -> usize { + // SAFETY: we have stricter preconditions than `ptr_offset_from`, so can + // call it, and its output has to be positive, so we can just cast. + unsafe { ptr_offset_from(ptr, base) as _ } +} diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 45964c3a444fe..203c78fda0b64 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -611,6 +611,67 @@ impl *const T { unsafe { intrinsics::ptr_offset_from(self, origin) } } + /// Calculates the distance between two pointers, *where it's known that + /// `self` is equal to or greater than `origin`*. The returned value is in + /// units of T: the distance in bytes is divided by `mem::size_of::()`. + /// + /// This computes the same value that [`offset_from`](#method.offset_from) + /// would compute, but with the added precondition that that the offset is + /// guaranteed to be non-negative. This method is equivalent to + /// `usize::from(self.offset_from(origin)).unwrap_unchecked()`, + /// but it provides slightly more information to the optimizer, which can + /// sometimes allow it to optimize slightly better with some backends. + /// + /// This method is the inverse of [`add`](#method.add) (and, with the parameters + /// in the other order, of [`sub`](#method.sub)). + /// + /// # Safety + /// + /// - The distance between the pointers must be non-negative (`self >= origin`) + /// + /// - *All* the safety conditions of [`offset_from`](#method.offset_from) + /// apply to this method as well; see it for the full details. + /// + /// Importantly, despite the return type of this method being able to represent + /// a larger offset, it's still *not permitted* to pass pointers which differ + /// by more than `isize::MAX` *bytes*. As such, the result of this method will + /// always be less than or equal to `isize::MAX as usize`. + /// + /// # Panics + /// + /// This function panics if `T` is a Zero-Sized Type ("ZST"). + /// + /// # Examples + /// + /// ``` + /// #![feature(ptr_unsigned_offset_from)] + /// + /// let a = [0; 5]; + /// let ptr1: *const i32 = &a[1]; + /// let ptr2: *const i32 = &a[3]; + /// unsafe { + /// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2); + /// assert_eq!(ptr1.add(2), ptr2); + /// assert_eq!(ptr2.sub(2), ptr1); + /// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0); + /// } + /// + /// // This would be incorrect, as the pointers are not correctly ordered: + /// // ptr1.offset_from(ptr2) + /// ``` + #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] + #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] + #[inline] + pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize + where + T: Sized, + { + let pointee_size = mem::size_of::(); + assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); + // SAFETY: the caller must uphold the safety contract for `ptr_offset_from_unsigned`. + unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) } + } + /// Returns whether two pointers are guaranteed to be equal. /// /// At runtime this function behaves like `self == other`. diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index ed80cdc9bf9e1..a0457df1b88bd 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -787,6 +787,66 @@ impl *mut T { unsafe { (self as *const T).offset_from(origin) } } + /// Calculates the distance between two pointers, *where it's known that + /// `self` is equal to or greater than `origin`*. The returned value is in + /// units of T: the distance in bytes is divided by `mem::size_of::()`. + /// + /// This computes the same value that [`offset_from`](#method.offset_from) + /// would compute, but with the added precondition that that the offset is + /// guaranteed to be non-negative. This method is equivalent to + /// `usize::from(self.offset_from(origin)).unwrap_unchecked()`, + /// but it provides slightly more information to the optimizer, which can + /// sometimes allow it to optimize slightly better with some backends. + /// + /// This method is the inverse of [`add`](#method.add) (and, with the parameters + /// in the other order, of [`sub`](#method.sub)). + /// + /// # Safety + /// + /// - The distance between the pointers must be non-negative (`self >= origin`) + /// + /// - *All* the safety conditions of [`offset_from`](#method.offset_from) + /// apply to this method as well; see it for the full details. + /// + /// Importantly, despite the return type of this method being able to represent + /// a larger offset, it's still *not permitted* to pass pointers which differ + /// by more than `isize::MAX` *bytes*. As such, the result of this method will + /// always be less than or equal to `isize::MAX as usize`. + /// + /// # Panics + /// + /// This function panics if `T` is a Zero-Sized Type ("ZST"). + /// + /// # Examples + /// + /// ``` + /// #![feature(ptr_unsigned_offset_from)] + /// + /// let mut a = [0; 5]; + /// let p: *mut i32 = a.as_mut_ptr(); + /// unsafe { + /// let ptr1: *mut i32 = p.add(1); + /// let ptr2: *mut i32 = p.add(3); + /// + /// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2); + /// assert_eq!(ptr1.add(2), ptr2); + /// assert_eq!(ptr2.sub(2), ptr1); + /// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0); + /// } + /// + /// // This would be incorrect, as the pointers are not correctly ordered: + /// // ptr1.offset_from(ptr2) + #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] + #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] + #[inline] + pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize + where + T: Sized, + { + // SAFETY: the caller must uphold the safety contract for `unsigned_offset_from`. + unsafe { (self as *const T).unsigned_offset_from(origin) } + } + /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`). /// /// `count` is in units of T; e.g., a `count` of 3 represents a pointer diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 1b88573035d22..e95f8a3143a5f 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -215,7 +215,7 @@ pub const fn from_mut(s: &mut T) -> &mut [T] { #[unstable(feature = "slice_from_ptr_range", issue = "89792")] pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { // SAFETY: the caller must uphold the safety contract for `from_ptr_range`. - unsafe { from_raw_parts(range.start, range.end.offset_from(range.start) as usize) } + unsafe { from_raw_parts(range.start, range.end.unsigned_offset_from(range.start)) } } /// Performs the same functionality as [`from_ptr_range`], except that a @@ -265,5 +265,5 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { #[unstable(feature = "slice_from_ptr_range", issue = "89792")] pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] { // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`. - unsafe { from_raw_parts_mut(range.start, range.end.offset_from(range.start) as usize) } + unsafe { from_raw_parts_mut(range.start, range.end.unsigned_offset_from(range.start)) } } diff --git a/src/test/codegen/intrinsics/offset_from.rs b/src/test/codegen/intrinsics/offset_from.rs new file mode 100644 index 0000000000000..d0de4c8355d05 --- /dev/null +++ b/src/test/codegen/intrinsics/offset_from.rs @@ -0,0 +1,36 @@ +// compile-flags: -C opt-level=1 +// only-64bit (because we're using [ui]size) + +#![crate_type = "lib"] +#![feature(core_intrinsics)] + +//! Basic optimizations are enabled because otherwise `x86_64-gnu-nopt` had an alloca. +//! Uses a type with non-power-of-two size to avoid normalizations to shifts. + +use std::intrinsics::*; + +type RGB = [u8; 3]; + +// CHECK-LABEL: @offset_from_odd_size +#[no_mangle] +pub unsafe fn offset_from_odd_size(a: *const RGB, b: *const RGB) -> isize { + // CHECK: start + // CHECK-NEXT: ptrtoint + // CHECK-NEXT: ptrtoint + // CHECK-NEXT: sub i64 + // CHECK-NEXT: sdiv exact i64 %{{[0-9]+}}, 3 + // CHECK-NEXT: ret i64 + ptr_offset_from(a, b) +} + +// CHECK-LABEL: @offset_from_unsigned_odd_size +#[no_mangle] +pub unsafe fn offset_from_unsigned_odd_size(a: *const RGB, b: *const RGB) -> usize { + // CHECK: start + // CHECK-NEXT: ptrtoint + // CHECK-NEXT: ptrtoint + // CHECK-NEXT: sub nuw i64 + // CHECK-NEXT: udiv exact i64 %{{[0-9]+}}, 3 + // CHECK-NEXT: ret i64 + ptr_offset_from_unsigned(a, b) +} diff --git a/src/test/ui/consts/offset_from.rs b/src/test/ui/consts/offset_from.rs index 4c9b1c1571de4..ace6c5b85add7 100644 --- a/src/test/ui/consts/offset_from.rs +++ b/src/test/ui/consts/offset_from.rs @@ -1,6 +1,7 @@ // run-pass #![feature(const_ptr_offset_from)] +#![feature(ptr_unsigned_offset_from)] struct Struct { field: (), @@ -43,9 +44,16 @@ pub const OFFSET_EQUAL_INTS: isize = { unsafe { ptr.offset_from(ptr) } }; +pub const OFFSET_UNSIGNED: usize = { + let a = ['a', 'b', 'c']; + let ptr = a.as_ptr(); + unsafe { ptr.add(2).unsigned_offset_from(ptr) } +}; + fn main() { assert_eq!(OFFSET, 0); assert_eq!(OFFSET_2, 1); assert_eq!(OVERFLOW, -1); assert_eq!(OFFSET_EQUAL_INTS, 0); + assert_eq!(OFFSET_UNSIGNED, 2); } diff --git a/src/test/ui/consts/offset_from_ub.rs b/src/test/ui/consts/offset_from_ub.rs index 939c1e31f9a52..f604f57e39d98 100644 --- a/src/test/ui/consts/offset_from_ub.rs +++ b/src/test/ui/consts/offset_from_ub.rs @@ -1,7 +1,7 @@ #![feature(const_ptr_offset_from)] #![feature(core_intrinsics)] -use std::intrinsics::ptr_offset_from; +use std::intrinsics::{ptr_offset_from, ptr_offset_from_unsigned}; #[repr(C)] struct Struct { @@ -15,7 +15,7 @@ pub const DIFFERENT_ALLOC: usize = { let uninit2 = std::mem::MaybeUninit::::uninit(); let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct; let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed - //~| cannot compute offset of pointers into different allocations. + //~| ptr_offset_from cannot compute offset of pointers into different allocations. offset as usize }; @@ -70,4 +70,21 @@ const OUT_OF_BOUNDS_SAME: isize = { //~| pointer at offset 10 is out-of-bounds }; +pub const DIFFERENT_ALLOC_UNSIGNED: usize = { + let uninit = std::mem::MaybeUninit::::uninit(); + let base_ptr: *const Struct = &uninit as *const _ as *const Struct; + let uninit2 = std::mem::MaybeUninit::::uninit(); + let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct; + let offset = unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed + //~| ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + offset as usize +}; + +const WRONG_ORDER_UNSIGNED: usize = { + let a = ['a', 'b', 'c']; + let p = a.as_ptr(); + unsafe { ptr_offset_from_unsigned(p, p.add(2) ) } //~ERROR evaluation of constant value failed + //~| cannot compute a negative offset, but 0 < 8 +}; + fn main() {} diff --git a/src/test/ui/consts/offset_from_ub.stderr b/src/test/ui/consts/offset_from_ub.stderr index 293a2b47d30a5..4c98fd72cacc3 100644 --- a/src/test/ui/consts/offset_from_ub.stderr +++ b/src/test/ui/consts/offset_from_ub.stderr @@ -54,6 +54,18 @@ error[E0080]: evaluation of constant value failed LL | unsafe { ptr_offset_from(end_ptr, end_ptr) } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: alloc26 has size 4, so pointer at offset 10 is out-of-bounds -error: aborting due to 8 previous errors +error[E0080]: evaluation of constant value failed + --> $DIR/offset_from_ub.rs:78:27 + | +LL | let offset = unsafe { ptr_offset_from_unsigned(field_ptr, base_ptr) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from_unsigned cannot compute offset of pointers into different allocations. + +error[E0080]: evaluation of constant value failed + --> $DIR/offset_from_ub.rs:86:14 + | +LL | unsafe { ptr_offset_from_unsigned(p, p.add(2) ) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from_unsigned cannot compute a negative offset, but 0 < 8 + +error: aborting due to 10 previous errors For more information about this error, try `rustc --explain E0080`. From e76b3f3b5b1bdb39d064a34f8c9a601633e749c2 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 9 Apr 2022 14:14:35 -0700 Subject: [PATCH 2/4] Rename `unsigned_offset_from` to `sub_ptr` --- .../src/intrinsics/mod.rs | 2 +- library/alloc/src/slice.rs | 2 +- library/alloc/src/vec/drain.rs | 2 +- library/alloc/src/vec/in_place_collect.rs | 2 +- library/alloc/src/vec/in_place_drop.rs | 2 +- library/alloc/src/vec/into_iter.rs | 2 +- library/core/src/intrinsics.rs | 2 +- library/core/src/ptr/const_ptr.rs | 22 ++++++++++++---- library/core/src/ptr/mut_ptr.rs | 26 ++++++++++++++----- library/core/src/slice/raw.rs | 4 +-- src/test/ui/consts/offset_from.rs | 2 +- 11 files changed, 46 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index b254ca3bec843..f7a83373e870b 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -721,7 +721,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let diff_bytes = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. let diff = if intrinsic == sym::ptr_offset_from_unsigned { - // Because diff_bytes ULT isize::MAX, this would be fine as signed, + // Because diff_bytes ULE isize::MAX, this would be fine as signed, // but unsigned is slightly easier to codegen, so might as well. fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64) } else { diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 376aa4812bd7a..199b3c9d0290c 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -1056,7 +1056,7 @@ where fn drop(&mut self) { // `T` is not a zero-sized type, and these are pointers into a slice's elements. unsafe { - let len = self.end.unsigned_offset_from(self.start); + let len = self.end.sub_ptr(self.start); ptr::copy_nonoverlapping(self.start, self.dest, len); } } diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 1e4d911e7e6d7..5cdee0bd4da49 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -163,7 +163,7 @@ impl Drop for Drain<'_, T, A> { // it from the original vec but also avoid creating a &mut to the front since that could // invalidate raw pointers to it which some unsafe code might rely on. let vec_ptr = vec.as_mut().as_mut_ptr(); - let drop_offset = drop_ptr.unsigned_offset_from(vec_ptr); + let drop_offset = drop_ptr.sub_ptr(vec_ptr); let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); ptr::drop_in_place(to_drop); } diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs index a393732567bba..55dcb84ad16f9 100644 --- a/library/alloc/src/vec/in_place_collect.rs +++ b/library/alloc/src/vec/in_place_collect.rs @@ -250,7 +250,7 @@ where let sink = self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).unwrap(); // iteration succeeded, don't drop head - unsafe { ManuallyDrop::new(sink).dst.unsigned_offset_from(dst_buf) } + unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) } } } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 62334f6daba65..1b1ef9130face 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -10,7 +10,7 @@ pub(super) struct InPlaceDrop { impl InPlaceDrop { fn len(&self) -> usize { - unsafe { self.dst.unsigned_offset_from(self.inner) } + unsafe { self.dst.sub_ptr(self.inner) } } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index b4797f3781305..9b84a1d9b4b64 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -169,7 +169,7 @@ impl Iterator for IntoIter { let exact = if mem::size_of::() == 0 { self.end.addr().wrapping_sub(self.ptr.addr()) } else { - unsafe { self.end.unsigned_offset_from(self.ptr) } + unsafe { self.end.sub_ptr(self.ptr) } }; (exact, Some(exact)) } diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 10de5ff2d1d19..88e4262922dc5 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -1903,7 +1903,7 @@ extern "rust-intrinsic" { #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] pub fn ptr_offset_from(ptr: *const T, base: *const T) -> isize; - /// See documentation of `<*const T>::unsigned_offset_from` for details. + /// See documentation of `<*const T>::sub_ptr` for details. #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] #[cfg(not(bootstrap))] pub fn ptr_offset_from_unsigned(ptr: *const T, base: *const T) -> usize; diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 203c78fda0b64..5be855bfabb51 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -622,8 +622,20 @@ impl *const T { /// but it provides slightly more information to the optimizer, which can /// sometimes allow it to optimize slightly better with some backends. /// - /// This method is the inverse of [`add`](#method.add) (and, with the parameters - /// in the other order, of [`sub`](#method.sub)). + /// This method can be though of as recovering the `count` that was passed + /// to [`add`](#method.add) (or, with the parameters in the other order, + /// to [`sub`](#method.sub)). The following are all equivalent, assuming + /// that their safety preconditions are met: + /// ```rust + /// # #![feature(ptr_unsigned_offset_from)] + /// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { + /// ptr.sub_ptr(origin) == count + /// # && + /// origin.add(count) == ptr + /// # && + /// ptr.sub(count) == origin + /// # } + /// ``` /// /// # Safety /// @@ -650,10 +662,10 @@ impl *const T { /// let ptr1: *const i32 = &a[1]; /// let ptr2: *const i32 = &a[3]; /// unsafe { - /// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2); + /// assert_eq!(ptr2.sub_ptr(ptr1), 2); /// assert_eq!(ptr1.add(2), ptr2); /// assert_eq!(ptr2.sub(2), ptr1); - /// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0); + /// assert_eq!(ptr2.sub_ptr(ptr2), 0); /// } /// /// // This would be incorrect, as the pointers are not correctly ordered: @@ -662,7 +674,7 @@ impl *const T { #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] #[inline] - pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize + pub const unsafe fn sub_ptr(self, origin: *const T) -> usize where T: Sized, { diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index a0457df1b88bd..c7f297f426bdb 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -798,8 +798,20 @@ impl *mut T { /// but it provides slightly more information to the optimizer, which can /// sometimes allow it to optimize slightly better with some backends. /// - /// This method is the inverse of [`add`](#method.add) (and, with the parameters - /// in the other order, of [`sub`](#method.sub)). + /// This method can be though of as recovering the `count` that was passed + /// to [`add`](#method.add) (or, with the parameters in the other order, + /// to [`sub`](#method.sub)). The following are all equivalent, assuming + /// that their safety preconditions are met: + /// ```rust + /// # #![feature(ptr_unsigned_offset_from)] + /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { + /// ptr.sub_ptr(origin) == count + /// # && + /// origin.add(count) == ptr + /// # && + /// ptr.sub(count) == origin + /// # } + /// ``` /// /// # Safety /// @@ -828,10 +840,10 @@ impl *mut T { /// let ptr1: *mut i32 = p.add(1); /// let ptr2: *mut i32 = p.add(3); /// - /// assert_eq!(ptr2.unsigned_offset_from(ptr1), 2); + /// assert_eq!(ptr2.sub_ptr(ptr1), 2); /// assert_eq!(ptr1.add(2), ptr2); /// assert_eq!(ptr2.sub(2), ptr1); - /// assert_eq!(ptr2.unsigned_offset_from(ptr2), 0); + /// assert_eq!(ptr2.sub_ptr(ptr2), 0); /// } /// /// // This would be incorrect, as the pointers are not correctly ordered: @@ -839,12 +851,12 @@ impl *mut T { #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] #[inline] - pub const unsafe fn unsigned_offset_from(self, origin: *const T) -> usize + pub const unsafe fn sub_ptr(self, origin: *const T) -> usize where T: Sized, { - // SAFETY: the caller must uphold the safety contract for `unsigned_offset_from`. - unsafe { (self as *const T).unsigned_offset_from(origin) } + // SAFETY: the caller must uphold the safety contract for `sub_ptr`. + unsafe { (self as *const T).sub_ptr(origin) } } /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`). diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index e95f8a3143a5f..6bc60b04b5c64 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -215,7 +215,7 @@ pub const fn from_mut(s: &mut T) -> &mut [T] { #[unstable(feature = "slice_from_ptr_range", issue = "89792")] pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { // SAFETY: the caller must uphold the safety contract for `from_ptr_range`. - unsafe { from_raw_parts(range.start, range.end.unsigned_offset_from(range.start)) } + unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) } } /// Performs the same functionality as [`from_ptr_range`], except that a @@ -265,5 +265,5 @@ pub unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] { #[unstable(feature = "slice_from_ptr_range", issue = "89792")] pub unsafe fn from_mut_ptr_range<'a, T>(range: Range<*mut T>) -> &'a mut [T] { // SAFETY: the caller must uphold the safety contract for `from_mut_ptr_range`. - unsafe { from_raw_parts_mut(range.start, range.end.unsigned_offset_from(range.start)) } + unsafe { from_raw_parts_mut(range.start, range.end.sub_ptr(range.start)) } } diff --git a/src/test/ui/consts/offset_from.rs b/src/test/ui/consts/offset_from.rs index ace6c5b85add7..ad834e885a51c 100644 --- a/src/test/ui/consts/offset_from.rs +++ b/src/test/ui/consts/offset_from.rs @@ -47,7 +47,7 @@ pub const OFFSET_EQUAL_INTS: isize = { pub const OFFSET_UNSIGNED: usize = { let a = ['a', 'b', 'c']; let ptr = a.as_ptr(); - unsafe { ptr.add(2).unsigned_offset_from(ptr) } + unsafe { ptr.add(2).sub_ptr(ptr) } }; fn main() { From 4bb15b3797452b87c6ea3189fa60dd52d59a567d Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 9 Apr 2022 16:29:39 -0700 Subject: [PATCH 3/4] Add a debug check for ordering, and check for isize overflow in CTFE --- .../rustc_const_eval/src/interpret/intrinsics.rs | 13 ++++++++++--- library/core/src/ptr/const_ptr.rs | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index f6dd02a9abacf..3bb3b3d539323 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -365,10 +365,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } else { usize_layout }; - 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, _ty) = + + // The subtraction is always done in `isize` to enforce + // the "no more than `isize::MAX` apart" requirement. + let a_offset = ImmTy::from_uint(a_offset.bytes(), isize_layout); + let b_offset = ImmTy::from_uint(b_offset.bytes(), isize_layout); + let (val, overflowed, _ty) = self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?; + if overflowed { + throw_ub_format!("Pointers were too far apart for {}", intrinsic_name); + } + let pointee_layout = self.layout_of(substs.type_at(0))?; let val = ImmTy::from_scalar(val, ret_layout); let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout); diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 5be855bfabb51..7fcdf21b03cde 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -678,6 +678,10 @@ impl *const T { where T: Sized, { + // SAFETY: The comparison has no side-effects, and the intrinsic + // does this check internally in the CTFE implementation. + unsafe { assert_unsafe_precondition!(self >= origin) }; + let pointee_size = mem::size_of::(); assert!(0 < pointee_size && pointee_size <= isize::MAX as usize); // SAFETY: the caller must uphold the safety contract for `ptr_offset_from_unsigned`. From 003b954a43a7f1f9058f25e8f9b6ddfd4a3dced9 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 10 Apr 2022 16:02:52 -0700 Subject: [PATCH 4/4] Apply CR suggestions; add real tracking issue --- compiler/rustc_const_eval/src/interpret/intrinsics.rs | 2 ++ library/alloc/src/lib.rs | 2 +- library/core/src/lib.rs | 1 + library/core/src/ptr/const_ptr.rs | 8 ++++---- library/core/src/ptr/mut_ptr.rs | 8 ++++---- src/test/ui/consts/offset_from.rs | 3 ++- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 3bb3b3d539323..59ea40dc2f94e 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -377,6 +377,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } let pointee_layout = self.layout_of(substs.type_at(0))?; + // This re-interprets an isize at ret_layout, but we already checked + // that if ret_layout is usize, then the result must be non-negative. let val = ImmTy::from_scalar(val, ret_layout); let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout); self.exact_div(&val, &size, dest)?; diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 1c569e156077b..fd21b3671182b 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -127,7 +127,7 @@ #![feature(pattern)] #![feature(ptr_internals)] #![feature(ptr_metadata)] -#![feature(ptr_unsigned_offset_from)] +#![feature(ptr_sub_ptr)] #![feature(receiver_trait)] #![feature(set_ptr_value)] #![feature(slice_group_by)] diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 1612aa582ad17..d1936b6b566c1 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -126,6 +126,7 @@ #![feature(const_option)] #![feature(const_option_ext)] #![feature(const_pin)] +#![feature(const_ptr_sub_ptr)] #![feature(const_replace)] #![feature(const_ptr_as_ref)] #![feature(const_ptr_is_null)] diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 7fcdf21b03cde..028adc796e526 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -627,7 +627,7 @@ impl *const T { /// to [`sub`](#method.sub)). The following are all equivalent, assuming /// that their safety preconditions are met: /// ```rust - /// # #![feature(ptr_unsigned_offset_from)] + /// # #![feature(ptr_sub_ptr)] /// # unsafe fn blah(ptr: *const i32, origin: *const i32, count: usize) -> bool { /// ptr.sub_ptr(origin) == count /// # && @@ -656,7 +656,7 @@ impl *const T { /// # Examples /// /// ``` - /// #![feature(ptr_unsigned_offset_from)] + /// #![feature(ptr_sub_ptr)] /// /// let a = [0; 5]; /// let ptr1: *const i32 = &a[1]; @@ -671,8 +671,8 @@ impl *const T { /// // This would be incorrect, as the pointers are not correctly ordered: /// // ptr1.offset_from(ptr2) /// ``` - #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] - #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] + #[unstable(feature = "ptr_sub_ptr", issue = "95892")] + #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[inline] pub const unsafe fn sub_ptr(self, origin: *const T) -> usize where diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index c7f297f426bdb..1a32dd62dfd55 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -803,7 +803,7 @@ impl *mut T { /// to [`sub`](#method.sub)). The following are all equivalent, assuming /// that their safety preconditions are met: /// ```rust - /// # #![feature(ptr_unsigned_offset_from)] + /// # #![feature(ptr_sub_ptr)] /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool { /// ptr.sub_ptr(origin) == count /// # && @@ -832,7 +832,7 @@ impl *mut T { /// # Examples /// /// ``` - /// #![feature(ptr_unsigned_offset_from)] + /// #![feature(ptr_sub_ptr)] /// /// let mut a = [0; 5]; /// let p: *mut i32 = a.as_mut_ptr(); @@ -848,8 +848,8 @@ impl *mut T { /// /// // This would be incorrect, as the pointers are not correctly ordered: /// // ptr1.offset_from(ptr2) - #[unstable(feature = "ptr_unsigned_offset_from", issue = "88888888")] - #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "92980")] + #[unstable(feature = "ptr_sub_ptr", issue = "95892")] + #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] #[inline] pub const unsafe fn sub_ptr(self, origin: *const T) -> usize where diff --git a/src/test/ui/consts/offset_from.rs b/src/test/ui/consts/offset_from.rs index ad834e885a51c..b53718316f3b5 100644 --- a/src/test/ui/consts/offset_from.rs +++ b/src/test/ui/consts/offset_from.rs @@ -1,7 +1,8 @@ // run-pass #![feature(const_ptr_offset_from)] -#![feature(ptr_unsigned_offset_from)] +#![feature(const_ptr_sub_ptr)] +#![feature(ptr_sub_ptr)] struct Struct { field: (),