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