diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index de643fb333efe..13c5f44674c11 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -178,6 +178,7 @@ #![feature(is_ascii_octdigit)] #![feature(isqrt)] #![feature(maybe_uninit_uninit_array)] +#![feature(offset_of)] #![feature(ptr_alignment_type)] #![feature(ptr_metadata)] #![feature(set_ptr_value)] diff --git a/library/core/src/option.rs b/library/core/src/option.rs index acf3dfbdf4cf4..69dbf7f31c7e2 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -774,12 +774,17 @@ impl Option { // just needs to be aligned, which it is because `&self` is aligned and // the offset used is a multiple of alignment. // - // In the new version, the intrinsic always returns a pointer to an + // In the new version, the `byte_add` always returns a pointer to an // in-bounds and correctly aligned position for a `T` (even if in the // `None` case it's just padding). unsafe { slice::from_raw_parts( + #[cfg(bootstrap)] crate::intrinsics::option_payload_ptr(crate::ptr::from_ref(self)), + #[cfg(not(bootstrap))] + crate::ptr::from_ref(self) + .cast::() + .byte_add(crate::mem::offset_of!(Self, Some.0)), usize::from(self.is_some()), ) } @@ -828,15 +833,20 @@ impl Option { // just needs to be aligned, which it is because `&self` is aligned and // the offset used is a multiple of alignment. // - // In the new version, the intrinsic creates a `*const T` from a + // In the new version, the `byte_add` creates a `*const T` from a // mutable reference so it is safe to cast back to a mutable pointer // here. As with `as_slice`, the intrinsic always returns a pointer to // an in-bounds and correctly aligned position for a `T` (even if in // the `None` case it's just padding). unsafe { slice::from_raw_parts_mut( + #[cfg(bootstrap)] crate::intrinsics::option_payload_ptr(crate::ptr::from_mut(self).cast_const()) .cast_mut(), + #[cfg(not(bootstrap))] + crate::ptr::from_mut(self) + .cast::() + .byte_add(crate::mem::offset_of!(Option, Some.0)), usize::from(self.is_some()), ) }