Debian Codegraph @

gcc/rust/typecheck/rust-hir-dot-operator.cc

raw
1 // Copyright (C) 2020-2025 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 MethodResolver::MethodResolver (bool autoderef_flag,
30 const HIR::PathIdentSegment &segment_name)
31 : AutoderefCycle (autoderef_flag), segment_name (segment_name), result ()
32 {}
33
34 std::set<MethodCandidate>
35 MethodResolver::Probe (TyTy::BaseType *receiver,
36 const HIR::PathIdentSegment &segment_name,
37 bool autoderef_flag)
38 {
39 MethodResolver resolver (autoderef_flag, segment_name);
40 resolver.cycle (receiver);
41 return resolver.result;
42 }
43
44 std::set<MethodCandidate>
45 MethodResolver::Select (std::set<MethodCandidate> &candidates,
46 TyTy::BaseType *receiver,
47 std::vector<TyTy::BaseType *> arguments)
48 {
49 std::set<MethodCandidate> selected;
50 for (auto &candidate : candidates)
51 {
52 TyTy::BaseType *candidate_type = candidate.candidate.ty;
53 rust_assert (candidate_type->get_kind () == TyTy::TypeKind::FNDEF);
54 TyTy::FnType &fn = *static_cast<TyTy::FnType *> (candidate_type);
55
56 // match the number of arguments
57 if (fn.num_params () != (arguments.size () + 1))
58 continue;
59
60 // match the arguments
61 bool failed = false;
62 for (size_t i = 0; i < arguments.size (); i++)
63 {
64 TyTy::BaseType *arg = arguments.at (i);
65 TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type ();
66 TyTy::BaseType *coerced
67 = try_coercion (0, TyTy::TyWithLocation (param),
68 TyTy::TyWithLocation (arg), UNDEF_LOCATION);
69 if (coerced->get_kind () == TyTy::TypeKind::ERROR)
70 {
71 failed = true;
72 break;
73 }
74 }
75
76 if (!failed)
77 selected.insert (candidate);
78 }
79
80 return selected;
81 }
82
83 void
84 MethodResolver::try_hook (const TyTy::BaseType &r)
85 {
86 rust_debug ("MethodResolver::try_hook get_predicate_items: [%s]",
87 r.debug_str ().c_str ());
88 const auto &specified_bounds = r.get_specified_bounds ();
89 predicate_items = get_predicate_items (segment_name, r, specified_bounds);
90
91 if (predicate_items.size () > 0)
92 return;
93
94 if (r.get_kind () == TyTy::TypeKind::REF)
95 {
96 const auto &ref = static_cast<const TyTy::ReferenceType &> (r);
97 const auto &element = ref.get_var_element_type ();
98 const auto &element_ty = *element.get_tyty ();
99 const auto &specified_bounds = element_ty.get_specified_bounds ();
100 predicate_items
101 = get_predicate_items (segment_name, element_ty, specified_bounds);
102 }
103 }
104
105 bool
106 MethodResolver::select (TyTy::BaseType &receiver)
107 {
108 rust_debug ("MethodResolver::select reciever=[%s] path=[%s]",
109 receiver.debug_str ().c_str (),
110 segment_name.as_string ().c_str ());
111
112 struct impl_item_candidate
113 {
114 HIR::Function *item;
115 HIR::ImplBlock *impl_block;
116 TyTy::FnType *ty;
117 };
118
119 const TyTy::BaseType *raw = receiver.destructure ();
120 bool receiver_is_raw_ptr = raw->get_kind () == TyTy::TypeKind::POINTER;
121 bool receiver_is_ref = raw->get_kind () == TyTy::TypeKind::REF;
122
123 // assemble inherent impl items
124 std::vector<impl_item_candidate> inherent_impl_fns;
125 mappings.iterate_impl_items (
126 [&] (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) mutable -> bool {
127 bool is_trait_impl = impl->has_trait_ref ();
128 if (is_trait_impl)
129 return true;
130
131 bool is_fn
132 = item->get_impl_item_type () == HIR::ImplItem::ImplItemType::FUNCTION;
133 if (!is_fn)
134 return true;
135
136 HIR::Function *func = static_cast<HIR::Function *> (item);
137 if (!func->is_method ())
138 return true;
139
140 bool name_matches = func->get_function_name ().as_string ().compare (
141 segment_name.as_string ())
142 == 0;
143 if (!name_matches)
144 return true;
145
146 TyTy::BaseType *ty = nullptr;
147 if (!query_type (func->get_mappings ().get_hirid (), &ty))
148 return true;
149 rust_assert (ty != nullptr);
150 if (ty->get_kind () == TyTy::TypeKind::ERROR)
151 return true;
152
153 rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
154 TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
155 const TyTy::BaseType *impl_self
156 = TypeCheckItem::ResolveImplBlockSelf (*impl);
157
158 // see:
159 // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280
160 // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660
161 bool impl_self_is_ptr = impl_self->get_kind () == TyTy::TypeKind::POINTER;
162 bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF;
163 if (receiver_is_raw_ptr && impl_self_is_ptr)
164 {
165 const TyTy::PointerType &sptr
166 = *static_cast<const TyTy::PointerType *> (impl_self);
167 const TyTy::PointerType &ptr
168 = *static_cast<const TyTy::PointerType *> (raw);
169
170 // we could do this via lang-item assemblies if we refactor this
171 bool mut_match = sptr.mutability () == ptr.mutability ();
172 if (!mut_match)
173 return true;
174 }
175 else if (receiver_is_ref && impl_self_is_ref)
176 {
177 const TyTy::ReferenceType &sptr
178 = *static_cast<const TyTy::ReferenceType *> (impl_self);
179 const TyTy::ReferenceType &ptr
180 = *static_cast<const TyTy::ReferenceType *> (raw);
181
182 // we could do this via lang-item assemblies if we refactor this
183 bool mut_match = sptr.mutability () == ptr.mutability ();
184 if (!mut_match)
185 return true;
186 }
187
188 inherent_impl_fns.push_back ({func, impl, fnty});
189
190 return true;
191 });
192
193 struct trait_item_candidate
194 {
195 const HIR::TraitItemFunc *item;
196 const HIR::Trait *trait;
197 TyTy::FnType *ty;
198 const TraitReference *reference;
199 const TraitItemReference *item_ref;
200 };
201
202 std::vector<trait_item_candidate> trait_fns;
203 mappings.iterate_impl_blocks ([&] (HirId id,
204 HIR::ImplBlock *impl) mutable -> bool {
205 bool is_trait_impl = impl->has_trait_ref ();
206 if (!is_trait_impl)
207 return true;
208
209 // look for impl implementation else lookup the associated trait item
210 for (auto &impl_item : impl->get_impl_items ())
211 {
212 bool is_fn = impl_item->get_impl_item_type ()
213 == HIR::ImplItem::ImplItemType::FUNCTION;
214 if (!is_fn)
215 continue;
216
217 HIR::Function *func = static_cast<HIR::Function *> (impl_item.get ());
218 if (!func->is_method ())
219 continue;
220
221 bool name_matches = func->get_function_name ().as_string ().compare (
222 segment_name.as_string ())
223 == 0;
224 if (!name_matches)
225 continue;
226
227 TyTy::BaseType *ty = nullptr;
228 if (!query_type (func->get_mappings ().get_hirid (), &ty))
229 continue;
230 if (ty->get_kind () == TyTy::TypeKind::ERROR)
231 continue;
232
233 rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
234 TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
235 const TyTy::BaseType *impl_self
236 = TypeCheckItem::ResolveImplBlockSelf (*impl);
237
238 // see:
239 // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280
240 // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660
241 bool impl_self_is_ptr
242 = impl_self->get_kind () == TyTy::TypeKind::POINTER;
243 bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF;
244 if (receiver_is_raw_ptr && impl_self_is_ptr)
245 {
246 const TyTy::PointerType &sptr
247 = *static_cast<const TyTy::PointerType *> (impl_self);
248 const TyTy::PointerType &ptr
249 = *static_cast<const TyTy::PointerType *> (raw);
250
251 // we could do this via lang-item assemblies if we refactor this
252 bool mut_match = sptr.mutability () == ptr.mutability ();
253 if (!mut_match)
254 continue;
255 }
256 else if (receiver_is_ref && impl_self_is_ref)
257 {
258 const TyTy::ReferenceType &sptr
259 = *static_cast<const TyTy::ReferenceType *> (impl_self);
260 const TyTy::ReferenceType &ptr
261 = *static_cast<const TyTy::ReferenceType *> (raw);
262
263 // we could do this via lang-item assemblies if we refactor this
264 bool mut_match = sptr.mutability () == ptr.mutability ();
265 if (!mut_match)
266 continue;
267 }
268
269 inherent_impl_fns.push_back ({func, impl, fnty});
270 return true;
271 }
272
273 TraitReference *trait_ref = TraitResolver::Resolve (impl->get_trait_ref ());
274 rust_assert (!trait_ref->is_error ());
275
276 auto item_ref
277 = trait_ref->lookup_trait_item (segment_name.as_string (),
278 TraitItemReference::TraitItemType::FN);
279 if (item_ref->is_error ())
280 return true;
281
282 const HIR::Trait *trait = trait_ref->get_hir_trait_ref ();
283 HIR::TraitItem *item = item_ref->get_hir_trait_item ();
284 if (item->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
285 return true;
286
287 HIR::TraitItemFunc *func = static_cast<HIR::TraitItemFunc *> (item);
288 if (!func->get_decl ().is_method ())
289 return true;
290
291 TyTy::BaseType *ty = item_ref->get_tyty ();
292 rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
293 TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
294
295 trait_item_candidate candidate{func, trait, fnty, trait_ref, item_ref};
296 trait_fns.push_back (candidate);
297
298 return true;
299 });
300
301 // lookup specified bounds for an associated item
302 struct precdicate_candidate
303 {
304 TyTy::TypeBoundPredicateItem lookup;
305 TyTy::FnType *fntype;
306 };
307
308 // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L580-L694
309
310 rust_debug ("inherent_impl_fns found {%lu}, trait_fns found {%lu}, "
311 "predicate_items found {%lu}",
312 (unsigned long) inherent_impl_fns.size (),
313 (unsigned long) trait_fns.size (),
314 (unsigned long) predicate_items.size ());
315
316 bool found_possible_candidate = false;
317 for (const auto &predicate : predicate_items)
318 {
319 const TyTy::FnType *fn = predicate.fntype;
320 rust_assert (fn->is_method ());
321
322 TyTy::BaseType *fn_self = fn->get_self_type ();
323 rust_debug ("dot-operator predicate fn_self={%s} can_eq receiver={%s}",
324 fn_self->debug_str ().c_str (),
325 receiver.debug_str ().c_str ());
326
327 auto res
328 = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
329 false /*allow-autoderef*/);
330 bool ok = !res.is_error ();
331 if (ok)
332 {
333 std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
334 const TraitReference *trait_ref
335 = predicate.lookup.get_parent ()->get ();
336 const TraitItemReference *trait_item
337 = predicate.lookup.get_raw_item ();
338
339 PathProbeCandidate::TraitItemCandidate c{trait_ref, trait_item,
340 nullptr};
341 auto try_result = MethodCandidate{
342 PathProbeCandidate (PathProbeCandidate::CandidateType::TRAIT_FUNC,
343 fn->clone (), trait_item->get_locus (), c),
344 adjs};
345 result.insert (std::move (try_result));
346 found_possible_candidate = true;
347 }
348 }
349 if (found_possible_candidate)
350 {
351 return true;
352 }
353
354 for (auto &impl_item : inherent_impl_fns)
355 {
356 bool is_trait_impl_block = impl_item.impl_block->has_trait_ref ();
357 if (is_trait_impl_block)
358 continue;
359
360 TyTy::FnType *fn = impl_item.ty;
361 rust_assert (fn->is_method ());
362
363 TyTy::BaseType *fn_self = fn->get_self_type ();
364 rust_debug ("dot-operator impl_item fn_self={%s} can_eq receiver={%s}",
365 fn_self->debug_str ().c_str (),
366 receiver.debug_str ().c_str ());
367
368 auto res
369 = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
370 false /*allow-autoderef*/);
371 bool ok = !res.is_error ();
372 if (ok)
373 {
374 std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
375 PathProbeCandidate::ImplItemCandidate c{impl_item.item,
376 impl_item.impl_block};
377 auto try_result = MethodCandidate{
378 PathProbeCandidate (PathProbeCandidate::CandidateType::IMPL_FUNC,
379 fn, impl_item.item->get_locus (), c),
380 adjs};
381 result.insert (std::move (try_result));
382 found_possible_candidate = true;
383 }
384 }
385 if (found_possible_candidate)
386 {
387 return true;
388 }
389
390 for (auto &impl_item : inherent_impl_fns)
391 {
392 bool is_trait_impl_block = impl_item.impl_block->has_trait_ref ();
393 if (!is_trait_impl_block)
394 continue;
395
396 TyTy::FnType *fn = impl_item.ty;
397 rust_assert (fn->is_method ());
398
399 TyTy::BaseType *fn_self = fn->get_self_type ();
400 rust_debug (
401 "dot-operator trait_impl_item fn_self={%s} can_eq receiver={%s}",
402 fn_self->debug_str ().c_str (), receiver.debug_str ().c_str ());
403
404 auto res
405 = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
406 false /*allow-autoderef*/);
407 bool ok = !res.is_error ();
408 if (ok)
409 {
410 std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
411 PathProbeCandidate::ImplItemCandidate c{impl_item.item,
412 impl_item.impl_block};
413 auto try_result = MethodCandidate{
414 PathProbeCandidate (PathProbeCandidate::CandidateType::IMPL_FUNC,
415 fn, impl_item.item->get_locus (), c),
416 adjs};
417 result.insert (std::move (try_result));
418 found_possible_candidate = true;
419 }
420 }
421 if (found_possible_candidate)
422 {
423 return true;
424 }
425
426 for (auto trait_item : trait_fns)
427 {
428 TyTy::FnType *fn = trait_item.ty;
429 rust_assert (fn->is_method ());
430
431 TyTy::BaseType *fn_self = fn->get_self_type ();
432 rust_debug ("dot-operator trait_item fn_self={%s} can_eq receiver={%s}",
433 fn_self->debug_str ().c_str (),
434 receiver.debug_str ().c_str ());
435
436 auto res
437 = TypeCoercionRules::TryCoerce (&receiver, fn_self, UNDEF_LOCATION,
438 false /*allow-autoderef*/);
439 bool ok = !res.is_error ();
440 if (ok)
441 {
442 std::vector<Adjustment> adjs = append_adjustments (res.adjustments);
443 PathProbeCandidate::TraitItemCandidate c{trait_item.reference,
444 trait_item.item_ref,
445 nullptr};
446 auto try_result = MethodCandidate{
447 PathProbeCandidate (PathProbeCandidate::CandidateType::TRAIT_FUNC,
448 fn, trait_item.item->get_locus (), c),
449 adjs};
450 result.insert (std::move (try_result));
451 found_possible_candidate = true;
452 }
453 }
454
455 return found_possible_candidate;
456 }
457
458 std::vector<MethodResolver::predicate_candidate>
459 MethodResolver::get_predicate_items (
460 const HIR::PathIdentSegment &segment_name, const TyTy::BaseType &receiver,
461 const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
462 {
463 std::vector<predicate_candidate> predicate_items;
464 for (auto &bound : specified_bounds)
465 {
466 TyTy::TypeBoundPredicateItem lookup
467 = bound.lookup_associated_item (segment_name.as_string ());
468 if (lookup.is_error ())
469 continue;
470
471 TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
472 if (ty->get_kind () == TyTy::TypeKind::FNDEF)
473 {
474 TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
475 if (fnty->is_method ())
476 {
477 predicate_candidate candidate{lookup, fnty};
478 predicate_items.push_back (candidate);
479 }
480 }
481 }
482
483 return predicate_items;
484 }
485
486 std::vector<Adjustment>
487 MethodResolver::append_adjustments (const std::vector<Adjustment> &adjs) const
488 {
489 std::vector<Adjustment> combined;
490 combined.reserve (adjustments.size () + adjs.size ());
491
492 for (const auto &a : adjustments)
493 combined.push_back (a);
494 for (const auto &a : adjs)
495 combined.push_back (a);
496
497 return combined;
498 }
499
500 } // namespace Resolver
501 } // namespace Rust