Skip to content

Commit

Permalink
add a canary test for complex repr(simd)
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 14, 2020
1 parent 74f2941 commit 7565809
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,13 @@ impl<'tcx> TyS<'tcx> {
let f0_ty = variant.fields[0].ty(tcx, substs);

match f0_ty.kind() {
Array(f0_elem_ty, f0_len) => (f0_len.eval_usize(tcx, ParamEnv::empty()) as u64, f0_elem_ty),
Array(f0_elem_ty, f0_len) => {
// FIXME(repr_simd): https://github.com/rust-lang/rust/pull/78863#discussion_r522784112
// The way we evaluate the `N` in `[T; N]` here only works since we use
// `simd_size_and_type` post-monomorphization. It will probably start to ICE
// if we use it in generic code. See the `simd-array-trait` ui test.
(f0_len.eval_usize(tcx, ParamEnv::empty()) as u64, f0_elem_ty)
}
_ => (variant.fields.len() as u64, f0_ty),
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/ui/simd/simd-array-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Figuring out the size of a vector type that depends on traits doesn't ICE

#![allow(dead_code)]

// pretty-expanded FIXME #23616

#![feature(repr_simd, platform_intrinsics, const_generics)]
#![allow(non_camel_case_types, incomplete_features)]

pub trait Simd {
type Lane: Clone + Copy;
const SIZE: usize;
}

pub struct i32x4;
impl Simd for i32x4 {
type Lane = i32;
const SIZE: usize = 4;
}

#[repr(simd)]
#[derive(Copy, Clone)]
pub struct T<S: Simd>([S::Lane; S::SIZE]); //~ ERROR constant expression depends on a generic parameter

extern "platform-intrinsic" {
fn simd_insert<T, E>(x: T, idx: u32, y: E) -> T;
fn simd_extract<T, E>(x: T, idx: u32) -> E;
}

pub fn main() {
let mut t = T::<i32x4>([0; 4]);
unsafe {
for i in 0_i32..4 {
t = simd_insert(t, i as u32, i);
}
for i in 0_i32..4 {
assert_eq!(i, simd_extract(t, i as u32));
}
}
}
10 changes: 10 additions & 0 deletions src/test/ui/simd/simd-array-trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: constant expression depends on a generic parameter
--> $DIR/simd-array-trait.rs:23:23
|
LL | pub struct T<S: Simd>([S::Lane; S::SIZE]);
| ^^^^^^^^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to previous error

0 comments on commit 7565809

Please sign in to comment.