aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorYap Zhi Heng <yapzhhg@gmail.com>2025-07-11 22:29:31 +0800
committerArthur Cohen <arthur.cohen@embecosm.com>2025-08-05 16:36:55 +0200
commit72eb21fdf97da9613693dfcebe7b3270b133b7f0 (patch)
treeca0a2c431f6fbcedda537fb078d5fcf201a982e3 /gcc
parenta7c999f8fea93e22af8e38489903f645531f2448 (diff)
downloadgcc-72eb21fdf97da9613693dfcebe7b3270b133b7f0.zip
gcc-72eb21fdf97da9613693dfcebe7b3270b133b7f0.tar.gz
gcc-72eb21fdf97da9613693dfcebe7b3270b133b7f0.tar.bz2
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>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-pattern.cc20
-rw-r--r--gcc/testsuite/rust/compile/slicepattern-size-mismatch.rs8
2 files changed, 25 insertions, 3 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index 5608030..bb0e27b 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -652,11 +652,25 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
{
case TyTy::ARRAY:
{
- // FIXME: implement compile-time size checks when ArrayType's capacity
- // is updated to be evaluated in compile-time
- // https://github.com/Rust-GCC/gccrs/issues/3882
auto &array_ty_ty = static_cast<TyTy::ArrayType &> (*parent);
parent_element_ty = array_ty_ty.get_element_type ();
+ tree cap = array_ty_ty.get_capacity ();
+ if (error_operand_p (cap))
+ {
+ rust_error_at (parent->get_locus (),
+ "capacity of array %qs is not known at compile time",
+ array_ty_ty.get_name ().c_str ());
+ break;
+ }
+ auto cap_wi = wi::to_wide (cap).to_uhwi ();
+ if (cap_wi != pattern.get_items ().size ())
+ {
+ rust_error_at (pattern.get_locus (), ErrorCode::E0527,
+ "pattern requires %lu elements but array has %lu",
+ (unsigned long) pattern.get_items ().size (),
+ (unsigned long) cap_wi);
+ break;
+ }
break;
}
case TyTy::SLICE:
diff --git a/gcc/testsuite/rust/compile/slicepattern-size-mismatch.rs b/gcc/testsuite/rust/compile/slicepattern-size-mismatch.rs
new file mode 100644
index 0000000..b54b532
--- /dev/null
+++ b/gcc/testsuite/rust/compile/slicepattern-size-mismatch.rs
@@ -0,0 +1,8 @@
+fn main() {
+ let arr = [0, 1];
+
+ match arr {
+ [0, 1, 2] => {} // { dg-error "pattern requires 3 elements but array has 2 .E0527." }
+ _ => {}
+ }
+} \ No newline at end of file