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