Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice support #1086

Merged
merged 4 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,

auto root = receiver->get_root ();
std::vector<Resolver::PathProbeCandidate> candidates
= Resolver::PathProbeType::Probe (root, segment, true, false, true);

= Resolver::PathProbeType::Probe (root, segment, true /* probe_impls */,
false /* probe_bounds */,
true /* ignore_mandatory_trait_items */);
if (candidates.size () == 0)
{
// this means we are defaulting back to the trait_item if
Expand Down Expand Up @@ -776,12 +777,22 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
rust_assert (candidates.size () == 1);
auto &candidate = candidates.at (0);
rust_assert (candidate.is_impl_candidate ());
rust_assert (candidate.ty->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *candidate_call = static_cast<TyTy::FnType *> (candidate.ty);

HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
if (!fntype->has_subsititions_defined ())
if (!candidate_call->has_subsititions_defined ())
return CompileInherentImplItem::Compile (impl_item, ctx);

return CompileInherentImplItem::Compile (impl_item, ctx, fntype);
TyTy::BaseType *monomorphized = candidate_call;
if (candidate_call->needs_generic_substitutions ())
{
TyTy::BaseType *infer_impl_call
= candidate_call->infer_substitions (expr_locus);
monomorphized = infer_impl_call->unify (fntype);
}

return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized);
}
}

Expand Down
18 changes: 1 addition & 17 deletions gcc/rust/backend/rust-compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ CompileCrate::~CompileCrate () {}

void
CompileCrate::Compile (HIR::Crate &crate, Context *ctx)

{
CompileCrate c (crate, ctx);
c.go ();
Expand Down Expand Up @@ -383,26 +382,11 @@ HIRCompileBase::compute_address_for_trait_item (
= self_bound->lookup_associated_item (ref->get_identifier ());
rust_assert (!associated_self_item.is_error ());

// apply any generic arguments from this predicate
TyTy::BaseType *mono1 = associated_self_item.get_tyty_for_receiver (self);
TyTy::BaseType *mono2 = nullptr;
if (predicate->has_generic_args ())
{
mono2 = associated_self_item.get_tyty_for_receiver (
self, predicate->get_generic_args ());
}
else
{
mono2 = associated_self_item.get_tyty_for_receiver (self);
}
rust_assert (mono1 != nullptr);
rust_assert (mono1->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *assocated_item_ty1 = static_cast<TyTy::FnType *> (mono1);

rust_assert (mono2 != nullptr);
rust_assert (mono2->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *assocated_item_ty2 = static_cast<TyTy::FnType *> (mono2);

// Lookup the impl-block for the associated impl_item if it exists
HIR::Function *associated_function = nullptr;
for (auto &impl_item : associated_impl_block->get_impl_items ())
Expand Down Expand Up @@ -434,7 +418,7 @@ HIRCompileBase::compute_address_for_trait_item (
{
TyTy::SubstitutionArgumentMappings mappings
= assocated_item_ty1->solve_missing_mappings_from_this (
*assocated_item_ty2, *lookup_fntype);
*trait_item_fntype, *lookup_fntype);
lookup_fntype = lookup_fntype->handle_substitions (mappings);
}

Expand Down
64 changes: 33 additions & 31 deletions gcc/rust/typecheck/rust-hir-dot-operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ MethodResolver::Try (const TyTy::BaseType *r,
PathProbeCandidate c = PathProbeCandidate::get_error ();
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds
= r->get_specified_bounds ();
const std::vector<MethodResolver::predicate_candidate> predicate_items
= get_predicate_items (segment_name, *r, specified_bounds);

// 1. try raw
MethodResolver raw (*r, segment_name, specified_bounds);
MethodResolver raw (*r, segment_name, predicate_items);
c = raw.select ();
if (!c.is_error ())
{
Expand All @@ -139,7 +141,7 @@ MethodResolver::Try (const TyTy::BaseType *r,
TyTy::ReferenceType *r1
= new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
Mutability::Imm);
MethodResolver imm_ref (*r1, segment_name, specified_bounds);
MethodResolver imm_ref (*r1, segment_name, predicate_items);
c = imm_ref.select ();
if (!c.is_error ())
{
Expand All @@ -152,7 +154,7 @@ MethodResolver::Try (const TyTy::BaseType *r,
TyTy::ReferenceType *r2
= new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
Mutability::Mut);
MethodResolver mut_ref (*r2, segment_name, specified_bounds);
MethodResolver mut_ref (*r2, segment_name, predicate_items);
c = mut_ref.select ();
if (!c.is_error ())
{
Expand Down Expand Up @@ -288,27 +290,6 @@ MethodResolver::select ()
TyTy::FnType *fntype;
};

std::vector<precdicate_candidate> predicate_items;
for (auto &bound : specified_bounds)
{
TyTy::TypeBoundPredicateItem lookup
= bound.lookup_associated_item (segment_name.as_string ());
if (lookup.is_error ())
continue;

bool is_fn = lookup.get_raw_item ()->get_trait_item_type ()
== TraitItemReference::TraitItemType::FN;
if (!is_fn)
continue;

TyTy::BaseType *ty = lookup.get_raw_item ()->get_tyty ();
rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);

precdicate_candidate candidate{lookup, fnty};
predicate_items.push_back (candidate);
}

for (auto impl_item : inherent_impl_fns)
{
TyTy::FnType *fn = impl_item.ty;
Expand Down Expand Up @@ -342,9 +323,9 @@ MethodResolver::select ()
}
}

for (auto predicate : predicate_items)
for (const auto &predicate : predicate_items)
{
TyTy::FnType *fn = predicate.fntype;
const TyTy::FnType *fn = predicate.fntype;
rust_assert (fn->is_method ());

TyTy::BaseType *fn_self = fn->get_self_type ();
Expand All @@ -355,20 +336,41 @@ MethodResolver::select ()
const TraitItemReference *trait_item
= predicate.lookup.get_raw_item ();

TyTy::BaseType *subst = predicate.lookup.get_tyty_for_receiver (
receiver.get_root (),
predicate.lookup.get_parent ()->get_generic_args ());

PathProbeCandidate::TraitItemCandidate c{trait_ref, trait_item,
nullptr};
return PathProbeCandidate (
PathProbeCandidate::CandidateType::TRAIT_FUNC, subst,
PathProbeCandidate::CandidateType::TRAIT_FUNC, fn->clone (),
trait_item->get_locus (), c);
}
}

return PathProbeCandidate::get_error ();
}

std::vector<MethodResolver::predicate_candidate>
MethodResolver::get_predicate_items (
const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
{
std::vector<predicate_candidate> predicate_items;
for (auto &bound : specified_bounds)
{
TyTy::TypeBoundPredicateItem lookup
= bound.lookup_associated_item (segment_name.as_string ());
if (lookup.is_error ())
continue;

TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
if (ty->get_kind () == TyTy::TypeKind::FNDEF)
{
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
predicate_candidate candidate{lookup, fnty};
predicate_items.push_back (candidate);
}
}

return predicate_items;
}

} // namespace Resolver
} // namespace Rust
20 changes: 15 additions & 5 deletions gcc/rust/typecheck/rust-hir-dot-operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,32 @@ class MethodResolver : public TypeCheckBase
bool autoderef_flag = false);

protected:
struct predicate_candidate
{
TyTy::TypeBoundPredicateItem lookup;
TyTy::FnType *fntype;
};

static MethodCandidate Try (const TyTy::BaseType *r,
const HIR::PathIdentSegment &segment_name,
std::vector<Adjustment> &adjustments);

static std::vector<predicate_candidate> get_predicate_items (
const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);

PathProbeCandidate select ();

MethodResolver (const TyTy::BaseType &receiver,
const HIR::PathIdentSegment &segment_name,
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
MethodResolver (
const TyTy::BaseType &receiver, const HIR::PathIdentSegment &segment_name,
const std::vector<MethodResolver::predicate_candidate> &predicate_items)
: receiver (receiver), segment_name (segment_name),
specified_bounds (specified_bounds)
predicate_items (predicate_items)
{}

const TyTy::BaseType &receiver;
const HIR::PathIdentSegment &segment_name;
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds;
const std::vector<MethodResolver::predicate_candidate> &predicate_items;
};

} // namespace Resolver
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/typecheck/rust-hir-trait-ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ class TraitReference
return hir_trait_ref->get_mappings ();
}

DefId get_defid () const { return get_mappings ().get_defid (); }

bool lookup_hir_trait_item (const HIR::TraitItem &item,
TraitItemReference **ref)
{
Expand Down Expand Up @@ -436,6 +438,9 @@ class AssociatedImplTrait

void setup_associated_types ();

void setup_associated_types2 (const TyTy::BaseType *self,
philberty marked this conversation as resolved.
Show resolved Hide resolved
const TyTy::TypeBoundPredicate &bound);

void reset_associated_types ();

TyTy::BaseType *get_projected_type (const TraitItemReference *trait_item_ref,
Expand Down
Loading