Skip to content

Commit 9bd4f8a

Browse files
committed
Auto merge of rust-lang#117525 - GKFX:remove_option_payload_ptr, r=petrochenkov
Remove option_payload_ptr; redundant to offset_of The `option_payload_ptr` intrinsic is no longer required as `offset_of` supports traversing enums (rust-lang#114208). This PR removes it in order to dogfood offset_of (as suggested at rust-lang#106655 (comment)). However, it will not build until those changes reach beta (which I think is within the next 8 days?) so I've opened it as a draft.
2 parents 9e9c2de + e2f628d commit 9bd4f8a

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

core/src/intrinsics.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,12 +2487,6 @@ extern "rust-intrinsic" {
24872487
where
24882488
G: FnOnce<ARG, Output = RET>,
24892489
F: FnOnce<ARG, Output = RET>;
2490-
2491-
/// This method creates a pointer to any `Some` value. If the argument is
2492-
/// `None`, an invalid within-bounds pointer (that is still acceptable for
2493-
/// constructing an empty slice) is returned.
2494-
#[rustc_nounwind]
2495-
pub fn option_payload_ptr<T>(arg: *const Option<T>) -> *const T;
24962490
}
24972491

24982492
// Some functions are defined here because they accidentally got made

core/src/iter/adapters/filter_map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ where
9797
// SAFETY: Loop conditions ensure the index is in bounds.
9898

9999
unsafe {
100-
let opt_payload_at = core::intrinsics::option_payload_ptr(&val);
100+
let opt_payload_at: *const MaybeUninit<B> = (&val as *const Option<B>)
101+
.byte_add(core::mem::offset_of!(Option<B>, Some.0))
102+
.cast();
101103
let dst = guard.array.as_mut_ptr().add(idx);
102-
crate::ptr::copy_nonoverlapping(opt_payload_at.cast(), dst, 1);
104+
crate::ptr::copy_nonoverlapping(opt_payload_at, dst, 1);
103105
crate::mem::forget(val);
104106
};
105107

core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@
178178
#![feature(is_ascii_octdigit)]
179179
#![feature(isqrt)]
180180
#![feature(maybe_uninit_uninit_array)]
181+
#![feature(offset_of)]
182+
#![feature(offset_of_enum)]
181183
#![feature(ptr_alignment_type)]
182184
#![feature(ptr_metadata)]
183185
#![feature(set_ptr_value)]

core/src/option.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ impl<T> Option<T> {
780780
// `None` case it's just padding).
781781
unsafe {
782782
slice::from_raw_parts(
783-
crate::intrinsics::option_payload_ptr(crate::ptr::from_ref(self)),
783+
(self as *const Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
784784
usize::from(self.is_some()),
785785
)
786786
}
@@ -836,8 +836,7 @@ impl<T> Option<T> {
836836
// the `None` case it's just padding).
837837
unsafe {
838838
slice::from_raw_parts_mut(
839-
crate::intrinsics::option_payload_ptr(crate::ptr::from_mut(self).cast_const())
840-
.cast_mut(),
839+
(self as *mut Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
841840
usize::from(self.is_some()),
842841
)
843842
}

core/tests/option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,11 @@ fn zip_unzip_roundtrip() {
568568
let a = z.unzip();
569569
assert_eq!(a, (x, y));
570570
}
571+
572+
#[test]
573+
fn as_slice() {
574+
assert_eq!(Some(42).as_slice(), &[42]);
575+
assert_eq!(Some(43).as_mut_slice(), &[43]);
576+
assert_eq!(None::<i32>.as_slice(), &[]);
577+
assert_eq!(None::<i32>.as_mut_slice(), &[]);
578+
}

0 commit comments

Comments
 (0)