Skip to content

Commit 7565809

Browse files
committed
add a canary test for complex repr(simd)
1 parent 74f2941 commit 7565809

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,13 @@ impl<'tcx> TyS<'tcx> {
19661966
let f0_ty = variant.fields[0].ty(tcx, substs);
19671967

19681968
match f0_ty.kind() {
1969-
Array(f0_elem_ty, f0_len) => (f0_len.eval_usize(tcx, ParamEnv::empty()) as u64, f0_elem_ty),
1969+
Array(f0_elem_ty, f0_len) => {
1970+
// FIXME(repr_simd): https://github.com/rust-lang/rust/pull/78863#discussion_r522784112
1971+
// The way we evaluate the `N` in `[T; N]` here only works since we use
1972+
// `simd_size_and_type` post-monomorphization. It will probably start to ICE
1973+
// if we use it in generic code. See the `simd-array-trait` ui test.
1974+
(f0_len.eval_usize(tcx, ParamEnv::empty()) as u64, f0_elem_ty)
1975+
}
19701976
_ => (variant.fields.len() as u64, f0_ty),
19711977
}
19721978
}

src/test/ui/simd/simd-array-trait.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Figuring out the size of a vector type that depends on traits doesn't ICE
2+
3+
#![allow(dead_code)]
4+
5+
// pretty-expanded FIXME #23616
6+
7+
#![feature(repr_simd, platform_intrinsics, const_generics)]
8+
#![allow(non_camel_case_types, incomplete_features)]
9+
10+
pub trait Simd {
11+
type Lane: Clone + Copy;
12+
const SIZE: usize;
13+
}
14+
15+
pub struct i32x4;
16+
impl Simd for i32x4 {
17+
type Lane = i32;
18+
const SIZE: usize = 4;
19+
}
20+
21+
#[repr(simd)]
22+
#[derive(Copy, Clone)]
23+
pub struct T<S: Simd>([S::Lane; S::SIZE]); //~ ERROR constant expression depends on a generic parameter
24+
25+
extern "platform-intrinsic" {
26+
fn simd_insert<T, E>(x: T, idx: u32, y: E) -> T;
27+
fn simd_extract<T, E>(x: T, idx: u32) -> E;
28+
}
29+
30+
pub fn main() {
31+
let mut t = T::<i32x4>([0; 4]);
32+
unsafe {
33+
for i in 0_i32..4 {
34+
t = simd_insert(t, i as u32, i);
35+
}
36+
for i in 0_i32..4 {
37+
assert_eq!(i, simd_extract(t, i as u32));
38+
}
39+
}
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/simd-array-trait.rs:23:23
3+
|
4+
LL | pub struct T<S: Simd>([S::Lane; S::SIZE]);
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)