Skip to content

Commit 0fc2084

Browse files
Polygonalrphilberty
authored andcommitted
gccrs: Update SlicePattern typechecking against slice reference parents
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>
1 parent 39a5c15 commit 0fc2084

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

gcc/rust/backend/rust-compile-pattern.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ CompilePatternCheckExpr::visit (HIR::SlicePattern &pattern)
529529
// pattern must either be ArrayType or SliceType, should be already confirmed
530530
// by type checking
531531
rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
532-
|| lookup->get_kind () == TyTy::TypeKind::SLICE);
532+
|| lookup->get_kind () == TyTy::TypeKind::SLICE
533+
|| lookup->get_kind () == TyTy::REF);
533534

534535
size_t array_element_index = 0;
535536
switch (lookup->get_kind ())
@@ -895,7 +896,8 @@ CompilePatternBindings::visit (HIR::SlicePattern &pattern)
895896
rust_assert (ok);
896897

897898
rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
898-
|| lookup->get_kind () == TyTy::TypeKind::SLICE);
899+
|| lookup->get_kind () == TyTy::TypeKind::SLICE
900+
|| lookup->get_kind () == TyTy::REF);
899901

900902
size_t array_element_index = 0;
901903
switch (lookup->get_kind ())

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,21 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
675675
}
676676
case TyTy::SLICE:
677677
{
678-
auto &array_ty_ty = static_cast<TyTy::SliceType &> (*parent);
679-
parent_element_ty = array_ty_ty.get_element_type ();
678+
auto &slice_ty_ty = static_cast<TyTy::SliceType &> (*parent);
679+
parent_element_ty = slice_ty_ty.get_element_type ();
680+
break;
681+
}
682+
case TyTy::REF:
683+
{
684+
auto &ref_ty_ty = static_cast<TyTy::ReferenceType &> (*parent);
685+
const TyTy::SliceType *slice = nullptr;
686+
if (!ref_ty_ty.is_dyn_slice_type (&slice))
687+
{
688+
rust_error_at (pattern.get_locus (), "expected %s, found slice",
689+
parent->as_string ().c_str ());
690+
return;
691+
}
692+
parent_element_ty = slice->get_element_type ();
680693
break;
681694
}
682695
default:

0 commit comments

Comments
 (0)