Skip to content

Commit 3b52e06

Browse files
authored
Use static assertions to bound-check const generics (#1019)
1 parent 8d50178 commit 3b52e06

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/core_arch/src/macros.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
//! Utility macros.
22
3+
#[allow(unused)]
4+
macro_rules! static_assert {
5+
($imm:ident : $ty:ty where $e:expr) => {
6+
struct Validate<const $imm: $ty>();
7+
impl<const $imm: $ty> Validate<$imm> {
8+
const VALID: () = {
9+
let _ = 1 / ($e as usize);
10+
};
11+
}
12+
let _ = Validate::<$imm>::VALID;
13+
};
14+
}
15+
316
#[allow(unused)]
417
macro_rules! constify_imm8 {
518
($imm8:expr, $expand:ident) => {

crates/core_arch/src/x86/sse.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ pub const fn _MM_SHUFFLE(z: u32, y: u32, x: u32, w: u32) -> i32 {
10101010
#[rustc_legacy_const_generics(2)]
10111011
#[stable(feature = "simd_x86", since = "1.27.0")]
10121012
pub unsafe fn _mm_shuffle_ps<const mask: i32>(a: __m128, b: __m128) -> __m128 {
1013-
assert!(mask >= 0 && mask <= 255);
1013+
static_assert!(mask: i32 where mask >= 0 && mask <= 255);
10141014
simd_shuffle4(
10151015
a,
10161016
b,

0 commit comments

Comments
 (0)