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-type-check.h"
20 : #include "rust-type-util.h"
21 : #include "rust-hir-type-check-expr.h"
22 :
23 : namespace Rust {
24 : namespace Resolver {
25 :
26 : TypeCheckContext *
27 90401940 : TypeCheckContext::get ()
28 : {
29 90401940 : static TypeCheckContext *instance;
30 90401940 : if (instance == nullptr)
31 5265 : instance = new TypeCheckContext ();
32 :
33 90401940 : return instance;
34 : }
35 :
36 5265 : TypeCheckContext::TypeCheckContext () { lifetime_resolver_stack.emplace (); }
37 :
38 0 : TypeCheckContext::~TypeCheckContext () {}
39 :
40 : bool
41 0 : TypeCheckContext::lookup_builtin (NodeId id, TyTy::BaseType **type)
42 : {
43 0 : auto ref_it = node_id_refs.find (id);
44 0 : if (ref_it == node_id_refs.end ())
45 : return false;
46 :
47 0 : auto it = resolved.find (ref_it->second);
48 0 : if (it == resolved.end ())
49 : return false;
50 :
51 0 : *type = it->second;
52 0 : return true;
53 : }
54 :
55 : bool
56 52649 : TypeCheckContext::lookup_builtin (std::string name, TyTy::BaseType **type)
57 : {
58 579029 : for (auto &builtin : builtins)
59 : {
60 579029 : if (name.compare (builtin->as_string ()) == 0)
61 : {
62 52649 : *type = builtin.get ();
63 52649 : return true;
64 : }
65 : }
66 : return false;
67 : }
68 :
69 : void
70 101346 : TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::BaseType *type)
71 : {
72 101346 : node_id_refs[ref] = id;
73 101346 : resolved[id] = type;
74 101346 : builtins.push_back (std::unique_ptr<TyTy::BaseType> (type));
75 101346 : }
76 :
77 : const std::vector<std::unique_ptr<TyTy::BaseType>> &
78 4977 : TypeCheckContext::get_builtins () const
79 : {
80 4977 : return builtins;
81 : }
82 :
83 : void
84 1579852 : TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings,
85 : TyTy::BaseType *type)
86 : {
87 1579852 : rust_assert (type != nullptr);
88 1579852 : NodeId ref = mappings.get_nodeid ();
89 1579852 : HirId id = mappings.get_hirid ();
90 1579852 : node_id_refs[ref] = id;
91 1579852 : resolved[id] = type;
92 1579852 : }
93 :
94 : void
95 690877 : TypeCheckContext::insert_implicit_type (HirId id, TyTy::BaseType *type)
96 : {
97 690877 : rust_assert (type != nullptr);
98 690877 : resolved[id] = type;
99 690877 : }
100 :
101 : static std::pair<DefId, std::vector<HirId>>
102 78337 : make_adt_substitution_key (DefId id,
103 : const TyTy::SubstitutionArgumentMappings &mappings)
104 : {
105 78337 : std::vector<HirId> arguments;
106 78337 : arguments.reserve (mappings.size ());
107 163368 : for (const auto &mapping : mappings.get_mappings ())
108 85031 : arguments.push_back (mapping.get_tyty ()->get_ty_ref ());
109 :
110 78337 : return {id, std::move (arguments)};
111 78337 : }
112 :
113 : bool
114 54010 : TypeCheckContext::lookup_adt_substitution (
115 : DefId id, const TyTy::SubstitutionArgumentMappings &mappings,
116 : TyTy::ADTType **type) const
117 : {
118 54010 : auto it = adt_substitutions.find (make_adt_substitution_key (id, mappings));
119 54010 : if (it == adt_substitutions.end ())
120 : return false;
121 :
122 29683 : *type = it->second;
123 29683 : return true;
124 : }
125 :
126 : void
127 24327 : TypeCheckContext::insert_adt_substitution (
128 : DefId id, const TyTy::SubstitutionArgumentMappings &mappings,
129 : TyTy::ADTType *type)
130 : {
131 24327 : rust_assert (type != nullptr);
132 24327 : auto inserted
133 24327 : = adt_substitutions.emplace (make_adt_substitution_key (id, mappings),
134 : type);
135 24327 : rust_assert (inserted.second);
136 24327 : }
137 :
138 : bool
139 60617798 : TypeCheckContext::lookup_type (HirId id, TyTy::BaseType **type) const
140 : {
141 60617798 : auto it = resolved.find (id);
142 60617798 : if (it == resolved.end ())
143 : return false;
144 :
145 59311868 : *type = it->second;
146 59311868 : return true;
147 : }
148 :
149 : void
150 25132 : TypeCheckContext::mark_function_body_pending (DefId id)
151 : {
152 25132 : function_bodies_pending.insert (id);
153 25132 : }
154 :
155 : void
156 19431 : TypeCheckContext::clear_function_body_pending (DefId id)
157 : {
158 19431 : function_bodies_pending.erase (id);
159 19431 : }
160 :
161 : bool
162 5048 : TypeCheckContext::function_body_pending (DefId id) const
163 : {
164 5048 : return function_bodies_pending.find (id) != function_bodies_pending.end ();
165 : }
166 :
167 : void
168 67238 : TypeCheckContext::clear_type (TyTy::BaseType *ty)
169 : {
170 67238 : auto it = resolved.find (ty->get_ref ());
171 67238 : if (it == resolved.end ())
172 67238 : return;
173 :
174 67238 : resolved.erase (it);
175 : }
176 :
177 : bool
178 29687 : TypeCheckContext::have_function_context () const
179 : {
180 29687 : return !return_type_stack.empty ();
181 : }
182 :
183 : TyTy::BaseType *
184 1251 : TypeCheckContext::peek_return_type ()
185 : {
186 1251 : rust_assert (!return_type_stack.empty ());
187 1251 : return return_type_stack.back ().second;
188 : }
189 :
190 : void
191 19431 : TypeCheckContext::push_return_type (TypeCheckContextItem item,
192 : TyTy::BaseType *return_type)
193 : {
194 19431 : return_type_stack.emplace_back (std::move (item), return_type);
195 : // a query can check another function body while an expression in the
196 : // caller has an expected type
197 19431 : push_expected_type (nullptr);
198 19431 : }
199 :
200 : void
201 19288 : TypeCheckContext::pop_return_type ()
202 : {
203 19288 : rust_assert (!return_type_stack.empty ());
204 19288 : pop_expected_type ();
205 19288 : return_type_stack.pop_back ();
206 19288 : }
207 :
208 : TypeCheckContextItem
209 28369 : TypeCheckContext::peek_context ()
210 : {
211 28369 : rust_assert (!return_type_stack.empty ());
212 28369 : return return_type_stack.back ().first;
213 : }
214 :
215 : void
216 87703 : TypeCheckContext::push_expected_type (TyTy::BaseType *expected)
217 : {
218 87703 : expected_type_stack.push_back (expected);
219 87703 : }
220 :
221 : void
222 87362 : TypeCheckContext::pop_expected_type ()
223 : {
224 87362 : rust_assert (!expected_type_stack.empty ());
225 87362 : expected_type_stack.pop_back ();
226 87362 : }
227 :
228 : TyTy::BaseType *
229 43447 : TypeCheckContext::peek_expected_type () const
230 : {
231 43447 : if (expected_type_stack.empty ())
232 : return nullptr;
233 43150 : return expected_type_stack.back ();
234 : }
235 :
236 : StackedContexts<TypeCheckBlockContextItem> &
237 123352 : TypeCheckContext::block_context ()
238 : {
239 123352 : return block_stack;
240 : }
241 :
242 : void
243 4749 : TypeCheckContext::iterate (std::function<bool (HirId, TyTy::BaseType *)> cb)
244 : {
245 590766 : for (auto it = resolved.begin (); it != resolved.end (); it++)
246 : {
247 586017 : if (!cb (it->first, it->second))
248 4749 : return;
249 : }
250 : }
251 :
252 : bool
253 183 : TypeCheckContext::have_loop_context () const
254 : {
255 183 : return !loop_type_stack.empty ();
256 : }
257 :
258 : void
259 190 : TypeCheckContext::push_new_loop_context (HirId id, location_t locus)
260 : {
261 190 : TyTy::BaseType *infer_var
262 : = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL,
263 190 : TyTy::InferType::TypeHint::Default (), locus);
264 190 : loop_type_stack.push_back (infer_var);
265 190 : }
266 :
267 : void
268 95 : TypeCheckContext::push_new_while_loop_context (HirId id)
269 : {
270 95 : TyTy::BaseType *infer_var = new TyTy::ErrorType (id);
271 95 : loop_type_stack.push_back (infer_var);
272 95 : }
273 :
274 : TyTy::BaseType *
275 310 : TypeCheckContext::peek_loop_context ()
276 : {
277 310 : return loop_type_stack.back ();
278 : }
279 :
280 : TyTy::BaseType *
281 285 : TypeCheckContext::pop_loop_context ()
282 : {
283 285 : auto back = peek_loop_context ();
284 285 : loop_type_stack.pop_back ();
285 285 : return back;
286 : }
287 :
288 : void
289 21 : TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val)
290 : {
291 21 : loop_type_stack.pop_back ();
292 21 : loop_type_stack.push_back (val);
293 21 : }
294 :
295 : bool
296 34420 : TypeCheckContext::find_matching_impl_trait_frame (
297 : const TraitReference &tref, TyTy::BaseType &self,
298 : struct ImplTraitContextFrame *find) const
299 : {
300 34420 : if (!have_impl_trait_context ())
301 : return false;
302 :
303 104464 : for (auto it = impl_trait_frame_stack.rbegin ();
304 104464 : it != impl_trait_frame_stack.rend (); ++it)
305 : {
306 97007 : const auto &i = *it;
307 97007 : if (!i.trait->is_equal (tref))
308 30781 : continue;
309 :
310 66226 : TyTy::BaseType *resolved_self = &self;
311 66226 : bool unresolved_trait_self = false;
312 66226 : if (auto param = self.try_as<TyTy::ParamType> ())
313 : {
314 85 : if (param->can_resolve ())
315 84 : resolved_self = param->resolve ();
316 : else
317 1 : unresolved_trait_self = param->is_implicit_self_trait ();
318 : }
319 :
320 : // Select an existing impl context without inferring a new Self
321 : // binding
322 66310 : bool compatible_self = unresolved_trait_self;
323 85 : if (!compatible_self)
324 : {
325 66225 : auto res
326 66225 : = unify_site_and (UNKNOWN_HIRID, TyTy::TyWithLocation (i.self),
327 66225 : TyTy::TyWithLocation (resolved_self),
328 : UNDEF_LOCATION, false /* emit_errors */,
329 : false /* commit */, false /* infer */,
330 : true /* cleanup */, false /* check_bounds */);
331 66225 : compatible_self = res->get_kind () != TyTy::TypeKind::ERROR;
332 : }
333 :
334 66225 : if (compatible_self)
335 : {
336 25096 : *find = i;
337 25096 : return true;
338 : }
339 : }
340 :
341 : return false;
342 : }
343 :
344 : bool
345 34420 : TypeCheckContext::have_impl_trait_context () const
346 : {
347 34420 : return !impl_trait_frame_stack.empty ();
348 : }
349 :
350 : void
351 38533 : TypeCheckContext::push_impl_trait_context (struct ImplTraitContextFrame frame)
352 : {
353 38533 : impl_trait_frame_stack.push_back (frame);
354 38533 : }
355 :
356 : struct ImplTraitContextFrame
357 38467 : TypeCheckContext::pop_impl_trait_context ()
358 : {
359 38467 : auto back = peek_impl_trait_context ();
360 38467 : impl_trait_frame_stack.pop_back ();
361 38467 : return back;
362 : }
363 :
364 : struct ImplTraitContextFrame
365 38467 : TypeCheckContext::peek_impl_trait_context ()
366 : {
367 38467 : return impl_trait_frame_stack.back ();
368 : }
369 :
370 : void
371 5835 : TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref)
372 : {
373 5835 : rust_assert (trait_context.find (id) == trait_context.end ());
374 5835 : trait_context.emplace (id, std::move (ref));
375 5835 : }
376 :
377 : bool
378 2118196 : TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref)
379 : {
380 2118196 : auto it = trait_context.find (id);
381 2118196 : if (it == trait_context.end ())
382 : return false;
383 :
384 2093470 : *ref = &it->second;
385 2093470 : return true;
386 : }
387 :
388 : bool
389 8823 : TypeCheckContext::insert_associated_trait_impl (
390 : HirId id, AssociatedImplTrait &&associated)
391 : {
392 8823 : auto it = associated_impl_traits.find (id);
393 8823 : if (it != associated_impl_traits.end ())
394 : {
395 : return false;
396 : }
397 8794 : associated_impl_traits.emplace (id, std::move (associated));
398 8794 : return true;
399 : }
400 :
401 : bool
402 39985 : TypeCheckContext::lookup_associated_trait_impl (
403 : HirId id, AssociatedImplTrait **associated)
404 : {
405 39985 : auto it = associated_impl_traits.find (id);
406 39985 : if (it == associated_impl_traits.end ())
407 : return false;
408 :
409 19971 : *associated = &it->second;
410 19971 : return true;
411 : }
412 :
413 : void
414 0 : TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping)
415 : {
416 0 : associated_type_mappings[id] = mapping;
417 0 : }
418 :
419 : void
420 0 : TypeCheckContext::clear_associated_type_mapping (HirId id)
421 : {
422 0 : auto it = associated_type_mappings.find (id);
423 0 : if (it != associated_type_mappings.end ())
424 0 : associated_type_mappings.erase (it);
425 0 : }
426 :
427 : // lookup any associated type mappings, the out parameter of mapping is
428 : // allowed to be nullptr which allows this interface to do a simple does exist
429 : // check
430 : bool
431 0 : TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping)
432 : {
433 0 : auto it = associated_type_mappings.find (id);
434 0 : if (it == associated_type_mappings.end ())
435 : return false;
436 :
437 0 : if (mapping != nullptr)
438 0 : *mapping = it->second;
439 :
440 : return true;
441 : }
442 :
443 : void
444 8823 : TypeCheckContext::insert_associated_impl_mapping (HirId trait_id,
445 : TyTy::BaseType *impl_type,
446 : HirId impl_id)
447 : {
448 8823 : auto it = associated_traits_to_impls.find (trait_id);
449 8823 : if (it == associated_traits_to_impls.end ())
450 : {
451 2200 : associated_traits_to_impls[trait_id] = {};
452 : }
453 :
454 8823 : associated_traits_to_impls[trait_id].emplace_back (impl_type, impl_id);
455 8823 : }
456 :
457 : bool
458 0 : TypeCheckContext::lookup_associated_impl_mapping_for_self (HirId trait_id,
459 : TyTy::BaseType *self,
460 : HirId *mapping)
461 : {
462 0 : auto it = associated_traits_to_impls.find (trait_id);
463 0 : if (it == associated_traits_to_impls.end ())
464 : return false;
465 :
466 0 : for (auto &item : it->second)
467 : {
468 0 : if (types_compatable (TyTy::TyWithLocation (item.first),
469 0 : TyTy::TyWithLocation (self), UNKNOWN_LOCATION,
470 : false))
471 : {
472 0 : *mapping = item.second;
473 0 : return true;
474 : }
475 : }
476 : return false;
477 : }
478 :
479 : void
480 59586 : TypeCheckContext::insert_autoderef_mappings (
481 : HirId id, std::vector<Adjustment> &&adjustments)
482 : {
483 59586 : autoderef_mappings.emplace (id, std::move (adjustments));
484 59586 : }
485 :
486 : bool
487 56246 : TypeCheckContext::lookup_autoderef_mappings (
488 : HirId id, std::vector<Adjustment> **adjustments)
489 : {
490 56246 : auto it = autoderef_mappings.find (id);
491 56246 : if (it == autoderef_mappings.end ())
492 : return false;
493 :
494 41499 : *adjustments = &it->second;
495 41499 : return true;
496 : }
497 :
498 : void
499 6924 : TypeCheckContext::insert_cast_autoderef_mappings (
500 : HirId id, std::vector<Adjustment> &&adjustments)
501 : {
502 6924 : cast_autoderef_mappings.emplace (id, std::move (adjustments));
503 6924 : }
504 :
505 : bool
506 5503 : TypeCheckContext::lookup_cast_autoderef_mappings (
507 : HirId id, std::vector<Adjustment> **adjustments)
508 : {
509 5503 : auto it = cast_autoderef_mappings.find (id);
510 5503 : if (it == cast_autoderef_mappings.end ())
511 : return false;
512 :
513 5503 : *adjustments = &it->second;
514 5503 : return true;
515 : }
516 :
517 : void
518 4774 : TypeCheckContext::insert_variant_definition (HirId id, HirId variant)
519 : {
520 4774 : auto it = variants.find (id);
521 4774 : rust_assert (it == variants.end ());
522 :
523 4774 : variants[id] = variant;
524 4774 : }
525 :
526 : bool
527 10318 : TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant)
528 : {
529 10318 : auto it = variants.find (id);
530 10318 : if (it == variants.end ())
531 : return false;
532 :
533 10311 : *variant = it->second;
534 10311 : return true;
535 : }
536 :
537 : void
538 2223 : TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site)
539 : {
540 2223 : auto it = operator_overloads.find (id);
541 2223 : rust_assert (it == operator_overloads.end ());
542 :
543 2223 : operator_overloads[id] = call_site;
544 2223 : }
545 :
546 : bool
547 34399 : TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call)
548 : {
549 34399 : auto it = operator_overloads.find (id);
550 34399 : if (it == operator_overloads.end ())
551 : return false;
552 :
553 2478 : *call = it->second;
554 2478 : return true;
555 : }
556 :
557 : void
558 6593 : TypeCheckContext::insert_deferred_operator_overload (
559 : DeferredOpOverload deferred)
560 : {
561 6593 : HirId expr_id = deferred.expr_id;
562 6593 : deferred_operator_overloads.emplace (std::make_pair (expr_id, deferred));
563 6593 : }
564 :
565 : bool
566 0 : TypeCheckContext::lookup_deferred_operator_overload (
567 : HirId id, DeferredOpOverload *deferred)
568 : {
569 0 : auto it = deferred_operator_overloads.find (id);
570 0 : if (it == deferred_operator_overloads.end ())
571 : return false;
572 :
573 0 : *deferred = it->second;
574 0 : return true;
575 : }
576 :
577 : void
578 0 : TypeCheckContext::iterate_deferred_operator_overloads (
579 : std::function<bool (HirId, DeferredOpOverload &)> cb)
580 : {
581 0 : for (auto it = deferred_operator_overloads.begin ();
582 0 : it != deferred_operator_overloads.end (); it++)
583 : {
584 0 : if (!cb (it->first, it->second))
585 0 : return;
586 : }
587 : }
588 :
589 : void
590 78364 : TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status)
591 : {
592 78364 : unconstrained[id] = status;
593 78364 : }
594 :
595 : bool
596 109001 : TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result)
597 : {
598 109001 : auto it = unconstrained.find (id);
599 109001 : bool found = it != unconstrained.end ();
600 109001 : if (!found)
601 : return false;
602 :
603 30637 : *result = it->second;
604 30637 : return true;
605 : }
606 :
607 : void
608 126205 : TypeCheckContext::insert_resolved_predicate (
609 : HirId id, const TyTy::TypeBoundPredicate &predicate)
610 : {
611 : // auto it = predicates.find (id);
612 : // rust_assert (it == predicates.end ());
613 :
614 126205 : predicates.emplace (id, predicate);
615 126205 : }
616 :
617 : bool
618 192234 : TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result)
619 : {
620 192234 : auto it = predicates.find (id);
621 192234 : bool found = it != predicates.end ();
622 192234 : if (!found)
623 : return false;
624 :
625 59257 : *result = it->second;
626 59257 : return true;
627 : }
628 :
629 : std::vector<TyTy::TypeBoundPredicate>
630 20451 : TypeCheckContext::predicates_for_type (const TyTy::BaseType *receiver) const
631 : {
632 20451 : std::vector<TyTy::TypeBoundPredicate> result
633 20451 : = receiver->get_specified_bounds ();
634 :
635 63200722 : for (const auto &entry : predicates)
636 : {
637 63180271 : const auto &predicate = entry.second;
638 63180271 : const auto &args = predicate.get_substitution_arguments ();
639 63180271 : if (args.get_mappings ().empty ())
640 63180266 : continue;
641 :
642 63180271 : TyTy::BaseType *bound_self = args.get_mappings ().front ().get_tyty ();
643 63644892 : if (bound_self == nullptr
644 63180271 : || bound_self->get_kind () == TyTy::TypeKind::ERROR)
645 464621 : continue;
646 :
647 62715650 : if (receiver->get_kind () != TyTy::TypeKind::PROJECTION
648 62715650 : || bound_self->get_kind () != TyTy::TypeKind::PROJECTION)
649 62715642 : continue;
650 :
651 8 : const auto *receiver_projection
652 : = static_cast<const TyTy::ProjectionType *> (receiver);
653 8 : const auto *bound_projection
654 : = static_cast<const TyTy::ProjectionType *> (bound_self);
655 :
656 8 : DefId receiver_item_defid = receiver_projection->get_item_defid ();
657 8 : DefId bound_item_defid = bound_projection->get_item_defid ();
658 8 : bool same_item = receiver_item_defid == bound_item_defid;
659 8 : if (!same_item)
660 1 : continue;
661 :
662 7 : DefId receiver_trait_defid
663 7 : = receiver_projection->get_trait_ref ()->get_mappings ().get_defid ();
664 7 : DefId bound_trait_defid
665 7 : = bound_projection->get_trait_ref ()->get_mappings ().get_defid ();
666 7 : bool same_trait = receiver_trait_defid == bound_trait_defid;
667 7 : if (!same_trait)
668 0 : continue;
669 :
670 : // Compatibility can unify unrelated parameters. Match their declaration
671 : // identities instead; copies and synthetic bound parameters retain them.
672 7 : const TyTy::BaseType *receiver_self = receiver_projection->get_self ();
673 7 : const TyTy::BaseType *bound_self_ty = bound_projection->get_self ();
674 7 : const auto *receiver_param
675 7 : = receiver_self->try_as<const TyTy::ParamType> ();
676 7 : const auto *bound_param = bound_self_ty->try_as<const TyTy::ParamType> ();
677 :
678 7 : bool either_self_is_param
679 7 : = receiver_param != nullptr || bound_param != nullptr;
680 7 : if (either_self_is_param)
681 : {
682 7 : bool both_selfs_are_param
683 : = receiver_param != nullptr && bound_param != nullptr;
684 7 : bool same_declaration
685 : = both_selfs_are_param
686 7 : && receiver_param->get_decl_id () == bound_param->get_decl_id ();
687 2 : if (!same_declaration)
688 2 : continue;
689 : }
690 : else
691 : {
692 0 : bool selfs_compatible = types_compatable (
693 0 : TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver_self)),
694 0 : TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (bound_self_ty)),
695 : UNKNOWN_LOCATION, false, false);
696 0 : if (!selfs_compatible)
697 0 : continue;
698 : }
699 :
700 5 : bool projections_compatible = types_compatable (
701 5 : TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver)),
702 5 : TyTy::TyWithLocation (bound_self), UNKNOWN_LOCATION, false, false);
703 5 : if (projections_compatible)
704 5 : result.push_back (predicate);
705 : }
706 :
707 20451 : return result;
708 : }
709 :
710 : void
711 100881 : TypeCheckContext::insert_query (HirId id)
712 : {
713 100881 : querys_in_progress.insert (id);
714 100881 : }
715 :
716 : void
717 100650 : TypeCheckContext::query_completed (HirId id)
718 : {
719 100650 : querys_in_progress.erase (id);
720 100650 : }
721 :
722 : bool
723 178808 : TypeCheckContext::query_in_progress (HirId id) const
724 : {
725 178808 : return querys_in_progress.find (id) != querys_in_progress.end ();
726 : }
727 :
728 : void
729 5850 : TypeCheckContext::insert_trait_query (DefId id)
730 : {
731 5850 : trait_queries_in_progress.insert (id);
732 5850 : }
733 :
734 : void
735 5850 : TypeCheckContext::trait_query_completed (DefId id)
736 : {
737 5850 : trait_queries_in_progress.erase (id);
738 5850 : }
739 :
740 : bool
741 5854 : TypeCheckContext::trait_query_in_progress (DefId id) const
742 : {
743 5854 : return trait_queries_in_progress.find (id)
744 5854 : != trait_queries_in_progress.end ();
745 : }
746 :
747 : Lifetime
748 15548 : TypeCheckContext::intern_lifetime (const HIR::Lifetime &lifetime)
749 : {
750 15548 : if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
751 : {
752 15547 : auto maybe_interned = lookup_lifetime (lifetime);
753 15547 : if (maybe_interned)
754 15372 : return *maybe_interned;
755 :
756 175 : auto interned = next_lifetime_index.next ();
757 175 : lifetime_name_interner[lifetime.get_name ()] = interned;
758 175 : return interned;
759 : }
760 1 : if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
761 : {
762 1 : return next_lifetime_index.next ();
763 : }
764 0 : if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
765 : {
766 0 : return Lifetime::static_lifetime ();
767 : }
768 0 : rust_unreachable ();
769 : }
770 :
771 : tl::optional<Lifetime>
772 80708 : TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const
773 : {
774 80708 : if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
775 : {
776 28625 : if (lifetime.get_name () == "static")
777 : {
778 1 : rich_location r (line_table, lifetime.get_locus ());
779 1 : r.add_fixit_insert_after (lifetime.get_locus (),
780 : "static is a reserved lifetime name");
781 1 : rust_error_at (r, ErrorCode::E0262,
782 : "invalid lifetime parameter name: %qs",
783 1 : lifetime.get_name ().c_str ());
784 1 : return tl::nullopt;
785 1 : }
786 28624 : const auto name = lifetime.get_name ();
787 28624 : auto it = lifetime_name_interner.find (name);
788 28624 : if (it == lifetime_name_interner.end ())
789 179 : return tl::nullopt;
790 28445 : return it->second;
791 28624 : }
792 52083 : if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
793 : {
794 51909 : return Lifetime::anonymous_lifetime ();
795 : }
796 174 : if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
797 : {
798 174 : return Lifetime::static_lifetime ();
799 : }
800 0 : rust_unreachable ();
801 : }
802 :
803 : WARN_UNUSED_RESULT tl::optional<TyTy::Region>
804 65161 : TypeCheckContext::lookup_and_resolve_lifetime (
805 : const HIR::Lifetime &lifetime) const
806 : {
807 65161 : auto maybe_interned = lookup_lifetime (lifetime);
808 65161 : if (!maybe_interned)
809 5 : return tl::nullopt;
810 :
811 65156 : return get_lifetime_resolver ().resolve (maybe_interned.value ());
812 : }
813 : void
814 239 : TypeCheckContext::intern_and_insert_lifetime (const HIR::Lifetime &lifetime)
815 : {
816 239 : get_lifetime_resolver ().insert_mapping (intern_lifetime (lifetime));
817 239 : }
818 :
819 : WARN_UNUSED_RESULT std::vector<TyTy::Region>
820 171322 : TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
821 : {
822 171322 : std::vector<TyTy::Region> regions;
823 177865 : for (const auto &lifetime : args.get_lifetime_args ())
824 : {
825 6544 : auto resolved = lookup_and_resolve_lifetime (lifetime);
826 6544 : if (!resolved)
827 : {
828 1 : rust_error_at (lifetime.get_locus (), "unresolved lifetime");
829 1 : return {};
830 : }
831 6543 : regions.push_back (*resolved);
832 : }
833 171321 : return regions;
834 171322 : }
835 :
836 : bool
837 15 : TypeCheckContext::compute_ambigious_op_overload (HirId id,
838 : DeferredOpOverload &op)
839 : {
840 15 : rust_debug ("attempting resolution of deferred operator overload");
841 :
842 15 : TyTy::BaseType *lhs = nullptr;
843 15 : bool ok = lookup_type (op.op.get_lvalue_mappings ().get_hirid (), &lhs);
844 15 : rust_assert (ok);
845 :
846 15 : TyTy::BaseType *rhs = nullptr;
847 15 : if (op.op.has_rvalue_mappings ())
848 : {
849 15 : bool ok = lookup_type (op.op.get_rvalue_mappings ().get_hirid (), &rhs);
850 15 : rust_assert (ok);
851 : }
852 :
853 15 : TyTy::BaseType *current_result = op.result_type.get_tyty ();
854 15 : rust_assert (current_result != nullptr);
855 :
856 15 : rust_debug ("deferred operator expr=%u lhs=%s rhs=%s "
857 : "stored-result=%s current-result=%s result-ref=%u ty-ref=%u",
858 : id, lhs->debug_str ().c_str (),
859 : rhs == nullptr ? "<none>" : rhs->debug_str ().c_str (),
860 : current_result->debug_str ().c_str (),
861 : current_result->debug_str ().c_str (), op.result_type.get_ref (),
862 : current_result->get_ty_ref ());
863 :
864 15 : TyTy::BaseType *resolved
865 15 : = TypeCheckExpr::ResolveOpOverload (op.lang_item_type, op.op, lhs, rhs,
866 15 : op.specified_segment, current_result);
867 15 : if (resolved == nullptr)
868 : return false;
869 :
870 15 : TyTy::BaseType *result
871 15 : = unify_site (id, TyTy::TyWithLocation (current_result),
872 15 : TyTy::TyWithLocation (resolved), op.op.get_locus ());
873 15 : rust_assert (result != nullptr);
874 15 : rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
875 :
876 : return true;
877 : }
878 :
879 : void
880 4749 : TypeCheckContext::compute_inference_variables (bool emit_error)
881 : {
882 9498 : auto resolve_deferred_operator_overloads = [this] () {
883 9513 : bool progress;
884 9513 : do
885 : {
886 9513 : progress = false;
887 9513 : for (auto it = deferred_operator_overloads.begin ();
888 9528 : it != deferred_operator_overloads.end ();)
889 : {
890 15 : if (compute_ambigious_op_overload (it->first, it->second))
891 : {
892 15 : it = deferred_operator_overloads.erase (it);
893 15 : progress = true;
894 : }
895 : else
896 0 : ++it;
897 : }
898 : }
899 : while (progress);
900 14247 : };
901 :
902 4749 : resolve_deferred_operator_overloads ();
903 :
904 4749 : iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
905 586017 : return compute_infer_var (id, ty, emit_error);
906 : });
907 :
908 4749 : resolve_deferred_operator_overloads ();
909 4749 : }
910 :
911 : bool
912 586017 : TypeCheckContext::compute_infer_var (HirId id, TyTy::BaseType *ty,
913 : bool emit_error)
914 : {
915 586017 : auto &mappings = Analysis::Mappings::get ();
916 :
917 : // nothing to do
918 586017 : if (ty->get_kind () != TyTy::TypeKind::INFER)
919 : return true;
920 :
921 1671 : TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
922 1671 : TyTy::BaseType *default_type;
923 :
924 1671 : TyTy::BaseType *current = TyTy::TyVar (ty->get_ref ()).get_tyty ();
925 1671 : rust_assert (current != nullptr);
926 :
927 1671 : rust_debug_loc (mappings.lookup_location (id),
928 : "trying to default infer-var id=%u ref=%u ty-ref=%u "
929 : "stored=%s current=%s",
930 : id, ty->get_ref (), ty->get_ty_ref (),
931 : infer_var->debug_str ().c_str (),
932 : current->debug_str ().c_str ());
933 1671 : bool ok = infer_var->default_type (&default_type);
934 1671 : if (!ok)
935 : {
936 8 : if (emit_error)
937 8 : rust_error_at (mappings.lookup_location (id), ErrorCode::E0282,
938 : "type annotations needed");
939 : return true;
940 : }
941 :
942 1663 : rust_debug_loc (mappings.lookup_location (id), "default type selected: %s",
943 : default_type->debug_str ().c_str ());
944 :
945 1663 : auto result
946 1663 : = unify_site (id, TyTy::TyWithLocation (ty),
947 1663 : TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
948 1663 : rust_assert (result);
949 1663 : rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
950 1663 : result->set_ref (id);
951 1663 : insert_implicit_type (id, result);
952 :
953 1663 : return true;
954 : }
955 :
956 : TyTy::VarianceAnalysis::CrateCtx &
957 12357 : TypeCheckContext::get_variance_analysis_ctx ()
958 : {
959 12357 : return variance_analysis_ctx;
960 : }
961 :
962 : // TypeCheckContextItem
963 :
964 18967 : TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}
965 :
966 11127 : TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block,
967 : HIR::Function *item)
968 11127 : : impl_item ({impl_block, item})
969 11127 : {}
970 :
971 926 : TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item)
972 926 : : trait_item (trait_item)
973 926 : {}
974 :
975 7378 : TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item)
976 7378 : : type (ItemType::ITEM), item (item)
977 7378 : {}
978 :
979 11127 : TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock &impl_block,
980 : HIR::Function *item)
981 11127 : : type (ItemType::IMPL_ITEM), item (&impl_block, item)
982 11127 : {}
983 :
984 926 : TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item)
985 926 : : type (ItemType::TRAIT_ITEM), item (trait_item)
986 926 : {}
987 :
988 48600 : TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other)
989 48600 : : type (other.type), item (other.item)
990 : {
991 48600 : switch (other.type)
992 : {
993 17294 : case ITEM:
994 17294 : item.item = other.item.item;
995 17294 : break;
996 :
997 29816 : case IMPL_ITEM:
998 29816 : item.impl_item = other.item.impl_item;
999 29816 : break;
1000 :
1001 1490 : case TRAIT_ITEM:
1002 1490 : item.trait_item = other.item.trait_item;
1003 1490 : break;
1004 :
1005 0 : case ERROR:
1006 0 : item.item = nullptr;
1007 0 : break;
1008 : }
1009 48600 : }
1010 :
1011 11589 : TypeCheckContextItem::TypeCheckContextItem ()
1012 11589 : : type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr))
1013 11589 : {}
1014 :
1015 : TypeCheckContextItem &
1016 11541 : TypeCheckContextItem::operator= (const TypeCheckContextItem &other)
1017 : {
1018 11541 : type = other.type;
1019 11541 : switch (other.type)
1020 : {
1021 704 : case ITEM:
1022 704 : item.item = other.item.item;
1023 704 : break;
1024 :
1025 10723 : case IMPL_ITEM:
1026 10723 : item.impl_item = other.item.impl_item;
1027 10723 : break;
1028 :
1029 114 : case TRAIT_ITEM:
1030 114 : item.trait_item = other.item.trait_item;
1031 114 : break;
1032 :
1033 0 : case ERROR:
1034 0 : item.item = nullptr;
1035 0 : break;
1036 : }
1037 :
1038 11541 : return *this;
1039 : }
1040 :
1041 : TypeCheckContextItem
1042 11589 : TypeCheckContextItem::get_error ()
1043 : {
1044 11589 : return TypeCheckContextItem ();
1045 : }
1046 :
1047 : bool
1048 0 : TypeCheckContextItem::is_error () const
1049 : {
1050 0 : return type == ERROR;
1051 : }
1052 :
1053 : HIR::Function *
1054 63 : TypeCheckContextItem::get_item ()
1055 : {
1056 63 : rust_assert (get_type () == ItemType::ITEM);
1057 63 : return item.item;
1058 : }
1059 :
1060 : std::pair<HIR::ImplBlock *, HIR::Function *> &
1061 1606 : TypeCheckContextItem::get_impl_item ()
1062 : {
1063 1606 : rust_assert (get_type () == ItemType::IMPL_ITEM);
1064 1606 : return item.impl_item;
1065 : }
1066 :
1067 : HIR::TraitItemFunc *
1068 1 : TypeCheckContextItem::get_trait_item ()
1069 : {
1070 1 : rust_assert (get_type () == ItemType::TRAIT_ITEM);
1071 1 : return item.trait_item;
1072 : }
1073 :
1074 : TypeCheckContextItem::ItemType
1075 639492 : TypeCheckContextItem::get_type () const
1076 : {
1077 639492 : return type;
1078 : }
1079 :
1080 : TyTy::FnType *
1081 64 : TypeCheckContextItem::get_context_type ()
1082 : {
1083 64 : auto &context = *TypeCheckContext::get ();
1084 :
1085 64 : HirId reference = UNKNOWN_HIRID;
1086 64 : switch (get_type ())
1087 : {
1088 63 : case ITEM:
1089 63 : reference = get_item ()->get_mappings ().get_hirid ();
1090 63 : break;
1091 :
1092 0 : case IMPL_ITEM:
1093 0 : reference = get_impl_item ().second->get_mappings ().get_hirid ();
1094 0 : break;
1095 :
1096 1 : case TRAIT_ITEM:
1097 1 : reference = get_trait_item ()->get_mappings ().get_hirid ();
1098 1 : break;
1099 :
1100 0 : case ERROR:
1101 0 : rust_unreachable ();
1102 : return nullptr;
1103 : }
1104 :
1105 64 : rust_assert (reference != UNKNOWN_HIRID);
1106 :
1107 64 : TyTy::BaseType *lookup = nullptr;
1108 64 : bool ok = context.lookup_type (reference, &lookup);
1109 64 : rust_assert (ok);
1110 64 : rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
1111 64 : return static_cast<TyTy::FnType *> (lookup);
1112 : }
1113 :
1114 : DefId
1115 618608 : TypeCheckContextItem::get_defid () const
1116 : {
1117 618608 : switch (get_type ())
1118 : {
1119 942 : case ITEM:
1120 942 : return item.item->get_mappings ().get_defid ();
1121 :
1122 617192 : case IMPL_ITEM:
1123 617192 : return item.impl_item.second->get_mappings ().get_defid ();
1124 :
1125 114 : case TRAIT_ITEM:
1126 114 : return item.trait_item->get_mappings ().get_defid ();
1127 :
1128 360 : case ERROR:
1129 360 : return UNKNOWN_DEFID;
1130 : }
1131 :
1132 0 : return UNKNOWN_DEFID;
1133 : }
1134 :
1135 : // TypeCheckBlockContextItem
1136 :
1137 53478 : TypeCheckBlockContextItem::Item::Item (HIR::ImplBlock *b) : block (b) {}
1138 :
1139 5835 : TypeCheckBlockContextItem::Item::Item (HIR::Trait *t) : trait (t) {}
1140 :
1141 53478 : TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::ImplBlock *block)
1142 53478 : : type (TypeCheckBlockContextItem::ItemType::IMPL_BLOCK), item (block)
1143 53478 : {}
1144 :
1145 5835 : TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::Trait *trait)
1146 5835 : : type (TypeCheckBlockContextItem::ItemType::TRAIT), item (trait)
1147 5835 : {}
1148 :
1149 : bool
1150 0 : TypeCheckBlockContextItem::is_impl_block () const
1151 : {
1152 0 : return type == IMPL_BLOCK;
1153 : }
1154 :
1155 : bool
1156 2473 : TypeCheckBlockContextItem::is_trait_block () const
1157 : {
1158 2473 : return type == TRAIT;
1159 : }
1160 :
1161 : HIR::ImplBlock &
1162 166 : TypeCheckBlockContextItem::get_impl_block ()
1163 : {
1164 166 : return *(item.block);
1165 : }
1166 :
1167 : HIR::Trait &
1168 2307 : TypeCheckBlockContextItem::get_trait ()
1169 : {
1170 2307 : return *(item.trait);
1171 : }
1172 :
1173 : } // namespace Resolver
1174 : } // namespace Rust
|