Skip to content

Commit 354787b

Browse files
committed
Simplify select_unpredictable guard selection
Instead of a tuple, select the dropped value and its guard with two separate calls to the intrinsic which makes both calls have a pointer-valued argument that should be simpler in codegen. Use the same condition on all (not an inverted condition) to clarify the intent of parallel selection. This should also be a simpler value-dependency chain if the guard is deduced unused (i.e. drop_in_place a noop for the type).
1 parent 539f840 commit 354787b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

library/core/src/hint.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ 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`.
791792
inner: *mut MaybeUninit<T>,
792793
}
793794

@@ -806,12 +807,11 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
806807
// value that is not selected.
807808
unsafe {
808809
// Extract the selected value first, ensure it is dropped as well if dropping the unselected
809-
// value panics.
810-
let (guard, drop) = crate::intrinsics::select_unpredictable(
811-
condition,
812-
(true_ptr, false_ptr),
813-
(false_ptr, true_ptr),
814-
);
810+
// value panics. We construct a temporary by-pointer guard around the selected value while
811+
// dropping the unselected value. Arguments overlap here, so we can not use mutable
812+
// reference for these arguments.
813+
let guard = crate::intrinsics::select_unpredictable(condition, true_ptr, false_ptr);
814+
let drop = crate::intrinsics::select_unpredictable(condition, false_ptr, true_ptr);
815815

816816
// SAFETY: both pointers are to valid `MaybeUninit`, in both variants they do not alias but
817817
// the two arguments we have selected from did alias each other.

0 commit comments

Comments
 (0)