Debian Codegraph @

gcc/rust/typecheck/rust-tyty-cmp.h

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #ifndef RUST_TYTY_CMP_H
20 #define RUST_TYTY_CMP_H
21
22 #include "rust-diagnostics.h"
23 #include "rust-tyty.h"
24 #include "rust-tyty-visitor.h"
25 #include "rust-hir-map.h"
26 #include "rust-hir-type-check.h"
27
28 namespace Rust {
29 namespace TyTy {
30
31 class BaseCmp : public TyConstVisitor
32 {
33 public:
34 virtual bool can_eq (const BaseType *other)
35 {
36 if (other->get_kind () == TypeKind::PARAM)
37 {
38 const ParamType *p = static_cast<const ParamType *> (other);
39 other = p->resolve ();
40 }
41 if (other->get_kind () == TypeKind::PLACEHOLDER)
42 {
43 const PlaceholderType *p = static_cast<const PlaceholderType *> (other);
44 if (p->can_resolve ())
45 {
46 other = p->resolve ();
47 }
48 }
49 if (other->get_kind () == TypeKind::PROJECTION)
50 {
51 const ProjectionType *p = static_cast<const ProjectionType *> (other);
52 other = p->get ();
53 }
54
55 other->accept_vis (*this);
56 return ok;
57 }
58
59 virtual void visit (const TupleType &type) override
60 {
61 ok = false;
62
63 if (emit_error_flag)
64 {
65 location_t ref_locus = mappings.lookup_location (type.get_ref ());
66 location_t base_locus
67 = mappings.lookup_location (get_base ()->get_ref ());
68 rich_location r (line_table, ref_locus);
69 r.add_range (base_locus);
70 rust_error_at (r, "expected [%s] got [%s]",
71 get_base ()->as_string ().c_str (),
72 type.as_string ().c_str ());
73 }
74 }
75
76 virtual void visit (const ADTType &type) override
77 {
78 ok = false;
79 if (emit_error_flag)
80 {
81 location_t ref_locus = mappings.lookup_location (type.get_ref ());
82 location_t base_locus
83 = mappings.lookup_location (get_base ()->get_ref ());
84 rich_location r (line_table, ref_locus);
85 r.add_range (base_locus);
86 rust_error_at (r, "expected [%s] got [%s]",
87 get_base ()->as_string ().c_str (),
88 type.as_string ().c_str ());
89 }
90 }
91
92 virtual void visit (const InferType &type) override
93 {
94 ok = false;
95 if (emit_error_flag)
96 {
97 location_t ref_locus = mappings.lookup_location (type.get_ref ());
98 location_t base_locus
99 = mappings.lookup_location (get_base ()->get_ref ());
100 rich_location r (line_table, ref_locus);
101 r.add_range (base_locus);
102 rust_error_at (r, "expected [%s] got [%s]",
103 get_base ()->as_string ().c_str (),
104 type.as_string ().c_str ());
105 }
106 }
107
108 virtual void visit (const FnType &type) override
109 {
110 ok = false;
111 if (emit_error_flag)
112 {
113 location_t ref_locus = mappings.lookup_location (type.get_ref ());
114 location_t base_locus
115 = mappings.lookup_location (get_base ()->get_ref ());
116 rich_location r (line_table, ref_locus);
117 r.add_range (base_locus);
118 rust_error_at (r, "expected [%s] got [%s]",
119 get_base ()->as_string ().c_str (),
120 type.as_string ().c_str ());
121 }
122 }
123
124 virtual void visit (const FnPtr &type) override
125 {
126 ok = false;
127 if (emit_error_flag)
128 {
129 location_t ref_locus = mappings.lookup_location (type.get_ref ());
130 location_t base_locus
131 = mappings.lookup_location (get_base ()->get_ref ());
132 rich_location r (line_table, ref_locus);
133 r.add_range (base_locus);
134 rust_error_at (r, "expected [%s] got [%s]",
135 get_base ()->as_string ().c_str (),
136 type.as_string ().c_str ());
137 }
138 }
139
140 virtual void visit (const ArrayType &type) override
141 {
142 ok = false;
143 if (emit_error_flag)
144 {
145 location_t ref_locus = mappings.lookup_location (type.get_ref ());
146 location_t base_locus
147 = mappings.lookup_location (get_base ()->get_ref ());
148 rich_location r (line_table, ref_locus);
149 r.add_range (base_locus);
150 rust_error_at (r, "expected [%s] got [%s]",
151 get_base ()->as_string ().c_str (),
152 type.as_string ().c_str ());
153 }
154 }
155
156 virtual void visit (const SliceType &type) override
157 {
158 ok = false;
159 if (emit_error_flag)
160 {
161 location_t ref_locus = mappings.lookup_location (type.get_ref ());
162 location_t base_locus
163 = mappings.lookup_location (get_base ()->get_ref ());
164 rich_location r (line_table, ref_locus);
165 r.add_range (base_locus);
166 rust_error_at (r, "expected [%s] got [%s]",
167 get_base ()->as_string ().c_str (),
168 type.as_string ().c_str ());
169 }
170 }
171
172 virtual void visit (const BoolType &type) override
173 {
174 ok = false;
175 if (emit_error_flag)
176 {
177 location_t ref_locus = mappings.lookup_location (type.get_ref ());
178 location_t base_locus
179 = mappings.lookup_location (get_base ()->get_ref ());
180 rich_location r (line_table, ref_locus);
181 r.add_range (base_locus);
182 rust_error_at (r, "expected [%s] got [%s]",
183 get_base ()->as_string ().c_str (),
184 type.as_string ().c_str ());
185 }
186 }
187
188 virtual void visit (const IntType &type) override
189 {
190 ok = false;
191 if (emit_error_flag)
192 {
193 location_t ref_locus = mappings.lookup_location (type.get_ref ());
194 location_t base_locus
195 = mappings.lookup_location (get_base ()->get_ref ());
196 rich_location r (line_table, ref_locus);
197 r.add_range (base_locus);
198 rust_error_at (r, "expected [%s] got [%s]",
199 get_base ()->as_string ().c_str (),
200 type.as_string ().c_str ());
201 }
202 }
203
204 virtual void visit (const UintType &type) override
205 {
206 ok = false;
207 if (emit_error_flag)
208 {
209 location_t ref_locus = mappings.lookup_location (type.get_ref ());
210 location_t base_locus
211 = mappings.lookup_location (get_base ()->get_ref ());
212 rich_location r (line_table, ref_locus);
213 r.add_range (base_locus);
214 rust_error_at (r, "expected [%s] got [%s]",
215 get_base ()->as_string ().c_str (),
216 type.as_string ().c_str ());
217 }
218 }
219
220 virtual void visit (const USizeType &type) override
221 {
222 ok = false;
223 if (emit_error_flag)
224 {
225 location_t ref_locus = mappings.lookup_location (type.get_ref ());
226 location_t base_locus
227 = mappings.lookup_location (get_base ()->get_ref ());
228 rich_location r (line_table, ref_locus);
229 r.add_range (base_locus);
230 rust_error_at (r, "expected [%s] got [%s]",
231 get_base ()->as_string ().c_str (),
232 type.as_string ().c_str ());
233 }
234 }
235
236 virtual void visit (const ISizeType &type) override
237 {
238 ok = false;
239 if (emit_error_flag)
240 {
241 location_t ref_locus = mappings.lookup_location (type.get_ref ());
242 location_t base_locus
243 = mappings.lookup_location (get_base ()->get_ref ());
244 rich_location r (line_table, ref_locus);
245 r.add_range (base_locus);
246 rust_error_at (r, "expected [%s] got [%s]",
247 get_base ()->as_string ().c_str (),
248 type.as_string ().c_str ());
249 }
250 }
251
252 virtual void visit (const FloatType &type) override
253 {
254 ok = false;
255 if (emit_error_flag)
256 {
257 location_t ref_locus = mappings.lookup_location (type.get_ref ());
258 location_t base_locus
259 = mappings.lookup_location (get_base ()->get_ref ());
260 rich_location r (line_table, ref_locus);
261 r.add_range (base_locus);
262 rust_error_at (r, "expected [%s] got [%s]",
263 get_base ()->as_string ().c_str (),
264 type.as_string ().c_str ());
265 }
266 }
267
268 virtual void visit (const ErrorType &type) override
269 {
270 ok = false;
271 if (emit_error_flag)
272 {
273 location_t ref_locus = mappings.lookup_location (type.get_ref ());
274 location_t base_locus
275 = mappings.lookup_location (get_base ()->get_ref ());
276 rich_location r (line_table, ref_locus);
277 r.add_range (base_locus);
278 rust_error_at (r, "expected [%s] got [%s]",
279 get_base ()->as_string ().c_str (),
280 type.as_string ().c_str ());
281 }
282 }
283
284 virtual void visit (const CharType &type) override
285 {
286 ok = false;
287 if (emit_error_flag)
288 {
289 location_t ref_locus = mappings.lookup_location (type.get_ref ());
290 location_t base_locus
291 = mappings.lookup_location (get_base ()->get_ref ());
292 rich_location r (line_table, ref_locus);
293 r.add_range (base_locus);
294 rust_error_at (r, "expected [%s] got [%s]",
295 get_base ()->as_string ().c_str (),
296 type.as_string ().c_str ());
297 }
298 }
299
300 virtual void visit (const ReferenceType &type) override
301 {
302 ok = false;
303 if (emit_error_flag)
304 {
305 location_t ref_locus = mappings.lookup_location (type.get_ref ());
306 location_t base_locus
307 = mappings.lookup_location (get_base ()->get_ref ());
308 rich_location r (line_table, ref_locus);
309 r.add_range (base_locus);
310 rust_error_at (r, "expected [%s] got [%s]",
311 get_base ()->as_string ().c_str (),
312 type.as_string ().c_str ());
313 }
314 }
315
316 virtual void visit (const PointerType &type) override
317 {
318 ok = false;
319 if (emit_error_flag)
320 {
321 location_t ref_locus = mappings.lookup_location (type.get_ref ());
322 location_t base_locus
323 = mappings.lookup_location (get_base ()->get_ref ());
324 rich_location r (line_table, ref_locus);
325 r.add_range (base_locus);
326 rust_error_at (r, "expected [%s] got [%s]",
327 get_base ()->as_string ().c_str (),
328 type.as_string ().c_str ());
329 }
330 }
331
332 virtual void visit (const StrType &type) override
333 {
334 ok = false;
335 if (emit_error_flag)
336 {
337 location_t ref_locus = mappings.lookup_location (type.get_ref ());
338 location_t base_locus
339 = mappings.lookup_location (get_base ()->get_ref ());
340 rich_location r (line_table, ref_locus);
341 r.add_range (base_locus);
342 rust_error_at (r, "expected [%s] got [%s]",
343 get_base ()->as_string ().c_str (),
344 type.as_string ().c_str ());
345 }
346 }
347
348 virtual void visit (const NeverType &type) override
349 {
350 ok = false;
351 if (emit_error_flag)
352 {
353 location_t ref_locus = mappings.lookup_location (type.get_ref ());
354 location_t base_locus
355 = mappings.lookup_location (get_base ()->get_ref ());
356 rich_location r (line_table, ref_locus);
357 r.add_range (base_locus);
358 rust_error_at (r, "expected [%s] got [%s]",
359 get_base ()->as_string ().c_str (),
360 type.as_string ().c_str ());
361 }
362 }
363
364 virtual void visit (const ProjectionType &type) override
365 {
366 ok = false;
367 if (emit_error_flag)
368 {
369 location_t ref_locus = mappings.lookup_location (type.get_ref ());
370 location_t base_locus
371 = mappings.lookup_location (get_base ()->get_ref ());
372 rich_location r (line_table, ref_locus);
373 r.add_range (base_locus);
374 rust_error_at (r, "expected [%s] got [%s]",
375 get_base ()->as_string ().c_str (),
376 type.as_string ().c_str ());
377 }
378 }
379
380 virtual void visit (const PlaceholderType &type) override
381 {
382 // it is ok for types to can eq to a placeholder
383 ok = true;
384 }
385
386 virtual void visit (const ParamType &type) override
387 {
388 ok = false;
389 if (emit_error_flag)
390 {
391 location_t ref_locus = mappings.lookup_location (type.get_ref ());
392 location_t base_locus
393 = mappings.lookup_location (get_base ()->get_ref ());
394 rich_location r (line_table, ref_locus);
395 r.add_range (base_locus);
396 rust_error_at (r, "expected [%s] got [%s]",
397 get_base ()->as_string ().c_str (),
398 type.as_string ().c_str ());
399 }
400 }
401
402 virtual void visit (const DynamicObjectType &type) override
403 {
404 ok = false;
405 if (emit_error_flag)
406 {
407 location_t ref_locus = mappings.lookup_location (type.get_ref ());
408 location_t base_locus
409 = mappings.lookup_location (get_base ()->get_ref ());
410 rich_location r (line_table, ref_locus);
411 r.add_range (base_locus);
412 rust_error_at (r, "expected [%s] got [%s]",
413 get_base ()->as_string ().c_str (),
414 type.as_string ().c_str ());
415 }
416 }
417
418 virtual void visit (const ClosureType &type) override
419 {
420 ok = false;
421 if (emit_error_flag)
422 {
423 location_t ref_locus = mappings.lookup_location (type.get_ref ());
424 location_t base_locus
425 = mappings.lookup_location (get_base ()->get_ref ());
426 rich_location r (line_table, ref_locus);
427 r.add_range (base_locus);
428 rust_error_at (r, "expected [%s] got [%s]",
429 get_base ()->as_string ().c_str (),
430 type.as_string ().c_str ());
431 }
432 }
433
434 virtual void visit (const OpaqueType &type) override
435 {
436 ok = false;
437 if (emit_error_flag)
438 {
439 location_t ref_locus = mappings.lookup_location (type.get_ref ());
440 location_t base_locus
441 = mappings.lookup_location (get_base ()->get_ref ());
442 rich_location r (line_table, ref_locus);
443 r.add_range (base_locus);
444 rust_error_at (r, "expected [%s] got [%s]",
445 get_base ()->as_string ().c_str (),
446 type.as_string ().c_str ());
447 }
448 }
449
450 protected:
451 BaseCmp (const BaseType *base, bool emit_errors)
452 : mappings (Analysis::Mappings::get ()),
453 context (Resolver::TypeCheckContext::get ()), ok (false),
454 emit_error_flag (emit_errors)
455 {}
456
457 Analysis::Mappings &mappings;
458 Resolver::TypeCheckContext *context;
459
460 bool ok;
461 bool emit_error_flag;
462
463 private:
464 /* Returns a pointer to the ty that created this rule. */
465 virtual const BaseType *get_base () const = 0;
466 };
467
468 class InferCmp : public BaseCmp
469 {
470 using Rust::TyTy::BaseCmp::visit;
471
472 public:
473 InferCmp (const InferType *base, bool emit_errors)
474 : BaseCmp (base, emit_errors), base (base)
475 {}
476
477 void visit (const BoolType &type) override
478 {
479 bool is_valid
480 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
481 if (is_valid)
482 {
483 ok = true;
484 return;
485 }
486
487 BaseCmp::visit (type);
488 }
489
490 void visit (const IntType &type) override
491 {
492 bool is_valid
493 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
494 || (base->get_infer_kind ()
495 == TyTy::InferType::InferTypeKind::INTEGRAL);
496 if (is_valid)
497 {
498 ok = true;
499 return;
500 }
501
502 BaseCmp::visit (type);
503 }
504
505 void visit (const UintType &type) override
506 {
507 bool is_valid
508 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
509 || (base->get_infer_kind ()
510 == TyTy::InferType::InferTypeKind::INTEGRAL);
511 if (is_valid)
512 {
513 ok = true;
514 return;
515 }
516
517 BaseCmp::visit (type);
518 }
519
520 void visit (const USizeType &type) override
521 {
522 bool is_valid
523 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
524 || (base->get_infer_kind ()
525 == TyTy::InferType::InferTypeKind::INTEGRAL);
526 if (is_valid)
527 {
528 ok = true;
529 return;
530 }
531
532 BaseCmp::visit (type);
533 }
534
535 void visit (const ISizeType &type) override
536 {
537 bool is_valid
538 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
539 || (base->get_infer_kind ()
540 == TyTy::InferType::InferTypeKind::INTEGRAL);
541 if (is_valid)
542 {
543 ok = true;
544 return;
545 }
546
547 BaseCmp::visit (type);
548 }
549
550 void visit (const FloatType &type) override
551 {
552 bool is_valid
553 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
554 || (base->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT);
555 if (is_valid)
556 {
557 ok = true;
558 return;
559 }
560
561 BaseCmp::visit (type);
562 }
563
564 void visit (const ArrayType &type) override
565 {
566 bool is_valid
567 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
568 if (is_valid)
569 {
570 ok = true;
571 return;
572 }
573
574 BaseCmp::visit (type);
575 }
576
577 void visit (const SliceType &type) override
578 {
579 bool is_valid
580 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
581 if (is_valid)
582 {
583 ok = true;
584 return;
585 }
586
587 BaseCmp::visit (type);
588 }
589
590 void visit (const ADTType &type) override
591 {
592 bool is_valid
593 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
594 if (is_valid)
595 {
596 ok = true;
597 return;
598 }
599
600 BaseCmp::visit (type);
601 }
602
603 void visit (const TupleType &type) override
604 {
605 bool is_valid
606 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
607 if (is_valid)
608 {
609 ok = true;
610 return;
611 }
612
613 BaseCmp::visit (type);
614 }
615
616 void visit (const InferType &type) override
617 {
618 switch (base->get_infer_kind ())
619 {
620 case InferType::InferTypeKind::GENERAL:
621 ok = true;
622 return;
623
624 case InferType::InferTypeKind::INTEGRAL: {
625 if (type.get_infer_kind () == InferType::InferTypeKind::INTEGRAL)
626 {
627 ok = true;
628 return;
629 }
630 else if (type.get_infer_kind () == InferType::InferTypeKind::GENERAL)
631 {
632 ok = true;
633 return;
634 }
635 }
636 break;
637
638 case InferType::InferTypeKind::FLOAT: {
639 if (type.get_infer_kind () == InferType::InferTypeKind::FLOAT)
640 {
641 ok = true;
642 return;
643 }
644 else if (type.get_infer_kind () == InferType::InferTypeKind::GENERAL)
645 {
646 ok = true;
647 return;
648 }
649 }
650 break;
651 }
652
653 BaseCmp::visit (type);
654 }
655
656 void visit (const CharType &type) override
657 {
658 {
659 bool is_valid
660 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
661 if (is_valid)
662 {
663 ok = true;
664 return;
665 }
666
667 BaseCmp::visit (type);
668 }
669 }
670
671 void visit (const ReferenceType &type) override
672 {
673 bool is_valid
674 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
675 if (is_valid)
676 {
677 ok = true;
678 return;
679 }
680
681 BaseCmp::visit (type);
682 }
683
684 void visit (const PointerType &type) override
685 {
686 bool is_valid
687 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
688 if (is_valid)
689 {
690 ok = true;
691 return;
692 }
693
694 BaseCmp::visit (type);
695 }
696
697 void visit (const ParamType &) override { ok = true; }
698
699 void visit (const DynamicObjectType &type) override
700 {
701 bool is_valid
702 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
703 if (is_valid)
704 {
705 ok = true;
706 return;
707 }
708
709 BaseCmp::visit (type);
710 }
711
712 void visit (const ClosureType &type) override
713 {
714 bool is_valid
715 = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
716 if (is_valid)
717 {
718 ok = true;
719 return;
720 }
721
722 BaseCmp::visit (type);
723 }
724
725 private:
726 const BaseType *get_base () const override { return base; }
727 const InferType *base;
728 };
729
730 class FnCmp : public BaseCmp
731 {
732 using Rust::TyTy::BaseCmp::visit;
733
734 public:
735 FnCmp (const FnType *base, bool emit_errors)
736 : BaseCmp (base, emit_errors), base (base)
737 {}
738
739 void visit (const InferType &type) override
740 {
741 ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
742 }
743
744 void visit (const FnType &type) override
745 {
746 if (base->num_params () != type.num_params ())
747 {
748 BaseCmp::visit (type);
749 return;
750 }
751
752 for (size_t i = 0; i < base->num_params (); i++)
753 {
754 auto a = base->param_at (i).get_type ();
755 auto b = type.param_at (i).get_type ();
756
757 if (!a->can_eq (b, emit_error_flag))
758 {
759 emit_error_flag = false;
760 BaseCmp::visit (type);
761 return;
762 }
763 }
764
765 if (!base->get_return_type ()->can_eq (type.get_return_type (),
766 emit_error_flag))
767 {
768 emit_error_flag = false;
769 BaseCmp::visit (type);
770 return;
771 }
772
773 ok = true;
774 }
775
776 private:
777 const BaseType *get_base () const override { return base; }
778 const FnType *base;
779 };
780
781 class FnptrCmp : public BaseCmp
782 {
783 using Rust::TyTy::BaseCmp::visit;
784
785 public:
786 FnptrCmp (const FnPtr *base, bool emit_errors)
787 : BaseCmp (base, emit_errors), base (base)
788 {}
789
790 void visit (const InferType &type) override
791 {
792 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
793 {
794 BaseCmp::visit (type);
795 return;
796 }
797
798 ok = true;
799 }
800
801 void visit (const FnPtr &type) override
802 {
803 if (base->num_params () != type.num_params ())
804 {
805 BaseCmp::visit (type);
806 return;
807 }
808
809 auto this_ret_type = base->get_return_type ();
810 auto other_ret_type = type.get_return_type ();
811 if (!this_ret_type->can_eq (other_ret_type, emit_error_flag))
812 {
813 BaseCmp::visit (type);
814 return;
815 }
816
817 for (size_t i = 0; i < base->num_params (); i++)
818 {
819 auto this_param = base->get_param_type_at (i);
820 auto other_param = type.get_param_type_at (i);
821 if (!this_param->can_eq (other_param, emit_error_flag))
822 {
823 BaseCmp::visit (type);
824 return;
825 }
826 }
827
828 ok = true;
829 }
830
831 void visit (const FnType &type) override
832 {
833 if (base->num_params () != type.num_params ())
834 {
835 BaseCmp::visit (type);
836 return;
837 }
838
839 auto this_ret_type = base->get_return_type ();
840 auto other_ret_type = type.get_return_type ();
841 if (!this_ret_type->can_eq (other_ret_type, emit_error_flag))
842 {
843 BaseCmp::visit (type);
844 return;
845 }
846
847 for (size_t i = 0; i < base->num_params (); i++)
848 {
849 auto this_param = base->get_param_type_at (i);
850 auto other_param = type.param_at (i).get_type ();
851 if (!this_param->can_eq (other_param, emit_error_flag))
852 {
853 BaseCmp::visit (type);
854 return;
855 }
856 }
857
858 ok = true;
859 }
860
861 private:
862 const BaseType *get_base () const override { return base; }
863 const FnPtr *base;
864 };
865
866 class ClosureCmp : public BaseCmp
867 {
868 using Rust::TyTy::BaseCmp::visit;
869
870 public:
871 ClosureCmp (const ClosureType *base, bool emit_errors)
872 : BaseCmp (base, emit_errors), base (base)
873 {}
874
875 void visit (const InferType &type) override
876 {
877 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
878 {
879 BaseCmp::visit (type);
880 return;
881 }
882
883 ok = true;
884 }
885
886 void visit (const ClosureType &type) override
887 {
888 if (base->get_def_id () != type.get_def_id ())
889 {
890 BaseCmp::visit (type);
891 return;
892 }
893
894 if (!base->get_parameters ().can_eq (&type.get_parameters (), false))
895 {
896 BaseCmp::visit (type);
897 return;
898 }
899
900 if (!base->get_result_type ().can_eq (&type.get_result_type (), false))
901 {
902 BaseCmp::visit (type);
903 return;
904 }
905
906 ok = true;
907 }
908
909 private:
910 const BaseType *get_base () const override { return base; }
911 const ClosureType *base;
912 };
913
914 class ArrayCmp : public BaseCmp
915 {
916 using Rust::TyTy::BaseCmp::visit;
917
918 public:
919 ArrayCmp (const ArrayType *base, bool emit_errors)
920 : BaseCmp (base, emit_errors), base (base)
921 {}
922
923 void visit (const ArrayType &type) override
924 {
925 // check base type
926 const BaseType *base_element = base->get_element_type ();
927 const BaseType *other_element = type.get_element_type ();
928 if (!base_element->can_eq (other_element, emit_error_flag))
929 {
930 BaseCmp::visit (type);
931 return;
932 }
933
934 ok = true;
935 }
936
937 void visit (const InferType &type) override
938 {
939 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
940 {
941 BaseCmp::visit (type);
942 return;
943 }
944
945 ok = true;
946 }
947
948 private:
949 const BaseType *get_base () const override { return base; }
950 const ArrayType *base;
951 };
952
953 class SliceCmp : public BaseCmp
954 {
955 using Rust::TyTy::BaseCmp::visit;
956
957 public:
958 SliceCmp (const SliceType *base, bool emit_errors)
959 : BaseCmp (base, emit_errors), base (base)
960 {}
961
962 void visit (const SliceType &type) override
963 {
964 // check base type
965 const BaseType *base_element = base->get_element_type ();
966 const BaseType *other_element = type.get_element_type ();
967 if (!base_element->can_eq (other_element, emit_error_flag))
968 {
969 BaseCmp::visit (type);
970 return;
971 }
972
973 ok = true;
974 }
975
976 void visit (const InferType &type) override
977 {
978 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
979 {
980 BaseCmp::visit (type);
981 return;
982 }
983
984 ok = true;
985 }
986
987 private:
988 const BaseType *get_base () const override { return base; }
989 const SliceType *base;
990 };
991
992 class BoolCmp : public BaseCmp
993 {
994 using Rust::TyTy::BaseCmp::visit;
995
996 public:
997 BoolCmp (const BoolType *base, bool emit_errors)
998 : BaseCmp (base, emit_errors), base (base)
999 {}
1000
1001 void visit (const BoolType &type) override { ok = true; }
1002
1003 void visit (const InferType &type) override
1004 {
1005 ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
1006 }
1007
1008 private:
1009 const BaseType *get_base () const override { return base; }
1010 const BoolType *base;
1011 };
1012
1013 class IntCmp : public BaseCmp
1014 {
1015 using Rust::TyTy::BaseCmp::visit;
1016
1017 public:
1018 IntCmp (const IntType *base, bool emit_errors)
1019 : BaseCmp (base, emit_errors), base (base)
1020 {}
1021
1022 void visit (const InferType &type) override
1023 {
1024 ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1025 }
1026
1027 void visit (const IntType &type) override
1028 {
1029 ok = type.get_int_kind () == base->get_int_kind ();
1030 }
1031
1032 private:
1033 const BaseType *get_base () const override { return base; }
1034 const IntType *base;
1035 };
1036
1037 class UintCmp : public BaseCmp
1038 {
1039 using Rust::TyTy::BaseCmp::visit;
1040
1041 public:
1042 UintCmp (const UintType *base, bool emit_errors)
1043 : BaseCmp (base, emit_errors), base (base)
1044 {}
1045
1046 void visit (const InferType &type) override
1047 {
1048 ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1049 }
1050
1051 void visit (const UintType &type) override
1052 {
1053 ok = type.get_uint_kind () == base->get_uint_kind ();
1054 }
1055
1056 private:
1057 const BaseType *get_base () const override { return base; }
1058 const UintType *base;
1059 };
1060
1061 class FloatCmp : public BaseCmp
1062 {
1063 using Rust::TyTy::BaseCmp::visit;
1064
1065 public:
1066 FloatCmp (const FloatType *base, bool emit_errors)
1067 : BaseCmp (base, emit_errors), base (base)
1068 {}
1069
1070 void visit (const InferType &type) override
1071 {
1072 ok = type.get_infer_kind () != InferType::InferTypeKind::INTEGRAL;
1073 }
1074
1075 void visit (const FloatType &type) override
1076 {
1077 ok = type.get_float_kind () == base->get_float_kind ();
1078 }
1079
1080 private:
1081 const BaseType *get_base () const override { return base; }
1082 const FloatType *base;
1083 };
1084
1085 class ADTCmp : public BaseCmp
1086 {
1087 using Rust::TyTy::BaseCmp::visit;
1088
1089 public:
1090 ADTCmp (const ADTType *base, bool emit_errors)
1091 : BaseCmp (base, emit_errors), base (base)
1092 {}
1093
1094 void visit (const ADTType &type) override
1095 {
1096 if (base->get_adt_kind () != type.get_adt_kind ())
1097 {
1098 BaseCmp::visit (type);
1099 return;
1100 }
1101
1102 if (base->get_identifier ().compare (type.get_identifier ()) != 0)
1103 {
1104 BaseCmp::visit (type);
1105 return;
1106 }
1107
1108 if (base->number_of_variants () != type.number_of_variants ())
1109 {
1110 BaseCmp::visit (type);
1111 return;
1112 }
1113
1114 for (size_t i = 0; i < type.number_of_variants (); ++i)
1115 {
1116 TyTy::VariantDef *a = base->get_variants ().at (i);
1117 TyTy::VariantDef *b = type.get_variants ().at (i);
1118
1119 if (a->num_fields () != b->num_fields ())
1120 {
1121 BaseCmp::visit (type);
1122 return;
1123 }
1124
1125 for (size_t j = 0; j < a->num_fields (); j++)
1126 {
1127 TyTy::StructFieldType *base_field = a->get_field_at_index (j);
1128 TyTy::StructFieldType *other_field = b->get_field_at_index (j);
1129
1130 TyTy::BaseType *this_field_ty = base_field->get_field_type ();
1131 TyTy::BaseType *other_field_ty = other_field->get_field_type ();
1132
1133 if (!this_field_ty->can_eq (other_field_ty, emit_error_flag))
1134 {
1135 BaseCmp::visit (type);
1136 return;
1137 }
1138 }
1139 }
1140
1141 ok = true;
1142 }
1143
1144 void visit (const InferType &type) override
1145 {
1146 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1147 {
1148 BaseCmp::visit (type);
1149 return;
1150 }
1151
1152 ok = true;
1153 }
1154
1155 private:
1156 const BaseType *get_base () const override { return base; }
1157 const ADTType *base;
1158 };
1159
1160 class TupleCmp : public BaseCmp
1161 {
1162 using Rust::TyTy::BaseCmp::visit;
1163
1164 public:
1165 TupleCmp (const TupleType *base, bool emit_errors)
1166 : BaseCmp (base, emit_errors), base (base)
1167 {}
1168
1169 void visit (const TupleType &type) override
1170 {
1171 if (base->num_fields () != type.num_fields ())
1172 {
1173 BaseCmp::visit (type);
1174 return;
1175 }
1176
1177 for (size_t i = 0; i < base->num_fields (); i++)
1178 {
1179 BaseType *bo = base->get_field (i);
1180 BaseType *fo = type.get_field (i);
1181
1182 if (!bo->can_eq (fo, emit_error_flag))
1183 {
1184 BaseCmp::visit (type);
1185 return;
1186 }
1187 }
1188
1189 ok = true;
1190 }
1191
1192 void visit (const InferType &type) override
1193 {
1194 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1195 {
1196 BaseCmp::visit (type);
1197 return;
1198 }
1199
1200 ok = true;
1201 }
1202
1203 private:
1204 const BaseType *get_base () const override { return base; }
1205 const TupleType *base;
1206 };
1207
1208 class USizeCmp : public BaseCmp
1209 {
1210 using Rust::TyTy::BaseCmp::visit;
1211
1212 public:
1213 USizeCmp (const USizeType *base, bool emit_errors)
1214 : BaseCmp (base, emit_errors), base (base)
1215 {}
1216
1217 void visit (const InferType &type) override
1218 {
1219 ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1220 }
1221
1222 void visit (const USizeType &type) override { ok = true; }
1223
1224 private:
1225 const BaseType *get_base () const override { return base; }
1226 const USizeType *base;
1227 };
1228
1229 class ISizeCmp : public BaseCmp
1230 {
1231 using Rust::TyTy::BaseCmp::visit;
1232
1233 public:
1234 ISizeCmp (const ISizeType *base, bool emit_errors)
1235 : BaseCmp (base, emit_errors), base (base)
1236 {}
1237
1238 void visit (const InferType &type) override
1239 {
1240 ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1241 }
1242
1243 void visit (const ISizeType &type) override { ok = true; }
1244
1245 private:
1246 const BaseType *get_base () const override { return base; }
1247 const ISizeType *base;
1248 };
1249
1250 class CharCmp : public BaseCmp
1251 {
1252 using Rust::TyTy::BaseCmp::visit;
1253
1254 public:
1255 CharCmp (const CharType *base, bool emit_errors)
1256 : BaseCmp (base, emit_errors), base (base)
1257 {}
1258
1259 void visit (const InferType &type) override
1260 {
1261 ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
1262 }
1263
1264 void visit (const CharType &type) override { ok = true; }
1265
1266 private:
1267 const BaseType *get_base () const override { return base; }
1268 const CharType *base;
1269 };
1270
1271 class ReferenceCmp : public BaseCmp
1272 {
1273 using Rust::TyTy::BaseCmp::visit;
1274
1275 public:
1276 ReferenceCmp (const ReferenceType *base, bool emit_errors)
1277 : BaseCmp (base, emit_errors), base (base)
1278 {}
1279
1280 void visit (const ReferenceType &type) override
1281 {
1282 auto base_type = base->get_base ();
1283 auto other_base_type = type.get_base ();
1284
1285 bool mutability_ok = base->is_mutable () ? type.is_mutable () : true;
1286 if (!mutability_ok)
1287 {
1288 BaseCmp::visit (type);
1289 return;
1290 }
1291
1292 if (!base_type->can_eq (other_base_type, emit_error_flag))
1293 {
1294 BaseCmp::visit (type);
1295 return;
1296 }
1297
1298 ok = true;
1299 }
1300
1301 void visit (const InferType &type) override
1302 {
1303 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1304 {
1305 BaseCmp::visit (type);
1306 return;
1307 }
1308
1309 ok = true;
1310 }
1311
1312 private:
1313 const BaseType *get_base () const override { return base; }
1314 const ReferenceType *base;
1315 };
1316
1317 class PointerCmp : public BaseCmp
1318 {
1319 using Rust::TyTy::BaseCmp::visit;
1320
1321 public:
1322 PointerCmp (const PointerType *base, bool emit_errors)
1323 : BaseCmp (base, emit_errors), base (base)
1324 {}
1325
1326 void visit (const PointerType &type) override
1327 {
1328 auto base_type = base->get_base ();
1329 auto other_base_type = type.get_base ();
1330
1331 bool mutability_ok = base->is_mutable () ? type.is_mutable () : true;
1332 if (!mutability_ok)
1333 {
1334 BaseCmp::visit (type);
1335 return;
1336 }
1337
1338 if (!base_type->can_eq (other_base_type, emit_error_flag))
1339 {
1340 BaseCmp::visit (type);
1341 return;
1342 }
1343
1344 ok = true;
1345 }
1346
1347 void visit (const InferType &type) override
1348 {
1349 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1350 {
1351 BaseCmp::visit (type);
1352 return;
1353 }
1354
1355 ok = true;
1356 }
1357
1358 private:
1359 const BaseType *get_base () const override { return base; }
1360 const PointerType *base;
1361 };
1362
1363 class ParamCmp : public BaseCmp
1364 {
1365 using Rust::TyTy::BaseCmp::visit;
1366
1367 public:
1368 ParamCmp (const ParamType *base, bool emit_errors)
1369 : BaseCmp (base, emit_errors), base (base)
1370 {}
1371
1372 // param types are a placeholder we shouldn't have cases where we unify
1373 // against it. eg: struct foo<T> { a: T }; When we invoke it we can do either:
1374 //
1375 // foo<i32>{ a: 123 }.
1376 // Then this enforces the i32 type to be referenced on the
1377 // field via an hirid.
1378 //
1379 // rust also allows for a = foo{a:123}; Where we can use an Inference Variable
1380 // to handle the typing of the struct
1381 bool can_eq (const BaseType *other) override
1382 {
1383 if (!base->can_resolve ())
1384 return BaseCmp::can_eq (other);
1385
1386 auto lookup = base->resolve ();
1387 return lookup->can_eq (other, emit_error_flag);
1388 }
1389
1390 // imagine the case where we have:
1391 // struct Foo<T>(T);
1392 // Then we declare a generic impl block
1393 // impl <X>Foo<X> { ... }
1394 // both of these types are compatible so we mostly care about the number of
1395 // generic arguments
1396 void visit (const ParamType &) override { ok = true; }
1397
1398 void visit (const TupleType &) override { ok = true; }
1399
1400 void visit (const InferType &) override { ok = true; }
1401
1402 void visit (const FnType &) override { ok = true; }
1403
1404 void visit (const FnPtr &) override { ok = true; }
1405
1406 void visit (const ADTType &) override { ok = true; }
1407
1408 void visit (const ArrayType &) override { ok = true; }
1409
1410 void visit (const SliceType &) override { ok = true; }
1411
1412 void visit (const BoolType &) override { ok = true; }
1413
1414 void visit (const IntType &) override { ok = true; }
1415
1416 void visit (const UintType &) override { ok = true; }
1417
1418 void visit (const USizeType &) override { ok = true; }
1419
1420 void visit (const ISizeType &) override { ok = true; }
1421
1422 void visit (const FloatType &) override { ok = true; }
1423
1424 void visit (const CharType &) override { ok = true; }
1425
1426 void visit (const ReferenceType &) override { ok = true; }
1427
1428 void visit (const PointerType &) override { ok = true; }
1429
1430 void visit (const StrType &) override { ok = true; }
1431
1432 void visit (const NeverType &) override { ok = true; }
1433
1434 void visit (const DynamicObjectType &) override { ok = true; }
1435
1436 void visit (const PlaceholderType &type) override
1437 {
1438 ok = base->get_symbol ().compare (type.get_symbol ()) == 0;
1439 }
1440
1441 private:
1442 const BaseType *get_base () const override { return base; }
1443 const ParamType *base;
1444 };
1445
1446 class StrCmp : public BaseCmp
1447 {
1448 // FIXME we will need a enum for the StrType like ByteBuf etc..
1449 using Rust::TyTy::BaseCmp::visit;
1450
1451 public:
1452 StrCmp (const StrType *base, bool emit_errors)
1453 : BaseCmp (base, emit_errors), base (base)
1454 {}
1455
1456 void visit (const StrType &type) override { ok = true; }
1457
1458 void visit (const InferType &type) override
1459 {
1460 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1461 {
1462 BaseCmp::visit (type);
1463 return;
1464 }
1465
1466 ok = true;
1467 }
1468
1469 private:
1470 const BaseType *get_base () const override { return base; }
1471 const StrType *base;
1472 };
1473
1474 class NeverCmp : public BaseCmp
1475 {
1476 using Rust::TyTy::BaseCmp::visit;
1477
1478 public:
1479 NeverCmp (const NeverType *base, bool emit_errors)
1480 : BaseCmp (base, emit_errors), base (base)
1481 {}
1482
1483 void visit (const NeverType &type) override { ok = true; }
1484
1485 void visit (const InferType &type) override
1486 {
1487 if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1488 {
1489 BaseCmp::visit (type);
1490 return;
1491 }
1492
1493 ok = true;
1494 }
1495
1496 private:
1497 const BaseType *get_base () const override { return base; }
1498 const NeverType *base;
1499 };
1500
1501 class PlaceholderCmp : public BaseCmp
1502 {
1503 using Rust::TyTy::BaseCmp::visit;
1504
1505 public:
1506 PlaceholderCmp (const PlaceholderType *base, bool emit_errors)
1507 : BaseCmp (base, emit_errors), base (base)
1508 {}
1509
1510 bool can_eq (const BaseType *other) override
1511 {
1512 if (!base->can_resolve ())
1513 return BaseCmp::can_eq (other);
1514
1515 BaseType *lookup = base->resolve ();
1516 return lookup->can_eq (other, emit_error_flag);
1517 }
1518
1519 void visit (const TupleType &) override { ok = true; }
1520
1521 void visit (const ADTType &) override { ok = true; }
1522
1523 void visit (const InferType &) override { ok = true; }
1524
1525 void visit (const FnType &) override { ok = true; }
1526
1527 void visit (const FnPtr &) override { ok = true; }
1528
1529 void visit (const ArrayType &) override { ok = true; }
1530
1531 void visit (const BoolType &) override { ok = true; }
1532
1533 void visit (const IntType &) override { ok = true; }
1534
1535 void visit (const UintType &) override { ok = true; }
1536
1537 void visit (const USizeType &) override { ok = true; }
1538
1539 void visit (const ISizeType &) override { ok = true; }
1540
1541 void visit (const FloatType &) override { ok = true; }
1542
1543 void visit (const ErrorType &) override { ok = true; }
1544
1545 void visit (const CharType &) override { ok = true; }
1546
1547 void visit (const ReferenceType &) override { ok = true; }
1548
1549 void visit (const ParamType &) override { ok = true; }
1550
1551 void visit (const StrType &) override { ok = true; }
1552
1553 void visit (const NeverType &) override { ok = true; }
1554
1555 void visit (const SliceType &) override { ok = true; }
1556
1557 private:
1558 const BaseType *get_base () const override { return base; }
1559
1560 const PlaceholderType *base;
1561 };
1562
1563 class DynamicCmp : public BaseCmp
1564 {
1565 using Rust::TyTy::BaseCmp::visit;
1566
1567 public:
1568 DynamicCmp (const DynamicObjectType *base, bool emit_errors)
1569 : BaseCmp (base, emit_errors), base (base)
1570 {}
1571
1572 void visit (const DynamicObjectType &type) override
1573 {
1574 if (base->num_specified_bounds () != type.num_specified_bounds ())
1575 {
1576 BaseCmp::visit (type);
1577 return;
1578 }
1579
1580 location_t ref_locus = mappings.lookup_location (type.get_ref ());
1581 ok = base->bounds_compatible (type, ref_locus, false);
1582 }
1583
1584 private:
1585 const BaseType *get_base () const override { return base; }
1586
1587 const DynamicObjectType *base;
1588 };
1589
1590 class OpaqueCmp : public BaseCmp
1591 {
1592 using Rust::TyTy::BaseCmp::visit;
1593
1594 public:
1595 OpaqueCmp (const OpaqueType *base, bool emit_errors)
1596 : BaseCmp (base, emit_errors), base (base)
1597 {}
1598
1599 // TODO
1600
1601 private:
1602 const BaseType *get_base () const override { return base; }
1603
1604 const OpaqueType *base;
1605 };
1606
1607 } // namespace TyTy
1608 } // namespace Rust
1609
1610 #endif // RUST_TYTY_CMP_H