Skip to content

Commit 6e64e66

Browse files
bors[bot]philberty
andauthored
Merge #1008 #1009
1008: Add const_ptr lang item mappings r=philberty a=philberty In order to support slices, we need to be able to parse and contain mappings for the const_ptr lang item. We do not need to do any special handling of this lang item yet but this adds the mappings so when we hit it we do not output an unknown lang item error. Addresses #849 1009: Add missing type resolution to slices and arrays r=philberty a=philberty This adds in the missing type resolution for slices and generic slices and arrays. Since Arrays and Slices are both covariant types just like references and pointers for example they need to handle recursive substitutions where their element type might be a generic type that can bind substitution parameters such as functions and ADT's. Addresses #849 Co-authored-by: Philip Herron <[email protected]>
3 parents dbe59a3 + a1b0650 + 894e9d2 commit 6e64e66

7 files changed

+71
-4
lines changed

gcc/rust/hir/tree/rust-hir-type.h

+2
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,8 @@ class SliceType : public TypeNoBounds
668668
void accept_vis (HIRFullVisitor &vis) override;
669669
void accept_vis (HIRTypeVisitor &vis) override;
670670

671+
std::unique_ptr<Type> &get_element_type () { return elem_type; }
672+
671673
protected:
672674
/* Use covariance to implement clone function as returning this object rather
673675
* than base */

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

+10
Original file line numberDiff line numberDiff line change
@@ -600,5 +600,15 @@ TypeCheckType::visit (HIR::ArrayType &type)
600600
TyTy::TyVar (base->get_ref ()));
601601
}
602602

603+
void
604+
TypeCheckType::visit (HIR::SliceType &type)
605+
{
606+
TyTy::BaseType *base
607+
= TypeCheckType::Resolve (type.get_element_type ().get ());
608+
translated
609+
= new TyTy::SliceType (type.get_mappings ().get_hirid (), type.get_locus (),
610+
TyTy::TyVar (base->get_ref ()));
611+
}
612+
603613
} // namespace Resolver
604614
} // namespace Rust

gcc/rust/typecheck/rust-hir-type-check-type.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class TypeCheckType : public TypeCheckBase
120120

121121
void visit (HIR::ArrayType &type) override;
122122

123+
void visit (HIR::SliceType &type) override;
124+
123125
void visit (HIR::ReferenceType &type) override
124126
{
125127
TyTy::BaseType *base
@@ -347,8 +349,8 @@ class ResolveWhereClauseItem : public TypeCheckBase
347349
binding->inherit_bounds (specified_bounds);
348350

349351
// When we apply these bounds we must lookup which type this binding
350-
// resolves to, as this is the type which will be used during resolution of
351-
// the block.
352+
// resolves to, as this is the type which will be used during resolution
353+
// of the block.
352354
NodeId ast_node_id = binding_type_path->get_mappings ().get_nodeid ();
353355

354356
// then lookup the reference_node_id

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

+10-2
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

+32
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

+4
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
};

gcc/rust/util/rust-lang-item.h

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class RustLangItem
6868
RANGE_INCLUSIVE,
6969
RANGE_TO_INCLUSIVE,
7070

71+
// https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/const_ptr.rs
72+
CONST_PTR,
73+
7174
UNKNOWN,
7275
};
7376

@@ -201,6 +204,10 @@ class RustLangItem
201204
{
202205
return ItemType::RANGE_TO_INCLUSIVE;
203206
}
207+
else if (item.compare ("const_ptr") == 0)
208+
{
209+
return ItemType::CONST_PTR;
210+
}
204211

205212
return ItemType::UNKNOWN;
206213
}
@@ -273,6 +280,8 @@ class RustLangItem
273280
return "RangeInclusive";
274281
case RANGE_TO_INCLUSIVE:
275282
return "RangeToInclusive";
283+
case CONST_PTR:
284+
return "const_ptr";
276285

277286
case UNKNOWN:
278287
return "<UNKNOWN>";

0 commit comments

Comments
 (0)