Debian Codegraph @

gcc/rust/typecheck/rust-tyty.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-tyty.h"
20
21 #include "optional.h"
22 #include "rust-tyty-visitor.h"
23 #include "rust-hir-map.h"
24 #include "rust-location.h"
25 #include "rust-linemap.h"
26
27 #include "rust-substitution-mapper.h"
28 #include "rust-hir-trait-reference.h"
29 #include "rust-hir-trait-resolve.h"
30 #include "rust-tyty-cmp.h"
31 #include "rust-type-util.h"
32 #include "rust-hir-type-bounds.h"
33
34 #include "options.h"
35 #include "rust-system.h"
36
37 namespace Rust {
38 namespace TyTy {
39
40 std::string
41 TypeKindFormat::to_string (TypeKind kind)
42 {
43 switch (kind)
44 {
45 case TypeKind::INFER:
46 return "Infer";
47
48 case TypeKind::ADT:
49 return "ADT";
50
51 case TypeKind::STR:
52 return "STR";
53
54 case TypeKind::REF:
55 return "REF";
56
57 case TypeKind::POINTER:
58 return "POINTER";
59
60 case TypeKind::PARAM:
61 return "PARAM";
62
63 case TypeKind::ARRAY:
64 return "ARRAY";
65
66 case TypeKind::SLICE:
67 return "SLICE";
68
69 case TypeKind::FNDEF:
70 return "FnDef";
71
72 case TypeKind::FNPTR:
73 return "FnPtr";
74
75 case TypeKind::TUPLE:
76 return "Tuple";
77
78 case TypeKind::BOOL:
79 return "Bool";
80
81 case TypeKind::CHAR:
82 return "Char";
83
84 case TypeKind::INT:
85 return "Int";
86
87 case TypeKind::UINT:
88 return "Uint";
89
90 case TypeKind::FLOAT:
91 return "Float";
92
93 case TypeKind::USIZE:
94 return "Usize";
95
96 case TypeKind::ISIZE:
97 return "Isize";
98
99 case TypeKind::NEVER:
100 return "Never";
101
102 case TypeKind::PLACEHOLDER:
103 return "Placeholder";
104
105 case TypeKind::PROJECTION:
106 return "Projection";
107
108 case TypeKind::DYNAMIC:
109 return "Dynamic";
110
111 case TypeKind::CLOSURE:
112 return "Closure";
113
114 case TypeKind::OPAQUE:
115 return "Opaque";
116
117 case TypeKind::ERROR:
118 return "ERROR";
119 }
120 rust_unreachable ();
121 }
122
123 bool
124 is_primitive_type_kind (TypeKind kind)
125 {
126 switch (kind)
127 {
128 case TypeKind::BOOL:
129 case TypeKind::CHAR:
130 case TypeKind::INT:
131 case TypeKind::UINT:
132 case TypeKind::ISIZE:
133 case TypeKind::USIZE:
134 case TypeKind::FLOAT:
135 case TypeKind::NEVER:
136 case TypeKind::STR:
137 return true;
138 default:
139 return false;
140 }
141 }
142
143 // BASE TYPE
144
145 BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
146 std::set<HirId> refs)
147 : TypeBoundsMappings ({}), kind (kind), ref (ref), ty_ref (ty_ref),
148 orig_ref (ref), combined (refs), ident (ident),
149 mappings (Analysis::Mappings::get ())
150 {}
151
152 BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
153 std::vector<TypeBoundPredicate> specified_bounds,
154 std::set<HirId> refs)
155 : TypeBoundsMappings (specified_bounds), kind (kind), ref (ref),
156 ty_ref (ty_ref), orig_ref (ref), combined (refs), ident (ident),
157 mappings (Analysis::Mappings::get ())
158 {}
159
160 BaseType::~BaseType () {}
161
162 HirId
163 BaseType::get_ref () const
164 {
165 return ref;
166 }
167
168 void
169 BaseType::set_ref (HirId id)
170 {
171 if (id != ref)
172 append_reference (ref);
173 ref = id;
174 }
175
176 HirId
177 BaseType::get_ty_ref () const
178 {
179 return ty_ref;
180 }
181
182 void
183 BaseType::set_ty_ref (HirId id)
184 {
185 ty_ref = id;
186 }
187 HirId
188 BaseType::get_orig_ref () const
189 {
190 return orig_ref;
191 }
192
193 bool
194 BaseType::is_equal (const BaseType &other) const
195 {
196 return get_kind () == other.get_kind ();
197 }
198
199 bool
200 BaseType::is_unit () const
201 {
202 const TyTy::BaseType *x = destructure ();
203 switch (x->get_kind ())
204 {
205 case PARAM:
206 case PROJECTION:
207 case PLACEHOLDER:
208 case FNPTR:
209 case FNDEF:
210 case ARRAY:
211 case SLICE:
212 case POINTER:
213 case REF:
214 case CLOSURE:
215 case INFER:
216 case BOOL:
217 case CHAR:
218 case INT:
219 case UINT:
220 case FLOAT:
221 case USIZE:
222 case ISIZE:
223 case OPAQUE:
224 case STR:
225 case DYNAMIC:
226 case ERROR:
227 return false;
228
229 // FIXME ! is coerceable to () so we need to fix that
230 case NEVER:
231 return true;
232
233 case TUPLE: {
234 return x->as<const TupleType> ()->num_fields () == 0;
235 }
236
237 case ADT: {
238 auto adt = x->as<const ADTType> ();
239 if (adt->is_enum ())
240 return false;
241
242 for (const auto &variant : adt->get_variants ())
243 {
244 if (variant->num_fields () > 0)
245 return false;
246 }
247
248 return true;
249 }
250 }
251 return false;
252 }
253
254 TypeKind
255 BaseType::get_kind () const
256 {
257 return kind;
258 }
259
260 std::set<HirId>
261 BaseType::get_combined_refs () const
262 {
263 return combined;
264 }
265
266 void
267 BaseType::append_reference (HirId id)
268 {
269 combined.insert (id);
270 }
271
272 const RustIdent &
273 BaseType::get_ident () const
274 {
275 return ident;
276 }
277
278 location_t
279 BaseType::get_locus () const
280 {
281 return ident.locus;
282 }
283
284 // FIXME this is missing locus
285 bool
286 BaseType::satisfies_bound (const TypeBoundPredicate &predicate,
287 bool emit_error) const
288 {
289 const Resolver::TraitReference *query = predicate.get ();
290 for (const auto &bound : specified_bounds)
291 {
292 const Resolver::TraitReference *item = bound.get ();
293 if (item->satisfies_bound (*query))
294 return true;
295 }
296
297 if (destructure ()->is<InferType> ())
298 return true;
299
300 bool satisfied = false;
301 auto probed = Resolver::TypeBoundsProbe::Probe (this);
302 for (const auto &b : probed)
303 {
304 const Resolver::TraitReference *bound = b.first;
305 if (bound->satisfies_bound (*query))
306 {
307 satisfied = true;
308 break;
309 }
310 }
311
312 if (!satisfied)
313 return false;
314
315 for (const auto &b : probed)
316 {
317 const Resolver::TraitReference *bound = b.first;
318 if (!bound->is_equal (*query))
319 continue;
320
321 // builtin ones have no impl-block this needs fixed and use a builtin node
322 // of somekind
323 if (b.second == nullptr)
324 return true;
325
326 // need to check that associated types can match as well
327 const HIR::ImplBlock &impl = *(b.second);
328 for (const auto &item : impl.get_impl_items ())
329 {
330 bool is_associated_type = item->get_impl_item_type ()
331 == HIR::ImplItem::ImplItemType::TYPE_ALIAS;
332 if (!is_associated_type)
333 continue;
334
335 TyTy::BaseType *impl_item_ty = nullptr;
336 Analysis::NodeMapping i = item->get_impl_mappings ();
337 bool query_ok = Resolver::query_type (i.get_hirid (), &impl_item_ty);
338 if (!query_ok)
339 return false;
340
341 std::string item_name = item->get_impl_item_name ();
342 TypeBoundPredicateItem lookup
343 = predicate.lookup_associated_item (item_name);
344 if (lookup.is_error ())
345 return false;
346
347 const auto *item_ref = lookup.get_raw_item ();
348 TyTy::BaseType *bound_ty = item_ref->get_tyty ();
349
350 // compare the types
351 if (!bound_ty->can_eq (impl_item_ty, false))
352 {
353 if (!impl_item_ty->can_eq (bound_ty, false))
354 {
355 if (emit_error)
356 {
357 rich_location r (line_table,
358 mappings.lookup_location (get_ref ()));
359 r.add_range (predicate.get_locus ());
360 r.add_range (mappings.lookup_location (i.get_hirid ()));
361
362 std::string rich_msg
363 = "expected " + bound_ty->destructure ()->get_name ()
364 + ", found "
365 + impl_item_ty->destructure ()->get_name ();
366 r.add_fixit_replace (rich_msg.c_str ());
367
368 rust_error_at (
369 r, ErrorCode::E0271,
370 "type mismatch, expected %qs but got %qs",
371 bound_ty->destructure ()->get_name ().c_str (),
372 impl_item_ty->destructure ()->get_name ().c_str ());
373 }
374 return false;
375 }
376 }
377 }
378
379 return true;
380 }
381
382 return false;
383 }
384
385 bool
386 BaseType::bounds_compatible (const BaseType &other, location_t locus,
387 bool emit_error) const
388 {
389 std::vector<std::reference_wrapper<const TypeBoundPredicate>>
390 unsatisfied_bounds;
391 for (auto &bound : get_specified_bounds ())
392 {
393 if (!other.satisfies_bound (bound, emit_error))
394 unsatisfied_bounds.push_back (bound);
395 }
396
397 // lets emit a single error for this
398 if (unsatisfied_bounds.size () > 0)
399 {
400 rich_location r (line_table, locus);
401 std::string missing_preds;
402 for (size_t i = 0; i < unsatisfied_bounds.size (); i++)
403 {
404 const TypeBoundPredicate &pred = unsatisfied_bounds.at (i);
405 r.add_range (pred.get_locus ());
406 missing_preds += pred.get_name ();
407
408 bool have_next = (i + 1) < unsatisfied_bounds.size ();
409 if (have_next)
410 missing_preds += ", ";
411 }
412
413 if (emit_error)
414 {
415 rust_error_at (r, ErrorCode::E0277,
416 "bounds not satisfied for %s %qs is not satisfied",
417 other.get_name ().c_str (), missing_preds.c_str ());
418 // rust_assert (!emit_error);
419 }
420 }
421
422 return unsatisfied_bounds.size () == 0;
423 }
424
425 void
426 BaseType::inherit_bounds (const BaseType &other)
427 {
428 inherit_bounds (other.get_specified_bounds ());
429 }
430
431 void
432 BaseType::inherit_bounds (
433 const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
434 {
435 for (auto &bound : specified_bounds)
436 {
437 add_bound (bound);
438 }
439 }
440
441 const BaseType *
442 BaseType::get_root () const
443 {
444 // FIXME this needs to be it its own visitor class with a vector adjustments
445 const TyTy::BaseType *root = this;
446
447 if (const auto r = root->try_as<const ReferenceType> ())
448 {
449 root = r->get_base ()->get_root ();
450 }
451 else if (const auto r = root->try_as<const PointerType> ())
452 {
453 root = r->get_base ()->get_root ();
454 }
455 // these are an unsize
456 else if (const auto r = root->try_as<const SliceType> ())
457 {
458 root = r->get_element_type ()->get_root ();
459 }
460 // else if (const auto r = root->try_as<const ArrayType> ())
461 // {
462 // root = r->get_element_type ()->get_root ();
463 // }
464
465 return root;
466 }
467
468 BaseType *
469 BaseType::destructure ()
470 {
471 int recurisve_ops = 0;
472 BaseType *x = this;
473 while (true)
474 {
475 if (recurisve_ops++ >= rust_max_recursion_depth)
476 {
477 rust_error_at (
478 UNDEF_LOCATION,
479 "%<recursion depth%> count exceeds limit of %i (use "
480 "%<frust-max-recursion-depth=%> to increase the limit)",
481 rust_max_recursion_depth);
482 return new ErrorType (get_ref ());
483 }
484
485 if (auto p = x->try_as<ParamType> ())
486 {
487 auto pr = p->resolve ();
488 if (pr == x)
489 return pr;
490
491 x = pr;
492 }
493 else if (auto p = x->try_as<PlaceholderType> ())
494 {
495 if (!p->can_resolve ())
496 return p;
497
498 x = p->resolve ();
499 }
500 else if (auto p = x->try_as<ProjectionType> ())
501 {
502 x = p->get ();
503 }
504 else
505 {
506 return x;
507 }
508 }
509
510 return x;
511 }
512
513 const BaseType *
514 BaseType::destructure () const
515 {
516 int recurisve_ops = 0;
517 const BaseType *x = this;
518 while (true)
519 {
520 if (recurisve_ops++ >= rust_max_recursion_depth)
521 {
522 rust_error_at (
523 UNDEF_LOCATION,
524 "%<recursion depth%> count exceeds limit of %i (use "
525 "%<frust-max-recursion-depth=%> to increase the limit)",
526 rust_max_recursion_depth);
527 return new ErrorType (get_ref ());
528 }
529
530 if (auto p = x->try_as<const ParamType> ())
531 {
532 auto pr = p->resolve ();
533 if (pr == x)
534 return pr;
535
536 x = pr;
537 }
538 else if (auto p = x->try_as<const PlaceholderType> ())
539 {
540 if (!p->can_resolve ())
541 return p;
542
543 x = p->resolve ();
544 }
545 else if (auto p = x->try_as<const ProjectionType> ())
546 {
547 x = p->get ();
548 }
549 // else if (auto p = x->try_as<const OpaqueType> ())
550 // {
551 // auto pr = p->resolve ();
552
553 // rust_debug ("XXXXXX")
554
555 // if (pr == x)
556 // return pr;
557
558 // x = pr;
559 // }
560 else
561 {
562 return x;
563 }
564 }
565
566 return x;
567 }
568
569 BaseType *
570 BaseType::monomorphized_clone () const
571 {
572 const TyTy::BaseType *x = destructure ();
573
574 if (auto arr = x->try_as<const ArrayType> ())
575 {
576 TyVar elm = arr->get_var_element_type ().monomorphized_clone ();
577 return new ArrayType (arr->get_ref (), arr->get_ty_ref (), ident.locus,
578 arr->get_capacity_expr (), elm,
579 arr->get_combined_refs ());
580 }
581 else if (auto slice = x->try_as<const SliceType> ())
582 {
583 TyVar elm = slice->get_var_element_type ().monomorphized_clone ();
584 return new SliceType (slice->get_ref (), slice->get_ty_ref (),
585 ident.locus, elm, slice->get_combined_refs ());
586 }
587 else if (auto ptr = x->try_as<const PointerType> ())
588 {
589 TyVar elm = ptr->get_var_element_type ().monomorphized_clone ();
590 return new PointerType (ptr->get_ref (), ptr->get_ty_ref (), elm,
591 ptr->mutability (), ptr->get_combined_refs ());
592 }
593 else if (auto ref = x->try_as<const ReferenceType> ())
594 {
595 TyVar elm = ref->get_var_element_type ().monomorphized_clone ();
596 return new ReferenceType (ref->get_ref (), ref->get_ty_ref (), elm,
597 ref->mutability (), ref->get_region (),
598 ref->get_combined_refs ());
599 }
600 else if (auto tuple = x->try_as<const TupleType> ())
601 {
602 std::vector<TyVar> cloned_fields;
603 for (const auto &f : tuple->get_fields ())
604 cloned_fields.push_back (f.monomorphized_clone ());
605
606 return new TupleType (tuple->get_ref (), tuple->get_ty_ref (),
607 ident.locus, cloned_fields,
608 tuple->get_combined_refs ());
609 }
610 else if (auto fn = x->try_as<const FnType> ())
611 {
612 std::vector<TyTy::FnParam> cloned_params;
613 for (auto &p : fn->get_params ())
614 cloned_params.push_back (p.monomorphized_clone ());
615
616 BaseType *retty = fn->get_return_type ()->monomorphized_clone ();
617 return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (),
618 fn->get_identifier (), fn->ident, fn->get_flags (),
619 fn->get_abi (), std::move (cloned_params), retty,
620 fn->clone_substs (), fn->get_substitution_arguments (),
621 fn->get_region_constraints (),
622 fn->get_combined_refs ());
623 }
624 else if (auto fn = x->try_as<const FnPtr> ())
625 {
626 std::vector<TyVar> cloned_params;
627 for (auto &p : fn->get_params ())
628 cloned_params.push_back (p.monomorphized_clone ());
629
630 TyVar retty = fn->get_var_return_type ().monomorphized_clone ();
631 return new FnPtr (fn->get_ref (), fn->get_ty_ref (), ident.locus,
632 std::move (cloned_params), retty,
633 fn->get_combined_refs ());
634 }
635 else if (auto adt = x->try_as<const ADTType> ())
636 {
637 std::vector<VariantDef *> cloned_variants;
638 for (auto &variant : adt->get_variants ())
639 cloned_variants.push_back (variant->monomorphized_clone ());
640
641 return new ADTType (adt->get_id (), adt->get_ref (), adt->get_ty_ref (),
642 adt->get_identifier (), adt->ident,
643 adt->get_adt_kind (), cloned_variants,
644 adt->clone_substs (), adt->get_repr_options (),
645 adt->get_used_arguments (),
646 adt->get_region_constraints (),
647 adt->get_combined_refs ());
648 }
649 else
650 {
651 return x->clone ();
652 }
653
654 rust_unreachable ();
655 return nullptr;
656 }
657
658 std::string
659 BaseType::mappings_str () const
660 {
661 std::string buffer = "Ref: " + std::to_string (get_ref ())
662 + " TyRef: " + std::to_string (get_ty_ref ());
663 buffer += "[";
664 for (auto &ref : combined)
665 buffer += std::to_string (ref) + ",";
666 buffer += "]";
667 return "(" + buffer + ")";
668 }
669
670 std::string
671 BaseType::debug_str () const
672 {
673 // return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":"
674 // + mappings_str () + ":" + bounds_as_string ();
675 return get_name ();
676 }
677
678 void
679 BaseType::debug () const
680 {
681 rust_debug ("[%p] %s", static_cast<const void *> (this),
682 debug_str ().c_str ());
683 }
684
685 bool
686 BaseType::is_concrete () const
687 {
688 const TyTy::BaseType *x = destructure ();
689
690 if (x->is<ParamType> () || x->is<ProjectionType> ())
691 {
692 return false;
693 }
694 // placeholder is a special case for this case when it is not resolvable
695 // it means we its just an empty placeholder associated type which is
696 // concrete
697 else if (x->is<PlaceholderType> ())
698 {
699 return true;
700 }
701 else if (auto fn = x->try_as<const FnType> ())
702 {
703 for (const auto &param : fn->get_params ())
704 {
705 if (!param.get_type ()->is_concrete ())
706 return false;
707 }
708 return fn->get_return_type ()->is_concrete ();
709 }
710 else if (auto fn = x->try_as<const FnPtr> ())
711 {
712 for (const auto &param : fn->get_params ())
713 {
714 if (!param.get_tyty ()->is_concrete ())
715 return false;
716 }
717 return fn->get_return_type ()->is_concrete ();
718 }
719 else if (auto adt = x->try_as<const ADTType> ())
720 {
721 if (adt->is_unit ())
722 return !adt->needs_substitution ();
723
724 for (auto &variant : adt->get_variants ())
725 {
726 bool is_num_variant
727 = variant->get_variant_type () == VariantDef::VariantType::NUM;
728 if (is_num_variant)
729 continue;
730
731 for (auto &field : variant->get_fields ())
732 {
733 const BaseType *field_type = field->get_field_type ();
734 if (!field_type->is_concrete ())
735 return false;
736 }
737 }
738 return true;
739 }
740 else if (auto arr = x->try_as<const ArrayType> ())
741 {
742 return arr->get_element_type ()->is_concrete ();
743 }
744 else if (auto slice = x->try_as<const SliceType> ())
745 {
746 return slice->get_element_type ()->is_concrete ();
747 }
748 else if (auto ptr = x->try_as<const PointerType> ())
749 {
750 return ptr->get_base ()->is_concrete ();
751 }
752 else if (auto ref = x->try_as<const ReferenceType> ())
753 {
754 return ref->get_base ()->is_concrete ();
755 }
756 else if (auto tuple = x->try_as<const TupleType> ())
757 {
758 for (size_t i = 0; i < tuple->num_fields (); i++)
759 {
760 if (!tuple->get_field (i)->is_concrete ())
761 return false;
762 }
763 return true;
764 }
765 else if (auto closure = x->try_as<const ClosureType> ())
766 {
767 if (closure->get_parameters ().is_concrete ())
768 return false;
769 return closure->get_result_type ().is_concrete ();
770 }
771 else if (x->is<InferType> () || x->is<BoolType> () || x->is<CharType> ()
772 || x->is<IntType> () || x->is<UintType> () || x->is<FloatType> ()
773 || x->is<USizeType> () || x->is<ISizeType> () || x->is<NeverType> ()
774 || x->is<StrType> () || x->is<DynamicObjectType> ()
775 || x->is<ErrorType> ())
776 {
777 return true;
778 }
779
780 return false;
781 }
782
783 bool
784 BaseType::has_substitutions_defined () const
785 {
786 const TyTy::BaseType *x = destructure ();
787 switch (x->get_kind ())
788 {
789 case INFER:
790 case BOOL:
791 case CHAR:
792 case INT:
793 case UINT:
794 case FLOAT:
795 case USIZE:
796 case ISIZE:
797 case NEVER:
798 case STR:
799 case DYNAMIC:
800 case ERROR:
801 case FNPTR:
802 case ARRAY:
803 case SLICE:
804 case POINTER:
805 case REF:
806 case TUPLE:
807 case PARAM:
808 case PLACEHOLDER:
809 case OPAQUE:
810 return false;
811
812 case PROJECTION: {
813 const ProjectionType &p = *static_cast<const ProjectionType *> (x);
814 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p);
815 return ref.has_substitutions ();
816 }
817 break;
818
819 case FNDEF: {
820 const FnType &fn = *static_cast<const FnType *> (x);
821 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn);
822 return ref.has_substitutions ();
823 }
824 break;
825
826 case ADT: {
827 const ADTType &adt = *static_cast<const ADTType *> (x);
828 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt);
829 return ref.has_substitutions ();
830 }
831 break;
832
833 case CLOSURE: {
834 const ClosureType &closure = *static_cast<const ClosureType *> (x);
835 const SubstitutionRef &ref
836 = static_cast<const SubstitutionRef &> (closure);
837 return ref.has_substitutions ();
838 }
839 break;
840 }
841
842 return false;
843 }
844
845 bool
846 BaseType::needs_generic_substitutions () const
847 {
848 const TyTy::BaseType *x = destructure ();
849 switch (x->get_kind ())
850 {
851 case INFER:
852 case BOOL:
853 case CHAR:
854 case INT:
855 case UINT:
856 case FLOAT:
857 case USIZE:
858 case ISIZE:
859 case NEVER:
860 case STR:
861 case DYNAMIC:
862 case ERROR:
863 case FNPTR:
864 case ARRAY:
865 case SLICE:
866 case POINTER:
867 case REF:
868 case TUPLE:
869 case PARAM:
870 case PLACEHOLDER:
871 case OPAQUE:
872 return false;
873
874 case PROJECTION: {
875 const ProjectionType &p = *static_cast<const ProjectionType *> (x);
876 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p);
877 return ref.needs_substitution ();
878 }
879 break;
880
881 case FNDEF: {
882 const FnType &fn = *static_cast<const FnType *> (x);
883 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn);
884 return ref.needs_substitution ();
885 }
886 break;
887
888 case ADT: {
889 const ADTType &adt = *static_cast<const ADTType *> (x);
890 const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt);
891 return ref.needs_substitution ();
892 }
893 break;
894
895 case CLOSURE: {
896 const ClosureType &closure = *static_cast<const ClosureType *> (x);
897 const SubstitutionRef &ref
898 = static_cast<const SubstitutionRef &> (closure);
899 return ref.needs_substitution ();
900 }
901 break;
902 }
903
904 return false;
905 }
906
907 const SubstitutionArgumentMappings &
908 BaseType::get_subst_argument_mappings () const
909 {
910 static auto empty = SubstitutionArgumentMappings::empty ();
911 const TyTy::BaseType *x = destructure ();
912 switch (x->get_kind ())
913 {
914 case PROJECTION: {
915 const auto &p = *static_cast<const ProjectionType *> (x);
916 const auto &ref = static_cast<const SubstitutionRef &> (p);
917 return ref.get_substitution_arguments ();
918 }
919 break;
920
921 case FNDEF: {
922 const auto &fn = *static_cast<const FnType *> (x);
923 const auto &ref = static_cast<const SubstitutionRef &> (fn);
924 return ref.get_substitution_arguments ();
925 }
926 break;
927
928 case ADT: {
929 const auto &adt = *static_cast<const ADTType *> (x);
930 const auto &ref = static_cast<const SubstitutionRef &> (adt);
931 return ref.get_substitution_arguments ();
932 }
933 break;
934
935 case CLOSURE: {
936 const auto &closure = *static_cast<const ClosureType *> (x);
937 const auto &ref = static_cast<const SubstitutionRef &> (closure);
938 return ref.get_substitution_arguments ();
939 }
940 break;
941
942 default:
943 return empty;
944 }
945
946 return empty;
947 }
948
949 // InferType
950
951 InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
952 location_t locus, std::set<HirId> refs)
953 : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
954 refs),
955 infer_kind (infer_kind), default_hint (hint)
956 {}
957
958 InferType::InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind,
959 TypeHint hint, location_t locus, std::set<HirId> refs)
960 : BaseType (ref, ty_ref, KIND,
961 {Resolver::CanonicalPath::create_empty (), locus}, refs),
962 infer_kind (infer_kind), default_hint (hint)
963 {}
964
965 InferType::InferTypeKind
966 InferType::get_infer_kind () const
967 {
968 return infer_kind;
969 }
970
971 std::string
972 InferType::get_name () const
973 {
974 return as_string ();
975 }
976
977 void
978 InferType::accept_vis (TyVisitor &vis)
979 {
980 vis.visit (*this);
981 }
982
983 void
984 InferType::accept_vis (TyConstVisitor &vis) const
985 {
986 vis.visit (*this);
987 }
988
989 std::string
990 InferType::as_string () const
991 {
992 switch (infer_kind)
993 {
994 case GENERAL:
995 return "T?";
996 case INTEGRAL:
997 return "<integer>";
998 case FLOAT:
999 return "<float>";
1000 }
1001 return "<infer::error>";
1002 }
1003
1004 bool
1005 InferType::can_eq (const BaseType *other, bool emit_errors) const
1006 {
1007 InferCmp r (this, emit_errors);
1008 return r.can_eq (other);
1009 }
1010
1011 BaseType *
1012 InferType::clone () const
1013 {
1014 // clones for inference variables are special in that they _must_ exist within
1015 // the type check context and we must ensure we don't loose the chain
1016 // otherwise we will end up in the missing type annotations case
1017 //
1018 // This means we cannot simply take over the same reference we must generate a
1019 // new ref just like the get_implicit_infer_var code then we can setup the
1020 // chain of references accordingly to ensure we don't loose the ability to
1021 // update the inference variables when we solve the type
1022
1023 auto &mappings = Analysis::Mappings::get ();
1024 auto context = Resolver::TypeCheckContext::get ();
1025
1026 InferType *clone
1027 = new InferType (mappings.get_next_hir_id (), get_infer_kind (),
1028 default_hint, get_ident ().locus, get_combined_refs ());
1029
1030 context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
1031 UNKNOWN_NODEID,
1032 clone->get_ref (),
1033 UNKNOWN_LOCAL_DEFID),
1034 clone);
1035 mappings.insert_location (clone->get_ref (),
1036 mappings.lookup_location (get_ref ()));
1037
1038 // setup the chain to reference this
1039 clone->append_reference (get_ref ());
1040
1041 return clone;
1042 }
1043
1044 bool
1045 InferType::default_type (BaseType **type) const
1046 {
1047 auto context = Resolver::TypeCheckContext::get ();
1048 bool ok = false;
1049
1050 // NOTE: Calling this error is misleading.
1051 if (default_hint.kind == TypeKind::ERROR)
1052 {
1053 switch (infer_kind)
1054 {
1055 case GENERAL:
1056 return false;
1057
1058 case INTEGRAL: {
1059 ok = context->lookup_builtin ("i32", type);
1060 rust_assert (ok);
1061 return ok;
1062 }
1063
1064 case FLOAT: {
1065 ok = context->lookup_builtin ("f64", type);
1066 rust_assert (ok);
1067 return ok;
1068 }
1069 }
1070 return false;
1071 }
1072
1073 switch (default_hint.kind)
1074 {
1075 case ISIZE:
1076 ok = context->lookup_builtin ("isize", type);
1077 rust_assert (ok);
1078 return ok;
1079
1080 case USIZE:
1081 ok = context->lookup_builtin ("usize", type);
1082 rust_assert (ok);
1083 return ok;
1084
1085 case INT:
1086 switch (default_hint.szhint)
1087 {
1088 case TypeHint::SizeHint::S8:
1089 ok = context->lookup_builtin ("i8", type);
1090 rust_assert (ok);
1091 return ok;
1092
1093 case TypeHint::SizeHint::S16:
1094 ok = context->lookup_builtin ("i16", type);
1095 rust_assert (ok);
1096 return ok;
1097
1098 case TypeHint::SizeHint::S32:
1099 ok = context->lookup_builtin ("i32", type);
1100 rust_assert (ok);
1101 return ok;
1102
1103 case TypeHint::SizeHint::S64:
1104 ok = context->lookup_builtin ("i64", type);
1105 rust_assert (ok);
1106 return ok;
1107
1108 case TypeHint::SizeHint::S128:
1109 ok = context->lookup_builtin ("i128", type);
1110 rust_assert (ok);
1111 return ok;
1112
1113 default:
1114 return false;
1115 }
1116 break;
1117
1118 case UINT:
1119 switch (default_hint.szhint)
1120 {
1121 case TypeHint::SizeHint::S8:
1122 ok = context->lookup_builtin ("u8", type);
1123 rust_assert (ok);
1124 return ok;
1125
1126 case TypeHint::SizeHint::S16:
1127 ok = context->lookup_builtin ("u16", type);
1128 rust_assert (ok);
1129 return ok;
1130
1131 case TypeHint::SizeHint::S32:
1132 ok = context->lookup_builtin ("u32", type);
1133 rust_assert (ok);
1134 return ok;
1135
1136 case TypeHint::SizeHint::S64:
1137 ok = context->lookup_builtin ("u64", type);
1138 rust_assert (ok);
1139 return ok;
1140
1141 case TypeHint::SizeHint::S128:
1142 ok = context->lookup_builtin ("u128", type);
1143 rust_assert (ok);
1144 return ok;
1145
1146 default:
1147 return false;
1148 }
1149 break;
1150
1151 case TypeKind::FLOAT:
1152 switch (default_hint.szhint)
1153 {
1154 case TypeHint::SizeHint::S32:
1155 ok = context->lookup_builtin ("f32", type);
1156 rust_assert (ok);
1157 return ok;
1158
1159 case TypeHint::SizeHint::S64:
1160 ok = context->lookup_builtin ("f64", type);
1161 rust_assert (ok);
1162 return ok;
1163
1164 default:
1165 return false;
1166 }
1167 break;
1168
1169 default:
1170 return false;
1171 }
1172
1173 return false;
1174 }
1175
1176 void
1177 InferType::apply_primitive_type_hint (const BaseType &hint)
1178 {
1179 switch (hint.get_kind ())
1180 {
1181 case ISIZE:
1182 case USIZE:
1183 infer_kind = INTEGRAL;
1184 default_hint.kind = hint.get_kind ();
1185 break;
1186
1187 case INT: {
1188 infer_kind = INTEGRAL;
1189 default_hint.kind = hint.get_kind ();
1190 default_hint.shint = TypeHint::SignedHint::SIGNED;
1191 switch (hint.as<const IntType> ()->get_int_kind ())
1192 {
1193 case IntType::I8:
1194 default_hint.szhint = TypeHint::SizeHint::S8;
1195 break;
1196 case IntType::I16:
1197 default_hint.szhint = TypeHint::SizeHint::S16;
1198 break;
1199 case IntType::I32:
1200 default_hint.szhint = TypeHint::SizeHint::S32;
1201 break;
1202 case IntType::I64:
1203 default_hint.szhint = TypeHint::SizeHint::S64;
1204 break;
1205 case IntType::I128:
1206 default_hint.szhint = TypeHint::SizeHint::S128;
1207 break;
1208 }
1209 }
1210 break;
1211
1212 case UINT: {
1213 infer_kind = INTEGRAL;
1214 default_hint.kind = hint.get_kind ();
1215 default_hint.shint = TypeHint::SignedHint::UNSIGNED;
1216 switch (hint.as<const UintType> ()->get_uint_kind ())
1217 {
1218 case UintType::U8:
1219 default_hint.szhint = TypeHint::SizeHint::S8;
1220 break;
1221 case UintType::U16:
1222 default_hint.szhint = TypeHint::SizeHint::S16;
1223 break;
1224 case UintType::U32:
1225 default_hint.szhint = TypeHint::SizeHint::S32;
1226 break;
1227 case UintType::U64:
1228 default_hint.szhint = TypeHint::SizeHint::S64;
1229 break;
1230 case UintType::U128:
1231 default_hint.szhint = TypeHint::SizeHint::S128;
1232 break;
1233 }
1234 }
1235 break;
1236
1237 case TypeKind::FLOAT: {
1238 infer_kind = FLOAT;
1239 default_hint.shint = TypeHint::SignedHint::SIGNED;
1240 default_hint.kind = hint.get_kind ();
1241 switch (hint.as<const FloatType> ()->get_float_kind ())
1242 {
1243 case FloatType::F32:
1244 default_hint.szhint = TypeHint::SizeHint::S32;
1245 break;
1246
1247 case FloatType::F64:
1248 default_hint.szhint = TypeHint::SizeHint::S64;
1249 break;
1250 }
1251 }
1252 break;
1253
1254 default:
1255 // TODO bool, char, never??
1256 break;
1257 }
1258 }
1259
1260 // ErrorType
1261
1262 ErrorType::ErrorType (HirId ref, std::set<HirId> refs)
1263 : BaseType (ref, ref, KIND,
1264 {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
1265 {}
1266
1267 ErrorType::ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs)
1268 : BaseType (ref, ty_ref, KIND,
1269 {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
1270 {}
1271
1272 std::string
1273 ErrorType::get_name () const
1274 {
1275 return as_string ();
1276 }
1277
1278 void
1279 ErrorType::accept_vis (TyVisitor &vis)
1280 {
1281 vis.visit (*this);
1282 }
1283
1284 void
1285 ErrorType::accept_vis (TyConstVisitor &vis) const
1286 {
1287 vis.visit (*this);
1288 }
1289
1290 std::string
1291 ErrorType::as_string () const
1292 {
1293 return "<tyty::error>";
1294 }
1295
1296 bool
1297 ErrorType::can_eq (const BaseType *other, bool emit_errors) const
1298 {
1299 return get_kind () == other->get_kind ();
1300 }
1301
1302 BaseType *
1303 ErrorType::clone () const
1304 {
1305 return new ErrorType (get_ref (), get_ty_ref (), get_combined_refs ());
1306 }
1307
1308 // Struct Field type
1309
1310 StructFieldType::StructFieldType (HirId ref, std::string name, BaseType *ty,
1311 location_t locus)
1312 : ref (ref), name (name), ty (ty), locus (locus)
1313 {}
1314
1315 HirId
1316 StructFieldType::get_ref () const
1317 {
1318 return ref;
1319 }
1320
1321 std::string
1322 StructFieldType::get_name () const
1323 {
1324 return name;
1325 }
1326
1327 BaseType *
1328 StructFieldType::get_field_type () const
1329 {
1330 return ty;
1331 }
1332
1333 void
1334 StructFieldType::set_field_type (BaseType *fty)
1335 {
1336 ty = fty;
1337 }
1338
1339 void
1340 StructFieldType::debug () const
1341 {
1342 rust_debug ("%s", as_string ().c_str ());
1343 }
1344
1345 location_t
1346 StructFieldType::get_locus () const
1347 {
1348 return locus;
1349 }
1350
1351 std::string
1352 StructFieldType::as_string () const
1353 {
1354 return name + ":" + get_field_type ()->debug_str ();
1355 }
1356
1357 bool
1358 StructFieldType::is_equal (const StructFieldType &other) const
1359 {
1360 bool names_eq = get_name () == other.get_name ();
1361
1362 TyTy::BaseType *o = other.get_field_type ();
1363 if (auto op = o->try_as<ParamType> ())
1364 o = op->resolve ();
1365
1366 bool types_eq = get_field_type ()->is_equal (*o);
1367
1368 return names_eq && types_eq;
1369 }
1370
1371 StructFieldType *
1372 StructFieldType::clone () const
1373 {
1374 return new StructFieldType (get_ref (), get_name (),
1375 get_field_type ()->clone (), locus);
1376 }
1377
1378 StructFieldType *
1379 StructFieldType::monomorphized_clone () const
1380 {
1381 return new StructFieldType (get_ref (), get_name (),
1382 get_field_type ()->monomorphized_clone (), locus);
1383 }
1384
1385 // VariantDef
1386
1387 std::string
1388 VariantDef::variant_type_string (VariantType type)
1389 {
1390 switch (type)
1391 {
1392 case NUM:
1393 return "enumeral";
1394 case TUPLE:
1395 return "tuple";
1396 case STRUCT:
1397 return "struct";
1398 }
1399 rust_unreachable ();
1400 return "";
1401 }
1402
1403 VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
1404 RustIdent ident,
1405 tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant)
1406 : id (id), defid (defid), identifier (identifier), ident (ident),
1407 discriminant (std::move (discriminant))
1408
1409 {
1410 type = VariantType::NUM;
1411 fields = {};
1412 }
1413
1414 VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
1415 RustIdent ident, VariantType type,
1416 tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
1417 std::vector<StructFieldType *> fields)
1418 : id (id), defid (defid), identifier (identifier), ident (ident), type (type),
1419 discriminant (std::move (discriminant)), fields (fields)
1420 {
1421 rust_assert ((type == VariantType::NUM && fields.empty ())
1422 || (type == VariantType::TUPLE || type == VariantType::STRUCT));
1423 }
1424
1425 VariantDef &
1426 VariantDef::get_error_node ()
1427 {
1428 static VariantDef node
1429 = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "",
1430 {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
1431 tl::nullopt);
1432
1433 return node;
1434 }
1435
1436 bool
1437 VariantDef::is_error () const
1438 {
1439 return get_id () == UNKNOWN_HIRID;
1440 }
1441
1442 HirId
1443 VariantDef::get_id () const
1444 {
1445 return id;
1446 }
1447
1448 DefId
1449 VariantDef::get_defid () const
1450 {
1451 return defid;
1452 }
1453
1454 VariantDef::VariantType
1455 VariantDef::get_variant_type () const
1456 {
1457 return type;
1458 }
1459
1460 bool
1461 VariantDef::is_data_variant () const
1462 {
1463 return type != VariantType::NUM;
1464 }
1465
1466 bool
1467 VariantDef::is_dataless_variant () const
1468 {
1469 return type == VariantType::NUM;
1470 }
1471
1472 std::string
1473 VariantDef::get_identifier () const
1474 {
1475 return identifier;
1476 }
1477
1478 size_t
1479 VariantDef::num_fields () const
1480 {
1481 return fields.size ();
1482 }
1483
1484 StructFieldType *
1485 VariantDef::get_field_at_index (size_t index)
1486 {
1487 rust_assert (index < fields.size ());
1488 return fields.at (index);
1489 }
1490
1491 std::vector<StructFieldType *> &
1492 VariantDef::get_fields ()
1493 {
1494 rust_assert (type != NUM);
1495 return fields;
1496 }
1497
1498 bool
1499 VariantDef::lookup_field (const std::string &lookup,
1500 StructFieldType **field_lookup, size_t *index) const
1501 {
1502 size_t i = 0;
1503 for (auto &field : fields)
1504 {
1505 if (field->get_name ().compare (lookup) == 0)
1506 {
1507 if (index != nullptr)
1508 *index = i;
1509
1510 if (field_lookup != nullptr)
1511 *field_lookup = field;
1512
1513 return true;
1514 }
1515 i++;
1516 }
1517 return false;
1518 }
1519
1520 HIR::Expr &
1521 VariantDef::get_discriminant ()
1522 {
1523 return *discriminant.value ();
1524 }
1525
1526 const HIR::Expr &
1527 VariantDef::get_discriminant () const
1528 {
1529 return *discriminant.value ();
1530 }
1531
1532 bool
1533 VariantDef::has_discriminant () const
1534 {
1535 return discriminant.has_value ();
1536 }
1537
1538 std::string
1539 VariantDef::as_string () const
1540 {
1541 if (type == VariantType::NUM)
1542 return identifier
1543 + (has_discriminant () ? " = " + get_discriminant ().as_string ()
1544 : "");
1545
1546 std::string buffer;
1547 for (size_t i = 0; i < fields.size (); ++i)
1548 {
1549 buffer += fields.at (i)->as_string ();
1550 if ((i + 1) < fields.size ())
1551 buffer += ", ";
1552 }
1553
1554 if (type == VariantType::TUPLE)
1555 return identifier + " (" + buffer + ")";
1556 else
1557 return identifier + " {" + buffer + "}";
1558 }
1559
1560 bool
1561 VariantDef::is_equal (const VariantDef &other) const
1562 {
1563 if (type != other.type)
1564 return false;
1565
1566 if (identifier.compare (other.identifier) != 0)
1567 return false;
1568
1569 if (fields.size () != other.fields.size ())
1570 return false;
1571
1572 for (size_t i = 0; i < fields.size (); i++)
1573 {
1574 if (!fields.at (i)->is_equal (*other.fields.at (i)))
1575 return false;
1576 }
1577
1578 return true;
1579 }
1580
1581 VariantDef *
1582 VariantDef::clone () const
1583 {
1584 std::vector<StructFieldType *> cloned_fields;
1585 for (auto &f : fields)
1586 cloned_fields.push_back ((StructFieldType *) f->clone ());
1587
1588 auto &&discriminant_opt = has_discriminant ()
1589 ? tl::optional<std::unique_ptr<HIR::Expr>> (
1590 get_discriminant ().clone_expr ())
1591 : tl::nullopt;
1592
1593 return new VariantDef (id, defid, identifier, ident, type,
1594 std::move (discriminant_opt), cloned_fields);
1595 }
1596
1597 VariantDef *
1598 VariantDef::monomorphized_clone () const
1599 {
1600 std::vector<StructFieldType *> cloned_fields;
1601 for (auto &f : fields)
1602 cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ());
1603
1604 auto discriminant_opt = has_discriminant ()
1605 ? tl::optional<std::unique_ptr<HIR::Expr>> (
1606 get_discriminant ().clone_expr ())
1607 : tl::nullopt;
1608
1609 return new VariantDef (id, defid, identifier, ident, type,
1610 std::move (discriminant_opt), cloned_fields);
1611 }
1612
1613 const RustIdent &
1614 VariantDef::get_ident () const
1615 {
1616 return ident;
1617 }
1618
1619 // ADTType
1620
1621 ADTType::ADTType (DefId id, HirId ref, std::string identifier, RustIdent ident,
1622 ADTKind adt_kind, std::vector<VariantDef *> variants,
1623 std::vector<SubstitutionParamMapping> subst_refs,
1624 SubstitutionArgumentMappings generic_arguments,
1625 RegionConstraints region_constraints, std::set<HirId> refs)
1626 : BaseType (ref, ref, TypeKind::ADT, ident, refs),
1627 SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
1628 region_constraints),
1629 id (id), identifier (identifier), variants (variants), adt_kind (adt_kind)
1630 {}
1631
1632 ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
1633 RustIdent ident, ADTKind adt_kind,
1634 std::vector<VariantDef *> variants,
1635 std::vector<SubstitutionParamMapping> subst_refs,
1636 SubstitutionArgumentMappings generic_arguments,
1637 RegionConstraints region_constraints, std::set<HirId> refs)
1638 : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs),
1639 SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
1640 region_constraints),
1641 id (id), identifier (identifier), variants (variants), adt_kind (adt_kind)
1642 {}
1643
1644 ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
1645 RustIdent ident, ADTKind adt_kind,
1646 std::vector<VariantDef *> variants,
1647 std::vector<SubstitutionParamMapping> subst_refs,
1648 ReprOptions repr,
1649 SubstitutionArgumentMappings generic_arguments,
1650 RegionConstraints region_constraints, std::set<HirId> refs)
1651 : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs),
1652 SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
1653 region_constraints),
1654 id (id), identifier (identifier), variants (variants), adt_kind (adt_kind),
1655 repr (repr)
1656 {}
1657
1658 void
1659 ADTType::accept_vis (TyVisitor &vis)
1660 {
1661 vis.visit (*this);
1662 }
1663
1664 void
1665 ADTType::accept_vis (TyConstVisitor &vis) const
1666 {
1667 vis.visit (*this);
1668 }
1669
1670 std::string
1671 ADTType::as_string () const
1672 {
1673 std::string variants_buffer;
1674 for (size_t i = 0; i < number_of_variants (); ++i)
1675 {
1676 TyTy::VariantDef *variant = variants.at (i);
1677 variants_buffer += variant->as_string ();
1678 if ((i + 1) < number_of_variants ())
1679 variants_buffer += ", ";
1680 }
1681
1682 return identifier + subst_as_string () + "{" + variants_buffer + "}";
1683 }
1684
1685 bool
1686 ADTType::can_eq (const BaseType *other, bool emit_errors) const
1687 {
1688 ADTCmp r (this, emit_errors);
1689 return r.can_eq (other);
1690 }
1691
1692 bool
1693 ADTType::is_equal (const BaseType &other) const
1694 {
1695 if (get_kind () != other.get_kind ())
1696 return false;
1697
1698 auto other2 = other.as<const ADTType> ();
1699 if (get_adt_kind () != other2->get_adt_kind ())
1700 return false;
1701
1702 if (number_of_variants () != other2->number_of_variants ())
1703 return false;
1704
1705 if (has_substitutions_defined () != other2->has_substitutions_defined ())
1706 return false;
1707
1708 if (has_substitutions_defined ())
1709 {
1710 if (get_num_substitutions () != other2->get_num_substitutions ())
1711 return false;
1712
1713 for (size_t i = 0; i < get_num_substitutions (); i++)
1714 {
1715 const SubstitutionParamMapping &a = substitutions.at (i);
1716 const SubstitutionParamMapping &b = other2->substitutions.at (i);
1717
1718 const ParamType *aa = a.get_param_ty ();
1719 const ParamType *bb = b.get_param_ty ();
1720 BaseType *aaa = aa->resolve ();
1721 BaseType *bbb = bb->resolve ();
1722 if (!aaa->is_equal (*bbb))
1723 return false;
1724 }
1725 }
1726
1727 for (size_t i = 0; i < number_of_variants (); i++)
1728 {
1729 const TyTy::VariantDef *a = get_variants ().at (i);
1730 const TyTy::VariantDef *b = other2->get_variants ().at (i);
1731
1732 if (!a->is_equal (*b))
1733 return false;
1734 }
1735
1736 return true;
1737 }
1738
1739 DefId
1740 ADTType::get_id () const
1741 {
1742 return id;
1743 }
1744
1745 BaseType *
1746 ADTType::clone () const
1747 {
1748 std::vector<VariantDef *> cloned_variants;
1749 for (auto &variant : variants)
1750 cloned_variants.push_back (variant->clone ());
1751
1752 return new ADTType (get_id (), get_ref (), get_ty_ref (), identifier, ident,
1753 get_adt_kind (), cloned_variants, clone_substs (),
1754 get_repr_options (), used_arguments,
1755 get_region_constraints (), get_combined_refs ());
1756 }
1757
1758 static bool
1759 handle_substitions (SubstitutionArgumentMappings &subst_mappings,
1760 StructFieldType *field)
1761 {
1762 auto fty = field->get_field_type ();
1763 if (auto p = fty->try_as<ParamType> ())
1764 {
1765 SubstitutionArg arg = SubstitutionArg::error ();
1766 bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
1767 if (ok)
1768 {
1769 auto argt = arg.get_tyty ();
1770 bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
1771 bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
1772
1773 if (arg_is_param || arg_is_concrete)
1774 {
1775 auto new_field = argt->clone ();
1776 new_field->set_ref (fty->get_ref ());
1777 field->set_field_type (new_field);
1778 }
1779 else
1780 {
1781 field->get_field_type ()->set_ty_ref (argt->get_ref ());
1782 }
1783 }
1784 }
1785 else if (fty->has_substitutions_defined () || !fty->is_concrete ())
1786 {
1787 BaseType *concrete
1788 = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
1789
1790 if (concrete->get_kind () == TyTy::TypeKind::ERROR)
1791 {
1792 rust_error_at (subst_mappings.get_locus (),
1793 "Failed to resolve field substitution type: %s",
1794 fty->as_string ().c_str ());
1795 return false;
1796 }
1797
1798 auto new_field = concrete->clone ();
1799 new_field->set_ref (fty->get_ref ());
1800 field->set_field_type (new_field);
1801 }
1802
1803 return true;
1804 }
1805
1806 ADTType *
1807 ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
1808 {
1809 auto adt = clone ()->as<ADTType> ();
1810 adt->set_ty_ref (mappings.get_next_hir_id ());
1811 adt->used_arguments = subst_mappings;
1812
1813 for (auto &sub : adt->get_substs ())
1814 {
1815 SubstitutionArg arg = SubstitutionArg::error ();
1816 bool ok
1817 = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
1818 if (ok)
1819 sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
1820 }
1821
1822 for (auto &variant : adt->get_variants ())
1823 {
1824 if (variant->is_dataless_variant ())
1825 continue;
1826
1827 for (auto &field : variant->get_fields ())
1828 {
1829 bool ok = ::Rust::TyTy::handle_substitions (subst_mappings, field);
1830 if (!ok)
1831 return adt;
1832 }
1833 }
1834
1835 return adt;
1836 }
1837
1838 // TupleType
1839
1840 TupleType::TupleType (HirId ref, location_t locus, std::vector<TyVar> fields,
1841 std::set<HirId> refs)
1842 : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
1843 refs),
1844 fields (fields)
1845 {}
1846
1847 TupleType::TupleType (HirId ref, HirId ty_ref, location_t locus,
1848 std::vector<TyVar> fields, std::set<HirId> refs)
1849 : BaseType (ref, ty_ref, KIND,
1850 {Resolver::CanonicalPath::create_empty (), locus}, refs),
1851 fields (fields)
1852 {}
1853
1854 TupleType *
1855 TupleType::get_unit_type ()
1856 {
1857 static TupleType *ret = nullptr;
1858 if (ret == nullptr)
1859 ret = new TupleType (Analysis::Mappings::get ().get_next_hir_id (),
1860 BUILTINS_LOCATION);
1861 return ret;
1862 }
1863
1864 size_t
1865 TupleType::num_fields () const
1866 {
1867 return fields.size ();
1868 }
1869
1870 const std::vector<TyVar> &
1871 TupleType::get_fields () const
1872 {
1873 return fields;
1874 }
1875
1876 void
1877 TupleType::accept_vis (TyVisitor &vis)
1878 {
1879 vis.visit (*this);
1880 }
1881
1882 void
1883 TupleType::accept_vis (TyConstVisitor &vis) const
1884 {
1885 vis.visit (*this);
1886 }
1887
1888 std::string
1889 TupleType::as_string () const
1890 {
1891 size_t i = 0;
1892 std::string fields_buffer;
1893 for (const TyVar &field : get_fields ())
1894 {
1895 fields_buffer += field.get_tyty ()->as_string ();
1896 bool has_next = (i + 1) < get_fields ().size ();
1897 fields_buffer += has_next ? ", " : "";
1898 i++;
1899 }
1900 return "(" + fields_buffer + ")";
1901 }
1902
1903 std::string
1904 TupleType::get_name () const
1905 {
1906 size_t i = 0;
1907 std::string fields_buffer;
1908 for (const TyVar &field : get_fields ())
1909 {
1910 fields_buffer += field.get_tyty ()->as_string ();
1911 bool has_next = (i + 1) < get_fields ().size ();
1912 fields_buffer += has_next ? ", " : "";
1913 i++;
1914 }
1915 return "(" + fields_buffer + ")";
1916 }
1917
1918 BaseType *
1919 TupleType::get_field (size_t index) const
1920 {
1921 return fields.at (index).get_tyty ();
1922 }
1923
1924 bool
1925 TupleType::can_eq (const BaseType *other, bool emit_errors) const
1926 {
1927 TupleCmp r (this, emit_errors);
1928 return r.can_eq (other);
1929 }
1930
1931 bool
1932 TupleType::is_equal (const BaseType &other) const
1933 {
1934 if (get_kind () != other.get_kind ())
1935 return false;
1936
1937 auto other2 = other.as<const TupleType> ();
1938 if (num_fields () != other2->num_fields ())
1939 return false;
1940
1941 for (size_t i = 0; i < num_fields (); i++)
1942 {
1943 if (!get_field (i)->is_equal (*other2->get_field (i)))
1944 return false;
1945 }
1946 return true;
1947 }
1948
1949 BaseType *
1950 TupleType::clone () const
1951 {
1952 std::vector<TyVar> cloned_fields;
1953 for (const auto &f : fields)
1954 cloned_fields.push_back (f.clone ());
1955
1956 return new TupleType (get_ref (), get_ty_ref (), get_ident ().locus,
1957 cloned_fields, get_combined_refs ());
1958 }
1959
1960 TupleType *
1961 TupleType::handle_substitions (SubstitutionArgumentMappings &mappings)
1962 {
1963 auto &mappings_table = Analysis::Mappings::get ();
1964
1965 auto tuple = clone ()->as<TupleType> ();
1966 tuple->set_ref (mappings_table.get_next_hir_id ());
1967 tuple->set_ty_ref (mappings_table.get_next_hir_id ());
1968
1969 for (size_t i = 0; i < tuple->fields.size (); i++)
1970 {
1971 TyVar &field = fields.at (i);
1972 if (!field.get_tyty ()->is_concrete ())
1973 {
1974 BaseType *concrete
1975 = Resolver::SubstMapperInternal::Resolve (field.get_tyty (),
1976 mappings);
1977 tuple->fields[i]
1978 = TyVar::subst_covariant_var (field.get_tyty (), concrete);
1979 }
1980 }
1981
1982 return tuple;
1983 }
1984
1985 void
1986 FnType::accept_vis (TyVisitor &vis)
1987 {
1988 vis.visit (*this);
1989 }
1990
1991 void
1992 FnType::accept_vis (TyConstVisitor &vis) const
1993 {
1994 vis.visit (*this);
1995 }
1996
1997 std::string
1998 FnType::as_string () const
1999 {
2000 std::string params_str = "";
2001 for (auto &param : params)
2002 {
2003 auto &pattern = param.get_pattern ();
2004 auto ty = param.get_type ();
2005 params_str += pattern.as_string () + " " + ty->as_string ();
2006 params_str += ",";
2007 }
2008
2009 std::string ret_str = type->as_string ();
2010 return "fn" + subst_as_string () + " (" + params_str + ") -> " + ret_str;
2011 }
2012
2013 bool
2014 FnType::can_eq (const BaseType *other, bool emit_errors) const
2015 {
2016 FnCmp r (this, emit_errors);
2017 return r.can_eq (other);
2018 }
2019
2020 bool
2021 FnType::is_equal (const BaseType &other) const
2022 {
2023 if (get_kind () != other.get_kind ())
2024 return false;
2025
2026 auto &other2 = static_cast<const FnType &> (other);
2027 if (get_identifier ().compare (other2.get_identifier ()) != 0)
2028 return false;
2029
2030 if (!get_return_type ()->is_equal (*other2.get_return_type ()))
2031 return false;
2032
2033 if (has_substitutions_defined () != other2.has_substitutions_defined ())
2034 return false;
2035
2036 if (has_substitutions_defined ())
2037 {
2038 if (get_num_substitutions () != other2.get_num_substitutions ())
2039 return false;
2040
2041 const FnType &ofn = static_cast<const FnType &> (other);
2042 for (size_t i = 0; i < get_num_substitutions (); i++)
2043 {
2044 const SubstitutionParamMapping &a = get_substs ().at (i);
2045 const SubstitutionParamMapping &b = ofn.get_substs ().at (i);
2046
2047 const ParamType *pa = a.get_param_ty ();
2048 const ParamType *pb = b.get_param_ty ();
2049
2050 if (!pa->is_equal (*pb))
2051 return false;
2052 }
2053 }
2054
2055 if (num_params () != other2.num_params ())
2056 return false;
2057
2058 for (size_t i = 0; i < num_params (); i++)
2059 {
2060 auto lhs = param_at (i).get_type ();
2061 auto rhs = other2.param_at (i).get_type ();
2062 if (!lhs->is_equal (*rhs))
2063 return false;
2064 }
2065 return true;
2066 }
2067
2068 BaseType *
2069 FnType::clone () const
2070 {
2071 std::vector<TyTy::FnParam> cloned_params;
2072 for (auto &p : params)
2073 cloned_params.push_back (p.clone ());
2074
2075 return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (),
2076 ident, flags, abi, std::move (cloned_params),
2077 get_return_type ()->clone (), clone_substs (),
2078 get_substitution_arguments (), get_region_constraints (),
2079 get_combined_refs ());
2080 }
2081
2082 FnType *
2083 FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
2084 {
2085 FnType *fn = static_cast<FnType *> (clone ());
2086 fn->set_ty_ref (mappings.get_next_hir_id ());
2087 fn->used_arguments = subst_mappings;
2088
2089 for (auto &sub : fn->get_substs ())
2090 {
2091 SubstitutionArg arg = SubstitutionArg::error ();
2092
2093 bool ok
2094 = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
2095 if (ok)
2096 {
2097 sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
2098 }
2099 }
2100
2101 auto fty = fn->get_return_type ();
2102 bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
2103 if (is_param_ty)
2104 {
2105 ParamType *p = static_cast<ParamType *> (fty);
2106
2107 SubstitutionArg arg = SubstitutionArg::error ();
2108 bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
2109 if (ok)
2110 {
2111 auto argt = arg.get_tyty ();
2112 bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
2113 bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
2114
2115 if (arg_is_param || arg_is_concrete)
2116 {
2117 auto new_field = argt->clone ();
2118 new_field->set_ref (fty->get_ref ());
2119 fn->type = new_field;
2120 }
2121 else
2122 {
2123 fty->set_ty_ref (argt->get_ref ());
2124 }
2125 }
2126 }
2127 else if (fty->needs_generic_substitutions () || !fty->is_concrete ())
2128 {
2129 BaseType *concrete
2130 = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
2131
2132 if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR)
2133 {
2134 rust_error_at (subst_mappings.get_locus (),
2135 "Failed to resolve field substitution type: %s",
2136 fty->as_string ().c_str ());
2137 return nullptr;
2138 }
2139
2140 auto new_field = concrete->clone ();
2141 new_field->set_ref (fty->get_ref ());
2142 fn->type = new_field;
2143 }
2144
2145 for (auto &param : fn->get_params ())
2146 {
2147 auto fty = param.get_type ();
2148
2149 bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
2150 if (is_param_ty)
2151 {
2152 ParamType *p = static_cast<ParamType *> (fty);
2153
2154 SubstitutionArg arg = SubstitutionArg::error ();
2155 bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
2156 if (ok)
2157 {
2158 auto argt = arg.get_tyty ();
2159 bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
2160 bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
2161
2162 if (arg_is_param || arg_is_concrete)
2163 {
2164 auto new_field = argt->clone ();
2165 new_field->set_ref (fty->get_ref ());
2166 param.set_type (new_field);
2167 }
2168 else
2169 {
2170 fty->set_ty_ref (argt->get_ref ());
2171 }
2172 }
2173 }
2174 else if (fty->has_substitutions_defined () || !fty->is_concrete ())
2175 {
2176 BaseType *concrete
2177 = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
2178
2179 if (concrete == nullptr
2180 || concrete->get_kind () == TyTy::TypeKind::ERROR)
2181 {
2182 rust_error_at (subst_mappings.get_locus (),
2183 "Failed to resolve field substitution type: %s",
2184 fty->as_string ().c_str ());
2185 return nullptr;
2186 }
2187
2188 auto new_field = concrete->clone ();
2189 new_field->set_ref (fty->get_ref ());
2190 param.set_type (new_field);
2191 }
2192 }
2193
2194 return fn;
2195 }
2196
2197 void
2198 FnPtr::accept_vis (TyVisitor &vis)
2199 {
2200 vis.visit (*this);
2201 }
2202
2203 void
2204 FnPtr::accept_vis (TyConstVisitor &vis) const
2205 {
2206 vis.visit (*this);
2207 }
2208
2209 std::string
2210 FnPtr::as_string () const
2211 {
2212 std::string params_str;
2213
2214 auto &params = get_params ();
2215 for (auto &p : params)
2216 {
2217 params_str += p.get_tyty ()->as_string () + " ,";
2218 }
2219
2220 return "fnptr (" + params_str + ") -> " + get_return_type ()->as_string ();
2221 }
2222
2223 bool
2224 FnPtr::can_eq (const BaseType *other, bool emit_errors) const
2225 {
2226 FnptrCmp r (this, emit_errors);
2227 return r.can_eq (other);
2228 }
2229
2230 bool
2231 FnPtr::is_equal (const BaseType &other) const
2232 {
2233 if (get_kind () != other.get_kind ())
2234 return false;
2235
2236 auto other2 = static_cast<const FnPtr &> (other);
2237 auto this_ret_type = get_return_type ();
2238 auto other_ret_type = other2.get_return_type ();
2239 if (this_ret_type->is_equal (*other_ret_type))
2240 return false;
2241
2242 if (num_params () != other2.num_params ())
2243 return false;
2244
2245 for (size_t i = 0; i < num_params (); i++)
2246 {
2247 if (!get_param_type_at (i)->is_equal (*other2.get_param_type_at (i)))
2248 return false;
2249 }
2250 return true;
2251 }
2252
2253 BaseType *
2254 FnPtr::clone () const
2255 {
2256 std::vector<TyVar> cloned_params;
2257 for (auto &p : params)
2258 cloned_params.push_back (TyVar (p.get_ref ()));
2259
2260 return new FnPtr (get_ref (), get_ty_ref (), ident.locus,
2261 std::move (cloned_params), result_type,
2262 get_combined_refs ());
2263 }
2264
2265 void
2266 ClosureType::accept_vis (TyVisitor &vis)
2267 {
2268 vis.visit (*this);
2269 }
2270
2271 void
2272 ClosureType::accept_vis (TyConstVisitor &vis) const
2273 {
2274 vis.visit (*this);
2275 }
2276
2277 std::string
2278 ClosureType::as_string () const
2279 {
2280 std::string params_buf = parameters->as_string ();
2281 return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string () + "}";
2282 }
2283
2284 bool
2285 ClosureType::can_eq (const BaseType *other, bool emit_errors) const
2286 {
2287 ClosureCmp r (this, emit_errors);
2288 return r.can_eq (other);
2289 }
2290
2291 bool
2292 ClosureType::is_equal (const BaseType &other) const
2293 {
2294 if (other.get_kind () != TypeKind::CLOSURE)
2295 return false;
2296
2297 const ClosureType &other2 = static_cast<const ClosureType &> (other);
2298 if (get_def_id () != other2.get_def_id ())
2299 return false;
2300
2301 if (!get_parameters ().is_equal (other2.get_parameters ()))
2302 return false;
2303
2304 return get_result_type ().is_equal (other2.get_result_type ());
2305 }
2306
2307 BaseType *
2308 ClosureType::clone () const
2309 {
2310 return new ClosureType (get_ref (), get_ty_ref (), ident, id,
2311 (TyTy::TupleType *) parameters->clone (), result_type,
2312 clone_substs (), captures, get_combined_refs (),
2313 specified_bounds);
2314 }
2315
2316 ClosureType *
2317 ClosureType::handle_substitions (SubstitutionArgumentMappings &mappings)
2318 {
2319 rust_unreachable ();
2320 return nullptr;
2321 }
2322
2323 void
2324 ClosureType::setup_fn_once_output () const
2325 {
2326 // lookup the lang items
2327 auto fn_once_lookup = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE);
2328 auto fn_once_output_lookup
2329 = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE_OUTPUT);
2330 if (!fn_once_lookup)
2331 {
2332 rust_fatal_error (UNKNOWN_LOCATION,
2333 "Missing required %<fn_once%> lang item");
2334 return;
2335 }
2336 if (!fn_once_output_lookup)
2337 {
2338 rust_fatal_error (UNKNOWN_LOCATION,
2339 "Missing required %<fn_once_ouput%> lang item");
2340 return;
2341 }
2342
2343 DefId &trait_id = fn_once_lookup.value ();
2344 DefId &trait_item_id = fn_once_output_lookup.value ();
2345
2346 // resolve to the trait
2347 HIR::Item *item = mappings.lookup_defid (trait_id).value ();
2348 rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
2349 HIR::Trait *trait = static_cast<HIR::Trait *> (item);
2350
2351 Resolver::TraitReference *trait_ref
2352 = Resolver::TraitResolver::Resolve (*trait);
2353 rust_assert (!trait_ref->is_error ());
2354
2355 // resolve to trait item
2356 HIR::TraitItem *trait_item
2357 = mappings.lookup_trait_item_defid (trait_item_id).value ();
2358 rust_assert (trait_item->get_item_kind ()
2359 == HIR::TraitItem::TraitItemKind::TYPE);
2360 std::string item_identifier = trait_item->trait_identifier ();
2361
2362 // setup associated types #[lang = "fn_once_output"]
2363 Resolver::TraitItemReference *item_reference = nullptr;
2364 bool found = trait_ref->lookup_trait_item_by_type (
2365 item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
2366 &item_reference);
2367 rust_assert (found);
2368
2369 // setup
2370 item_reference->associated_type_set (&get_result_type ());
2371 }
2372
2373 void
2374 ArrayType::accept_vis (TyVisitor &vis)
2375 {
2376 vis.visit (*this);
2377 }
2378
2379 void
2380 ArrayType::accept_vis (TyConstVisitor &vis) const
2381 {
2382 vis.visit (*this);
2383 }
2384
2385 std::string
2386 ArrayType::as_string () const
2387 {
2388 return "[" + get_element_type ()->as_string () + ":" + "CAPACITY" + "]";
2389 }
2390
2391 bool
2392 ArrayType::can_eq (const BaseType *other, bool emit_errors) const
2393 {
2394 ArrayCmp r (this, emit_errors);
2395 return r.can_eq (other);
2396 }
2397
2398 bool
2399 ArrayType::is_equal (const BaseType &other) const
2400 {
2401 if (get_kind () != other.get_kind ())
2402 return false;
2403
2404 auto other2 = static_cast<const ArrayType &> (other);
2405
2406 auto this_element_type = get_element_type ();
2407 auto other_element_type = other2.get_element_type ();
2408
2409 return this_element_type->is_equal (*other_element_type);
2410 }
2411
2412 BaseType *
2413 ArrayType::get_element_type () const
2414 {
2415 return element_type.get_tyty ();
2416 }
2417
2418 const TyVar &
2419 ArrayType::get_var_element_type () const
2420 {
2421 return element_type;
2422 }
2423
2424 BaseType *
2425 ArrayType::clone () const
2426 {
2427 return new ArrayType (get_ref (), get_ty_ref (), ident.locus, capacity_expr,
2428 element_type, get_combined_refs ());
2429 }
2430
2431 ArrayType *
2432 ArrayType::handle_substitions (SubstitutionArgumentMappings &mappings)
2433 {
2434 auto &mappings_table = Analysis::Mappings::get ();
2435
2436 ArrayType *ref = static_cast<ArrayType *> (clone ());
2437 ref->set_ty_ref (mappings_table.get_next_hir_id ());
2438
2439 // might be &T or &ADT so this needs to be recursive
2440 auto base = ref->get_element_type ();
2441 BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
2442 ref->element_type = TyVar::subst_covariant_var (base, concrete);
2443
2444 return ref;
2445 }
2446
2447 void
2448 SliceType::accept_vis (TyVisitor &vis)
2449 {
2450 vis.visit (*this);
2451 }
2452
2453 void
2454 SliceType::accept_vis (TyConstVisitor &vis) const
2455 {
2456 vis.visit (*this);
2457 }
2458
2459 std::string
2460 SliceType::as_string () const
2461 {
2462 return "[" + get_element_type ()->as_string () + "]";
2463 }
2464
2465 bool
2466 SliceType::can_eq (const BaseType *other, bool emit_errors) const
2467 {
2468 SliceCmp r (this, emit_errors);
2469 return r.can_eq (other);
2470 }
2471
2472 bool
2473 SliceType::is_equal (const BaseType &other) const
2474 {
2475 if (get_kind () != other.get_kind ())
2476 return false;
2477
2478 auto other2 = static_cast<const SliceType &> (other);
2479
2480 auto this_element_type = get_element_type ();
2481 auto other_element_type = other2.get_element_type ();
2482
2483 return this_element_type->is_equal (*other_element_type);
2484 }
2485
2486 BaseType *
2487 SliceType::get_element_type () const
2488 {
2489 return element_type.get_tyty ();
2490 }
2491
2492 const TyVar &
2493 SliceType::get_var_element_type () const
2494 {
2495 return element_type;
2496 }
2497
2498 BaseType *
2499 SliceType::clone () const
2500 {
2501 return new SliceType (get_ref (), get_ty_ref (), ident.locus,
2502 element_type.clone (), get_combined_refs ());
2503 }
2504
2505 SliceType *
2506 SliceType::handle_substitions (SubstitutionArgumentMappings &mappings)
2507 {
2508 auto &mappings_table = Analysis::Mappings::get ();
2509
2510 SliceType *ref = static_cast<SliceType *> (clone ());
2511 ref->set_ty_ref (mappings_table.get_next_hir_id ());
2512
2513 // might be &T or &ADT so this needs to be recursive
2514 auto base = ref->get_element_type ();
2515 BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
2516 ref->element_type = TyVar::subst_covariant_var (base, concrete);
2517
2518 return ref;
2519 }
2520
2521 // BoolType
2522
2523 BoolType::BoolType (HirId ref, std::set<HirId> refs)
2524 : BaseType (ref, ref, KIND,
2525 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2526 refs)
2527 {}
2528
2529 BoolType::BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs)
2530 : BaseType (ref, ty_ref, KIND,
2531 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2532 refs)
2533 {}
2534
2535 std::string
2536 BoolType::get_name () const
2537 {
2538 return as_string ();
2539 }
2540
2541 void
2542 BoolType::accept_vis (TyVisitor &vis)
2543 {
2544 vis.visit (*this);
2545 }
2546
2547 void
2548 BoolType::accept_vis (TyConstVisitor &vis) const
2549 {
2550 vis.visit (*this);
2551 }
2552
2553 std::string
2554 BoolType::as_string () const
2555 {
2556 return "bool";
2557 }
2558
2559 bool
2560 BoolType::can_eq (const BaseType *other, bool emit_errors) const
2561 {
2562 BoolCmp r (this, emit_errors);
2563 return r.can_eq (other);
2564 }
2565
2566 BaseType *
2567 BoolType::clone () const
2568 {
2569 return new BoolType (get_ref (), get_ty_ref (), get_combined_refs ());
2570 }
2571
2572 // IntType
2573
2574 IntType::IntType (HirId ref, IntKind kind, std::set<HirId> refs)
2575 : BaseType (ref, ref, KIND,
2576 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2577 refs),
2578 int_kind (kind)
2579 {}
2580
2581 IntType::IntType (HirId ref, HirId ty_ref, IntKind kind, std::set<HirId> refs)
2582 : BaseType (ref, ty_ref, KIND,
2583 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2584 refs),
2585 int_kind (kind)
2586 {}
2587
2588 std::string
2589 IntType::get_name () const
2590 {
2591 return as_string ();
2592 }
2593
2594 IntType::IntKind
2595 IntType::get_int_kind () const
2596 {
2597 return int_kind;
2598 }
2599
2600 void
2601 IntType::accept_vis (TyVisitor &vis)
2602 {
2603 vis.visit (*this);
2604 }
2605
2606 void
2607 IntType::accept_vis (TyConstVisitor &vis) const
2608 {
2609 vis.visit (*this);
2610 }
2611
2612 std::string
2613 IntType::as_string () const
2614 {
2615 switch (int_kind)
2616 {
2617 case I8:
2618 return "i8";
2619 case I16:
2620 return "i16";
2621 case I32:
2622 return "i32";
2623 case I64:
2624 return "i64";
2625 case I128:
2626 return "i128";
2627 }
2628 rust_unreachable ();
2629 return "__unknown_int_type";
2630 }
2631
2632 bool
2633 IntType::can_eq (const BaseType *other, bool emit_errors) const
2634 {
2635 IntCmp r (this, emit_errors);
2636 return r.can_eq (other);
2637 }
2638
2639 BaseType *
2640 IntType::clone () const
2641 {
2642 return new IntType (get_ref (), get_ty_ref (), get_int_kind (),
2643 get_combined_refs ());
2644 }
2645
2646 bool
2647 IntType::is_equal (const BaseType &other) const
2648 {
2649 if (!BaseType::is_equal (other))
2650 return false;
2651
2652 const IntType &o = static_cast<const IntType &> (other);
2653 return get_int_kind () == o.get_int_kind ();
2654 }
2655
2656 // UintType
2657
2658 UintType::UintType (HirId ref, UintKind kind, std::set<HirId> refs)
2659 : BaseType (ref, ref, KIND,
2660 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2661 refs),
2662 uint_kind (kind)
2663 {}
2664
2665 UintType::UintType (HirId ref, HirId ty_ref, UintKind kind,
2666 std::set<HirId> refs)
2667 : BaseType (ref, ty_ref, KIND,
2668 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2669 refs),
2670 uint_kind (kind)
2671 {}
2672
2673 std::string
2674 UintType::get_name () const
2675 {
2676 return as_string ();
2677 }
2678
2679 UintType::UintKind
2680 UintType::get_uint_kind () const
2681 {
2682 return uint_kind;
2683 }
2684
2685 void
2686 UintType::accept_vis (TyVisitor &vis)
2687 {
2688 vis.visit (*this);
2689 }
2690
2691 void
2692 UintType::accept_vis (TyConstVisitor &vis) const
2693 {
2694 vis.visit (*this);
2695 }
2696
2697 std::string
2698 UintType::as_string () const
2699 {
2700 switch (uint_kind)
2701 {
2702 case U8:
2703 return "u8";
2704 case U16:
2705 return "u16";
2706 case U32:
2707 return "u32";
2708 case U64:
2709 return "u64";
2710 case U128:
2711 return "u128";
2712 }
2713 rust_unreachable ();
2714 return "__unknown_uint_type";
2715 }
2716
2717 bool
2718 UintType::can_eq (const BaseType *other, bool emit_errors) const
2719 {
2720 UintCmp r (this, emit_errors);
2721 return r.can_eq (other);
2722 }
2723
2724 BaseType *
2725 UintType::clone () const
2726 {
2727 return new UintType (get_ref (), get_ty_ref (), get_uint_kind (),
2728 get_combined_refs ());
2729 }
2730
2731 bool
2732 UintType::is_equal (const BaseType &other) const
2733 {
2734 if (!BaseType::is_equal (other))
2735 return false;
2736
2737 const UintType &o = static_cast<const UintType &> (other);
2738 return get_uint_kind () == o.get_uint_kind ();
2739 }
2740
2741 // FloatType
2742
2743 FloatType::FloatType (HirId ref, FloatKind kind, std::set<HirId> refs)
2744 : BaseType (ref, ref, KIND,
2745 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2746 refs),
2747 float_kind (kind)
2748 {}
2749
2750 FloatType::FloatType (HirId ref, HirId ty_ref, FloatKind kind,
2751 std::set<HirId> refs)
2752 : BaseType (ref, ty_ref, KIND,
2753 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2754 refs),
2755 float_kind (kind)
2756 {}
2757
2758 std::string
2759 FloatType::get_name () const
2760 {
2761 return as_string ();
2762 }
2763
2764 FloatType::FloatKind
2765 FloatType::get_float_kind () const
2766 {
2767 return float_kind;
2768 }
2769
2770 void
2771 FloatType::accept_vis (TyVisitor &vis)
2772 {
2773 vis.visit (*this);
2774 }
2775
2776 void
2777 FloatType::accept_vis (TyConstVisitor &vis) const
2778 {
2779 vis.visit (*this);
2780 }
2781
2782 std::string
2783 FloatType::as_string () const
2784 {
2785 switch (float_kind)
2786 {
2787 case F32:
2788 return "f32";
2789 case F64:
2790 return "f64";
2791 }
2792 rust_unreachable ();
2793 return "__unknown_float_type";
2794 }
2795
2796 bool
2797 FloatType::can_eq (const BaseType *other, bool emit_errors) const
2798 {
2799 FloatCmp r (this, emit_errors);
2800 return r.can_eq (other);
2801 }
2802
2803 BaseType *
2804 FloatType::clone () const
2805 {
2806 return new FloatType (get_ref (), get_ty_ref (), get_float_kind (),
2807 get_combined_refs ());
2808 }
2809
2810 bool
2811 FloatType::is_equal (const BaseType &other) const
2812 {
2813 if (!BaseType::is_equal (other))
2814 return false;
2815
2816 const FloatType &o = static_cast<const FloatType &> (other);
2817 return get_float_kind () == o.get_float_kind ();
2818 }
2819
2820 // UsizeType
2821
2822 USizeType::USizeType (HirId ref, std::set<HirId> refs)
2823 : BaseType (ref, ref, KIND,
2824 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2825 refs)
2826 {}
2827
2828 USizeType::USizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
2829 : BaseType (ref, ty_ref, KIND,
2830 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2831 refs)
2832 {}
2833
2834 std::string
2835 USizeType::get_name () const
2836 {
2837 return as_string ();
2838 }
2839
2840 void
2841 USizeType::accept_vis (TyVisitor &vis)
2842 {
2843 vis.visit (*this);
2844 }
2845
2846 void
2847 USizeType::accept_vis (TyConstVisitor &vis) const
2848 {
2849 vis.visit (*this);
2850 }
2851
2852 std::string
2853 USizeType::as_string () const
2854 {
2855 return "usize";
2856 }
2857
2858 bool
2859 USizeType::can_eq (const BaseType *other, bool emit_errors) const
2860 {
2861 USizeCmp r (this, emit_errors);
2862 return r.can_eq (other);
2863 }
2864
2865 BaseType *
2866 USizeType::clone () const
2867 {
2868 return new USizeType (get_ref (), get_ty_ref (), get_combined_refs ());
2869 }
2870
2871 // ISizeType
2872
2873 ISizeType::ISizeType (HirId ref, std::set<HirId> refs)
2874 : BaseType (ref, ref, KIND,
2875 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2876 refs)
2877 {}
2878
2879 ISizeType::ISizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
2880 : BaseType (ref, ty_ref, KIND,
2881 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2882 refs)
2883 {}
2884
2885 std::string
2886 ISizeType::get_name () const
2887 {
2888 return as_string ();
2889 }
2890
2891 void
2892 ISizeType::accept_vis (TyVisitor &vis)
2893 {
2894 vis.visit (*this);
2895 }
2896
2897 void
2898 ISizeType::accept_vis (TyConstVisitor &vis) const
2899 {
2900 vis.visit (*this);
2901 }
2902
2903 std::string
2904 ISizeType::as_string () const
2905 {
2906 return "isize";
2907 }
2908
2909 bool
2910 ISizeType::can_eq (const BaseType *other, bool emit_errors) const
2911 {
2912 ISizeCmp r (this, emit_errors);
2913 return r.can_eq (other);
2914 }
2915
2916 BaseType *
2917 ISizeType::clone () const
2918 {
2919 return new ISizeType (get_ref (), get_ty_ref (), get_combined_refs ());
2920 }
2921
2922 // Char Type
2923
2924 CharType::CharType (HirId ref, std::set<HirId> refs)
2925 : BaseType (ref, ref, KIND,
2926 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2927 refs)
2928 {}
2929
2930 CharType::CharType (HirId ref, HirId ty_ref, std::set<HirId> refs)
2931 : BaseType (ref, ty_ref, KIND,
2932 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2933 refs)
2934 {}
2935
2936 std::string
2937 CharType::get_name () const
2938 {
2939 return as_string ();
2940 }
2941
2942 void
2943 CharType::accept_vis (TyVisitor &vis)
2944 {
2945 vis.visit (*this);
2946 }
2947
2948 void
2949 CharType::accept_vis (TyConstVisitor &vis) const
2950 {
2951 vis.visit (*this);
2952 }
2953
2954 std::string
2955 CharType::as_string () const
2956 {
2957 return "char";
2958 }
2959
2960 bool
2961 CharType::can_eq (const BaseType *other, bool emit_errors) const
2962 {
2963 CharCmp r (this, emit_errors);
2964 return r.can_eq (other);
2965 }
2966
2967 BaseType *
2968 CharType::clone () const
2969 {
2970 return new CharType (get_ref (), get_ty_ref (), get_combined_refs ());
2971 }
2972
2973 // Reference Type
2974
2975 ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut,
2976 Region region, std::set<HirId> refs)
2977 : BaseType (ref, ref, KIND,
2978 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2979 std::move (refs)),
2980 base (base), mut (mut), region (region)
2981 {}
2982
2983 ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base,
2984 Mutability mut, Region region,
2985 std::set<HirId> refs)
2986 : BaseType (ref, ty_ref, KIND,
2987 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
2988 std::move (refs)),
2989 base (base), mut (mut), region (region)
2990 {}
2991
2992 Mutability
2993 ReferenceType::mutability () const
2994 {
2995 return mut;
2996 }
2997
2998 bool
2999 ReferenceType::is_mutable () const
3000 {
3001 return mut == Mutability::Mut;
3002 }
3003 Region
3004 ReferenceType::get_region () const
3005 {
3006 return region;
3007 }
3008
3009 bool
3010 ReferenceType::is_dyn_object () const
3011 {
3012 return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
3013 }
3014
3015 bool
3016 ReferenceType::is_dyn_slice_type (const TyTy::SliceType **slice) const
3017 {
3018 const TyTy::BaseType *element = get_base ()->destructure ();
3019 if (element->get_kind () != TyTy::TypeKind::SLICE)
3020 return false;
3021 if (slice == nullptr)
3022 return true;
3023
3024 *slice = static_cast<const TyTy::SliceType *> (element);
3025 return true;
3026 }
3027
3028 bool
3029 ReferenceType::is_dyn_str_type (const TyTy::StrType **str) const
3030 {
3031 const TyTy::BaseType *element = get_base ()->destructure ();
3032 if (element->get_kind () != TyTy::TypeKind::STR)
3033 return false;
3034 if (str == nullptr)
3035 return true;
3036
3037 *str = static_cast<const TyTy::StrType *> (element);
3038 return true;
3039 }
3040
3041 bool
3042 ReferenceType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const
3043 {
3044 const TyTy::BaseType *element = get_base ()->destructure ();
3045 if (element->get_kind () != TyTy::TypeKind::DYNAMIC)
3046 return false;
3047 if (dyn == nullptr)
3048 return true;
3049
3050 *dyn = static_cast<const TyTy::DynamicObjectType *> (element);
3051 return true;
3052 }
3053
3054 void
3055 ReferenceType::accept_vis (TyVisitor &vis)
3056 {
3057 vis.visit (*this);
3058 }
3059
3060 void
3061 ReferenceType::accept_vis (TyConstVisitor &vis) const
3062 {
3063 vis.visit (*this);
3064 }
3065
3066 std::string
3067 ReferenceType::as_string () const
3068 {
3069 return std::string ("&") + (is_mutable () ? "mut" : "") + " "
3070 + get_base ()->as_string ();
3071 }
3072
3073 std::string
3074 ReferenceType::get_name () const
3075 {
3076 return std::string ("&") + (is_mutable () ? "mut" : "") + " "
3077 + get_base ()->get_name ();
3078 }
3079
3080 bool
3081 ReferenceType::can_eq (const BaseType *other, bool emit_errors) const
3082 {
3083 ReferenceCmp r (this, emit_errors);
3084 return r.can_eq (other);
3085 }
3086
3087 bool
3088 ReferenceType::is_equal (const BaseType &other) const
3089 {
3090 if (get_kind () != other.get_kind ())
3091 return false;
3092
3093 auto other2 = static_cast<const ReferenceType &> (other);
3094 if (mutability () != other2.mutability ())
3095 return false;
3096
3097 return get_base ()->is_equal (*other2.get_base ());
3098 }
3099
3100 BaseType *
3101 ReferenceType::get_base () const
3102 {
3103 return base.get_tyty ();
3104 }
3105
3106 const TyVar &
3107 ReferenceType::get_var_element_type () const
3108 {
3109 return base;
3110 }
3111
3112 BaseType *
3113 ReferenceType::clone () const
3114 {
3115 return new ReferenceType (get_ref (), get_ty_ref (), base, mutability (),
3116 get_region (), get_combined_refs ());
3117 }
3118
3119 ReferenceType *
3120 ReferenceType::handle_substitions (SubstitutionArgumentMappings &mappings)
3121 {
3122 auto &mappings_table = Analysis::Mappings::get ();
3123
3124 ReferenceType *ref = static_cast<ReferenceType *> (clone ());
3125 ref->set_ty_ref (mappings_table.get_next_hir_id ());
3126
3127 // might be &T or &ADT so this needs to be recursive
3128 auto base = ref->get_base ();
3129 BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
3130 ref->base = TyVar::subst_covariant_var (base, concrete);
3131
3132 return ref;
3133 }
3134
3135 // PointerType
3136
3137 PointerType::PointerType (HirId ref, TyVar base, Mutability mut,
3138 std::set<HirId> refs)
3139 : BaseType (ref, ref, KIND,
3140 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3141 refs),
3142 base (base), mut (mut)
3143 {}
3144
3145 PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
3146 std::set<HirId> refs)
3147 : BaseType (ref, ty_ref, KIND,
3148 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3149 refs),
3150 base (base), mut (mut)
3151 {}
3152
3153 Mutability
3154 PointerType::mutability () const
3155 {
3156 return mut;
3157 }
3158
3159 bool
3160 PointerType::is_mutable () const
3161 {
3162 return mut == Mutability::Mut;
3163 }
3164
3165 bool
3166 PointerType::is_const () const
3167 {
3168 return mut == Mutability::Imm;
3169 }
3170
3171 bool
3172 PointerType::is_dyn_object () const
3173 {
3174 return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
3175 }
3176
3177 bool
3178 PointerType::is_dyn_slice_type (const TyTy::SliceType **slice) const
3179 {
3180 const TyTy::BaseType *element = get_base ()->destructure ();
3181 if (element->get_kind () != TyTy::TypeKind::SLICE)
3182 return false;
3183 if (slice == nullptr)
3184 return true;
3185
3186 *slice = static_cast<const TyTy::SliceType *> (element);
3187 return true;
3188 }
3189
3190 bool
3191 PointerType::is_dyn_str_type (const TyTy::StrType **str) const
3192 {
3193 const TyTy::BaseType *element = get_base ()->destructure ();
3194 if (element->get_kind () != TyTy::TypeKind::STR)
3195 return false;
3196 if (str == nullptr)
3197 return true;
3198
3199 *str = static_cast<const TyTy::StrType *> (element);
3200 return true;
3201 }
3202
3203 bool
3204 PointerType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const
3205 {
3206 const TyTy::BaseType *element = get_base ()->destructure ();
3207 if (element->get_kind () != TyTy::TypeKind::DYNAMIC)
3208 return false;
3209 if (dyn == nullptr)
3210 return true;
3211
3212 *dyn = static_cast<const TyTy::DynamicObjectType *> (element);
3213 return true;
3214 }
3215
3216 void
3217 PointerType::accept_vis (TyVisitor &vis)
3218 {
3219 vis.visit (*this);
3220 }
3221
3222 void
3223 PointerType::accept_vis (TyConstVisitor &vis) const
3224 {
3225 vis.visit (*this);
3226 }
3227
3228 std::string
3229 PointerType::as_string () const
3230 {
3231 return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
3232 + get_base ()->as_string ();
3233 }
3234
3235 std::string
3236 PointerType::get_name () const
3237 {
3238 return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
3239 + get_base ()->get_name ();
3240 }
3241
3242 bool
3243 PointerType::can_eq (const BaseType *other, bool emit_errors) const
3244 {
3245 PointerCmp r (this, emit_errors);
3246 return r.can_eq (other);
3247 }
3248
3249 bool
3250 PointerType::is_equal (const BaseType &other) const
3251 {
3252 if (get_kind () != other.get_kind ())
3253 return false;
3254
3255 auto other2 = static_cast<const PointerType &> (other);
3256 if (mutability () != other2.mutability ())
3257 return false;
3258
3259 return get_base ()->is_equal (*other2.get_base ());
3260 }
3261
3262 BaseType *
3263 PointerType::get_base () const
3264 {
3265 return base.get_tyty ();
3266 }
3267
3268 const TyVar &
3269 PointerType::get_var_element_type () const
3270 {
3271 return base;
3272 }
3273
3274 BaseType *
3275 PointerType::clone () const
3276 {
3277 return new PointerType (get_ref (), get_ty_ref (), base, mutability (),
3278 get_combined_refs ());
3279 }
3280
3281 PointerType *
3282 PointerType::handle_substitions (SubstitutionArgumentMappings &mappings)
3283 {
3284 auto &mappings_table = Analysis::Mappings::get ();
3285
3286 PointerType *ref = static_cast<PointerType *> (clone ());
3287 ref->set_ty_ref (mappings_table.get_next_hir_id ());
3288
3289 // might be &T or &ADT so this needs to be recursive
3290 auto base = ref->get_base ();
3291 BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
3292 ref->base = TyVar::subst_covariant_var (base, concrete);
3293
3294 return ref;
3295 }
3296
3297 // PARAM Type
3298
3299 ParamType::ParamType (std::string symbol, location_t locus, HirId ref,
3300 HIR::GenericParam &param,
3301 std::vector<TypeBoundPredicate> specified_bounds,
3302 std::set<HirId> refs)
3303 : BaseType (ref, ref, KIND,
3304 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
3305 locus},
3306 specified_bounds, refs),
3307 is_trait_self (false), symbol (symbol), param (param)
3308 {}
3309
3310 ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus,
3311 HirId ref, HirId ty_ref, HIR::GenericParam &param,
3312 std::vector<TypeBoundPredicate> specified_bounds,
3313 std::set<HirId> refs)
3314 : BaseType (ref, ty_ref, KIND,
3315 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
3316 locus},
3317 specified_bounds, refs),
3318 is_trait_self (is_trait_self), symbol (symbol), param (param)
3319 {}
3320
3321 HIR::GenericParam &
3322 ParamType::get_generic_param ()
3323 {
3324 return param;
3325 }
3326
3327 bool
3328 ParamType::can_resolve () const
3329 {
3330 return get_ref () != get_ty_ref ();
3331 }
3332
3333 void
3334 ParamType::accept_vis (TyVisitor &vis)
3335 {
3336 vis.visit (*this);
3337 }
3338
3339 void
3340 ParamType::accept_vis (TyConstVisitor &vis) const
3341 {
3342 vis.visit (*this);
3343 }
3344
3345 std::string
3346 ParamType::as_string () const
3347 {
3348 if (!can_resolve ())
3349 {
3350 return get_symbol () + " REF: " + std::to_string (get_ref ());
3351 }
3352
3353 BaseType *lookup = resolve ();
3354 return get_symbol () + "=" + lookup->as_string ();
3355 }
3356
3357 std::string
3358 ParamType::get_name () const
3359 {
3360 if (!can_resolve ())
3361 return get_symbol ();
3362
3363 return destructure ()->get_name ();
3364 }
3365
3366 bool
3367 ParamType::can_eq (const BaseType *other, bool emit_errors) const
3368 {
3369 ParamCmp r (this, emit_errors);
3370 return r.can_eq (other);
3371 }
3372
3373 BaseType *
3374 ParamType::clone () const
3375 {
3376 return new ParamType (is_trait_self, get_symbol (), ident.locus, get_ref (),
3377 get_ty_ref (), param, get_specified_bounds (),
3378 get_combined_refs ());
3379 }
3380
3381 std::string
3382 ParamType::get_symbol () const
3383 {
3384 return symbol;
3385 }
3386
3387 BaseType *
3388 ParamType::resolve () const
3389 {
3390 TyVar var (get_ty_ref ());
3391 BaseType *r = var.get_tyty ();
3392
3393 while (r->get_kind () == TypeKind::PARAM)
3394 {
3395 ParamType *rr = static_cast<ParamType *> (r);
3396 if (!rr->can_resolve ())
3397 break;
3398
3399 TyVar v (rr->get_ty_ref ());
3400 BaseType *n = v.get_tyty ();
3401
3402 // fix infinite loop
3403 if (r == n)
3404 break;
3405
3406 r = n;
3407 }
3408
3409 if (r->get_kind () == TypeKind::PARAM && (r->get_ref () == r->get_ty_ref ()))
3410 return TyVar (r->get_ty_ref ()).get_tyty ();
3411
3412 return r;
3413 }
3414
3415 bool
3416 ParamType::is_equal (const BaseType &other) const
3417 {
3418 if (get_kind () != other.get_kind ())
3419 {
3420 if (!can_resolve ())
3421 return false;
3422
3423 return resolve ()->is_equal (other);
3424 }
3425
3426 auto other2 = static_cast<const ParamType &> (other);
3427 if (can_resolve () != other2.can_resolve ())
3428 return false;
3429
3430 if (can_resolve ())
3431 return resolve ()->can_eq (other2.resolve (), false);
3432
3433 return get_symbol ().compare (other2.get_symbol ()) == 0;
3434 }
3435
3436 ParamType *
3437 ParamType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
3438 {
3439 SubstitutionArg arg = SubstitutionArg::error ();
3440 bool ok = subst_mappings.get_argument_for_symbol (this, &arg);
3441 if (!ok || arg.is_error ())
3442 return this;
3443
3444 ParamType *p = static_cast<ParamType *> (clone ());
3445 subst_mappings.on_param_subst (*p, arg);
3446
3447 // there are two cases one where we substitute directly to a new PARAM and
3448 // otherwise
3449 if (arg.get_tyty ()->get_kind () == TyTy::TypeKind::PARAM)
3450 {
3451 p->set_ty_ref (arg.get_tyty ()->get_ref ());
3452 return p;
3453 }
3454
3455 // this is the new subst that this needs to pass
3456 p->set_ref (mappings.get_next_hir_id ());
3457 p->set_ty_ref (arg.get_tyty ()->get_ref ());
3458
3459 return p;
3460 }
3461
3462 void
3463 ParamType::set_implicit_self_trait ()
3464 {
3465 is_trait_self = true;
3466 }
3467
3468 bool
3469 ParamType::is_implicit_self_trait () const
3470 {
3471 return is_trait_self;
3472 }
3473
3474 // OpaqueType
3475
3476 OpaqueType::OpaqueType (location_t locus, HirId ref,
3477 std::vector<TypeBoundPredicate> specified_bounds,
3478 std::set<HirId> refs)
3479 : BaseType (ref, ref, KIND,
3480 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"),
3481 locus},
3482 specified_bounds, refs)
3483 {}
3484
3485 OpaqueType::OpaqueType (location_t locus, HirId ref, HirId ty_ref,
3486 std::vector<TypeBoundPredicate> specified_bounds,
3487 std::set<HirId> refs)
3488 : BaseType (ref, ty_ref, KIND,
3489 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"),
3490 locus},
3491 specified_bounds, refs)
3492 {}
3493
3494 bool
3495 OpaqueType::can_resolve () const
3496 {
3497 return get_ref () != get_ty_ref ();
3498 }
3499
3500 void
3501 OpaqueType::accept_vis (TyVisitor &vis)
3502 {
3503 vis.visit (*this);
3504 }
3505
3506 void
3507 OpaqueType::accept_vis (TyConstVisitor &vis) const
3508 {
3509 vis.visit (*this);
3510 }
3511
3512 std::string
3513 OpaqueType::as_string () const
3514 {
3515 return get_name ();
3516 }
3517
3518 std::string
3519 OpaqueType::get_name () const
3520 {
3521 return "impl " + raw_bounds_as_name ();
3522 }
3523
3524 bool
3525 OpaqueType::can_eq (const BaseType *other, bool emit_errors) const
3526 {
3527 OpaqueCmp r (this, emit_errors);
3528 return r.can_eq (other);
3529 }
3530
3531 BaseType *
3532 OpaqueType::clone () const
3533 {
3534 return new OpaqueType (ident.locus, get_ref (), get_ty_ref (),
3535 get_specified_bounds (), get_combined_refs ());
3536 }
3537
3538 BaseType *
3539 OpaqueType::resolve () const
3540 {
3541 TyVar var (get_ty_ref ());
3542 BaseType *r = var.get_tyty ();
3543
3544 while (r->get_kind () == TypeKind::OPAQUE)
3545 {
3546 OpaqueType *rr = static_cast<OpaqueType *> (r);
3547 if (!rr->can_resolve ())
3548 break;
3549
3550 TyVar v (rr->get_ty_ref ());
3551 BaseType *n = v.get_tyty ();
3552
3553 // fix infinite loop
3554 if (r == n)
3555 break;
3556
3557 r = n;
3558 }
3559
3560 if (r->get_kind () == TypeKind::OPAQUE && (r->get_ref () == r->get_ty_ref ()))
3561 return TyVar (r->get_ty_ref ()).get_tyty ();
3562
3563 return r;
3564 }
3565
3566 bool
3567 OpaqueType::is_equal (const BaseType &other) const
3568 {
3569 auto other2 = static_cast<const OpaqueType &> (other);
3570 if (can_resolve () != other2.can_resolve ())
3571 return false;
3572
3573 if (can_resolve ())
3574 return resolve ()->can_eq (other2.resolve (), false);
3575
3576 return bounds_compatible (other, UNDEF_LOCATION, false);
3577 }
3578
3579 OpaqueType *
3580 OpaqueType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
3581 {
3582 // SubstitutionArg arg = SubstitutionArg::error ();
3583 // bool ok = subst_mappings.get_argument_for_symbol (this, &arg);
3584 // if (!ok || arg.is_error ())
3585 // return this;
3586
3587 // OpaqueType *p = static_cast<OpaqueType *> (clone ());
3588 // subst_mappings.on_param_subst (*p, arg);
3589
3590 // // there are two cases one where we substitute directly to a new PARAM and
3591 // // otherwise
3592 // if (arg.get_tyty ()->get_kind () == TyTy::TypeKind::PARAM)
3593 // {
3594 // p->set_ty_ref (arg.get_tyty ()->get_ref ());
3595 // return p;
3596 // }
3597
3598 // // this is the new subst that this needs to pass
3599 // p->set_ref (mappings.get_next_hir_id ());
3600 // p->set_ty_ref (arg.get_tyty ()->get_ref ());
3601
3602 // return p;
3603
3604 rust_unreachable ();
3605 return nullptr;
3606 }
3607
3608 // StrType
3609
3610 StrType::StrType (HirId ref, std::set<HirId> refs)
3611 : BaseType (ref, ref, KIND,
3612 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3613 refs)
3614 {}
3615
3616 StrType::StrType (HirId ref, HirId ty_ref, std::set<HirId> refs)
3617 : BaseType (ref, ty_ref, KIND,
3618 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3619 refs)
3620 {}
3621
3622 std::string
3623 StrType::get_name () const
3624 {
3625 return as_string ();
3626 }
3627
3628 BaseType *
3629 StrType::clone () const
3630 {
3631 return new StrType (get_ref (), get_ty_ref (), get_combined_refs ());
3632 }
3633
3634 void
3635 StrType::accept_vis (TyVisitor &vis)
3636 {
3637 vis.visit (*this);
3638 }
3639
3640 void
3641 StrType::accept_vis (TyConstVisitor &vis) const
3642 {
3643 vis.visit (*this);
3644 }
3645
3646 std::string
3647 StrType::as_string () const
3648 {
3649 return "str";
3650 }
3651
3652 bool
3653 StrType::can_eq (const BaseType *other, bool emit_errors) const
3654 {
3655 StrCmp r (this, emit_errors);
3656 return r.can_eq (other);
3657 }
3658
3659 bool
3660 StrType::is_equal (const BaseType &other) const
3661 {
3662 return get_kind () == other.get_kind ();
3663 }
3664
3665 // Never Type
3666
3667 NeverType::NeverType (HirId ref, std::set<HirId> refs)
3668 : BaseType (ref, ref, KIND,
3669 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3670 refs)
3671 {}
3672
3673 NeverType::NeverType (HirId ref, HirId ty_ref, std::set<HirId> refs)
3674 : BaseType (ref, ty_ref, KIND,
3675 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3676 refs)
3677 {}
3678
3679 std::string
3680 NeverType::get_name () const
3681 {
3682 return as_string ();
3683 }
3684
3685 void
3686 NeverType::accept_vis (TyVisitor &vis)
3687 {
3688 vis.visit (*this);
3689 }
3690
3691 void
3692 NeverType::accept_vis (TyConstVisitor &vis) const
3693 {
3694 vis.visit (*this);
3695 }
3696
3697 std::string
3698 NeverType::as_string () const
3699 {
3700 return "!";
3701 }
3702
3703 bool
3704 NeverType::can_eq (const BaseType *other, bool emit_errors) const
3705 {
3706 NeverCmp r (this, emit_errors);
3707 return r.can_eq (other);
3708 }
3709
3710 BaseType *
3711 NeverType::clone () const
3712 {
3713 return new NeverType (get_ref (), get_ty_ref (), get_combined_refs ());
3714 }
3715
3716 // placeholder type
3717
3718 PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref,
3719 std::set<HirId> refs)
3720 : BaseType (ref, ref, KIND,
3721 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3722 refs),
3723 symbol (symbol), defId (id)
3724 {}
3725
3726 PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref,
3727 HirId ty_ref, std::set<HirId> refs)
3728 : BaseType (ref, ty_ref, KIND,
3729 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3730 refs),
3731 symbol (symbol), defId (id)
3732 {}
3733
3734 std::string
3735 PlaceholderType::get_name () const
3736 {
3737 return as_string ();
3738 }
3739
3740 std::string
3741 PlaceholderType::get_symbol () const
3742 {
3743 return symbol;
3744 }
3745
3746 void
3747 PlaceholderType::accept_vis (TyVisitor &vis)
3748 {
3749 vis.visit (*this);
3750 }
3751
3752 void
3753 PlaceholderType::accept_vis (TyConstVisitor &vis) const
3754 {
3755 vis.visit (*this);
3756 }
3757
3758 std::string
3759 PlaceholderType::as_string () const
3760 {
3761 return "<placeholder:" + (can_resolve () ? resolve ()->as_string () : "")
3762 + ">";
3763 }
3764
3765 bool
3766 PlaceholderType::can_eq (const BaseType *other, bool emit_errors) const
3767 {
3768 PlaceholderCmp r (this, emit_errors);
3769 return r.can_eq (other);
3770 }
3771
3772 BaseType *
3773 PlaceholderType::clone () const
3774 {
3775 return new PlaceholderType (get_symbol (), get_def_id (), get_ref (),
3776 get_ty_ref (), get_combined_refs ());
3777 }
3778
3779 void
3780 PlaceholderType::set_associated_type (HirId ref)
3781 {
3782 auto context = Resolver::TypeCheckContext::get ();
3783 context->insert_associated_type_mapping (get_ty_ref (), ref);
3784 }
3785
3786 void
3787 PlaceholderType::clear_associated_type ()
3788 {
3789 auto context = Resolver::TypeCheckContext::get ();
3790 context->clear_associated_type_mapping (get_ty_ref ());
3791 }
3792
3793 bool
3794 PlaceholderType::can_resolve () const
3795 {
3796 auto context = Resolver::TypeCheckContext::get ();
3797
3798 BaseType *lookup = nullptr;
3799 HirId mapping;
3800
3801 if (!context->lookup_associated_type_mapping (get_ty_ref (), &mapping))
3802 return false;
3803
3804 if (!context->lookup_type (mapping, &lookup))
3805 return false;
3806
3807 return lookup != nullptr;
3808 }
3809
3810 BaseType *
3811 PlaceholderType::resolve () const
3812 {
3813 auto context = Resolver::TypeCheckContext::get ();
3814
3815 HirId mapping;
3816 bool ok = context->lookup_associated_type_mapping (get_ty_ref (), &mapping);
3817 rust_assert (ok);
3818
3819 return TyVar (mapping).get_tyty ();
3820 }
3821
3822 bool
3823 PlaceholderType::is_equal (const BaseType &other) const
3824 {
3825 if (get_kind () != other.get_kind ())
3826 {
3827 if (!can_resolve ())
3828 return false;
3829
3830 return resolve ()->is_equal (other);
3831 }
3832
3833 auto other2 = static_cast<const PlaceholderType &> (other);
3834 return get_symbol ().compare (other2.get_symbol ()) == 0;
3835 }
3836
3837 DefId
3838 PlaceholderType::get_def_id () const
3839 {
3840 return defId;
3841 }
3842
3843 // Projection type
3844
3845 ProjectionType::ProjectionType (
3846 HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item,
3847 std::vector<SubstitutionParamMapping> subst_refs,
3848 SubstitutionArgumentMappings generic_arguments,
3849 RegionConstraints region_constraints, std::set<HirId> refs)
3850 : BaseType (ref, ref, KIND,
3851 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3852 std::move (refs)),
3853 SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
3854 std::move (region_constraints)),
3855 base (base), trait (trait), item (item)
3856 {}
3857
3858 ProjectionType::ProjectionType (
3859 HirId ref, HirId ty_ref, BaseType *base,
3860 const Resolver::TraitReference *trait, DefId item,
3861 std::vector<SubstitutionParamMapping> subst_refs,
3862 SubstitutionArgumentMappings generic_arguments,
3863 RegionConstraints region_constraints, std::set<HirId> refs)
3864 : BaseType (ref, ty_ref, KIND,
3865 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
3866 refs),
3867 SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
3868 std::move (region_constraints)),
3869 base (base), trait (trait), item (item)
3870 {}
3871
3872 std::string
3873 ProjectionType::get_name () const
3874 {
3875 return as_string ();
3876 }
3877
3878 const BaseType *
3879 ProjectionType::get () const
3880 {
3881 return base;
3882 }
3883
3884 BaseType *
3885 ProjectionType::get ()
3886 {
3887 return base;
3888 }
3889
3890 void
3891 ProjectionType::accept_vis (TyVisitor &vis)
3892 {
3893 vis.visit (*this);
3894 }
3895
3896 void
3897 ProjectionType::accept_vis (TyConstVisitor &vis) const
3898 {
3899 vis.visit (*this);
3900 }
3901
3902 std::string
3903 ProjectionType::as_string () const
3904 {
3905 return "<Projection=" + subst_as_string () + "::" + base->as_string () + ">";
3906 }
3907
3908 bool
3909 ProjectionType::can_eq (const BaseType *other, bool emit_errors) const
3910 {
3911 return base->can_eq (other, emit_errors);
3912 }
3913
3914 BaseType *
3915 ProjectionType::clone () const
3916 {
3917 return new ProjectionType (get_ref (), get_ty_ref (), base->clone (), trait,
3918 item, clone_substs (), used_arguments,
3919 region_constraints, get_combined_refs ());
3920 }
3921
3922 ProjectionType *
3923 ProjectionType::handle_substitions (
3924 SubstitutionArgumentMappings &subst_mappings)
3925 {
3926 // // do we really need to substitute this?
3927 // if (base->needs_generic_substitutions () ||
3928 // base->contains_type_parameters
3929 // ())
3930 // {
3931 // return this;
3932 // }
3933
3934 ProjectionType *projection = static_cast<ProjectionType *> (clone ());
3935 projection->set_ty_ref (mappings.get_next_hir_id ());
3936 projection->used_arguments = subst_mappings;
3937
3938 auto context = Resolver::TypeCheckContext::get ();
3939 context->insert_implicit_type (projection->get_ty_ref (), projection);
3940
3941 for (auto &sub : projection->get_substs ())
3942 {
3943 SubstitutionArg arg = SubstitutionArg::error ();
3944 bool ok
3945 = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
3946 if (ok)
3947 sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
3948 }
3949
3950 auto fty = projection->base;
3951 bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
3952 if (is_param_ty)
3953 {
3954 ParamType *p = static_cast<ParamType *> (fty);
3955
3956 SubstitutionArg arg = SubstitutionArg::error ();
3957 bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
3958 if (ok)
3959 {
3960 auto argt = arg.get_tyty ();
3961 bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
3962 bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
3963
3964 if (arg_is_param || arg_is_concrete)
3965 {
3966 auto new_field = argt->clone ();
3967 new_field->set_ref (fty->get_ref ());
3968 projection->base = new_field;
3969 }
3970 else
3971 {
3972 fty->set_ty_ref (argt->get_ref ());
3973 }
3974 }
3975 }
3976 else if (fty->needs_generic_substitutions () || !fty->is_concrete ())
3977 {
3978 BaseType *concrete
3979 = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
3980
3981 if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR)
3982 {
3983 rust_error_at (subst_mappings.get_locus (),
3984 "Failed to resolve field substitution type: %s",
3985 fty->as_string ().c_str ());
3986 return nullptr;
3987 }
3988
3989 projection->base = concrete;
3990 }
3991
3992 return projection;
3993 }
3994
3995 // DynObjectType
3996
3997 DynamicObjectType::DynamicObjectType (
3998 HirId ref, RustIdent ident, std::vector<TypeBoundPredicate> specified_bounds,
3999 std::set<HirId> refs)
4000 : BaseType (ref, ref, KIND, ident, specified_bounds, refs)
4001 {}
4002
4003 DynamicObjectType::DynamicObjectType (
4004 HirId ref, HirId ty_ref, RustIdent ident,
4005 std::vector<TypeBoundPredicate> specified_bounds, std::set<HirId> refs)
4006 : BaseType (ref, ty_ref, KIND, ident, specified_bounds, refs)
4007 {}
4008
4009 void
4010 DynamicObjectType::accept_vis (TyVisitor &vis)
4011 {
4012 vis.visit (*this);
4013 }
4014
4015 void
4016 DynamicObjectType::accept_vis (TyConstVisitor &vis) const
4017 {
4018 vis.visit (*this);
4019 }
4020
4021 std::string
4022 DynamicObjectType::as_string () const
4023 {
4024 return "dyn [" + raw_bounds_as_string () + "]";
4025 }
4026
4027 bool
4028 DynamicObjectType::can_eq (const BaseType *other, bool emit_errors) const
4029 {
4030 DynamicCmp r (this, emit_errors);
4031 return r.can_eq (other);
4032 }
4033
4034 BaseType *
4035 DynamicObjectType::clone () const
4036 {
4037 return new DynamicObjectType (get_ref (), get_ty_ref (), ident,
4038 specified_bounds, get_combined_refs ());
4039 }
4040
4041 std::string
4042 DynamicObjectType::get_name () const
4043 {
4044 return "dyn [" + raw_bounds_as_name () + "]";
4045 }
4046
4047 bool
4048 DynamicObjectType::is_equal (const BaseType &other) const
4049 {
4050 if (get_kind () != other.get_kind ())
4051 return false;
4052
4053 if (num_specified_bounds () != other.num_specified_bounds ())
4054 return false;
4055
4056 return bounds_compatible (other, UNDEF_LOCATION, false);
4057 }
4058
4059 const std::vector<
4060 std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
4061 DynamicObjectType::get_object_items () const
4062 {
4063 std::vector<
4064 std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
4065 items;
4066 for (const TypeBoundPredicate &bound : get_specified_bounds ())
4067 {
4068 const Resolver::TraitReference *trait = bound.get ();
4069 std::vector<const Resolver::TraitItemReference *> trait_items;
4070 trait->get_trait_items_and_supers (trait_items);
4071
4072 for (auto &item : trait_items)
4073 {
4074 if (item->get_trait_item_type ()
4075 == Resolver::TraitItemReference::TraitItemType::FN
4076 && item->is_object_safe ())
4077 items.push_back ({item, &bound});
4078 }
4079 }
4080 return items;
4081 }
4082
4083 } // namespace TyTy
4084 } // namespace Rust