Rollup merge of #137483 - bend-n:😅, r=Noratrieb

rename sub_ptr to offset_from_unsigned

i also made `byte_sub_ptr` `byte_offset_from_unsigned`

fixes #137121
tracking issue #95892
This commit is contained in:
Trevor Gross 2025-02-23 14:30:28 -05:00 committed by GitHub
commit 18ffee2126
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 51 additions and 51 deletions

View file

@ -3,5 +3,5 @@ fn main() {
let arr = [0u8; 8];
let ptr1 = arr.as_ptr();
let ptr2 = ptr1.wrapping_add(4);
let _val = unsafe { ptr1.sub_ptr(ptr2) }; //~ERROR: first pointer has smaller address than second
let _val = unsafe { ptr1.offset_from_unsigned(ptr2) }; //~ERROR: first pointer has smaller address than second
}

View file

@ -1,8 +1,8 @@
error: Undefined Behavior: `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
--> tests/fail/intrinsics/ptr_offset_from_unsigned_neg.rs:LL:CC
|
LL | let _val = unsafe { ptr1.sub_ptr(ptr2) };
| ^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
LL | let _val = unsafe { ptr1.offset_from_unsigned(ptr2) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller address than second: $ADDR < $ADDR
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View file

@ -21,7 +21,7 @@ fn smoke() {
let _val = ptr.wrapping_sub(0);
let _val = unsafe { ptr.sub(0) };
let _val = unsafe { ptr.offset_from(ptr) };
let _val = unsafe { ptr.sub_ptr(ptr) };
let _val = unsafe { ptr.offset_from_unsigned(ptr) };
}
fn test_offset_from() {
@ -32,14 +32,14 @@ fn test_offset_from() {
let y = x.offset(12);
assert_eq!(y.offset_from(x), 12);
assert_eq!(y.sub_ptr(x), 12);
assert_eq!(y.offset_from_unsigned(x), 12);
assert_eq!(x.offset_from(y), -12);
assert_eq!((y as *const u32).offset_from(x as *const u32), 12 / 4);
assert_eq!((x as *const u32).offset_from(y as *const u32), -12 / 4);
let x = (((x as usize) * 2) / 2) as *const u8;
assert_eq!(y.offset_from(x), 12);
assert_eq!(y.sub_ptr(x), 12);
assert_eq!(y.offset_from_unsigned(x), 12);
assert_eq!(x.offset_from(y), -12);
}
}