@@ -788,19 +788,20 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
788
788
let mut false_val = MaybeUninit :: new ( false_val) ;
789
789
790
790
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 ,
793
794
}
794
795
795
796
impl < T > Drop for DropOnPanic < T > {
796
797
fn drop ( & mut self ) {
797
798
// SAFETY: Must be guaranteed on construction of local type `DropOnPanic`.
798
- unsafe { ( * self . inner ) . assume_init_drop ( ) }
799
+ unsafe { self . inner . drop_in_place ( ) }
799
800
}
800
801
}
801
802
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 ( ) ;
804
805
805
806
// SAFETY: The value that is not selected is dropped, and the selected one
806
807
// 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
813
814
let guard = crate :: intrinsics:: select_unpredictable ( condition, true_ptr, false_ptr) ;
814
815
let drop = crate :: intrinsics:: select_unpredictable ( condition, false_ptr, true_ptr) ;
815
816
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).
818
821
let guard = DropOnPanic { inner : guard } ;
819
- ( * drop) . assume_init_drop ( ) ;
822
+ drop. drop_in_place ( ) ;
820
823
crate :: mem:: forget ( guard) ;
821
824
822
825
// Note that it is important to use the values here. Reading from the pointer we got makes
0 commit comments