Skip to content

Commit 5dc8c4d

Browse files
Polygonalrphilberty
authored andcommitted
gccrs: Add size checking to SlicePattern
gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-pattern.cc(TypeCheckPattern::visit(SlicePattern)): Implement size checking for SlicePattern when type checking against array parent Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent 5c3e2ad commit 5dc8c4d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,25 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
652652
{
653653
case TyTy::ARRAY:
654654
{
655-
// FIXME: implement compile-time size checks when ArrayType's capacity
656-
// is updated to be evaluated in compile-time
657-
// https://github.com/Rust-GCC/gccrs/issues/3882
658655
auto &array_ty_ty = static_cast<TyTy::ArrayType &> (*parent);
659656
parent_element_ty = array_ty_ty.get_element_type ();
657+
tree cap = array_ty_ty.get_capacity ();
658+
if (error_operand_p (cap))
659+
{
660+
rust_error_at (parent->get_locus (),
661+
"capacity of array %qs is not known at compile time",
662+
array_ty_ty.get_name ().c_str ());
663+
break;
664+
}
665+
auto cap_wi = wi::to_wide (cap).to_uhwi ();
666+
if (cap_wi != pattern.get_items ().size ())
667+
{
668+
rust_error_at (pattern.get_locus (), ErrorCode::E0527,
669+
"pattern requires %lu elements but array has %lu",
670+
(unsigned long) pattern.get_items ().size (),
671+
(unsigned long) cap_wi);
672+
break;
673+
}
660674
break;
661675
}
662676
case TyTy::SLICE:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let arr = [0, 1];
3+
4+
match arr {
5+
[0, 1, 2] => {} // { dg-error "pattern requires 3 elements but array has 2 .E0527." }
6+
_ => {}
7+
}
8+
}

0 commit comments

Comments
 (0)