Branch data Line data Source code
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-autoderef.h"
20 : : #include "rust-hir-path-probe.h"
21 : : #include "rust-hir-dot-operator.h"
22 : : #include "rust-hir-trait-resolve.h"
23 : : #include "rust-type-util.h"
24 : : #include "rust-substitution-mapper.h"
25 : :
26 : : namespace Rust {
27 : : namespace Resolver {
28 : :
29 : : static bool
30 : : resolve_operator_overload_fn (
31 : : LangItem::Kind lang_item_type, TyTy::BaseType *ty, TyTy::FnType **resolved_fn,
32 : : Adjustment::AdjustmentType *requires_ref_adjustment);
33 : :
34 : : TyTy::BaseType *
35 : 2580 : Adjuster::adjust_type (const std::vector<Adjustment> &adjustments)
36 : : {
37 : 2580 : if (adjustments.size () == 0)
38 : 2051 : return base->clone ();
39 : :
40 : 529 : return adjustments.back ().get_expected ()->clone ();
41 : : }
42 : :
43 : : Adjustment
44 : 435 : Adjuster::try_deref_type (TyTy::BaseType *ty, LangItem::Kind deref_lang_item)
45 : : {
46 : 435 : TyTy::FnType *fn = nullptr;
47 : 435 : Adjustment::AdjustmentType requires_ref_adjustment
48 : : = Adjustment::AdjustmentType::ERROR;
49 : 435 : bool operator_overloaded
50 : 435 : = resolve_operator_overload_fn (deref_lang_item, ty, &fn,
51 : : &requires_ref_adjustment);
52 : 435 : if (!operator_overloaded)
53 : : {
54 : 353 : return Adjustment::get_error ();
55 : : }
56 : :
57 : 82 : auto resolved_base = fn->get_return_type ()->destructure ();
58 : 82 : bool is_valid_type = resolved_base->get_kind () == TyTy::TypeKind::REF;
59 : 82 : if (!is_valid_type)
60 : 0 : return Adjustment::get_error ();
61 : :
62 : 82 : TyTy::ReferenceType *ref_base
63 : : = static_cast<TyTy::ReferenceType *> (resolved_base);
64 : :
65 : 82 : Adjustment::AdjustmentType adjustment_type
66 : : = Adjustment::AdjustmentType::ERROR;
67 : 82 : switch (deref_lang_item)
68 : : {
69 : 68 : case LangItem::Kind::DEREF:
70 : 68 : adjustment_type = Adjustment::AdjustmentType::DEREF;
71 : 68 : break;
72 : :
73 : 14 : case LangItem::Kind::DEREF_MUT:
74 : 14 : adjustment_type = Adjustment::AdjustmentType::DEREF_MUT;
75 : 14 : break;
76 : :
77 : : default:
78 : : break;
79 : : }
80 : :
81 : 82 : return Adjustment::get_op_overload_deref_adjustment (adjustment_type, ty,
82 : : ref_base, fn,
83 : : requires_ref_adjustment);
84 : : }
85 : :
86 : : Adjustment
87 : 200 : Adjuster::try_raw_deref_type (TyTy::BaseType *ty)
88 : : {
89 : 200 : bool is_valid_type = ty->get_kind () == TyTy::TypeKind::REF;
90 : 200 : if (!is_valid_type)
91 : 46 : return Adjustment::get_error ();
92 : :
93 : 154 : const TyTy::ReferenceType *ref_base
94 : : = static_cast<const TyTy::ReferenceType *> (ty);
95 : 154 : auto infered = ref_base->get_base ()->destructure ();
96 : :
97 : 154 : return Adjustment (Adjustment::AdjustmentType::INDIRECTION, ty, infered);
98 : : }
99 : :
100 : : Adjustment
101 : 295 : Adjuster::try_unsize_type (TyTy::BaseType *ty)
102 : : {
103 : 295 : bool is_valid_type = ty->get_kind () == TyTy::TypeKind::ARRAY;
104 : 295 : if (!is_valid_type)
105 : 228 : return Adjustment::get_error ();
106 : :
107 : 67 : auto &mappings = Analysis::Mappings::get ();
108 : 67 : auto context = TypeCheckContext::get ();
109 : :
110 : 67 : const auto ref_base = static_cast<const TyTy::ArrayType *> (ty);
111 : 67 : auto slice_elem = ref_base->get_element_type ();
112 : :
113 : 67 : auto slice
114 : 67 : = new TyTy::SliceType (mappings.get_next_hir_id (), ty->get_ident ().locus,
115 : 134 : TyTy::TyVar (slice_elem->get_ref ()));
116 : 67 : context->insert_implicit_type (slice->get_ref (), slice);
117 : :
118 : 67 : return Adjustment (Adjustment::AdjustmentType::UNSIZE, ty, slice);
119 : : }
120 : :
121 : : static bool
122 : 435 : resolve_operator_overload_fn (
123 : : LangItem::Kind lang_item_type, TyTy::BaseType *lhs,
124 : : TyTy::FnType **resolved_fn,
125 : : Adjustment::AdjustmentType *requires_ref_adjustment)
126 : : {
127 : 435 : auto context = TypeCheckContext::get ();
128 : 435 : auto &mappings = Analysis::Mappings::get ();
129 : :
130 : : // look up lang item for arithmetic type
131 : 435 : std::string associated_item_name = LangItem::ToString (lang_item_type);
132 : 435 : auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
133 : :
134 : 435 : if (!lang_item_defined)
135 : : return false;
136 : 105 : DefId &respective_lang_item_id = lang_item_defined.value ();
137 : :
138 : : // we might be in a static or const context and unknown is fine
139 : 105 : TypeCheckContextItem current_context = TypeCheckContextItem::get_error ();
140 : 105 : if (context->have_function_context ())
141 : : {
142 : 105 : current_context = context->peek_context ();
143 : : }
144 : :
145 : : // this flags stops recurisve calls to try and deref when none is available
146 : : // which will cause an infinite loop
147 : 105 : bool autoderef_flag = true;
148 : 210 : auto segment = HIR::PathIdentSegment (associated_item_name);
149 : 105 : auto candidates = MethodResolver::Probe (lhs, segment, autoderef_flag);
150 : :
151 : : // remove any recursive candidates
152 : 105 : std::set<MethodCandidate> resolved_candidates;
153 : 229 : for (auto &c : candidates)
154 : : {
155 : 124 : const TyTy::BaseType *candidate_type = c.candidate.ty;
156 : 124 : rust_assert (candidate_type->get_kind () == TyTy::TypeKind::FNDEF);
157 : :
158 : 124 : const TyTy::FnType &fn
159 : : = *static_cast<const TyTy::FnType *> (candidate_type);
160 : :
161 : 124 : DefId current_fn_defid = current_context.get_defid ();
162 : 248 : bool recursive_candidated = fn.get_id () == current_fn_defid;
163 : 124 : if (!recursive_candidated)
164 : : {
165 : 124 : resolved_candidates.insert (c);
166 : : }
167 : : }
168 : :
169 : 105 : auto selected_candidates
170 : 105 : = MethodResolver::Select (resolved_candidates, lhs, {});
171 : 105 : bool have_implementation_for_lang_item = selected_candidates.size () > 0;
172 : 105 : if (!have_implementation_for_lang_item)
173 : : return false;
174 : :
175 : 103 : if (selected_candidates.size () > 1)
176 : : {
177 : : // no need to error out as we are just trying to see if there is a fit
178 : : return false;
179 : : }
180 : :
181 : : // Get the adjusted self
182 : 82 : MethodCandidate candidate = *selected_candidates.begin ();
183 : 82 : Adjuster adj (lhs);
184 : 82 : TyTy::BaseType *adjusted_self = adj.adjust_type (candidate.adjustments);
185 : :
186 : 82 : PathProbeCandidate &resolved_candidate = candidate.candidate;
187 : 82 : TyTy::BaseType *lookup_tyty = candidate.candidate.ty;
188 : 82 : rust_assert (lookup_tyty->get_kind () == TyTy::TypeKind::FNDEF);
189 : 82 : TyTy::BaseType *lookup = lookup_tyty;
190 : 82 : TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup);
191 : 164 : rust_assert (fn->is_method ());
192 : :
193 : 93 : rust_debug ("is_impl_item_candidate: %s",
194 : : resolved_candidate.is_impl_candidate () ? "true" : "false");
195 : :
196 : : // in the case where we resolve to a trait bound we have to be careful we are
197 : : // able to do so there is a case where we are currently resolving the deref
198 : : // operator overload function which is generic and this might resolve to the
199 : : // trait item of deref which is not valid as its just another recursive case
200 : 82 : if (current_context.get_type () == TypeCheckContextItem::ItemType::IMPL_ITEM)
201 : : {
202 : 0 : auto &impl_item = current_context.get_impl_item ();
203 : 0 : HIR::ImplBlock *parent = impl_item.first;
204 : 0 : HIR::Function *fn = impl_item.second;
205 : :
206 : 0 : if (parent->has_trait_ref ()
207 : 0 : && fn->get_function_name ().as_string ().compare (
208 : : associated_item_name)
209 : : == 0)
210 : : {
211 : 0 : TraitReference *trait_reference
212 : 0 : = TraitResolver::Lookup (parent->get_trait_ref ());
213 : 0 : if (!trait_reference->is_error ())
214 : : {
215 : 0 : TyTy::BaseType *lookup = nullptr;
216 : 0 : bool ok = context->lookup_type (fn->get_mappings ().get_hirid (),
217 : : &lookup);
218 : 0 : rust_assert (ok);
219 : 0 : rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
220 : :
221 : 0 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup);
222 : 0 : rust_assert (fntype->is_method ());
223 : :
224 : 0 : bool is_lang_item_impl
225 : 0 : = trait_reference->get_mappings ().get_defid ()
226 : 0 : == respective_lang_item_id;
227 : 0 : bool self_is_lang_item_self
228 : 0 : = fntype->get_self_type ()->is_equal (*adjusted_self);
229 : 0 : bool recursive_operator_overload
230 : : = is_lang_item_impl && self_is_lang_item_self;
231 : :
232 : 0 : if (recursive_operator_overload)
233 : 0 : return false;
234 : : }
235 : : }
236 : : }
237 : :
238 : : // we found a valid operator overload
239 : 82 : fn->prepare_higher_ranked_bounds ();
240 : 82 : rust_debug ("resolved operator overload to: {%u} {%s}",
241 : : candidate.candidate.ty->get_ref (),
242 : : candidate.candidate.ty->debug_str ().c_str ());
243 : :
244 : 82 : if (fn->needs_substitution ())
245 : : {
246 : 82 : if (lhs->get_kind () == TyTy::TypeKind::ADT)
247 : : {
248 : 64 : const TyTy::ADTType *adt = static_cast<const TyTy::ADTType *> (lhs);
249 : :
250 : 64 : auto s = fn->get_self_type ()->get_root ();
251 : 64 : rust_assert (s->can_eq (adt, false));
252 : 64 : rust_assert (s->get_kind () == TyTy::TypeKind::ADT);
253 : 64 : const TyTy::ADTType *self_adt
254 : : = static_cast<const TyTy::ADTType *> (s);
255 : :
256 : : // we need to grab the Self substitutions as the inherit type
257 : : // parameters for this
258 : 64 : if (self_adt->needs_substitution ())
259 : : {
260 : 64 : rust_assert (adt->was_substituted ());
261 : :
262 : 64 : TyTy::SubstitutionArgumentMappings used_args_in_prev_segment
263 : 64 : = GetUsedSubstArgs::From (adt);
264 : :
265 : 64 : TyTy::SubstitutionArgumentMappings inherit_type_args
266 : : = self_adt->solve_mappings_from_receiver_for_self (
267 : 64 : used_args_in_prev_segment);
268 : :
269 : : // there may or may not be inherited type arguments
270 : 64 : if (!inherit_type_args.is_error ())
271 : : {
272 : : // need to apply the inherited type arguments to the
273 : : // function
274 : 64 : lookup = fn->handle_substitions (inherit_type_args);
275 : : }
276 : 64 : }
277 : : }
278 : : else
279 : : {
280 : 18 : rust_assert (candidate.adjustments.size () < 2);
281 : :
282 : : // lets infer the params for this we could probably fix this up by
283 : : // actually just performing a substitution of a single param but this
284 : : // seems more generic i think.
285 : : //
286 : : // this is the case where we had say Foo<&Bar>> and we have derefed to
287 : : // the &Bar and we are trying to match a method self of Bar which
288 : : // requires another deref which is matched to the deref trait impl of
289 : : // &&T so this requires another reference and deref call
290 : :
291 : 18 : lookup = fn->infer_substitions (UNDEF_LOCATION);
292 : 18 : rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
293 : 18 : fn = static_cast<TyTy::FnType *> (lookup);
294 : :
295 : 18 : location_t unify_locus = mappings.lookup_location (lhs->get_ref ());
296 : 18 : unify_site (lhs->get_ref (),
297 : 18 : TyTy::TyWithLocation (fn->get_self_type ()),
298 : 18 : TyTy::TyWithLocation (adjusted_self), unify_locus);
299 : :
300 : 18 : lookup = fn;
301 : : }
302 : : }
303 : :
304 : 82 : if (candidate.adjustments.size () > 0)
305 : 78 : *requires_ref_adjustment = candidate.adjustments.at (0).get_type ();
306 : :
307 : 82 : *resolved_fn = static_cast<TyTy::FnType *> (lookup);
308 : :
309 : 82 : return true;
310 : 187 : }
311 : :
312 : 52588 : AutoderefCycle::AutoderefCycle (bool autoderef_flag)
313 : 52588 : : autoderef_flag (autoderef_flag)
314 : 52588 : {}
315 : :
316 : 52588 : AutoderefCycle::~AutoderefCycle () {}
317 : :
318 : : void
319 : 3145 : AutoderefCycle::try_hook (const TyTy::BaseType &)
320 : 3145 : {}
321 : :
322 : : bool
323 : 5750 : AutoderefCycle::cycle (TyTy::BaseType *receiver)
324 : : {
325 : 5750 : TyTy::BaseType *r = receiver;
326 : 6044 : while (true)
327 : : {
328 : 5897 : rust_debug ("autoderef try 1: {%s}", r->debug_str ().c_str ());
329 : 5897 : if (try_autoderefed (r))
330 : 5750 : return true;
331 : :
332 : : // 4. deref to to 1, if cannot deref then quit
333 : 688 : if (autoderef_flag)
334 : : return false;
335 : :
336 : : // try unsize
337 : 295 : Adjustment unsize = Adjuster::try_unsize_type (r);
338 : 295 : if (!unsize.is_error ())
339 : : {
340 : 67 : adjustments.push_back (unsize);
341 : 67 : auto unsize_r = unsize.get_expected ();
342 : :
343 : 67 : rust_debug ("autoderef try unsize: {%s}",
344 : : unsize_r->debug_str ().c_str ());
345 : 67 : if (try_autoderefed (unsize_r))
346 : : return true;
347 : :
348 : 2 : adjustments.pop_back ();
349 : : }
350 : :
351 : 230 : bool is_ptr = receiver->get_kind () == TyTy::TypeKind::POINTER;
352 : 230 : if (is_ptr)
353 : : {
354 : : // deref of raw pointers is unsafe
355 : : return false;
356 : : }
357 : :
358 : 228 : Adjustment deref = Adjuster::try_deref_type (r, LangItem::Kind::DEREF);
359 : 228 : if (!deref.is_error ())
360 : : {
361 : 68 : auto deref_r = deref.get_expected ();
362 : 68 : adjustments.push_back (deref);
363 : :
364 : 68 : rust_debug ("autoderef try lang-item DEREF: {%s}",
365 : : deref_r->debug_str ().c_str ());
366 : 68 : if (try_autoderefed (deref_r))
367 : : return true;
368 : :
369 : 47 : adjustments.pop_back ();
370 : : }
371 : :
372 : 207 : Adjustment deref_mut
373 : 207 : = Adjuster::try_deref_type (r, LangItem::Kind::DEREF_MUT);
374 : 207 : if (!deref_mut.is_error ())
375 : : {
376 : 14 : auto deref_r = deref_mut.get_expected ();
377 : 14 : adjustments.push_back (deref_mut);
378 : :
379 : 14 : rust_debug ("autoderef try lang-item DEREF_MUT: {%s}",
380 : : deref_r->debug_str ().c_str ());
381 : 14 : if (try_autoderefed (deref_r))
382 : : return true;
383 : :
384 : 7 : adjustments.pop_back ();
385 : : }
386 : :
387 : 200 : if (!deref_mut.is_error ())
388 : : {
389 : 7 : auto deref_r = deref_mut.get_expected ();
390 : 7 : adjustments.push_back (deref_mut);
391 : 7 : Adjustment raw_deref = Adjuster::try_raw_deref_type (deref_r);
392 : 7 : adjustments.push_back (raw_deref);
393 : 7 : deref_r = raw_deref.get_expected ();
394 : :
395 : 7 : if (try_autoderefed (deref_r))
396 : 7 : return true;
397 : :
398 : 0 : adjustments.pop_back ();
399 : 0 : adjustments.pop_back ();
400 : : }
401 : :
402 : 193 : if (!deref.is_error ())
403 : : {
404 : 33 : r = deref.get_expected ();
405 : 33 : adjustments.push_back (deref);
406 : : }
407 : 193 : Adjustment raw_deref = Adjuster::try_raw_deref_type (r);
408 : 193 : if (raw_deref.is_error ())
409 : : return false;
410 : :
411 : 147 : r = raw_deref.get_expected ();
412 : 147 : adjustments.push_back (raw_deref);
413 : 147 : }
414 : : return false;
415 : : }
416 : :
417 : : bool
418 : 6053 : AutoderefCycle::try_autoderefed (TyTy::BaseType *r)
419 : : {
420 : 6053 : try_hook (*r);
421 : :
422 : : // 1. try raw
423 : 6053 : if (select (*r))
424 : : return true;
425 : :
426 : : // 2. try ref
427 : 1277 : TyTy::ReferenceType *r1
428 : 1277 : = new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
429 : 2554 : Mutability::Imm);
430 : 1277 : adjustments.push_back (
431 : 1277 : Adjustment (Adjustment::AdjustmentType::IMM_REF, r, r1));
432 : 1277 : if (select (*r1))
433 : : return true;
434 : :
435 : 799 : adjustments.pop_back ();
436 : :
437 : : // 3. try mut ref
438 : 799 : TyTy::ReferenceType *r2
439 : 799 : = new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()),
440 : 1598 : Mutability::Mut);
441 : 799 : adjustments.push_back (
442 : 799 : Adjustment (Adjustment::AdjustmentType::MUT_REF, r, r2));
443 : 799 : if (select (*r2))
444 : : return true;
445 : :
446 : 744 : adjustments.pop_back ();
447 : :
448 : 744 : return false;
449 : : }
450 : :
451 : : } // namespace Resolver
452 : : } // namespace Rust
|