@@ -126,9 +126,11 @@ MethodResolver::Try (const TyTy::BaseType *r,
126
126
PathProbeCandidate c = PathProbeCandidate::get_error ();
127
127
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds
128
128
= r->get_specified_bounds ();
129
+ const std::vector<MethodResolver::predicate_candidate> predicate_items
130
+ = get_predicate_items (segment_name, *r, specified_bounds);
129
131
130
132
// 1. try raw
131
- MethodResolver raw (*r, segment_name, specified_bounds );
133
+ MethodResolver raw (*r, segment_name, predicate_items );
132
134
c = raw.select ();
133
135
if (!c.is_error ())
134
136
{
@@ -139,7 +141,7 @@ MethodResolver::Try (const TyTy::BaseType *r,
139
141
TyTy::ReferenceType *r1
140
142
= new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
141
143
Mutability::Imm);
142
- MethodResolver imm_ref (*r1, segment_name, specified_bounds );
144
+ MethodResolver imm_ref (*r1, segment_name, predicate_items );
143
145
c = imm_ref.select ();
144
146
if (!c.is_error ())
145
147
{
@@ -152,7 +154,7 @@ MethodResolver::Try (const TyTy::BaseType *r,
152
154
TyTy::ReferenceType *r2
153
155
= new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
154
156
Mutability::Mut);
155
- MethodResolver mut_ref (*r2, segment_name, specified_bounds );
157
+ MethodResolver mut_ref (*r2, segment_name, predicate_items );
156
158
c = mut_ref.select ();
157
159
if (!c.is_error ())
158
160
{
@@ -288,27 +290,6 @@ MethodResolver::select ()
288
290
TyTy::FnType *fntype;
289
291
};
290
292
291
- std::vector<precdicate_candidate> predicate_items;
292
- for (auto &bound : specified_bounds)
293
- {
294
- TyTy::TypeBoundPredicateItem lookup
295
- = bound.lookup_associated_item (segment_name.as_string ());
296
- if (lookup.is_error ())
297
- continue ;
298
-
299
- bool is_fn = lookup.get_raw_item ()->get_trait_item_type ()
300
- == TraitItemReference::TraitItemType::FN;
301
- if (!is_fn)
302
- continue ;
303
-
304
- TyTy::BaseType *ty = lookup.get_raw_item ()->get_tyty ();
305
- rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
306
- TyTy::FnType *fnty = static_cast <TyTy::FnType *> (ty);
307
-
308
- precdicate_candidate candidate{lookup, fnty};
309
- predicate_items.push_back (candidate);
310
- }
311
-
312
293
for (auto impl_item : inherent_impl_fns)
313
294
{
314
295
TyTy::FnType *fn = impl_item.ty ;
@@ -342,9 +323,9 @@ MethodResolver::select ()
342
323
}
343
324
}
344
325
345
- for (auto predicate : predicate_items)
326
+ for (const auto & predicate : predicate_items)
346
327
{
347
- TyTy::FnType *fn = predicate.fntype ;
328
+ const TyTy::FnType *fn = predicate.fntype ;
348
329
rust_assert (fn->is_method ());
349
330
350
331
TyTy::BaseType *fn_self = fn->get_self_type ();
@@ -355,20 +336,41 @@ MethodResolver::select ()
355
336
const TraitItemReference *trait_item
356
337
= predicate.lookup .get_raw_item ();
357
338
358
- TyTy::BaseType *subst = predicate.lookup .get_tyty_for_receiver (
359
- receiver.get_root (),
360
- predicate.lookup .get_parent ()->get_generic_args ());
361
-
362
339
PathProbeCandidate::TraitItemCandidate c{trait_ref, trait_item,
363
340
nullptr };
364
341
return PathProbeCandidate (
365
- PathProbeCandidate::CandidateType::TRAIT_FUNC, subst ,
342
+ PathProbeCandidate::CandidateType::TRAIT_FUNC, fn-> clone () ,
366
343
trait_item->get_locus (), c);
367
344
}
368
345
}
369
346
370
347
return PathProbeCandidate::get_error ();
371
348
}
372
349
350
+ std::vector<MethodResolver::predicate_candidate>
351
+ MethodResolver::get_predicate_items (
352
+ const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
353
+ const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
354
+ {
355
+ std::vector<predicate_candidate> predicate_items;
356
+ for (auto &bound : specified_bounds)
357
+ {
358
+ TyTy::TypeBoundPredicateItem lookup
359
+ = bound.lookup_associated_item (segment_name.as_string ());
360
+ if (lookup.is_error ())
361
+ continue ;
362
+
363
+ TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
364
+ if (ty->get_kind () == TyTy::TypeKind::FNDEF)
365
+ {
366
+ TyTy::FnType *fnty = static_cast <TyTy::FnType *> (ty);
367
+ predicate_candidate candidate{lookup, fnty};
368
+ predicate_items.push_back (candidate);
369
+ }
370
+ }
371
+
372
+ return predicate_items;
373
+ }
374
+
373
375
} // namespace Resolver
374
376
} // namespace Rust
0 commit comments