use black_box instead of local optimziation barriers in const tests where possible

This commit is contained in:
Ralf Jung 2020-09-07 11:37:35 +02:00
parent caeb5544ec
commit 4b5cd544d1
2 changed files with 12 additions and 19 deletions

View File

@ -1,5 +1,6 @@
// run-pass
// Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32.
use std::hint::black_box;
#[derive(Copy, Clone)]
enum Nums {
@ -12,9 +13,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 +29,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,9 @@
// run-pass
#![feature(const_discriminant)]
#![feature(const_discriminant, 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 +26,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)));
}