Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-hir-dot-operator.h"
20 : #include "rust-hir-path-probe.h"
21 : #include "rust-hir-trait-resolve.h"
22 : #include "rust-hir-type-check-item.h"
23 : #include "rust-type-util.h"
24 : #include "rust-coercion.h"
25 :
26 : namespace Rust {
27 : namespace Resolver {
28 :
29 12114 : MethodResolver::MethodResolver (bool autoderef_flag,
30 : const HIR::PathIdentSegment &segment_name,
31 : const HIR::Trait *specified_trait)
32 12114 : : AutoderefCycle (autoderef_flag), segment_name (segment_name),
33 12114 : specified_trait (specified_trait), result ()
34 12114 : {}
35 :
36 : std::set<MethodCandidate>
37 12114 : MethodResolver::Probe (TyTy::BaseType *receiver,
38 : const HIR::PathIdentSegment &segment_name,
39 : bool autoderef_flag, const HIR::Trait *specified_trait)
40 : {
41 12114 : MethodResolver resolver (autoderef_flag, segment_name, specified_trait);
42 12114 : resolver.cycle (receiver);
43 12023 : return resolver.result;
44 12023 : }
45 :
46 : std::set<MethodCandidate>
47 8502 : MethodResolver::Select (std::set<MethodCandidate> &candidates,
48 : TyTy::BaseType *receiver,
49 : std::vector<TyTy::BaseType *> arguments,
50 : TyTy::BaseType *result_type)
51 : {
52 8502 : std::set<MethodCandidate> selected;
53 401343 : for (auto &candidate : candidates)
54 : {
55 392841 : TyTy::BaseType *candidate_type = candidate.candidate.ty;
56 392841 : rust_assert (candidate_type->get_kind () == TyTy::TypeKind::FNDEF);
57 392841 : if (candidate_type == nullptr
58 392841 : || candidate_type->get_kind () != TyTy::TypeKind::FNDEF)
59 0 : continue;
60 392841 : TyTy::FnType &fn = *static_cast<TyTy::FnType *> (candidate_type);
61 :
62 : // match the number of arguments
63 392841 : if (fn.num_params () != (arguments.size () + 1))
64 0 : continue;
65 :
66 : // match the arguments
67 489130 : bool failed = false;
68 489130 : for (size_t i = 0; i < arguments.size (); i++)
69 : {
70 391562 : TyTy::BaseType *arg = arguments.at (i);
71 391562 : TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type ();
72 391562 : TyTy::BaseType *coerced
73 391562 : = try_coercion (0, TyTy::TyWithLocation (param),
74 391562 : TyTy::TyWithLocation (arg), UNDEF_LOCATION);
75 391562 : if (coerced->get_kind () == TyTy::TypeKind::ERROR)
76 : {
77 : failed = true;
78 : break;
79 : }
80 : }
81 :
82 392841 : if (!failed && result_type != nullptr)
83 : {
84 96 : TyTy::BaseType *return_type = fn.get_return_type ();
85 96 : rust_debug ("method candidate output check fn=%s expected=%s "
86 : "return=%s",
87 : fn.debug_str ().c_str (),
88 : result_type->debug_str ().c_str (),
89 : return_type->debug_str ().c_str ());
90 96 : failed = !types_compatable (TyTy::TyWithLocation (result_type),
91 96 : TyTy::TyWithLocation (return_type),
92 : UNDEF_LOCATION, false /* emit_errors */);
93 96 : rust_debug ("method candidate output check result=%s",
94 : failed ? "rejected" : "accepted");
95 : }
96 :
97 392841 : if (!failed)
98 97487 : selected.insert (candidate);
99 : }
100 :
101 8502 : return selected;
102 : }
103 :
104 : void
105 12356 : MethodResolver::try_hook (const TyTy::BaseType &r)
106 : {
107 12356 : rust_debug ("MethodResolver::try_hook get_predicate_items: [%s]",
108 : r.debug_str ().c_str ());
109 12356 : predicate_bounds = context->predicates_for_type (&r);
110 12356 : predicate_items = get_predicate_items (segment_name, r, predicate_bounds);
111 :
112 12356 : if (predicate_items.size () > 0)
113 : return;
114 :
115 12058 : if (r.get_kind () == TyTy::TypeKind::REF)
116 : {
117 3118 : const auto &ref = static_cast<const TyTy::ReferenceType &> (r);
118 3118 : const auto &element = ref.get_var_element_type ();
119 3118 : const auto &element_ty = *element.get_tyty ();
120 3118 : predicate_bounds = context->predicates_for_type (&element_ty);
121 3118 : predicate_items
122 3118 : = get_predicate_items (segment_name, element_ty, predicate_bounds);
123 : }
124 8940 : else if (auto inner
125 8940 : = TyTy::try_get_box_inner_type (const_cast<TyTy::BaseType *> (&r)))
126 : {
127 4 : predicate_bounds = context->predicates_for_type (*inner);
128 4 : predicate_items
129 4 : = get_predicate_items (segment_name, **inner, predicate_bounds);
130 : }
131 : }
132 :
133 : std::vector<MethodResolver::impl_item_candidate>
134 3538 : MethodResolver::assemble_inherent_impl_candidates (
135 : const TyTy::BaseType &receiver)
136 : {
137 3538 : std::vector<impl_item_candidate> inherent_impl_fns;
138 3538 : const TyTy::BaseType *raw = receiver.destructure ();
139 3538 : bool receiver_is_raw_ptr = raw->get_kind () == TyTy::TypeKind::POINTER;
140 3538 : bool receiver_is_ref = raw->get_kind () == TyTy::TypeKind::REF;
141 :
142 : // Assemble inherent impl items (non-trait impl blocks).
143 3482 : mappings.iterate_inherent_impl_items (
144 3538 : segment_name.to_string (),
145 3538 : [&] (HIR::ImplItem *item, HIR::ImplBlock *impl) mutable -> bool {
146 10342 : HIR::Function *func = static_cast<HIR::Function *> (item);
147 :
148 10342 : TyTy::BaseType *impl_self = TypeCheckItem::ResolveImplBlockSelf (*impl);
149 :
150 10342 : if (impl_self == nullptr
151 10342 : || impl_self->get_kind () == TyTy::TypeKind::ERROR)
152 : return true;
153 :
154 10342 : if (receiver.get_kind () == TyTy::TypeKind::ADT
155 10342 : && impl_self->get_kind () == TyTy::TypeKind::ADT)
156 : {
157 343 : const auto &receiver_adt
158 : = static_cast<const TyTy::ADTType &> (receiver);
159 343 : const auto &impl_adt
160 : = static_cast<const TyTy::ADTType &> (*impl_self);
161 656 : if (receiver_adt.get_id () != impl_adt.get_id ())
162 30 : return true;
163 : }
164 :
165 : // see:
166 : // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280
167 : // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660
168 10312 : bool impl_self_is_ptr = impl_self->get_kind () == TyTy::TypeKind::POINTER;
169 10312 : bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF;
170 10312 : if (receiver_is_raw_ptr && impl_self_is_ptr)
171 : {
172 601 : const TyTy::PointerType &sptr
173 : = *static_cast<const TyTy::PointerType *> (impl_self);
174 601 : const TyTy::PointerType &ptr
175 601 : = *static_cast<const TyTy::PointerType *> (raw);
176 :
177 : // we could do this via lang-item assemblies if we refactor this
178 601 : bool mut_match = sptr.mutability () == ptr.mutability ();
179 601 : if (!mut_match)
180 : return true;
181 : }
182 9711 : else if (receiver_is_ref && impl_self_is_ref)
183 : {
184 0 : const TyTy::ReferenceType &sptr
185 : = *static_cast<const TyTy::ReferenceType *> (impl_self);
186 0 : const TyTy::ReferenceType &ptr
187 0 : = *static_cast<const TyTy::ReferenceType *> (raw);
188 :
189 : // we could do this via lang-item assemblies if we refactor this
190 0 : bool mut_match = sptr.mutability () == ptr.mutability ();
191 0 : if (!mut_match)
192 : return true;
193 : }
194 :
195 10157 : TyTy::BaseType *ty = nullptr;
196 10157 : if (!query_type (func->get_mappings ().get_hirid (), &ty))
197 : return true;
198 10101 : if (ty == nullptr || ty->get_kind () == TyTy::TypeKind::ERROR)
199 : return true;
200 10101 : if (ty->get_kind () != TyTy::TypeKind::FNDEF)
201 : return true;
202 :
203 10101 : TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
204 10101 : inherent_impl_fns.emplace_back (func, impl, fnty);
205 10101 : return true;
206 : });
207 :
208 3482 : return inherent_impl_fns;
209 : }
210 :
211 : void
212 10405 : MethodResolver::assemble_trait_impl_candidates (
213 : const TyTy::BaseType &receiver,
214 : std::vector<impl_item_candidate> &impl_candidates,
215 : std::vector<trait_item_candidate> &trait_candidates)
216 : {
217 10405 : const TyTy::BaseType *raw = receiver.destructure ();
218 10405 : bool receiver_is_raw_ptr = raw->get_kind () == TyTy::TypeKind::POINTER;
219 10405 : bool receiver_is_ref = raw->get_kind () == TyTy::TypeKind::REF;
220 :
221 682067 : auto process_impl = [&] (HirId id, HIR::ImplBlock *impl) mutable -> bool {
222 671662 : bool is_trait_impl = impl->has_trait_ref ();
223 671662 : if (!is_trait_impl)
224 : return true;
225 :
226 : // look for impl implementation else lookup the associated trait item
227 1354893 : for (auto &impl_item : impl->get_impl_items ())
228 : {
229 1335172 : bool is_fn = impl_item->get_impl_item_type ()
230 1335172 : == HIR::ImplItem::ImplItemType::FUNCTION;
231 1335172 : if (!is_fn)
232 683231 : continue;
233 :
234 676746 : HIR::Function *func = static_cast<HIR::Function *> (impl_item.get ());
235 676746 : if (!func->is_method ())
236 0 : continue;
237 :
238 676746 : bool name_matches = func->get_function_name ().as_string ().compare (
239 1353492 : segment_name.to_string ())
240 676746 : == 0;
241 676746 : if (!name_matches)
242 6598 : continue;
243 :
244 670148 : TyTy::BaseType *impl_self = TypeCheckItem::ResolveImplBlockSelf (*impl);
245 :
246 670148 : if (impl_self == nullptr
247 670148 : || impl_self->get_kind () == TyTy::TypeKind::ERROR)
248 0 : continue;
249 :
250 670148 : if (receiver.get_kind () == TyTy::TypeKind::ADT
251 670148 : && impl_self->get_kind () == TyTy::TypeKind::ADT)
252 : {
253 2487 : const auto &receiver_adt
254 : = static_cast<const TyTy::ADTType &> (receiver);
255 2487 : const auto &impl_adt
256 : = static_cast<const TyTy::ADTType &> (*impl_self);
257 4748 : if (receiver_adt.get_id () != impl_adt.get_id ())
258 226 : continue;
259 : }
260 :
261 : // see:
262 : // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280
263 : // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660
264 669922 : bool impl_self_is_ptr
265 669922 : = impl_self->get_kind () == TyTy::TypeKind::POINTER;
266 669922 : bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF;
267 669922 : if (receiver_is_raw_ptr && impl_self_is_ptr)
268 : {
269 0 : const TyTy::PointerType &sptr
270 : = *static_cast<const TyTy::PointerType *> (impl_self);
271 0 : const TyTy::PointerType &ptr
272 0 : = *static_cast<const TyTy::PointerType *> (raw);
273 :
274 : // we could do this via lang-item assemblies if we refactor this
275 0 : bool mut_match = sptr.mutability () == ptr.mutability ();
276 0 : if (!mut_match)
277 0 : continue;
278 : }
279 669922 : else if (receiver_is_ref && impl_self_is_ref)
280 : {
281 421 : const TyTy::ReferenceType &sptr
282 : = *static_cast<const TyTy::ReferenceType *> (impl_self);
283 421 : const TyTy::ReferenceType &ptr
284 421 : = *static_cast<const TyTy::ReferenceType *> (raw);
285 :
286 : // we could do this via lang-item assemblies if we refactor this
287 421 : bool mut_match = sptr.mutability () == ptr.mutability ();
288 421 : if (!mut_match)
289 184 : continue;
290 : }
291 :
292 669738 : TyTy::BaseType *ty = nullptr;
293 669738 : if (!query_type (func->get_mappings ().get_hirid (), &ty))
294 17797 : continue;
295 651906 : if (ty == nullptr || ty->get_kind () == TyTy::TypeKind::ERROR)
296 0 : continue;
297 651906 : if (ty->get_kind () != TyTy::TypeKind::FNDEF)
298 0 : continue;
299 :
300 651906 : TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
301 :
302 651906 : impl_candidates.emplace_back (func, impl, fnty);
303 651906 : return true;
304 : }
305 :
306 19721 : TraitReference *trait_ref = TraitResolver::Resolve (impl->get_trait_ref ());
307 19721 : rust_assert (!trait_ref->is_error ());
308 :
309 19721 : auto item_ref
310 39442 : = trait_ref->lookup_trait_item (segment_name.to_string (),
311 19721 : TraitItemReference::TraitItemType::FN);
312 19721 : if (item_ref->is_error ())
313 : return true;
314 :
315 19385 : const HIR::Trait *trait = trait_ref->get_hir_trait_ref ();
316 19385 : HIR::TraitItem *item = item_ref->get_hir_trait_item ();
317 19385 : if (item->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
318 : return true;
319 :
320 19385 : HIR::TraitItemFunc *func = static_cast<HIR::TraitItemFunc *> (item);
321 19385 : if (!func->get_decl ().is_method ())
322 : return true;
323 :
324 19385 : TyTy::BaseType *ty = item_ref->get_tyty ();
325 19385 : if (ty == nullptr || ty->get_kind () != TyTy::TypeKind::FNDEF)
326 : return true;
327 19385 : TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
328 :
329 19385 : trait_candidates.emplace_back (func, trait, fnty, trait_ref, item_ref);
330 :
331 19385 : return true;
332 10405 : };
333 :
334 10405 : if (specified_trait == nullptr)
335 3138 : mappings.iterate_trait_impl_blocks_for_item (segment_name.to_string (),
336 3138 : process_impl);
337 : else
338 8836 : mappings.iterate_trait_impl_blocks (
339 17637 : specified_trait->get_mappings ().get_defid (), process_impl);
340 10370 : }
341 :
342 : bool
343 13914 : MethodResolver::try_select_predicate_candidates (TyTy::BaseType &receiver)
344 : {
345 13914 : bool found_possible_candidate = false;
346 15459 : for (const auto &predicate : predicate_items)
347 : {
348 1545 : if (specified_trait != nullptr)
349 : {
350 284 : const TraitReference *parent = predicate.lookup.get_parent ()->get ();
351 284 : if (parent->get_mappings ().get_defid ()
352 568 : != specified_trait->get_mappings ().get_defid ())
353 0 : continue;
354 : }
355 :
356 1545 : const TyTy::FnType *fn = predicate.fntype;
357 3090 : if (!fn->is_method ())
358 0 : continue;
359 :
360 1545 : TyTy::BaseType *fn_self = fn->get_self_type ();
361 1545 : rust_debug ("dot-operator predicate fn_self={%s} can_eq receiver={%s}",
362 : fn_self->debug_str ().c_str (),
363 : receiver.debug_str ().c_str ());
364 :
365 1545 : auto res
366 : = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
367 : false /*allow-autoderef*/,
368 : false /*is_cast_site*/,
369 1545 : false /*allow_never_coercion*/);
370 1545 : bool ok = !res.is_error ();
371 1545 : if (ok)
372 : {
373 1396 : std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
374 1396 : const TraitReference *trait_ref
375 1396 : = predicate.lookup.get_parent ()->get ();
376 1396 : const TraitItemReference *trait_item
377 1396 : = predicate.lookup.get_raw_item ();
378 :
379 1396 : PathProbeCandidate::TraitItemCandidate c{trait_ref, trait_item,
380 1396 : nullptr};
381 1396 : auto try_result = MethodCandidate{
382 : PathProbeCandidate (PathProbeCandidate::CandidateType::TRAIT_FUNC,
383 : fn->clone (), trait_item->get_locus (), c),
384 1396 : adjs};
385 1396 : result.insert (std::move (try_result));
386 1396 : found_possible_candidate = true;
387 1396 : }
388 1545 : }
389 13914 : return found_possible_candidate;
390 : }
391 :
392 : bool
393 13852 : MethodResolver::try_select_inherent_impl_candidates (
394 : TyTy::BaseType &receiver, const std::vector<impl_item_candidate> &candidates,
395 : bool trait_impl_blocks_only)
396 : {
397 13852 : bool found_possible_candidate = false;
398 675565 : for (auto &impl_item : candidates)
399 : {
400 661713 : bool is_trait_impl_block = impl_item.impl_block->has_trait_ref ();
401 661713 : if (trait_impl_blocks_only && !is_trait_impl_block)
402 0 : continue;
403 661713 : if (!trait_impl_blocks_only && is_trait_impl_block)
404 0 : continue;
405 :
406 661713 : TyTy::FnType *fn = impl_item.ty;
407 1323426 : if (!fn->is_method ())
408 0 : continue;
409 :
410 661713 : TyTy::BaseType *fn_self = fn->get_self_type ();
411 :
412 9884 : const char *debug_prefix
413 661713 : = trait_impl_blocks_only ? "trait_impl_item" : "impl_item";
414 661713 : rust_debug ("dot-operator %s fn_self={%s} can_eq receiver={%s}",
415 : debug_prefix, fn_self->debug_str ().c_str (),
416 : receiver.debug_str ().c_str ());
417 :
418 661713 : auto res
419 : = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
420 : false /*allow-autoderef*/,
421 : false /*is_cast_site*/,
422 661713 : false /*allow_never_coercion*/);
423 661713 : bool ok = !res.is_error ();
424 661713 : if (ok)
425 : {
426 396975 : std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
427 396975 : PathProbeCandidate::ImplItemCandidate c{impl_item.item,
428 396975 : impl_item.impl_block};
429 396975 : auto try_result = MethodCandidate{
430 : PathProbeCandidate (PathProbeCandidate::CandidateType::IMPL_FUNC,
431 396975 : fn, impl_item.item->get_locus (), c),
432 396975 : adjs};
433 396975 : result.insert (std::move (try_result));
434 396975 : found_possible_candidate = true;
435 396975 : }
436 661713 : }
437 13852 : return found_possible_candidate;
438 : }
439 :
440 : bool
441 1921 : MethodResolver::try_select_trait_impl_candidates (
442 : TyTy::BaseType &receiver, const std::vector<trait_item_candidate> &candidates)
443 : {
444 1921 : bool found_possible_candidate = false;
445 2298 : for (auto trait_item : candidates)
446 : {
447 377 : TyTy::FnType *fn = trait_item.ty;
448 754 : if (!fn->is_method ())
449 0 : continue;
450 :
451 377 : TyTy::BaseType *fn_self = fn->get_self_type ();
452 377 : rust_debug ("dot-operator trait_item fn_self={%s} can_eq receiver={%s}",
453 : fn_self->debug_str ().c_str (),
454 : receiver.debug_str ().c_str ());
455 :
456 377 : auto res
457 : = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
458 : false /*allow-autoderef*/,
459 : false /*is_cast_site*/,
460 377 : false /*allow_never_coercion*/);
461 377 : bool ok = !res.is_error ();
462 377 : if (ok)
463 : {
464 223 : std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
465 223 : PathProbeCandidate::TraitItemCandidate c{trait_item.reference,
466 : trait_item.item_ref,
467 223 : nullptr};
468 223 : auto try_result = MethodCandidate{
469 : PathProbeCandidate (PathProbeCandidate::CandidateType::TRAIT_FUNC,
470 223 : fn, trait_item.item->get_locus (), c),
471 223 : adjs};
472 223 : result.insert (std::move (try_result));
473 223 : found_possible_candidate = true;
474 223 : }
475 377 : }
476 1921 : return found_possible_candidate;
477 : }
478 :
479 : bool
480 13914 : MethodResolver::select (TyTy::BaseType &receiver)
481 : {
482 13914 : rust_debug ("MethodResolver::select reciever=[%s] path=[%s]",
483 : receiver.debug_str ().c_str (),
484 : segment_name.to_string ().c_str ());
485 :
486 : // Predicate candidates have the highest priority. Try them before
487 : // assembling impl candidates, which can trigger expensive trait resolution.
488 13914 : if (try_select_predicate_candidates (receiver))
489 : return true;
490 :
491 : // A receiver that is still a bare unresolved generic type parameter can only
492 : // ever satisfy a method through its own trait bounds
493 12518 : if (receiver.destructure ()->get_kind () == TyTy::TypeKind::PARAM)
494 : return false;
495 :
496 : // Assemble candidates
497 12374 : std::vector<impl_item_candidate> inherent_impl_fns;
498 12374 : if (specified_trait == nullptr)
499 : {
500 3538 : inherent_impl_fns = assemble_inherent_impl_candidates (receiver);
501 3482 : if (try_select_inherent_impl_candidates (receiver, inherent_impl_fns,
502 : false))
503 : return true;
504 : }
505 :
506 10405 : std::vector<impl_item_candidate> trait_impl_fns;
507 10405 : std::vector<trait_item_candidate> trait_fns;
508 10405 : assemble_trait_impl_candidates (receiver, trait_impl_fns, trait_fns);
509 :
510 : // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L580-L694
511 :
512 10370 : rust_debug ("inherent_impl_fns found {%lu}, trait_fns found {%lu}, "
513 : "predicate_items found {%lu}",
514 : (unsigned long) inherent_impl_fns.size (),
515 : (unsigned long) trait_fns.size (),
516 : (unsigned long) predicate_items.size ());
517 :
518 : // Try selection in the priority order defined by Rust's method resolution:
519 :
520 : // 1. Try inherent impl functions from trait impl blocks
521 10370 : if (try_select_inherent_impl_candidates (receiver, trait_impl_fns, true))
522 : return true;
523 :
524 : // 2. Try trait functions (lowest priority)
525 1921 : return try_select_trait_impl_candidates (receiver, trait_fns);
526 22653 : }
527 :
528 : std::vector<MethodResolver::predicate_candidate>
529 15478 : MethodResolver::get_predicate_items (
530 : const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
531 : const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
532 : {
533 15478 : std::vector<predicate_candidate> predicate_items;
534 18072 : for (auto &bound : specified_bounds)
535 : {
536 2594 : tl::optional<TyTy::TypeBoundPredicateItem> lookup
537 2594 : = bound.lookup_associated_item (segment_name.to_string ());
538 2594 : if (!lookup.has_value ())
539 1193 : continue;
540 :
541 1401 : TyTy::BaseType *ty = lookup->get_tyty_for_receiver (&receiver);
542 1401 : if (ty->get_kind () == TyTy::TypeKind::FNDEF)
543 : {
544 1401 : TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
545 4199 : if (fnty->is_method ())
546 1397 : predicate_items.emplace_back (lookup.value (), fnty);
547 : }
548 : }
549 :
550 15478 : return predicate_items;
551 : }
552 :
553 : std::vector<Adjustment>
554 398594 : MethodResolver::append_adjustments (const std::vector<Adjustment> &adjs) const
555 : {
556 398594 : std::vector<Adjustment> combined;
557 398594 : combined.reserve (adjustments.size () + adjs.size ());
558 :
559 399492 : for (const auto &a : adjustments)
560 898 : combined.push_back (a);
561 398727 : for (const auto &a : adjs)
562 133 : combined.push_back (a);
563 :
564 398594 : return combined;
565 : }
566 :
567 : } // namespace Resolver
568 : } // namespace Rust
|