Skip to content

Commit 4425c97

Browse files
committed
gccrs: Fix const generics handling on array types
When we were processing generic const param types on arrays the size type was overriding the const param decl because of a hirid reference mismatch Fixes #3879 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): fix mappings gcc/testsuite/ChangeLog: * rust/compile/const_generics_18.rs: New test. * rust/compile/const_generics_19.rs: New test. * rust/execute/torture/const-generics-3.rs: New test. * rust/execute/torture/const-generics-4.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
1 parent 24ed1a3 commit 4425c97

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

gcc/rust/typecheck/rust-hir-type-check-type.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ TypeCheckType::visit (HIR::ArrayType &type)
704704
TyTy::BaseType *expected_ty = nullptr;
705705
bool ok = context->lookup_builtin ("usize", &expected_ty);
706706
rust_assert (ok);
707-
context->insert_type (type.get_size_expr ().get_mappings (), expected_ty);
708707

709708
TyTy::BaseConstType *const_type = nullptr;
710709
if (capacity_type->get_kind () == TyTy::TypeKind::CONST)
@@ -745,7 +744,7 @@ TypeCheckType::visit (HIR::ArrayType &type)
745744
translated
746745
= new TyTy::ArrayType (type.get_mappings ().get_hirid (), type.get_locus (),
747746
TyTy::TyVar (
748-
const_type->as_base_type ()->get_ty_ref ()),
747+
const_type->as_base_type ()->get_ref ()),
749748
TyTy::TyVar (element_type->get_ref ()));
750749
}
751750

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[lang = "sized"]
2+
trait Sized {}
3+
4+
struct Foo<const N: usize>;
5+
type Alias = Foo<4>;
6+
7+
fn main() -> i32 {
8+
let _x: Alias = Foo::<4> {};
9+
0
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[lang = "sized"]
2+
trait Sized {}
3+
4+
struct Foo<const N: usize>;
5+
struct Wrapper<T>(T);
6+
7+
fn main() -> i32 {
8+
let _: Wrapper<Foo<3>> = Wrapper(Foo::<3> {});
9+
0
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[lang = "sized"]
2+
pub trait Sized {}
3+
4+
fn simd_shuffle<const N: usize>(idx: [u32; N]) -> [u32; N] {
5+
idx
6+
}
7+
8+
fn main() -> i32 {
9+
let a = [1u32, 2, 3, 4];
10+
let out = simd_shuffle(a);
11+
let _check: [u32; 4] = out;
12+
0
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#[lang = "sized"]
2+
trait Sized {}
3+
4+
#[allow(unused)]
5+
macro_rules! simd_shuffle {
6+
($x:expr, $y:expr, $idx:expr $(,)?) => {{
7+
simd_shuffle(
8+
$x,
9+
$y,
10+
const {
11+
let v: [u32; _] = $idx;
12+
v
13+
},
14+
)
15+
}};
16+
}
17+
18+
const fn simd_shuffle(_a: [u32; 4], _b: [u32; 4], idx: [u32; 4]) -> [u32; 4] {
19+
idx
20+
}
21+
22+
fn main() -> i32 {
23+
let a = [1, 2, 3, 4];
24+
let b = [5, 6, 7, 8];
25+
let indices = [3, 2, 1, 0];
26+
27+
let result: [u32; 4] = simd_shuffle!(a, b, indices);
28+
29+
if result[0] != 3 {
30+
return 1;
31+
}
32+
if result[1] != 2 {
33+
return 2;
34+
}
35+
if result[2] != 1 {
36+
return 3;
37+
}
38+
if result[3] != 0 {
39+
return 4;
40+
}
41+
42+
0
43+
}

0 commit comments

Comments
 (0)