fix fallback impl for select_unpredictable intrinsic

This commit is contained in:
Daedalus 2026-01-12 19:02:03 -07:00
parent db1484bdee
commit 7d3bf37c4d
No known key found for this signature in database
GPG key ID: 7744376024E5074B
2 changed files with 28 additions and 6 deletions

View file

@ -55,7 +55,7 @@
#![allow(missing_docs)]
use crate::ffi::va_list::{VaArgSafe, VaList};
use crate::marker::{ConstParamTy, Destruct, DiscriminantKind, PointeeSized, Tuple};
use crate::marker::{ConstParamTy, DiscriminantKind, PointeeSized, Tuple};
use crate::{mem, ptr};
mod bounds;
@ -482,11 +482,14 @@ pub const fn unlikely(b: bool) -> bool {
#[rustc_nounwind]
#[miri::intrinsic_fallback_is_spec]
#[inline]
pub const fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T
where
T: [const] Destruct,
{
if b { true_val } else { false_val }
pub const fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
if b {
forget(false_val);
true_val
} else {
forget(true_val);
false_val
}
}
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:

View file

@ -0,0 +1,19 @@
//! Check that `select_unpredictable` properly forgets the value it does not select.
#![feature(core_intrinsics)]
use std::cell::Cell;
use std::intrinsics::select_unpredictable;
fn main() {
let (true_val, false_val) = (Cell::new(false), Cell::new(false));
_ = select_unpredictable(true, TraceDrop(&true_val), TraceDrop(&false_val));
assert!(true_val.get());
assert!(!false_val.get());
}
struct TraceDrop<'a>(&'a Cell<bool>);
impl<'a> Drop for TraceDrop<'a> {
fn drop(&mut self) {
self.0.set(true);
}
}