Skip to content

Commit ee9803a

Browse files
committed
Switch select_unpredictable guard to raw pointer
1 parent 354787b commit ee9803a

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

library/core/src/hint.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -788,19 +788,20 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
788788
let mut false_val = MaybeUninit::new(false_val);
789789

790790
struct DropOnPanic<T> {
791-
// Invariant: valid pointer and points to an initialized `MaybeUninit`.
792-
inner: *mut MaybeUninit<T>,
791+
// Invariant: valid pointer and points to an initialized value that is not further used,
792+
// i.e. it can be dropped by this guard.
793+
inner: *mut T,
793794
}
794795

795796
impl<T> Drop for DropOnPanic<T> {
796797
fn drop(&mut self) {
797798
// SAFETY: Must be guaranteed on construction of local type `DropOnPanic`.
798-
unsafe { (*self.inner).assume_init_drop() }
799+
unsafe { self.inner.drop_in_place() }
799800
}
800801
}
801802

802-
let true_ptr = (&mut true_val) as *mut _;
803-
let false_ptr = (&mut false_val) as *mut _;
803+
let true_ptr = true_val.as_mut_ptr();
804+
let false_ptr = false_val.as_mut_ptr();
804805

805806
// SAFETY: The value that is not selected is dropped, and the selected one
806807
// is returned. This is necessary because the intrinsic doesn't drop the
@@ -813,10 +814,12 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
813814
let guard = crate::intrinsics::select_unpredictable(condition, true_ptr, false_ptr);
814815
let drop = crate::intrinsics::select_unpredictable(condition, false_ptr, true_ptr);
815816

816-
// SAFETY: both pointers are to valid `MaybeUninit`, in both variants they do not alias but
817-
// the two arguments we have selected from did alias each other.
817+
// SAFETY: both pointers are well-aligned and point to initialized values inside a
818+
// `MaybeUninit` each. In both possible values for `condition` the pointer `guard` and
819+
// `drop` do not alias (even though the two argument pairs we have selected from did alias
820+
// each other).
818821
let guard = DropOnPanic { inner: guard };
819-
(*drop).assume_init_drop();
822+
drop.drop_in_place();
820823
crate::mem::forget(guard);
821824

822825
// Note that it is important to use the values here. Reading from the pointer we got makes

0 commit comments

Comments
 (0)