From 750250023f4b8a94d7cb0a45de9485a2c4ee47c3 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sat, 15 May 2021 18:47:59 -0400 Subject: [PATCH] Use `#![feature(const_panic)]` to produce better assertion errors (#1165) --- library/stdarch/crates/core_arch/src/lib.rs | 1 + library/stdarch/crates/core_arch/src/macros.rs | 4 ++-- library/stdarch/crates/core_arch/src/x86/macros.rs | 10 +++++----- library/stdarch/crates/core_arch/src/x86_64/macros.rs | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/library/stdarch/crates/core_arch/src/lib.rs b/library/stdarch/crates/core_arch/src/lib.rs index 30209f94446d..f69de392557d 100644 --- a/library/stdarch/crates/core_arch/src/lib.rs +++ b/library/stdarch/crates/core_arch/src/lib.rs @@ -7,6 +7,7 @@ asm, const_fn_union, const_fn_transmute, + const_panic, custom_inner_attributes, link_llvm_intrinsics, platform_intrinsics, diff --git a/library/stdarch/crates/core_arch/src/macros.rs b/library/stdarch/crates/core_arch/src/macros.rs index a82604304ff3..5f68ff79f8af 100644 --- a/library/stdarch/crates/core_arch/src/macros.rs +++ b/library/stdarch/crates/core_arch/src/macros.rs @@ -5,7 +5,7 @@ pub(crate) struct ValidateConstImm; impl ValidateConstImm { pub(crate) const VALID: () = { - let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize); + assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range"); }; } @@ -71,7 +71,7 @@ macro_rules! static_assert { struct Validate(); impl Validate<$imm> { const VALID: () = { - let _ = 1 / ($e as usize); + assert!($e, concat!("Assertion failed: ", stringify!($e))); }; } let _ = Validate::<$imm>::VALID; diff --git a/library/stdarch/crates/core_arch/src/x86/macros.rs b/library/stdarch/crates/core_arch/src/x86/macros.rs index 114bdf32012a..b9550ce79c4a 100644 --- a/library/stdarch/crates/core_arch/src/x86/macros.rs +++ b/library/stdarch/crates/core_arch/src/x86/macros.rs @@ -5,7 +5,7 @@ pub(crate) struct ValidateConstRound; impl ValidateConstRound { pub(crate) const VALID: () = { - let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) as usize); + assert!(IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11, "Invalid IMM value"); }; } @@ -21,7 +21,7 @@ macro_rules! static_assert_rounding { pub(crate) struct ValidateConstSae; impl ValidateConstSae { pub(crate) const VALID: () = { - let _ = 1 / ((IMM == 4 || IMM == 8) as usize); + assert!(IMM == 4 || IMM == 8, "Invalid IMM value"); }; } @@ -37,7 +37,7 @@ macro_rules! static_assert_sae { pub(crate) struct ValidateConstMantissasSae; impl ValidateConstMantissasSae { pub(crate) const VALID: () = { - let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 12) as usize); + assert!(IMM == 4 || IMM == 8 || IMM == 12, "Invalid IMM value"); }; } @@ -53,7 +53,7 @@ macro_rules! static_assert_mantissas_sae { pub(crate) struct ValidateConstImmU32; impl ValidateConstImmU32 { pub(crate) const VALID: () = { - let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize); + assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range"); }; } @@ -70,7 +70,7 @@ macro_rules! static_assert_imm_u8 { pub(crate) struct ValidateConstGatherScale; impl ValidateConstGatherScale { pub(crate) const VALID: () = { - let _ = 1 / ((SCALE == 1 || SCALE == 2 || SCALE == 4 || SCALE == 8) as usize); + assert!(SCALE == 1 || SCALE == 2 || SCALE == 4 || SCALE == 8, "Invalid SCALE value"); }; } diff --git a/library/stdarch/crates/core_arch/src/x86_64/macros.rs b/library/stdarch/crates/core_arch/src/x86_64/macros.rs index cafa37dd6fd2..9e3faf444d32 100644 --- a/library/stdarch/crates/core_arch/src/x86_64/macros.rs +++ b/library/stdarch/crates/core_arch/src/x86_64/macros.rs @@ -5,7 +5,7 @@ pub(crate) struct ValidateConstRound; impl ValidateConstRound { pub(crate) const VALID: () = { - let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) as usize); + assert!(IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11, "Invalid IMM value"); }; } @@ -21,7 +21,7 @@ macro_rules! static_assert_rounding { pub(crate) struct ValidateConstSae; impl ValidateConstSae { pub(crate) const VALID: () = { - let _ = 1 / ((IMM == 4 || IMM == 8) as usize); + assert!(IMM == 4 || IMM == 8, "Invalid IMM value"); }; }