Skip to content

Commit

Permalink
Merge #1008 #1009
Browse files Browse the repository at this point in the history
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 <philip.herron@embecosm.com>
  • Loading branch information
bors[bot] and philberty authored Mar 11, 2022
3 parents dbe59a3 + a1b0650 + 894e9d2 commit 6e64e66
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions gcc/rust/hir/tree/rust-hir-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ class SliceType : public TypeNoBounds
void accept_vis (HIRFullVisitor &vis) override;
void accept_vis (HIRTypeVisitor &vis) override;

std::unique_ptr<Type> &get_element_type () { return elem_type; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,5 +600,15 @@ TypeCheckType::visit (HIR::ArrayType &type)
TyTy::TyVar (base->get_ref ()));
}

void
TypeCheckType::visit (HIR::SliceType &type)
{
TyTy::BaseType *base
= TypeCheckType::Resolve (type.get_element_type ().get ());
translated
= new TyTy::SliceType (type.get_mappings ().get_hirid (), type.get_locus (),
TyTy::TyVar (base->get_ref ()));
}

} // namespace Resolver
} // namespace Rust
6 changes: 4 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class TypeCheckType : public TypeCheckBase

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

void visit (HIR::SliceType &type) override;

void visit (HIR::ReferenceType &type) override
{
TyTy::BaseType *base
Expand Down Expand Up @@ -347,8 +349,8 @@ class ResolveWhereClauseItem : public TypeCheckBase
binding->inherit_bounds (specified_bounds);

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

// then lookup the reference_node_id
Expand Down
12 changes: 10 additions & 2 deletions gcc/rust/typecheck/rust-substitution-mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,19 @@ class SubstMapperInternal : public TyTy::TyVisitor
resolved = type.handle_substitions (mappings);
}

void visit (TyTy::ArrayType &type) override
{
resolved = type.handle_substitions (mappings);
}

void visit (TyTy::SliceType &type) override
{
resolved = type.handle_substitions (mappings);
}

// nothing to do for these
void visit (TyTy::InferType &) override { gcc_unreachable (); }
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
void visit (TyTy::SliceType &) override { gcc_unreachable (); }
void visit (TyTy::BoolType &) override { gcc_unreachable (); }
void visit (TyTy::IntType &) override { gcc_unreachable (); }
void visit (TyTy::UintType &) override { gcc_unreachable (); }
Expand Down
32 changes: 32 additions & 0 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,22 @@ ArrayType::clone () const
element_type, get_combined_refs ());
}

ArrayType *
ArrayType::handle_substitions (SubstitutionArgumentMappings mappings)
{
auto mappings_table = Analysis::Mappings::get ();

ArrayType *ref = static_cast<ArrayType *> (clone ());
ref->set_ty_ref (mappings_table->get_next_hir_id ());

// might be &T or &ADT so this needs to be recursive
auto base = ref->get_element_type ();
BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
ref->element_type = TyVar (concrete->get_ty_ref ());

return ref;
}

void
SliceType::accept_vis (TyVisitor &vis)
{
Expand Down Expand Up @@ -1581,6 +1597,22 @@ SliceType::clone () const
get_combined_refs ());
}

SliceType *
SliceType::handle_substitions (SubstitutionArgumentMappings mappings)
{
auto mappings_table = Analysis::Mappings::get ();

SliceType *ref = static_cast<SliceType *> (clone ());
ref->set_ty_ref (mappings_table->get_next_hir_id ());

// might be &T or &ADT so this needs to be recursive
auto base = ref->get_element_type ();
BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
ref->element_type = TyVar (concrete->get_ty_ref ());

return ref;
}

void
BoolType::accept_vis (TyVisitor &vis)
{
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,8 @@ class ArrayType : public BaseType

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

ArrayType *handle_substitions (SubstitutionArgumentMappings mappings);

private:
TyVar element_type;
HIR::Expr &capacity_expr;
Expand Down Expand Up @@ -1710,6 +1712,8 @@ class SliceType : public BaseType
return get_element_type ()->is_concrete ();
}

SliceType *handle_substitions (SubstitutionArgumentMappings mappings);

private:
TyVar element_type;
};
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/util/rust-lang-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class RustLangItem
RANGE_INCLUSIVE,
RANGE_TO_INCLUSIVE,

// https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/const_ptr.rs
CONST_PTR,

UNKNOWN,
};

Expand Down Expand Up @@ -201,6 +204,10 @@ class RustLangItem
{
return ItemType::RANGE_TO_INCLUSIVE;
}
else if (item.compare ("const_ptr") == 0)
{
return ItemType::CONST_PTR;
}

return ItemType::UNKNOWN;
}
Expand Down Expand Up @@ -273,6 +280,8 @@ class RustLangItem
return "RangeInclusive";
case RANGE_TO_INCLUSIVE:
return "RangeToInclusive";
case CONST_PTR:
return "const_ptr";

case UNKNOWN:
return "<UNKNOWN>";
Expand Down

0 comments on commit 6e64e66

Please sign in to comment.