Skip to content

Commit 3d5b465

Browse files
bors[bot]philberty
andauthored
1007: Add missing canonicalization of slices and raw pointer types r=philberty a=philberty This is part of my patch series for slices. This adds the missing visitors for name canonicalization. More information in the patch, once we get slice support in we need to start taking advantage of `@dkm's` HIR visitor refactoring to avoid these issues with missing visitors making simple bugs hard to track down. Fixes #1005 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]>
4 parents ddd087b + 31413eb + a1b0650 + 894e9d2 commit 3d5b465

10 files changed

+118
-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/resolve/rust-ast-resolve-type.cc

+39
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,45 @@ ResolveTypeToCanonicalPath::visit (AST::ReferenceType &ref)
167167
result = result.append (ident_seg);
168168
}
169169

170+
void
171+
ResolveTypeToCanonicalPath::visit (AST::RawPointerType &ref)
172+
{
173+
auto inner_type
174+
= ResolveTypeToCanonicalPath::resolve (*ref.get_type_pointed_to ().get (),
175+
include_generic_args_flag,
176+
type_resolve_generic_args_flag);
177+
178+
std::string segment_string ("*");
179+
switch (ref.get_pointer_type ())
180+
{
181+
case AST::RawPointerType::PointerType::MUT:
182+
segment_string += "mut ";
183+
break;
184+
185+
case AST::RawPointerType::PointerType::CONST:
186+
segment_string += "const ";
187+
break;
188+
}
189+
190+
segment_string += inner_type.get ();
191+
192+
auto ident_seg = CanonicalPath::new_seg (ref.get_node_id (), segment_string);
193+
result = result.append (ident_seg);
194+
}
195+
196+
void
197+
ResolveTypeToCanonicalPath::visit (AST::SliceType &slice)
198+
{
199+
auto inner_type
200+
= ResolveTypeToCanonicalPath::resolve (*slice.get_elem_type ().get (),
201+
include_generic_args_flag,
202+
type_resolve_generic_args_flag);
203+
std::string segment_string = "[" + inner_type.get () + "]";
204+
auto ident_seg
205+
= CanonicalPath::new_seg (slice.get_node_id (), segment_string);
206+
result = result.append (ident_seg);
207+
}
208+
170209
void
171210
ResolveType::visit (AST::ReferenceType &type)
172211
{

gcc/rust/resolve/rust-ast-resolve-type.h

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ class ResolveTypeToCanonicalPath : public ResolverBase
122122
}
123123
}
124124

125+
void visit (AST::SliceType &slice) override;
126+
127+
void visit (AST::RawPointerType &ptr) override;
128+
125129
void visit (AST::ReferenceType &ref) override;
126130

127131
void visit (AST::TypePathSegmentGeneric &seg) override;

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>";
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// { dg-additional-options "-w" }
2+
impl<T> *const T {
3+
fn test(self) {}
4+
}

0 commit comments

Comments
 (0)