Skip to content

Commit 89e02f5

Browse files
committed
Update path probe with optional filter for specific associated trait
Sometimes when we probe for associated items we need to limit it to items which are directly associated with a trait. This adds an optional parameter to achieve this.
1 parent 795d70d commit 89e02f5

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

gcc/rust/typecheck/rust-hir-path-probe.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ class PathProbeType : public TypeCheckBase
119119
static std::vector<PathProbeCandidate>
120120
Probe (const TyTy::BaseType *receiver,
121121
const HIR::PathIdentSegment &segment_name, bool probe_impls,
122-
bool probe_bounds, bool ignore_mandatory_trait_items)
122+
bool probe_bounds, bool ignore_mandatory_trait_items,
123+
DefId specific_trait_id = UNKNOWN_DEFID)
123124
{
124-
PathProbeType probe (receiver, segment_name);
125+
PathProbeType probe (receiver, segment_name, specific_trait_id);
125126
if (probe_impls)
126127
{
127128
if (receiver->get_kind () == TyTy::TypeKind::ADT)
@@ -145,6 +146,13 @@ class PathProbeType : public TypeCheckBase
145146
for (auto &candidate : probed_bounds)
146147
{
147148
const TraitReference *trait_ref = candidate.first;
149+
if (specific_trait_id != UNKNOWN_DEFID)
150+
{
151+
if (trait_ref->get_mappings ().get_defid ()
152+
!= specific_trait_id)
153+
continue;
154+
}
155+
148156
HIR::ImplBlock *impl = candidate.second;
149157
probe.process_associated_trait_for_candidates (
150158
trait_ref, impl, ignore_mandatory_trait_items);
@@ -154,6 +162,13 @@ class PathProbeType : public TypeCheckBase
154162
for (const TyTy::TypeBoundPredicate &predicate :
155163
receiver->get_specified_bounds ())
156164
{
165+
const TraitReference *trait_ref = predicate.get ();
166+
if (specific_trait_id != UNKNOWN_DEFID)
167+
{
168+
if (trait_ref->get_mappings ().get_defid () != specific_trait_id)
169+
continue;
170+
}
171+
157172
probe.process_predicate_for_candidates (predicate,
158173
ignore_mandatory_trait_items);
159174
}
@@ -221,6 +236,9 @@ class PathProbeType : public TypeCheckBase
221236
protected:
222237
void process_enum_item_for_candiates (const TyTy::ADTType *adt)
223238
{
239+
if (specific_trait_id != UNKNOWN_DEFID)
240+
return;
241+
224242
TyTy::VariantDef *v;
225243
if (!adt->lookup_variant (search.as_string (), &v))
226244
return;
@@ -385,9 +403,9 @@ class PathProbeType : public TypeCheckBase
385403

386404
protected:
387405
PathProbeType (const TyTy::BaseType *receiver,
388-
const HIR::PathIdentSegment &query)
406+
const HIR::PathIdentSegment &query, DefId specific_trait_id)
389407
: TypeCheckBase (), receiver (receiver), search (query),
390-
current_impl (nullptr)
408+
current_impl (nullptr), specific_trait_id (specific_trait_id)
391409
{}
392410

393411
std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>>
@@ -427,6 +445,7 @@ class PathProbeType : public TypeCheckBase
427445
const HIR::PathIdentSegment &search;
428446
std::vector<PathProbeCandidate> candidates;
429447
HIR::ImplBlock *current_impl;
448+
DefId specific_trait_id;
430449
};
431450

432451
class ReportMultipleCandidateError : private TypeCheckBase
@@ -507,7 +526,8 @@ class PathProbeImplTrait : public PathProbeType
507526
PathProbeImplTrait (const TyTy::BaseType *receiver,
508527
const HIR::PathIdentSegment &query,
509528
const TraitReference *trait_reference)
510-
: PathProbeType (receiver, query), trait_reference (trait_reference)
529+
: PathProbeType (receiver, query, UNKNOWN_DEFID),
530+
trait_reference (trait_reference)
511531
{}
512532

513533
const TraitReference *trait_reference;

0 commit comments

Comments
 (0)