Rollup merge of #76434 - RalfJung:black-box, r=Mark-Simulacrum

do not inline black_box when building for Miri

We cannot do the assembly trick in Miri, but let's at least make sure MIR inlining does not circumvent the black_box.

Also use black_box instead of local optimization barriers in a few const tests.
This commit is contained in:
Ralf Jung 2020-09-19 11:47:43 +02:00 committed by GitHub
commit 1720fd94e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 19 deletions

View File

@ -108,7 +108,8 @@ pub fn spin_loop() {
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
#[inline]
#[cfg_attr(not(miri), inline)]
#[cfg_attr(miri, inline(never))]
#[unstable(feature = "test", issue = "50297")]
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
pub fn black_box<T>(mut dummy: T) -> T {

View File

@ -1,5 +1,7 @@
// run-pass
// Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32.
#![feature(test)]
use std::hint::black_box;
#[derive(Copy, Clone)]
enum Nums {
@ -12,9 +14,6 @@ const NEG_ONE_I32: i32 = Nums::NegOne as i32;
const NEG_ONE_I64: i64 = Nums::NegOne as i64;
const NEG_ONE_I128: i128 = Nums::NegOne as i128;
#[inline(never)]
fn identity<T>(t: T) -> T { t }
fn test_as_arg(n: Nums) {
assert_eq!(-1i8, n as i8);
assert_eq!(-1i16, n as i16);
@ -31,11 +30,11 @@ fn main() {
assert_eq!(-1i64, kind as i64);
assert_eq!(-1i128, kind as i128);
assert_eq!(-1i8, identity(kind) as i8);
assert_eq!(-1i16, identity(kind) as i16);
assert_eq!(-1i32, identity(kind) as i32);
assert_eq!(-1i64, identity(kind) as i64);
assert_eq!(-1i128, identity(kind) as i128);
assert_eq!(-1i8, black_box(kind) as i8);
assert_eq!(-1i16, black_box(kind) as i16);
assert_eq!(-1i32, black_box(kind) as i32);
assert_eq!(-1i64, black_box(kind) as i64);
assert_eq!(-1i128, black_box(kind) as i128);
test_as_arg(Nums::NegOne);

View File

@ -1,14 +1,10 @@
// run-pass
#![feature(const_discriminant)]
#![feature(test)]
#![allow(dead_code)]
use std::mem::{discriminant, Discriminant};
// `discriminant(const_expr)` may get const-propagated.
// As we want to check that const-eval is equal to ordinary exection,
// we wrap `const_expr` with a function which is not const to prevent this.
#[inline(never)]
fn identity<T>(x: T) -> T { x }
use std::hint::black_box;
enum Test {
A(u8),
@ -31,10 +27,10 @@ const TEST_V: Discriminant<SingleVariant> = discriminant(&SingleVariant::V);
fn main() {
assert_eq!(TEST_A, TEST_A_OTHER);
assert_eq!(TEST_A, discriminant(identity(&Test::A(17))));
assert_eq!(TEST_B, discriminant(identity(&Test::B)));
assert_eq!(TEST_A, discriminant(black_box(&Test::A(17))));
assert_eq!(TEST_B, discriminant(black_box(&Test::B)));
assert_ne!(TEST_A, TEST_B);
assert_ne!(TEST_B, discriminant(identity(&Test::C { a: 42, b: 7 })));
assert_ne!(TEST_B, discriminant(black_box(&Test::C { a: 42, b: 7 })));
assert_eq!(TEST_V, discriminant(identity(&SingleVariant::V)));
assert_eq!(TEST_V, discriminant(black_box(&SingleVariant::V)));
}