fix fallback impl for select_unpredictable intrinsic
This commit is contained in:
parent
db1484bdee
commit
7d3bf37c4d
2 changed files with 28 additions and 6 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue