Skip to content

Commit

Permalink
Rollup merge of rust-lang#76434 - RalfJung:black-box, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RalfJung authored Sep 19, 2020
2 parents 0d80a97 + 284b169 commit d46d8f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
3 changes: 2 additions & 1 deletion library/core/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 7 additions & 8 deletions src/test/ui/consts/cast-discriminant-zst-enum.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand All @@ -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);

Expand Down
16 changes: 6 additions & 10 deletions src/test/ui/consts/const_discriminant.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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)));
}

0 comments on commit d46d8f1

Please sign in to comment.