]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Update SlicePattern typechecking against slice reference parents
authorYap Zhi Heng <yapzhhg@gmail.com>
Sun, 20 Jul 2025 07:55:51 +0000 (15:55 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:58 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit(SlicePattern)):
Add new type check case for SliceType wrapped in ReferenceType.
* backend/rust-compile-pattern.cc: Adjusted the asserts accordingly for
CompilePatternCheckExpr(SlicePattern) & CompilePatternBindings(SlicePattern).

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
gcc/rust/backend/rust-compile-pattern.cc
gcc/rust/typecheck/rust-hir-type-check-pattern.cc

index 6d889baa1992ce18e0301b3a8bb6742451fbae6b..e00de4f467f30f9e595a6a2fa923c54489a46e4e 100644 (file)
@@ -529,7 +529,8 @@ CompilePatternCheckExpr::visit (HIR::SlicePattern &pattern)
   // pattern must either be ArrayType or SliceType, should be already confirmed
   // by type checking
   rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
-              || lookup->get_kind () == TyTy::TypeKind::SLICE);
+              || lookup->get_kind () == TyTy::TypeKind::SLICE
+              || lookup->get_kind () == TyTy::REF);
 
   size_t array_element_index = 0;
   switch (lookup->get_kind ())
@@ -895,7 +896,8 @@ CompilePatternBindings::visit (HIR::SlicePattern &pattern)
   rust_assert (ok);
 
   rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
-              || lookup->get_kind () == TyTy::TypeKind::SLICE);
+              || lookup->get_kind () == TyTy::TypeKind::SLICE
+              || lookup->get_kind () == TyTy::REF);
 
   size_t array_element_index = 0;
   switch (lookup->get_kind ())
index bb0e27bb2a6c06b6e0b44c4e8232de8c192901cb..13fc9c85cc863d947746526769b696b0e6c5c258 100644 (file)
@@ -675,8 +675,21 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
       }
     case TyTy::SLICE:
       {
-       auto &array_ty_ty = static_cast<TyTy::SliceType &> (*parent);
-       parent_element_ty = array_ty_ty.get_element_type ();
+       auto &slice_ty_ty = static_cast<TyTy::SliceType &> (*parent);
+       parent_element_ty = slice_ty_ty.get_element_type ();
+       break;
+      }
+    case TyTy::REF:
+      {
+       auto &ref_ty_ty = static_cast<TyTy::ReferenceType &> (*parent);
+       const TyTy::SliceType *slice = nullptr;
+       if (!ref_ty_ty.is_dyn_slice_type (&slice))
+         {
+           rust_error_at (pattern.get_locus (), "expected %s, found slice",
+                          parent->as_string ().c_str ());
+           return;
+         }
+       parent_element_ty = slice->get_element_type ();
        break;
       }
     default: