From: Yap Zhi Heng <yapz...@gmail.com>

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 <yapz...@gmail.com>
---
 .../typecheck/rust-hir-type-check-pattern.cc  | 20 ++++++++++++++++---
 .../compile/slicepattern-size-mismatch.rs     |  8 ++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/slicepattern-size-mismatch.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc 
b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index 5608030db72..bb0e27bb2a6 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 00000000000..b54b5320604
--- /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
-- 
2.49.0

Reply via email to