Skip to content

Commit ce5415b

Browse files
committed
Use #![feature(const_panic)] to produce better assertion errors
Currently, `core_arch` uses `1 / (bool_expr as usize)` to generate a post-monomorphization error when a condition is false. However, this approach generates opaque error messages ('evaluation of constant value failed'), which could easily be mistaken for an internal compiler error by users. Using the `const_panic` feature, we can `panic!()` with a more descriptive error message. Unfortunately, we cannot include any of the actual values in our message, since `const` traits do not yet exist.
1 parent c957acd commit ce5415b

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

crates/core_arch/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
asm,
88
const_fn_union,
99
const_fn_transmute,
10+
const_panic,
1011
custom_inner_attributes,
1112
link_llvm_intrinsics,
1213
platform_intrinsics,

crates/core_arch/src/macros.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
pub(crate) struct ValidateConstImm<const IMM: i32, const MIN: i32, const MAX: i32>;
66
impl<const IMM: i32, const MIN: i32, const MAX: i32> ValidateConstImm<IMM, MIN, MAX> {
77
pub(crate) const VALID: () = {
8-
let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
8+
if !(IMM >= MIN && IMM <= MAX) {
9+
panic!("IMM value not in expected range");
10+
}
911
};
1012
}
1113

@@ -71,7 +73,9 @@ macro_rules! static_assert {
7173
struct Validate<const $imm: $ty>();
7274
impl<const $imm: $ty> Validate<$imm> {
7375
const VALID: () = {
74-
let _ = 1 / ($e as usize);
76+
if !$e {
77+
panic!("Assertion failed");
78+
}
7579
};
7680
}
7781
let _ = Validate::<$imm>::VALID;

crates/core_arch/src/x86/macros.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
pub(crate) struct ValidateConstRound<const IMM: i32>;
66
impl<const IMM: i32> ValidateConstRound<IMM> {
77
pub(crate) const VALID: () = {
8-
let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) as usize);
8+
if !(IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) {
9+
panic!("Invalid IMM value");
10+
}
911
};
1012
}
1113

@@ -21,7 +23,9 @@ macro_rules! static_assert_rounding {
2123
pub(crate) struct ValidateConstSae<const IMM: i32>;
2224
impl<const IMM: i32> ValidateConstSae<IMM> {
2325
pub(crate) const VALID: () = {
24-
let _ = 1 / ((IMM == 4 || IMM == 8) as usize);
26+
if !(IMM == 4 || IMM == 8) {
27+
panic!("Invalid IMM value");
28+
}
2529
};
2630
}
2731

@@ -37,7 +41,9 @@ macro_rules! static_assert_sae {
3741
pub(crate) struct ValidateConstMantissasSae<const IMM: i32>;
3842
impl<const IMM: i32> ValidateConstMantissasSae<IMM> {
3943
pub(crate) const VALID: () = {
40-
let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 12) as usize);
44+
if !(IMM == 4 || IMM == 8 || IMM == 12) {
45+
panic!("Invalid IMM value");
46+
}
4147
};
4248
}
4349

@@ -53,7 +59,9 @@ macro_rules! static_assert_mantissas_sae {
5359
pub(crate) struct ValidateConstImmU32<const IMM: u32, const MIN: u32, const MAX: u32>;
5460
impl<const IMM: u32, const MIN: u32, const MAX: u32> ValidateConstImmU32<IMM, MIN, MAX> {
5561
pub(crate) const VALID: () = {
56-
let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
62+
if !(IMM >= MIN && IMM <= MAX) {
63+
panic!("IMM value not in expected range");
64+
}
5765
};
5866
}
5967

@@ -70,7 +78,9 @@ macro_rules! static_assert_imm_u8 {
7078
pub(crate) struct ValidateConstGatherScale<const SCALE: i32>;
7179
impl<const SCALE: i32> ValidateConstGatherScale<SCALE> {
7280
pub(crate) const VALID: () = {
73-
let _ = 1 / ((SCALE == 1 || SCALE == 2 || SCALE == 4 || SCALE == 8) as usize);
81+
if !(SCALE == 1 || SCALE == 2 || SCALE == 4 || SCALE == 8) {
82+
panic!("Invalid SCALE value");
83+
}
7484
};
7585
}
7686

crates/core_arch/src/x86_64/macros.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
pub(crate) struct ValidateConstRound<const IMM: i32>;
66
impl<const IMM: i32> ValidateConstRound<IMM> {
77
pub(crate) const VALID: () = {
8-
let _ = 1 / ((IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) as usize);
8+
if !(IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11) {
9+
panic!("Invalid IMM value");
10+
}
911
};
1012
}
1113

@@ -21,7 +23,9 @@ macro_rules! static_assert_rounding {
2123
pub(crate) struct ValidateConstSae<const IMM: i32>;
2224
impl<const IMM: i32> ValidateConstSae<IMM> {
2325
pub(crate) const VALID: () = {
24-
let _ = 1 / ((IMM == 4 || IMM == 8) as usize);
26+
if !(IMM == 4 || IMM == 8) {
27+
panic!("Invalid IMM value");
28+
}
2529
};
2630
}
2731

0 commit comments

Comments
 (0)