Skip to content

Commit 13c8bc5

Browse files
alexcrichtonAmanieu
authored andcommitted
Add static assertions for all const generics
1 parent 6725b97 commit 13c8bc5

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

crates/core_arch/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ macro_rules! static_assert_imm16 {
6767

6868
#[allow(unused)]
6969
macro_rules! static_assert {
70-
($imm:ident : $ty:ty where $e:expr) => {
70+
($imm:ident : $ty:ty where $e:expr) => {{
7171
struct Validate<const $imm: $ty>();
7272
impl<const $imm: $ty> Validate<$imm> {
7373
const VALID: () = {
7474
let _ = 1 / ($e as usize);
7575
};
7676
}
7777
let _ = Validate::<$imm>::VALID;
78-
};
78+
}};
7979
}
8080

8181
#[allow(unused)]

crates/core_arch/src/wasm32/memory.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use stdarch_test::assert_instr;
33

44
extern "C" {
55
#[link_name = "llvm.wasm.memory.grow.i32"]
6-
fn llvm_memory_grow(mem: i32, pages: i32) -> i32;
6+
fn llvm_memory_grow(mem: u32, pages: i32) -> i32;
77
#[link_name = "llvm.wasm.memory.size.i32"]
8-
fn llvm_memory_size(mem: i32) -> i32;
8+
fn llvm_memory_size(mem: u32) -> i32;
99
}
1010

1111
/// Corresponding intrinsic to wasm's [`memory.size` instruction][instr]
@@ -25,13 +25,8 @@ extern "C" {
2525
#[rustc_legacy_const_generics(0)]
2626
#[stable(feature = "simd_wasm32", since = "1.33.0")]
2727
pub fn memory_size<const MEM: u32>() -> usize {
28-
unsafe {
29-
// FIXME: Consider replacing with a static_assert!
30-
if MEM != 0 {
31-
crate::intrinsics::abort();
32-
}
33-
llvm_memory_size(0) as usize
34-
}
28+
static_assert!(MEM: u32 where MEM == 0);
29+
unsafe { llvm_memory_size(MEM) as usize }
3530
}
3631

3732
/// Corresponding intrinsic to wasm's [`memory.grow` instruction][instr]
@@ -55,10 +50,7 @@ pub fn memory_size<const MEM: u32>() -> usize {
5550
#[stable(feature = "simd_wasm32", since = "1.33.0")]
5651
pub fn memory_grow<const MEM: u32>(delta: usize) -> usize {
5752
unsafe {
58-
// FIXME: Consider replacing with a static_assert!
59-
if MEM != 0 {
60-
crate::intrinsics::abort();
61-
}
62-
llvm_memory_grow(0, delta as i32) as isize as usize
53+
static_assert!(MEM: u32 where MEM == 0);
54+
llvm_memory_grow(MEM, delta as i32) as isize as usize
6355
}
6456
}

crates/core_arch/src/wasm32/simd128.rs

+50
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ pub unsafe fn v128_store(m: *mut v128, a: v128) {
404404
#[cfg_attr(test, assert_instr(v128.load8_lane, L = 0))]
405405
#[target_feature(enable = "simd128")]
406406
pub unsafe fn v128_load8_lane<const L: usize>(v: v128, m: *const u8) -> v128 {
407+
static_assert!(L: usize where L < 16);
407408
transmute(llvm_load8_lane(m, v.as_u8x16(), L))
408409
}
409410

@@ -412,6 +413,7 @@ pub unsafe fn v128_load8_lane<const L: usize>(v: v128, m: *const u8) -> v128 {
412413
#[cfg_attr(test, assert_instr(v128.load16_lane, L = 0))]
413414
#[target_feature(enable = "simd128")]
414415
pub unsafe fn v128_load16_lane<const L: usize>(v: v128, m: *const u16) -> v128 {
416+
static_assert!(L: usize where L < 8);
415417
transmute(llvm_load16_lane(m, v.as_u16x8(), L))
416418
}
417419

@@ -420,6 +422,7 @@ pub unsafe fn v128_load16_lane<const L: usize>(v: v128, m: *const u16) -> v128 {
420422
#[cfg_attr(test, assert_instr(v128.load32_lane, L = 0))]
421423
#[target_feature(enable = "simd128")]
422424
pub unsafe fn v128_load32_lane<const L: usize>(v: v128, m: *const u32) -> v128 {
425+
static_assert!(L: usize where L < 4);
423426
transmute(llvm_load32_lane(m, v.as_u32x4(), L))
424427
}
425428

@@ -428,6 +431,7 @@ pub unsafe fn v128_load32_lane<const L: usize>(v: v128, m: *const u32) -> v128 {
428431
#[cfg_attr(test, assert_instr(v128.load64_lane, L = 0))]
429432
#[target_feature(enable = "simd128")]
430433
pub unsafe fn v128_load64_lane<const L: usize>(v: v128, m: *const u64) -> v128 {
434+
static_assert!(L: usize where L < 2);
431435
transmute(llvm_load64_lane(m, v.as_u64x2(), L))
432436
}
433437

@@ -436,6 +440,7 @@ pub unsafe fn v128_load64_lane<const L: usize>(v: v128, m: *const u64) -> v128 {
436440
#[cfg_attr(test, assert_instr(v128.store8_lane, L = 0))]
437441
#[target_feature(enable = "simd128")]
438442
pub unsafe fn v128_store8_lane<const L: usize>(v: v128, m: *mut u8) {
443+
static_assert!(L: usize where L < 16);
439444
llvm_store8_lane(m, v.as_u8x16(), L);
440445
}
441446

@@ -444,6 +449,7 @@ pub unsafe fn v128_store8_lane<const L: usize>(v: v128, m: *mut u8) {
444449
#[cfg_attr(test, assert_instr(v128.store16_lane, L = 0))]
445450
#[target_feature(enable = "simd128")]
446451
pub unsafe fn v128_store16_lane<const L: usize>(v: v128, m: *mut u16) {
452+
static_assert!(L: usize where L < 8);
447453
llvm_store16_lane(m, v.as_u16x8(), L)
448454
}
449455

@@ -452,6 +458,7 @@ pub unsafe fn v128_store16_lane<const L: usize>(v: v128, m: *mut u16) {
452458
#[cfg_attr(test, assert_instr(v128.store32_lane, L = 0))]
453459
#[target_feature(enable = "simd128")]
454460
pub unsafe fn v128_store32_lane<const L: usize>(v: v128, m: *mut u32) {
461+
static_assert!(L: usize where L < 4);
455462
llvm_store32_lane(m, v.as_u32x4(), L)
456463
}
457464

@@ -460,6 +467,7 @@ pub unsafe fn v128_store32_lane<const L: usize>(v: v128, m: *mut u32) {
460467
#[cfg_attr(test, assert_instr(v128.store64_lane, L = 0))]
461468
#[target_feature(enable = "simd128")]
462469
pub unsafe fn v128_store64_lane<const L: usize>(v: v128, m: *mut u64) {
470+
static_assert!(L: usize where L < 2);
463471
llvm_store64_lane(m, v.as_u64x2(), L)
464472
}
465473

@@ -649,6 +657,22 @@ pub unsafe fn i8x16_shuffle<
649657
a: v128,
650658
b: v128,
651659
) -> v128 {
660+
static_assert!(I0: usize where I0 < 32);
661+
static_assert!(I1: usize where I1 < 32);
662+
static_assert!(I2: usize where I2 < 32);
663+
static_assert!(I3: usize where I3 < 32);
664+
static_assert!(I4: usize where I4 < 32);
665+
static_assert!(I5: usize where I5 < 32);
666+
static_assert!(I6: usize where I6 < 32);
667+
static_assert!(I7: usize where I7 < 32);
668+
static_assert!(I8: usize where I8 < 32);
669+
static_assert!(I9: usize where I9 < 32);
670+
static_assert!(I10: usize where I10 < 32);
671+
static_assert!(I11: usize where I11 < 32);
672+
static_assert!(I12: usize where I12 < 32);
673+
static_assert!(I13: usize where I13 < 32);
674+
static_assert!(I14: usize where I14 < 32);
675+
static_assert!(I15: usize where I15 < 32);
652676
let shuf = simd_shuffle16::<u8x16, u8x16>(
653677
a.as_u8x16(),
654678
b.as_u8x16(),
@@ -696,6 +720,14 @@ pub unsafe fn i16x8_shuffle<
696720
a: v128,
697721
b: v128,
698722
) -> v128 {
723+
static_assert!(I0: usize where I0 < 16);
724+
static_assert!(I1: usize where I1 < 16);
725+
static_assert!(I2: usize where I2 < 16);
726+
static_assert!(I3: usize where I3 < 16);
727+
static_assert!(I4: usize where I4 < 16);
728+
static_assert!(I5: usize where I5 < 16);
729+
static_assert!(I6: usize where I6 < 16);
730+
static_assert!(I7: usize where I7 < 16);
699731
let shuf = simd_shuffle8::<u16x8, u16x8>(
700732
a.as_u16x8(),
701733
b.as_u16x8(),
@@ -720,6 +752,10 @@ pub unsafe fn i32x4_shuffle<const I0: usize, const I1: usize, const I2: usize, c
720752
a: v128,
721753
b: v128,
722754
) -> v128 {
755+
static_assert!(I0: usize where I0 < 8);
756+
static_assert!(I1: usize where I1 < 8);
757+
static_assert!(I2: usize where I2 < 8);
758+
static_assert!(I3: usize where I3 < 8);
723759
let shuf = simd_shuffle4::<u32x4, u32x4>(
724760
a.as_u32x4(),
725761
b.as_u32x4(),
@@ -739,6 +775,8 @@ pub unsafe fn i32x4_shuffle<const I0: usize, const I1: usize, const I2: usize, c
739775
#[cfg_attr(test, assert_instr(i8x16.shuffle, I0 = 0, I1 = 2))]
740776
#[target_feature(enable = "simd128")]
741777
pub unsafe fn i64x2_shuffle<const I0: usize, const I1: usize>(a: v128, b: v128) -> v128 {
778+
static_assert!(I0: usize where I0 < 4);
779+
static_assert!(I1: usize where I1 < 4);
742780
let shuf = simd_shuffle2::<u64x2, u64x2>(a.as_u64x2(), b.as_u64x2(), [I0 as u32, I1 as u32]);
743781
transmute(shuf)
744782
}
@@ -751,6 +789,7 @@ pub unsafe fn i64x2_shuffle<const I0: usize, const I1: usize>(a: v128, b: v128)
751789
#[cfg_attr(test, assert_instr(i8x16.extract_lane_s, N = 3))]
752790
#[target_feature(enable = "simd128")]
753791
pub unsafe fn i8x16_extract_lane<const N: usize>(a: v128) -> i8 {
792+
static_assert!(N: usize where N < 16);
754793
simd_extract(a.as_i8x16(), N as u32)
755794
}
756795

@@ -762,6 +801,7 @@ pub unsafe fn i8x16_extract_lane<const N: usize>(a: v128) -> i8 {
762801
#[cfg_attr(test, assert_instr(i8x16.replace_lane, N = 2))]
763802
#[target_feature(enable = "simd128")]
764803
pub unsafe fn i8x16_replace_lane<const N: usize>(a: v128, val: i8) -> v128 {
804+
static_assert!(N: usize where N < 16);
765805
transmute(simd_insert(a.as_i8x16(), N as u32, val))
766806
}
767807

@@ -773,6 +813,7 @@ pub unsafe fn i8x16_replace_lane<const N: usize>(a: v128, val: i8) -> v128 {
773813
#[cfg_attr(test, assert_instr(i16x8.extract_lane_s, N = 2))]
774814
#[target_feature(enable = "simd128")]
775815
pub unsafe fn i16x8_extract_lane<const N: usize>(a: v128) -> i16 {
816+
static_assert!(N: usize where N < 8);
776817
simd_extract(a.as_i16x8(), N as u32)
777818
}
778819

@@ -784,6 +825,7 @@ pub unsafe fn i16x8_extract_lane<const N: usize>(a: v128) -> i16 {
784825
#[cfg_attr(test, assert_instr(i16x8.replace_lane, N = 2))]
785826
#[target_feature(enable = "simd128")]
786827
pub unsafe fn i16x8_replace_lane<const N: usize>(a: v128, val: i16) -> v128 {
828+
static_assert!(N: usize where N < 8);
787829
transmute(simd_insert(a.as_i16x8(), N as u32, val))
788830
}
789831

@@ -795,6 +837,7 @@ pub unsafe fn i16x8_replace_lane<const N: usize>(a: v128, val: i16) -> v128 {
795837
#[cfg_attr(test, assert_instr(i32x4.extract_lane, N = 2))]
796838
#[target_feature(enable = "simd128")]
797839
pub unsafe fn i32x4_extract_lane<const N: usize>(a: v128) -> i32 {
840+
static_assert!(N: usize where N < 4);
798841
simd_extract(a.as_i32x4(), N as u32)
799842
}
800843

@@ -806,6 +849,7 @@ pub unsafe fn i32x4_extract_lane<const N: usize>(a: v128) -> i32 {
806849
#[cfg_attr(test, assert_instr(i32x4.replace_lane, N = 2))]
807850
#[target_feature(enable = "simd128")]
808851
pub unsafe fn i32x4_replace_lane<const N: usize>(a: v128, val: i32) -> v128 {
852+
static_assert!(N: usize where N < 4);
809853
transmute(simd_insert(a.as_i32x4(), N as u32, val))
810854
}
811855

@@ -817,6 +861,7 @@ pub unsafe fn i32x4_replace_lane<const N: usize>(a: v128, val: i32) -> v128 {
817861
#[cfg_attr(test, assert_instr(i64x2.extract_lane, N = 1))]
818862
#[target_feature(enable = "simd128")]
819863
pub unsafe fn i64x2_extract_lane<const N: usize>(a: v128) -> i64 {
864+
static_assert!(N: usize where N < 2);
820865
simd_extract(a.as_i64x2(), N as u32)
821866
}
822867

@@ -828,6 +873,7 @@ pub unsafe fn i64x2_extract_lane<const N: usize>(a: v128) -> i64 {
828873
#[cfg_attr(test, assert_instr(i64x2.replace_lane, N = 0))]
829874
#[target_feature(enable = "simd128")]
830875
pub unsafe fn i64x2_replace_lane<const N: usize>(a: v128, val: i64) -> v128 {
876+
static_assert!(N: usize where N < 2);
831877
transmute(simd_insert(a.as_i64x2(), N as u32, val))
832878
}
833879

@@ -839,6 +885,7 @@ pub unsafe fn i64x2_replace_lane<const N: usize>(a: v128, val: i64) -> v128 {
839885
#[cfg_attr(test, assert_instr(f32x4.extract_lane, N = 1))]
840886
#[target_feature(enable = "simd128")]
841887
pub unsafe fn f32x4_extract_lane<const N: usize>(a: v128) -> f32 {
888+
static_assert!(N: usize where N < 4);
842889
simd_extract(a.as_f32x4(), N as u32)
843890
}
844891

@@ -850,6 +897,7 @@ pub unsafe fn f32x4_extract_lane<const N: usize>(a: v128) -> f32 {
850897
#[cfg_attr(test, assert_instr(f32x4.replace_lane, N = 1))]
851898
#[target_feature(enable = "simd128")]
852899
pub unsafe fn f32x4_replace_lane<const N: usize>(a: v128, val: f32) -> v128 {
900+
static_assert!(N: usize where N < 4);
853901
transmute(simd_insert(a.as_f32x4(), N as u32, val))
854902
}
855903

@@ -861,6 +909,7 @@ pub unsafe fn f32x4_replace_lane<const N: usize>(a: v128, val: f32) -> v128 {
861909
#[cfg_attr(test, assert_instr(f64x2.extract_lane, N = 1))]
862910
#[target_feature(enable = "simd128")]
863911
pub unsafe fn f64x2_extract_lane<const N: usize>(a: v128) -> f64 {
912+
static_assert!(N: usize where N < 2);
864913
simd_extract(a.as_f64x2(), N as u32)
865914
}
866915

@@ -872,6 +921,7 @@ pub unsafe fn f64x2_extract_lane<const N: usize>(a: v128) -> f64 {
872921
#[cfg_attr(test, assert_instr(f64x2.replace_lane, N = 1))]
873922
#[target_feature(enable = "simd128")]
874923
pub unsafe fn f64x2_replace_lane<const N: usize>(a: v128, val: f64) -> v128 {
924+
static_assert!(N: usize where N < 2);
875925
transmute(simd_insert(a.as_f64x2(), N as u32, val))
876926
}
877927

0 commit comments

Comments
 (0)