Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/tools/miri/tests/pass/intrinsics/select-unpredictable-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![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