Skip to content

Commit 64982cd

Browse files
Centri3NCGThompson
authored andcommitted
Add const eval test
1 parent 34b2d52 commit 64982cd

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

src/tools/miri/src/shims/intrinsics/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
149149
// Would not be considered UB, or the other way around (`is_val_statically_known(0)`).
150150
"is_val_statically_known" => {
151151
let [_] = check_arg_count(args)?;
152-
let branch = this.machine.rng.get_mut().gen();
152+
let branch: bool = this.machine.rng.get_mut().gen();
153153
this.write_scalar(Scalar::from_bool(branch), dest)?;
154154
}
155155

@@ -477,11 +477,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
477477
}
478478
}
479479
// Nothing else
480-
_ =>
481-
span_bug!(
482-
this.cur_span(),
483-
"`float_to_int_unchecked` called with non-int output type {dest_ty:?}"
484-
),
480+
_ => span_bug!(
481+
this.cur_span(),
482+
"`float_to_int_unchecked` called with non-int output type {dest_ty:?}"
483+
),
485484
})
486485
}
487486
>>>>>>> acbf90a9489 (Make MIRI choose the path randomly and rename the intrinsic)

src/tools/miri/tests/pass/intrinsics.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ fn main() {
3333
assert_eq!(intrinsics::likely(false), false);
3434
assert_eq!(intrinsics::unlikely(true), true);
3535

36-
if unsafe { intrinsics::is_val_statically_known(&0 as _) } {
37-
0
38-
} else {
39-
0
40-
};
36+
let mut saw_true = false;
37+
let mut saw_false = false;
38+
39+
for _ in 0..50 {
40+
if unsafe { intrinsics::is_val_statically_known(0) } {
41+
saw_true = true;
42+
} else {
43+
saw_false = true;
44+
}
45+
}
46+
assert!(
47+
saw_true && saw_false,
48+
"`is_val_statically_known` failed to return both true and false. Congrats, you won the lottery!"
49+
);
4150

4251
intrinsics::forget(Bomb);
4352

tests/codegen/is_val_statically_known.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// compile-flags: --crate-type=lib
1+
// #[cfg(bootstrap)]
2+
// ignore-stage1
3+
// compile-flags: --crate-type=lib -Zmerge-functions=disabled
4+
25
#![feature(core_intrinsics)]
36

47
use std::intrinsics::is_val_statically_known;

tests/codegen/pow_of_two.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// compile-flags: --crate-type=lib
1+
// #[cfg(bootstrap)]
2+
// ignore-stage1
3+
// compile-flags: --crate-type=lib -Zmerge-functions=disabled
24

35
// CHECK-LABEL: @a(
46
#[no_mangle]
@@ -10,9 +12,12 @@ pub fn a(exp: u32) -> u64 {
1012
2u64.pow(exp)
1113
}
1214

13-
// This is sometimes an alias to `a`, but this isn't guaranteed
1415
#[no_mangle]
1516
pub fn b(exp: u32) -> i64 {
17+
// CHECK: %[[R:.+]] = and i32 %exp, 63
18+
// CHECK: %[[R:.+]] = zext i32 %[[R:.+]] to i64
19+
// CHECK: %[[R:.+]] = shl nuw i64 %[[R:.+]].i, %[[R:.+]]
20+
// CHECK: ret i64 %[[R:.+]]
1621
2i64.pow(exp)
1722
}
1823

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
3+
#![feature(core_intrinsics)]
4+
#![feature(is_val_statically_known)]
5+
6+
use std::intrinsics::is_val_statically_known;
7+
8+
const CONST_TEST: bool = unsafe { is_val_statically_known(0) };
9+
10+
fn main() {
11+
if CONST_TEST {
12+
unreachable!("guaranteed to return false during const eval");
13+
}
14+
}

0 commit comments

Comments
 (0)