Skip to content

Commit 894e9d2

Browse files
committed
Handle generic Slices and Arrays
Slices and Arrays are covariant types which means they can contain elements which bind generics such as ADT or FnTypes. This means substitutions can be recursive and this gives the typechecker a chance to handle this recursion on these types.
1 parent a620a22 commit 894e9d2

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

gcc/rust/typecheck/rust-substitution-mapper.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,19 @@ class SubstMapperInternal : public TyTy::TyVisitor
221221
resolved = type.handle_substitions (mappings);
222222
}
223223

224+
void visit (TyTy::ArrayType &type) override
225+
{
226+
resolved = type.handle_substitions (mappings);
227+
}
228+
229+
void visit (TyTy::SliceType &type) override
230+
{
231+
resolved = type.handle_substitions (mappings);
232+
}
233+
224234
// nothing to do for these
225235
void visit (TyTy::InferType &) override { gcc_unreachable (); }
226236
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
227-
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
228-
void visit (TyTy::SliceType &) override { gcc_unreachable (); }
229237
void visit (TyTy::BoolType &) override { gcc_unreachable (); }
230238
void visit (TyTy::IntType &) override { gcc_unreachable (); }
231239
void visit (TyTy::UintType &) override { gcc_unreachable (); }

gcc/rust/typecheck/rust-tyty.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,22 @@ ArrayType::clone () const
15081508
element_type, get_combined_refs ());
15091509
}
15101510

1511+
ArrayType *
1512+
ArrayType::handle_substitions (SubstitutionArgumentMappings mappings)
1513+
{
1514+
auto mappings_table = Analysis::Mappings::get ();
1515+
1516+
ArrayType *ref = static_cast<ArrayType *> (clone ());
1517+
ref->set_ty_ref (mappings_table->get_next_hir_id ());
1518+
1519+
// might be &T or &ADT so this needs to be recursive
1520+
auto base = ref->get_element_type ();
1521+
BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
1522+
ref->element_type = TyVar (concrete->get_ty_ref ());
1523+
1524+
return ref;
1525+
}
1526+
15111527
void
15121528
SliceType::accept_vis (TyVisitor &vis)
15131529
{
@@ -1581,6 +1597,22 @@ SliceType::clone () const
15811597
get_combined_refs ());
15821598
}
15831599

1600+
SliceType *
1601+
SliceType::handle_substitions (SubstitutionArgumentMappings mappings)
1602+
{
1603+
auto mappings_table = Analysis::Mappings::get ();
1604+
1605+
SliceType *ref = static_cast<SliceType *> (clone ());
1606+
ref->set_ty_ref (mappings_table->get_next_hir_id ());
1607+
1608+
// might be &T or &ADT so this needs to be recursive
1609+
auto base = ref->get_element_type ();
1610+
BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
1611+
ref->element_type = TyVar (concrete->get_ty_ref ());
1612+
1613+
return ref;
1614+
}
1615+
15841616
void
15851617
BoolType::accept_vis (TyVisitor &vis)
15861618
{

gcc/rust/typecheck/rust-tyty.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,8 @@ class ArrayType : public BaseType
16651665

16661666
HIR::Expr &get_capacity_expr () const { return capacity_expr; }
16671667

1668+
ArrayType *handle_substitions (SubstitutionArgumentMappings mappings);
1669+
16681670
private:
16691671
TyVar element_type;
16701672
HIR::Expr &capacity_expr;
@@ -1710,6 +1712,8 @@ class SliceType : public BaseType
17101712
return get_element_type ()->is_concrete ();
17111713
}
17121714

1715+
SliceType *handle_substitions (SubstitutionArgumentMappings mappings);
1716+
17131717
private:
17141718
TyVar element_type;
17151719
};

0 commit comments

Comments
 (0)