Skip to content

Commit 92ac8f6

Browse files
committed
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 <yapzhhg@gmail.com>
1 parent 5c3e2ad commit 92ac8f6

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,24 @@ 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 %zu elements but array has %llu",
670+
pattern.get_items ().size (), cap_wi);
671+
break;
672+
}
660673
break;
661674
}
662675
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)