From 3b52e0641d59c096363f2b84d67a531b97aa2e68 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sat, 27 Feb 2021 16:16:47 +0000 Subject: [PATCH] Use static assertions to bound-check const generics (#1019) --- crates/core_arch/src/macros.rs | 13 +++++++++++++ crates/core_arch/src/x86/sse.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/core_arch/src/macros.rs b/crates/core_arch/src/macros.rs index 7ebff27e8c87c..ab643d9a29bf2 100644 --- a/crates/core_arch/src/macros.rs +++ b/crates/core_arch/src/macros.rs @@ -1,5 +1,18 @@ //! Utility macros. +#[allow(unused)] +macro_rules! static_assert { + ($imm:ident : $ty:ty where $e:expr) => { + struct Validate(); + impl Validate<$imm> { + const VALID: () = { + let _ = 1 / ($e as usize); + }; + } + let _ = Validate::<$imm>::VALID; + }; +} + #[allow(unused)] macro_rules! constify_imm8 { ($imm8:expr, $expand:ident) => { diff --git a/crates/core_arch/src/x86/sse.rs b/crates/core_arch/src/x86/sse.rs index 1289379e7344a..3e7b54e302bec 100644 --- a/crates/core_arch/src/x86/sse.rs +++ b/crates/core_arch/src/x86/sse.rs @@ -1010,7 +1010,7 @@ pub const fn _MM_SHUFFLE(z: u32, y: u32, x: u32, w: u32) -> i32 { #[rustc_legacy_const_generics(2)] #[stable(feature = "simd_x86", since = "1.27.0")] pub unsafe fn _mm_shuffle_ps(a: __m128, b: __m128) -> __m128 { - assert!(mask >= 0 && mask <= 255); + static_assert!(mask: i32 where mask >= 0 && mask <= 255); simd_shuffle4( a, b,