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>
This commit is contained in:
bors[bot] 2022-03-11 10:39:24 +00:00 committed by GitHub
commit 6e64e6636e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 4 deletions

View File

@ -668,6 +668,8 @@ public:
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 */

View File

@ -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

View File

@ -120,6 +120,8 @@ public:
void visit (HIR::ArrayType &type) override;
void visit (HIR::SliceType &type) override;
void visit (HIR::ReferenceType &type) override
{
TyTy::BaseType *base
@ -347,8 +349,8 @@ public:
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

View File

@ -221,11 +221,19 @@ public:
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 (); }

View File

@ -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)
{
@ -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)
{

View File

@ -1665,6 +1665,8 @@ public:
HIR::Expr &get_capacity_expr () const { return capacity_expr; }
ArrayType *handle_substitions (SubstitutionArgumentMappings mappings);
private:
TyVar element_type;
HIR::Expr &capacity_expr;
@ -1710,6 +1712,8 @@ public:
return get_element_type ()->is_concrete ();
}
SliceType *handle_substitions (SubstitutionArgumentMappings mappings);
private:
TyVar element_type;
};

View File

@ -68,6 +68,9 @@ public:
RANGE_INCLUSIVE,
RANGE_TO_INCLUSIVE,
// https://github.com/rust-lang/rust/blob/master/library/core/src/ptr/const_ptr.rs
CONST_PTR,
UNKNOWN,
};
@ -201,6 +204,10 @@ public:
{
return ItemType::RANGE_TO_INCLUSIVE;
}
else if (item.compare ("const_ptr") == 0)
{
return ItemType::CONST_PTR;
}
return ItemType::UNKNOWN;
}
@ -273,6 +280,8 @@ public:
return "RangeInclusive";
case RANGE_TO_INCLUSIVE:
return "RangeToInclusive";
case CONST_PTR:
return "const_ptr";
case UNKNOWN:
return "<UNKNOWN>";