From 78ab9042cbbe2607511ab98fd0ae7b2e2ec4466c 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) --- library/stdarch/crates/core_arch/src/macros.rs | 13 +++++++++++++ library/stdarch/crates/core_arch/src/x86/sse.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/library/stdarch/crates/core_arch/src/macros.rs b/library/stdarch/crates/core_arch/src/macros.rs index 7ebff27e8c87..ab643d9a29bf 100644 --- a/library/stdarch/crates/core_arch/src/macros.rs +++ b/library/stdarch/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/library/stdarch/crates/core_arch/src/x86/sse.rs b/library/stdarch/crates/core_arch/src/x86/sse.rs index 1289379e7344..3e7b54e302be 100644 --- a/library/stdarch/crates/core_arch/src/x86/sse.rs +++ b/library/stdarch/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,