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 : : #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 : 1184976 : virtual bool can_eq (const BaseType *other)
35 : : {
36 : 1184976 : if (other->get_kind () == TypeKind::PARAM)
37 : : {
38 : 39898 : const ParamType *p = static_cast<const ParamType *> (other);
39 : 39898 : other = p->resolve ();
40 : : }
41 : 1184976 : if (other->get_kind () == TypeKind::PLACEHOLDER)
42 : : {
43 : 33 : const PlaceholderType *p = static_cast<const PlaceholderType *> (other);
44 : 33 : if (p->can_resolve ())
45 : : {
46 : 25 : other = p->resolve ();
47 : : }
48 : : }
49 : 1184976 : if (other->get_kind () == TypeKind::PROJECTION)
50 : : {
51 : 915 : const ProjectionType *p = static_cast<const ProjectionType *> (other);
52 : 915 : other = p->get ();
53 : : }
54 : :
55 : 1184976 : other->accept_vis (*this);
56 : 1184976 : return ok;
57 : : }
58 : :
59 : 46 : virtual void visit (const TupleType &type) override
60 : : {
61 : 46 : ok = false;
62 : :
63 : 46 : if (emit_error_flag)
64 : : {
65 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
66 : 0 : location_t base_locus
67 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
68 : 0 : rich_location r (line_table, ref_locus);
69 : 0 : r.add_range (base_locus);
70 : 0 : rust_error_at (r, "expected [%s] got [%s]",
71 : 0 : get_base ()->as_string ().c_str (),
72 : 0 : type.as_string ().c_str ());
73 : 0 : }
74 : 46 : }
75 : :
76 : 52905 : virtual void visit (const ADTType &type) override
77 : : {
78 : 52905 : ok = false;
79 : 52905 : if (emit_error_flag)
80 : : {
81 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
82 : 0 : location_t base_locus
83 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
84 : 0 : rich_location r (line_table, ref_locus);
85 : 0 : r.add_range (base_locus);
86 : 0 : rust_error_at (r, "expected [%s] got [%s]",
87 : 0 : get_base ()->as_string ().c_str (),
88 : 0 : type.as_string ().c_str ());
89 : 0 : }
90 : 52905 : }
91 : :
92 : 188 : virtual void visit (const InferType &type) override
93 : : {
94 : 188 : ok = false;
95 : 188 : if (emit_error_flag)
96 : : {
97 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
98 : 0 : location_t base_locus
99 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
100 : 0 : rich_location r (line_table, ref_locus);
101 : 0 : r.add_range (base_locus);
102 : 0 : rust_error_at (r, "expected [%s] got [%s]",
103 : 0 : get_base ()->as_string ().c_str (),
104 : 0 : type.as_string ().c_str ());
105 : 0 : }
106 : 188 : }
107 : :
108 : 0 : virtual void visit (const FnType &type) override
109 : : {
110 : 0 : ok = false;
111 : 0 : if (emit_error_flag)
112 : : {
113 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
114 : 0 : location_t base_locus
115 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
116 : 0 : rich_location r (line_table, ref_locus);
117 : 0 : r.add_range (base_locus);
118 : 0 : rust_error_at (r, "expected [%s] got [%s]",
119 : 0 : get_base ()->as_string ().c_str (),
120 : 0 : type.as_string ().c_str ());
121 : 0 : }
122 : 0 : }
123 : :
124 : 0 : virtual void visit (const FnPtr &type) override
125 : : {
126 : 0 : ok = false;
127 : 0 : if (emit_error_flag)
128 : : {
129 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
130 : 0 : location_t base_locus
131 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
132 : 0 : rich_location r (line_table, ref_locus);
133 : 0 : r.add_range (base_locus);
134 : 0 : rust_error_at (r, "expected [%s] got [%s]",
135 : 0 : get_base ()->as_string ().c_str (),
136 : 0 : type.as_string ().c_str ());
137 : 0 : }
138 : 0 : }
139 : :
140 : 12386 : virtual void visit (const ArrayType &type) override
141 : : {
142 : 12386 : ok = false;
143 : 12386 : if (emit_error_flag)
144 : : {
145 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
146 : 0 : location_t base_locus
147 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
148 : 0 : rich_location r (line_table, ref_locus);
149 : 0 : r.add_range (base_locus);
150 : 0 : rust_error_at (r, "expected [%s] got [%s]",
151 : 0 : get_base ()->as_string ().c_str (),
152 : 0 : type.as_string ().c_str ());
153 : 0 : }
154 : 12386 : }
155 : :
156 : 2328 : virtual void visit (const SliceType &type) override
157 : : {
158 : 2328 : ok = false;
159 : 2328 : if (emit_error_flag)
160 : : {
161 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
162 : 0 : location_t base_locus
163 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
164 : 0 : rich_location r (line_table, ref_locus);
165 : 0 : r.add_range (base_locus);
166 : 0 : rust_error_at (r, "expected [%s] got [%s]",
167 : 0 : get_base ()->as_string ().c_str (),
168 : 0 : type.as_string ().c_str ());
169 : 0 : }
170 : 2328 : }
171 : :
172 : 29783 : virtual void visit (const BoolType &type) override
173 : : {
174 : 29783 : ok = false;
175 : 29783 : if (emit_error_flag)
176 : : {
177 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
178 : 0 : location_t base_locus
179 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
180 : 0 : rich_location r (line_table, ref_locus);
181 : 0 : r.add_range (base_locus);
182 : 0 : rust_error_at (r, "expected [%s] got [%s]",
183 : 0 : get_base ()->as_string ().c_str (),
184 : 0 : type.as_string ().c_str ());
185 : 0 : }
186 : 29783 : }
187 : :
188 : 126642 : virtual void visit (const IntType &type) override
189 : : {
190 : 126642 : ok = false;
191 : 126642 : if (emit_error_flag)
192 : : {
193 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
194 : 0 : location_t base_locus
195 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
196 : 0 : rich_location r (line_table, ref_locus);
197 : 0 : r.add_range (base_locus);
198 : 0 : rust_error_at (r, "expected [%s] got [%s]",
199 : 0 : get_base ()->as_string ().c_str (),
200 : 0 : type.as_string ().c_str ());
201 : 0 : }
202 : 126642 : }
203 : :
204 : 260282 : virtual void visit (const UintType &type) override
205 : : {
206 : 260282 : ok = false;
207 : 260282 : if (emit_error_flag)
208 : : {
209 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
210 : 0 : location_t base_locus
211 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
212 : 0 : rich_location r (line_table, ref_locus);
213 : 0 : r.add_range (base_locus);
214 : 0 : rust_error_at (r, "expected [%s] got [%s]",
215 : 0 : get_base ()->as_string ().c_str (),
216 : 0 : type.as_string ().c_str ());
217 : 0 : }
218 : 260282 : }
219 : :
220 : 149861 : virtual void visit (const USizeType &type) override
221 : : {
222 : 149861 : ok = false;
223 : 149861 : if (emit_error_flag)
224 : : {
225 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
226 : 0 : location_t base_locus
227 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
228 : 0 : rich_location r (line_table, ref_locus);
229 : 0 : r.add_range (base_locus);
230 : 0 : rust_error_at (r, "expected [%s] got [%s]",
231 : 0 : get_base ()->as_string ().c_str (),
232 : 0 : type.as_string ().c_str ());
233 : 0 : }
234 : 149861 : }
235 : :
236 : 34107 : virtual void visit (const ISizeType &type) override
237 : : {
238 : 34107 : ok = false;
239 : 34107 : if (emit_error_flag)
240 : : {
241 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
242 : 0 : location_t base_locus
243 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
244 : 0 : rich_location r (line_table, ref_locus);
245 : 0 : r.add_range (base_locus);
246 : 0 : rust_error_at (r, "expected [%s] got [%s]",
247 : 0 : get_base ()->as_string ().c_str (),
248 : 0 : type.as_string ().c_str ());
249 : 0 : }
250 : 34107 : }
251 : :
252 : 126204 : virtual void visit (const FloatType &type) override
253 : : {
254 : 126204 : ok = false;
255 : 126204 : if (emit_error_flag)
256 : : {
257 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
258 : 0 : location_t base_locus
259 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
260 : 0 : rich_location r (line_table, ref_locus);
261 : 0 : r.add_range (base_locus);
262 : 0 : rust_error_at (r, "expected [%s] got [%s]",
263 : 0 : get_base ()->as_string ().c_str (),
264 : 0 : type.as_string ().c_str ());
265 : 0 : }
266 : 126204 : }
267 : :
268 : 2 : virtual void visit (const ErrorType &type) override
269 : : {
270 : 2 : ok = false;
271 : 2 : if (emit_error_flag)
272 : : {
273 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
274 : 0 : location_t base_locus
275 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
276 : 0 : rich_location r (line_table, ref_locus);
277 : 0 : r.add_range (base_locus);
278 : 0 : rust_error_at (r, "expected [%s] got [%s]",
279 : 0 : get_base ()->as_string ().c_str (),
280 : 0 : type.as_string ().c_str ());
281 : 0 : }
282 : 2 : }
283 : :
284 : 28896 : virtual void visit (const CharType &type) override
285 : : {
286 : 28896 : ok = false;
287 : 28896 : if (emit_error_flag)
288 : : {
289 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
290 : 0 : location_t base_locus
291 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
292 : 0 : rich_location r (line_table, ref_locus);
293 : 0 : r.add_range (base_locus);
294 : 0 : rust_error_at (r, "expected [%s] got [%s]",
295 : 0 : get_base ()->as_string ().c_str (),
296 : 0 : type.as_string ().c_str ());
297 : 0 : }
298 : 28896 : }
299 : :
300 : 35465 : virtual void visit (const ReferenceType &type) override
301 : : {
302 : 35465 : ok = false;
303 : 35465 : if (emit_error_flag)
304 : : {
305 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
306 : 0 : location_t base_locus
307 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
308 : 0 : rich_location r (line_table, ref_locus);
309 : 0 : r.add_range (base_locus);
310 : 0 : rust_error_at (r, "expected [%s] got [%s]",
311 : 0 : get_base ()->as_string ().c_str (),
312 : 0 : type.as_string ().c_str ());
313 : 0 : }
314 : 35465 : }
315 : :
316 : 1799 : virtual void visit (const PointerType &type) override
317 : : {
318 : 1799 : ok = false;
319 : 1799 : if (emit_error_flag)
320 : : {
321 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
322 : 0 : location_t base_locus
323 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
324 : 0 : rich_location r (line_table, ref_locus);
325 : 0 : r.add_range (base_locus);
326 : 0 : rust_error_at (r, "expected [%s] got [%s]",
327 : 0 : get_base ()->as_string ().c_str (),
328 : 0 : type.as_string ().c_str ());
329 : 0 : }
330 : 1799 : }
331 : :
332 : 7 : virtual void visit (const StrType &type) override
333 : : {
334 : 7 : ok = false;
335 : 7 : if (emit_error_flag)
336 : : {
337 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
338 : 0 : location_t base_locus
339 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
340 : 0 : rich_location r (line_table, ref_locus);
341 : 0 : r.add_range (base_locus);
342 : 0 : rust_error_at (r, "expected [%s] got [%s]",
343 : 0 : get_base ()->as_string ().c_str (),
344 : 0 : type.as_string ().c_str ());
345 : 0 : }
346 : 7 : }
347 : :
348 : 4806 : virtual void visit (const NeverType &type) override
349 : : {
350 : 4806 : ok = false;
351 : 4806 : if (emit_error_flag)
352 : : {
353 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
354 : 0 : location_t base_locus
355 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
356 : 0 : rich_location r (line_table, ref_locus);
357 : 0 : r.add_range (base_locus);
358 : 0 : rust_error_at (r, "expected [%s] got [%s]",
359 : 0 : get_base ()->as_string ().c_str (),
360 : 0 : type.as_string ().c_str ());
361 : 0 : }
362 : 4806 : }
363 : :
364 : 0 : virtual void visit (const ProjectionType &type) override
365 : : {
366 : 0 : ok = false;
367 : 0 : if (emit_error_flag)
368 : : {
369 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
370 : 0 : location_t base_locus
371 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
372 : 0 : rich_location r (line_table, ref_locus);
373 : 0 : r.add_range (base_locus);
374 : 0 : rust_error_at (r, "expected [%s] got [%s]",
375 : 0 : get_base ()->as_string ().c_str (),
376 : 0 : type.as_string ().c_str ());
377 : 0 : }
378 : 0 : }
379 : :
380 : 75 : virtual void visit (const PlaceholderType &type) override
381 : : {
382 : : // it is ok for types to can eq to a placeholder
383 : 75 : ok = true;
384 : 75 : }
385 : :
386 : 28268 : virtual void visit (const ParamType &type) override
387 : : {
388 : 28268 : ok = false;
389 : 28268 : if (emit_error_flag)
390 : : {
391 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
392 : 0 : location_t base_locus
393 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
394 : 0 : rich_location r (line_table, ref_locus);
395 : 0 : r.add_range (base_locus);
396 : 0 : rust_error_at (r, "expected [%s] got [%s]",
397 : 0 : get_base ()->as_string ().c_str (),
398 : 0 : type.as_string ().c_str ());
399 : 0 : }
400 : 28268 : }
401 : :
402 : 13299 : virtual void visit (const DynamicObjectType &type) override
403 : : {
404 : 13299 : ok = false;
405 : 13299 : if (emit_error_flag)
406 : : {
407 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
408 : 0 : location_t base_locus
409 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
410 : 0 : rich_location r (line_table, ref_locus);
411 : 0 : r.add_range (base_locus);
412 : 0 : rust_error_at (r, "expected [%s] got [%s]",
413 : 0 : get_base ()->as_string ().c_str (),
414 : 0 : type.as_string ().c_str ());
415 : 0 : }
416 : 13299 : }
417 : :
418 : 7 : virtual void visit (const ClosureType &type) override
419 : : {
420 : 7 : ok = false;
421 : 7 : if (emit_error_flag)
422 : : {
423 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
424 : 0 : location_t base_locus
425 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
426 : 0 : rich_location r (line_table, ref_locus);
427 : 0 : r.add_range (base_locus);
428 : 0 : rust_error_at (r, "expected [%s] got [%s]",
429 : 0 : get_base ()->as_string ().c_str (),
430 : 0 : type.as_string ().c_str ());
431 : 0 : }
432 : 7 : }
433 : :
434 : 0 : virtual void visit (const OpaqueType &type) override
435 : : {
436 : 0 : ok = false;
437 : 0 : if (emit_error_flag)
438 : : {
439 : 0 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
440 : 0 : location_t base_locus
441 : 0 : = mappings.lookup_location (get_base ()->get_ref ());
442 : 0 : rich_location r (line_table, ref_locus);
443 : 0 : r.add_range (base_locus);
444 : 0 : rust_error_at (r, "expected [%s] got [%s]",
445 : 0 : get_base ()->as_string ().c_str (),
446 : 0 : type.as_string ().c_str ());
447 : 0 : }
448 : 0 : }
449 : :
450 : : protected:
451 : 1220037 : BaseCmp (const BaseType *base, bool emit_errors)
452 : 2440074 : : mappings (Analysis::Mappings::get ()),
453 : 1220037 : context (Resolver::TypeCheckContext::get ()), ok (false),
454 : 1220037 : emit_error_flag (emit_errors)
455 : 1220037 : {}
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 : 15910 : InferCmp (const InferType *base, bool emit_errors)
474 : 15910 : : BaseCmp (base, emit_errors), base (base)
475 : : {}
476 : :
477 : 545 : void visit (const BoolType &type) override
478 : : {
479 : 545 : bool is_valid
480 : 545 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
481 : 545 : if (is_valid)
482 : : {
483 : 502 : ok = true;
484 : 502 : return;
485 : : }
486 : :
487 : 43 : BaseCmp::visit (type);
488 : : }
489 : :
490 : 2388 : void visit (const IntType &type) override
491 : : {
492 : 2388 : bool is_valid
493 : 2388 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
494 : 2388 : || (base->get_infer_kind ()
495 : 2388 : == TyTy::InferType::InferTypeKind::INTEGRAL);
496 : 2388 : if (is_valid)
497 : : {
498 : 2388 : ok = true;
499 : 2388 : return;
500 : : }
501 : :
502 : 0 : BaseCmp::visit (type);
503 : : }
504 : :
505 : 4258 : void visit (const UintType &type) override
506 : : {
507 : 4258 : bool is_valid
508 : 4258 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
509 : 4258 : || (base->get_infer_kind ()
510 : 4258 : == TyTy::InferType::InferTypeKind::INTEGRAL);
511 : 4258 : if (is_valid)
512 : : {
513 : 4258 : ok = true;
514 : 4258 : return;
515 : : }
516 : :
517 : 0 : BaseCmp::visit (type);
518 : : }
519 : :
520 : 2226 : void visit (const USizeType &type) override
521 : : {
522 : 2226 : bool is_valid
523 : 2226 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
524 : 2226 : || (base->get_infer_kind ()
525 : 2226 : == TyTy::InferType::InferTypeKind::INTEGRAL);
526 : 2226 : if (is_valid)
527 : : {
528 : 2226 : ok = true;
529 : 2226 : return;
530 : : }
531 : :
532 : 0 : BaseCmp::visit (type);
533 : : }
534 : :
535 : 574 : void visit (const ISizeType &type) override
536 : : {
537 : 574 : bool is_valid
538 : 574 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
539 : 574 : || (base->get_infer_kind ()
540 : 574 : == TyTy::InferType::InferTypeKind::INTEGRAL);
541 : 574 : if (is_valid)
542 : : {
543 : 574 : ok = true;
544 : 574 : return;
545 : : }
546 : :
547 : 0 : BaseCmp::visit (type);
548 : : }
549 : :
550 : 2156 : void visit (const FloatType &type) override
551 : : {
552 : 2156 : bool is_valid
553 : 2156 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL)
554 : 2156 : || (base->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT);
555 : 2156 : if (is_valid)
556 : : {
557 : 2012 : ok = true;
558 : 2012 : return;
559 : : }
560 : :
561 : 144 : BaseCmp::visit (type);
562 : : }
563 : :
564 : 0 : void visit (const ArrayType &type) override
565 : : {
566 : 0 : bool is_valid
567 : 0 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
568 : 0 : if (is_valid)
569 : : {
570 : 0 : ok = true;
571 : 0 : return;
572 : : }
573 : :
574 : 0 : BaseCmp::visit (type);
575 : : }
576 : :
577 : 77 : void visit (const SliceType &type) override
578 : : {
579 : 77 : bool is_valid
580 : 77 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
581 : 77 : if (is_valid)
582 : : {
583 : 0 : ok = true;
584 : 0 : return;
585 : : }
586 : :
587 : 77 : BaseCmp::visit (type);
588 : : }
589 : :
590 : 380 : void visit (const ADTType &type) override
591 : : {
592 : 380 : bool is_valid
593 : 380 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
594 : 380 : if (is_valid)
595 : : {
596 : 283 : ok = true;
597 : 283 : return;
598 : : }
599 : :
600 : 97 : BaseCmp::visit (type);
601 : : }
602 : :
603 : 0 : void visit (const TupleType &type) override
604 : : {
605 : 0 : bool is_valid
606 : 0 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
607 : 0 : if (is_valid)
608 : : {
609 : 0 : ok = true;
610 : 0 : return;
611 : : }
612 : :
613 : 0 : BaseCmp::visit (type);
614 : : }
615 : :
616 : 57 : void visit (const InferType &type) override
617 : : {
618 : 57 : switch (base->get_infer_kind ())
619 : : {
620 : 57 : case InferType::InferTypeKind::GENERAL:
621 : 57 : ok = true;
622 : 57 : return;
623 : :
624 : 0 : case InferType::InferTypeKind::INTEGRAL: {
625 : 0 : if (type.get_infer_kind () == InferType::InferTypeKind::INTEGRAL)
626 : : {
627 : 0 : ok = true;
628 : 0 : return;
629 : : }
630 : 0 : else if (type.get_infer_kind () == InferType::InferTypeKind::GENERAL)
631 : : {
632 : 0 : ok = true;
633 : 0 : return;
634 : : }
635 : : }
636 : : break;
637 : :
638 : 0 : case InferType::InferTypeKind::FLOAT: {
639 : 0 : if (type.get_infer_kind () == InferType::InferTypeKind::FLOAT)
640 : : {
641 : 0 : ok = true;
642 : 0 : return;
643 : : }
644 : 0 : else if (type.get_infer_kind () == InferType::InferTypeKind::GENERAL)
645 : : {
646 : 0 : ok = true;
647 : 0 : return;
648 : : }
649 : : }
650 : : break;
651 : : }
652 : :
653 : 0 : BaseCmp::visit (type);
654 : : }
655 : :
656 : 538 : void visit (const CharType &type) override
657 : : {
658 : 538 : {
659 : 538 : bool is_valid
660 : 538 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
661 : 538 : if (is_valid)
662 : : {
663 : 502 : ok = true;
664 : 502 : return;
665 : : }
666 : :
667 : 36 : BaseCmp::visit (type);
668 : : }
669 : : }
670 : :
671 : 14 : void visit (const ReferenceType &type) override
672 : : {
673 : 14 : bool is_valid
674 : 14 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
675 : 14 : if (is_valid)
676 : : {
677 : 0 : ok = true;
678 : 0 : return;
679 : : }
680 : :
681 : 14 : BaseCmp::visit (type);
682 : : }
683 : :
684 : 2 : void visit (const PointerType &type) override
685 : : {
686 : 2 : bool is_valid
687 : 2 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
688 : 2 : if (is_valid)
689 : : {
690 : 0 : ok = true;
691 : 0 : return;
692 : : }
693 : :
694 : 2 : BaseCmp::visit (type);
695 : : }
696 : :
697 : 2695 : void visit (const ParamType &) override { ok = true; }
698 : :
699 : 0 : void visit (const DynamicObjectType &type) override
700 : : {
701 : 0 : bool is_valid
702 : 0 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
703 : 0 : if (is_valid)
704 : : {
705 : 0 : ok = true;
706 : 0 : return;
707 : : }
708 : :
709 : 0 : BaseCmp::visit (type);
710 : : }
711 : :
712 : 0 : void visit (const ClosureType &type) override
713 : : {
714 : 0 : bool is_valid
715 : 0 : = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
716 : 0 : if (is_valid)
717 : : {
718 : 0 : ok = true;
719 : 0 : return;
720 : : }
721 : :
722 : 0 : BaseCmp::visit (type);
723 : : }
724 : :
725 : : private:
726 : 0 : 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 : 0 : FnCmp (const FnType *base, bool emit_errors)
736 : 0 : : BaseCmp (base, emit_errors), base (base)
737 : : {}
738 : :
739 : 0 : void visit (const InferType &type) override
740 : : {
741 : 0 : ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
742 : 0 : }
743 : :
744 : 0 : void visit (const FnType &type) override
745 : : {
746 : 0 : if (base->num_params () != type.num_params ())
747 : : {
748 : 0 : BaseCmp::visit (type);
749 : 0 : return;
750 : : }
751 : :
752 : 0 : for (size_t i = 0; i < base->num_params (); i++)
753 : : {
754 : 0 : auto a = base->param_at (i).get_type ();
755 : 0 : auto b = type.param_at (i).get_type ();
756 : :
757 : 0 : if (!a->can_eq (b, emit_error_flag))
758 : : {
759 : 0 : emit_error_flag = false;
760 : 0 : BaseCmp::visit (type);
761 : 0 : return;
762 : : }
763 : : }
764 : :
765 : 0 : if (!base->get_return_type ()->can_eq (type.get_return_type (),
766 : 0 : emit_error_flag))
767 : : {
768 : 0 : emit_error_flag = false;
769 : 0 : BaseCmp::visit (type);
770 : 0 : return;
771 : : }
772 : :
773 : 0 : ok = true;
774 : : }
775 : :
776 : : private:
777 : 0 : 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 : 0 : FnptrCmp (const FnPtr *base, bool emit_errors)
787 : 0 : : BaseCmp (base, emit_errors), base (base)
788 : : {}
789 : :
790 : 0 : void visit (const InferType &type) override
791 : : {
792 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
793 : : {
794 : 0 : BaseCmp::visit (type);
795 : 0 : return;
796 : : }
797 : :
798 : 0 : ok = true;
799 : : }
800 : :
801 : 0 : void visit (const FnPtr &type) override
802 : : {
803 : 0 : if (base->num_params () != type.num_params ())
804 : : {
805 : 0 : BaseCmp::visit (type);
806 : 0 : return;
807 : : }
808 : :
809 : 0 : auto this_ret_type = base->get_return_type ();
810 : 0 : auto other_ret_type = type.get_return_type ();
811 : 0 : if (!this_ret_type->can_eq (other_ret_type, emit_error_flag))
812 : : {
813 : 0 : BaseCmp::visit (type);
814 : 0 : return;
815 : : }
816 : :
817 : 0 : for (size_t i = 0; i < base->num_params (); i++)
818 : : {
819 : 0 : auto this_param = base->get_param_type_at (i);
820 : 0 : auto other_param = type.get_param_type_at (i);
821 : 0 : if (!this_param->can_eq (other_param, emit_error_flag))
822 : : {
823 : 0 : BaseCmp::visit (type);
824 : 0 : return;
825 : : }
826 : : }
827 : :
828 : 0 : ok = true;
829 : : }
830 : :
831 : 0 : void visit (const FnType &type) override
832 : : {
833 : 0 : if (base->num_params () != type.num_params ())
834 : : {
835 : 0 : BaseCmp::visit (type);
836 : 0 : return;
837 : : }
838 : :
839 : 0 : auto this_ret_type = base->get_return_type ();
840 : 0 : auto other_ret_type = type.get_return_type ();
841 : 0 : if (!this_ret_type->can_eq (other_ret_type, emit_error_flag))
842 : : {
843 : 0 : BaseCmp::visit (type);
844 : 0 : return;
845 : : }
846 : :
847 : 0 : for (size_t i = 0; i < base->num_params (); i++)
848 : : {
849 : 0 : auto this_param = base->get_param_type_at (i);
850 : 0 : auto other_param = type.param_at (i).get_type ();
851 : 0 : if (!this_param->can_eq (other_param, emit_error_flag))
852 : : {
853 : 0 : BaseCmp::visit (type);
854 : 0 : return;
855 : : }
856 : : }
857 : :
858 : 0 : ok = true;
859 : : }
860 : :
861 : : private:
862 : 0 : 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 : 64 : ClosureCmp (const ClosureType *base, bool emit_errors)
872 : 64 : : BaseCmp (base, emit_errors), base (base)
873 : : {}
874 : :
875 : 0 : void visit (const InferType &type) override
876 : : {
877 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
878 : : {
879 : 0 : BaseCmp::visit (type);
880 : 0 : return;
881 : : }
882 : :
883 : 0 : ok = true;
884 : : }
885 : :
886 : 64 : void visit (const ClosureType &type) override
887 : : {
888 : 64 : if (base->get_def_id () != type.get_def_id ())
889 : : {
890 : 7 : BaseCmp::visit (type);
891 : 7 : return;
892 : : }
893 : :
894 : 57 : if (!base->get_parameters ().can_eq (&type.get_parameters (), false))
895 : : {
896 : 0 : BaseCmp::visit (type);
897 : 0 : return;
898 : : }
899 : :
900 : 57 : if (!base->get_result_type ().can_eq (&type.get_result_type (), false))
901 : : {
902 : 0 : BaseCmp::visit (type);
903 : 0 : return;
904 : : }
905 : :
906 : 57 : ok = true;
907 : : }
908 : :
909 : : private:
910 : 0 : 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 : 13294 : ArrayCmp (const ArrayType *base, bool emit_errors)
920 : 13294 : : BaseCmp (base, emit_errors), base (base)
921 : : {}
922 : :
923 : 98 : void visit (const ArrayType &type) override
924 : : {
925 : : // check base type
926 : 98 : const BaseType *base_element = base->get_element_type ();
927 : 98 : const BaseType *other_element = type.get_element_type ();
928 : 98 : if (!base_element->can_eq (other_element, emit_error_flag))
929 : : {
930 : 0 : BaseCmp::visit (type);
931 : 0 : return;
932 : : }
933 : :
934 : 98 : ok = true;
935 : : }
936 : :
937 : 0 : void visit (const InferType &type) override
938 : : {
939 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
940 : : {
941 : 0 : BaseCmp::visit (type);
942 : 0 : return;
943 : : }
944 : :
945 : 0 : ok = true;
946 : : }
947 : :
948 : : private:
949 : 0 : 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 : 3085 : SliceCmp (const SliceType *base, bool emit_errors)
959 : 3085 : : BaseCmp (base, emit_errors), base (base)
960 : : {}
961 : :
962 : 756 : void visit (const SliceType &type) override
963 : : {
964 : : // check base type
965 : 756 : const BaseType *base_element = base->get_element_type ();
966 : 756 : const BaseType *other_element = type.get_element_type ();
967 : 756 : if (!base_element->can_eq (other_element, emit_error_flag))
968 : : {
969 : 49 : BaseCmp::visit (type);
970 : 49 : return;
971 : : }
972 : :
973 : 707 : ok = true;
974 : : }
975 : :
976 : 77 : void visit (const InferType &type) override
977 : : {
978 : 77 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
979 : : {
980 : 77 : BaseCmp::visit (type);
981 : 77 : return;
982 : : }
983 : :
984 : 0 : ok = true;
985 : : }
986 : :
987 : : private:
988 : 0 : 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 : 30904 : BoolCmp (const BoolType *base, bool emit_errors)
998 : 30904 : : BaseCmp (base, emit_errors), base (base)
999 : : {}
1000 : :
1001 : 813 : void visit (const BoolType &type) override { ok = true; }
1002 : :
1003 : 43 : void visit (const InferType &type) override
1004 : : {
1005 : 43 : ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
1006 : 43 : }
1007 : :
1008 : : private:
1009 : 0 : 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 : 147897 : IntCmp (const IntType *base, bool emit_errors)
1019 : 147897 : : BaseCmp (base, emit_errors), base (base)
1020 : : {}
1021 : :
1022 : 4 : void visit (const InferType &type) override
1023 : : {
1024 : 4 : ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1025 : 4 : }
1026 : :
1027 : 19427 : void visit (const IntType &type) override
1028 : : {
1029 : 19427 : ok = type.get_int_kind () == base->get_int_kind ();
1030 : 19427 : }
1031 : :
1032 : : private:
1033 : 0 : 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 : 433573 : UintCmp (const UintType *base, bool emit_errors)
1043 : 433573 : : BaseCmp (base, emit_errors), base (base)
1044 : : {}
1045 : :
1046 : 0 : void visit (const InferType &type) override
1047 : : {
1048 : 0 : ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1049 : 0 : }
1050 : :
1051 : 159875 : void visit (const UintType &type) override
1052 : : {
1053 : 159875 : ok = type.get_uint_kind () == base->get_uint_kind ();
1054 : 159875 : }
1055 : :
1056 : : private:
1057 : 0 : 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 : 144022 : FloatCmp (const FloatType *base, bool emit_errors)
1067 : 144022 : : BaseCmp (base, emit_errors), base (base)
1068 : : {}
1069 : :
1070 : 144 : void visit (const InferType &type) override
1071 : : {
1072 : 144 : ok = type.get_infer_kind () != InferType::InferTypeKind::INTEGRAL;
1073 : 144 : }
1074 : :
1075 : 14860 : void visit (const FloatType &type) override
1076 : : {
1077 : 14860 : ok = type.get_float_kind () == base->get_float_kind ();
1078 : 14860 : }
1079 : :
1080 : : private:
1081 : 0 : 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 : 61889 : ADTCmp (const ADTType *base, bool emit_errors)
1091 : 61889 : : BaseCmp (base, emit_errors), base (base)
1092 : : {}
1093 : :
1094 : 13120 : void visit (const ADTType &type) override
1095 : : {
1096 : 13120 : if (base->get_adt_kind () != type.get_adt_kind ())
1097 : : {
1098 : 2031 : BaseCmp::visit (type);
1099 : 2031 : return;
1100 : : }
1101 : :
1102 : 33267 : if (base->get_identifier ().compare (type.get_identifier ()) != 0)
1103 : : {
1104 : 2821 : BaseCmp::visit (type);
1105 : 2821 : return;
1106 : : }
1107 : :
1108 : 8268 : if (base->number_of_variants () != type.number_of_variants ())
1109 : : {
1110 : 0 : BaseCmp::visit (type);
1111 : 0 : return;
1112 : : }
1113 : :
1114 : 15844 : for (size_t i = 0; i < type.number_of_variants (); ++i)
1115 : : {
1116 : 8643 : TyTy::VariantDef *a = base->get_variants ().at (i);
1117 : 8643 : TyTy::VariantDef *b = type.get_variants ().at (i);
1118 : :
1119 : 8643 : if (a->num_fields () != b->num_fields ())
1120 : : {
1121 : 0 : BaseCmp::visit (type);
1122 : 0 : return;
1123 : : }
1124 : :
1125 : 16826 : for (size_t j = 0; j < a->num_fields (); j++)
1126 : : {
1127 : 9250 : TyTy::StructFieldType *base_field = a->get_field_at_index (j);
1128 : 9250 : TyTy::StructFieldType *other_field = b->get_field_at_index (j);
1129 : :
1130 : 9250 : TyTy::BaseType *this_field_ty = base_field->get_field_type ();
1131 : 9250 : TyTy::BaseType *other_field_ty = other_field->get_field_type ();
1132 : :
1133 : 9250 : if (!this_field_ty->can_eq (other_field_ty, emit_error_flag))
1134 : : {
1135 : 1067 : BaseCmp::visit (type);
1136 : 1067 : return;
1137 : : }
1138 : : }
1139 : : }
1140 : :
1141 : 7201 : ok = true;
1142 : : }
1143 : :
1144 : 97 : void visit (const InferType &type) override
1145 : : {
1146 : 97 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1147 : : {
1148 : 97 : BaseCmp::visit (type);
1149 : 97 : return;
1150 : : }
1151 : :
1152 : 0 : ok = true;
1153 : : }
1154 : :
1155 : : private:
1156 : 0 : 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 : 194 : TupleCmp (const TupleType *base, bool emit_errors)
1166 : 194 : : BaseCmp (base, emit_errors), base (base)
1167 : : {}
1168 : :
1169 : 116 : void visit (const TupleType &type) override
1170 : : {
1171 : 116 : if (base->num_fields () != type.num_fields ())
1172 : : {
1173 : 0 : BaseCmp::visit (type);
1174 : 0 : return;
1175 : : }
1176 : :
1177 : 230 : for (size_t i = 0; i < base->num_fields (); i++)
1178 : : {
1179 : 114 : BaseType *bo = base->get_field (i);
1180 : 114 : BaseType *fo = type.get_field (i);
1181 : :
1182 : 114 : if (!bo->can_eq (fo, emit_error_flag))
1183 : : {
1184 : 0 : BaseCmp::visit (type);
1185 : 0 : return;
1186 : : }
1187 : : }
1188 : :
1189 : 116 : ok = true;
1190 : : }
1191 : :
1192 : 0 : void visit (const InferType &type) override
1193 : : {
1194 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1195 : : {
1196 : 0 : BaseCmp::visit (type);
1197 : 0 : return;
1198 : : }
1199 : :
1200 : 0 : ok = true;
1201 : : }
1202 : :
1203 : : private:
1204 : 0 : 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 : 171963 : USizeCmp (const USizeType *base, bool emit_errors)
1214 : 171963 : : BaseCmp (base, emit_errors), base (base)
1215 : : {}
1216 : :
1217 : 0 : void visit (const InferType &type) override
1218 : : {
1219 : 0 : ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1220 : 0 : }
1221 : :
1222 : 16385 : void visit (const USizeType &type) override { ok = true; }
1223 : :
1224 : : private:
1225 : 0 : 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 : 35076 : ISizeCmp (const ISizeType *base, bool emit_errors)
1235 : 35076 : : BaseCmp (base, emit_errors), base (base)
1236 : : {}
1237 : :
1238 : 0 : void visit (const InferType &type) override
1239 : : {
1240 : 0 : ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT;
1241 : 0 : }
1242 : :
1243 : 668 : void visit (const ISizeType &type) override { ok = true; }
1244 : :
1245 : : private:
1246 : 0 : 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 : 29541 : CharCmp (const CharType *base, bool emit_errors)
1256 : 29541 : : BaseCmp (base, emit_errors), base (base)
1257 : : {}
1258 : :
1259 : 36 : void visit (const InferType &type) override
1260 : : {
1261 : 36 : ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL;
1262 : 36 : }
1263 : :
1264 : 402 : void visit (const CharType &type) override { ok = true; }
1265 : :
1266 : : private:
1267 : 0 : 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 : 36451 : ReferenceCmp (const ReferenceType *base, bool emit_errors)
1277 : 36451 : : BaseCmp (base, emit_errors), base (base)
1278 : : {}
1279 : :
1280 : 4623 : void visit (const ReferenceType &type) override
1281 : : {
1282 : 4623 : auto base_type = base->get_base ();
1283 : 4623 : auto other_base_type = type.get_base ();
1284 : :
1285 : 4623 : bool mutability_ok = base->is_mutable () ? type.is_mutable () : true;
1286 : 376 : if (!mutability_ok)
1287 : : {
1288 : 218 : BaseCmp::visit (type);
1289 : 218 : return;
1290 : : }
1291 : :
1292 : 4405 : if (!base_type->can_eq (other_base_type, emit_error_flag))
1293 : : {
1294 : 3747 : BaseCmp::visit (type);
1295 : 3747 : return;
1296 : : }
1297 : :
1298 : 658 : ok = true;
1299 : : }
1300 : :
1301 : 14 : void visit (const InferType &type) override
1302 : : {
1303 : 14 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1304 : : {
1305 : 14 : BaseCmp::visit (type);
1306 : 14 : return;
1307 : : }
1308 : :
1309 : 0 : ok = true;
1310 : : }
1311 : :
1312 : : private:
1313 : 0 : 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 : 1882 : PointerCmp (const PointerType *base, bool emit_errors)
1323 : 1882 : : BaseCmp (base, emit_errors), base (base)
1324 : : {}
1325 : :
1326 : 182 : void visit (const PointerType &type) override
1327 : : {
1328 : 182 : auto base_type = base->get_base ();
1329 : 182 : auto other_base_type = type.get_base ();
1330 : :
1331 : 182 : bool mutability_ok = base->is_mutable () ? type.is_mutable () : true;
1332 : 35 : if (!mutability_ok)
1333 : : {
1334 : 35 : BaseCmp::visit (type);
1335 : 35 : return;
1336 : : }
1337 : :
1338 : 147 : if (!base_type->can_eq (other_base_type, emit_error_flag))
1339 : : {
1340 : 50 : BaseCmp::visit (type);
1341 : 50 : return;
1342 : : }
1343 : :
1344 : 97 : ok = true;
1345 : : }
1346 : :
1347 : 0 : void visit (const InferType &type) override
1348 : : {
1349 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1350 : : {
1351 : 0 : BaseCmp::visit (type);
1352 : 0 : return;
1353 : : }
1354 : :
1355 : 0 : ok = true;
1356 : : }
1357 : :
1358 : : private:
1359 : 0 : 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 : 71399 : ParamCmp (const ParamType *base, bool emit_errors)
1369 : 71399 : : 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 : 71399 : bool can_eq (const BaseType *other) override
1382 : : {
1383 : 71399 : if (!base->can_resolve ())
1384 : 36408 : return BaseCmp::can_eq (other);
1385 : :
1386 : 34991 : auto lookup = base->resolve ();
1387 : 34991 : 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 : 5395 : void visit (const ParamType &) override { ok = true; }
1397 : :
1398 : 0 : void visit (const TupleType &) override { ok = true; }
1399 : :
1400 : 27 : void visit (const InferType &) override { ok = true; }
1401 : :
1402 : 0 : void visit (const FnType &) override { ok = true; }
1403 : :
1404 : 0 : void visit (const FnPtr &) override { ok = true; }
1405 : :
1406 : 1923 : void visit (const ADTType &) override { ok = true; }
1407 : :
1408 : 810 : void visit (const ArrayType &) override { ok = true; }
1409 : :
1410 : 52 : void visit (const SliceType &) override { ok = true; }
1411 : :
1412 : 400 : void visit (const BoolType &) override { ok = true; }
1413 : :
1414 : 2327 : void visit (const IntType &) override { ok = true; }
1415 : :
1416 : 14274 : void visit (const UintType &) override { ok = true; }
1417 : :
1418 : 6231 : void visit (const USizeType &) override { ok = true; }
1419 : :
1420 : 407 : void visit (const ISizeType &) override { ok = true; }
1421 : :
1422 : 3350 : void visit (const FloatType &) override { ok = true; }
1423 : :
1424 : 351 : void visit (const CharType &) override { ok = true; }
1425 : :
1426 : 349 : void visit (const ReferenceType &) override { ok = true; }
1427 : :
1428 : 0 : void visit (const PointerType &) override { ok = true; }
1429 : :
1430 : 0 : void visit (const StrType &) override { ok = true; }
1431 : :
1432 : 278 : void visit (const NeverType &) override { ok = true; }
1433 : :
1434 : 234 : void visit (const DynamicObjectType &) override { ok = true; }
1435 : :
1436 : 0 : void visit (const PlaceholderType &type) override
1437 : : {
1438 : 0 : ok = base->get_symbol ().compare (type.get_symbol ()) == 0;
1439 : 0 : }
1440 : :
1441 : : private:
1442 : 0 : 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 : 7 : StrCmp (const StrType *base, bool emit_errors)
1453 : 7 : : BaseCmp (base, emit_errors), base (base)
1454 : : {}
1455 : :
1456 : 0 : void visit (const StrType &type) override { ok = true; }
1457 : :
1458 : 0 : void visit (const InferType &type) override
1459 : : {
1460 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1461 : : {
1462 : 0 : BaseCmp::visit (type);
1463 : 0 : return;
1464 : : }
1465 : :
1466 : 0 : ok = true;
1467 : : }
1468 : :
1469 : : private:
1470 : 0 : 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 : 5117 : NeverCmp (const NeverType *base, bool emit_errors)
1480 : 5117 : : BaseCmp (base, emit_errors), base (base)
1481 : : {}
1482 : :
1483 : 33 : void visit (const NeverType &type) override { ok = true; }
1484 : :
1485 : 0 : void visit (const InferType &type) override
1486 : : {
1487 : 0 : if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
1488 : : {
1489 : 0 : BaseCmp::visit (type);
1490 : 0 : return;
1491 : : }
1492 : :
1493 : 0 : ok = true;
1494 : : }
1495 : :
1496 : : private:
1497 : 0 : 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 : 4229 : PlaceholderCmp (const PlaceholderType *base, bool emit_errors)
1507 : 4229 : : BaseCmp (base, emit_errors), base (base)
1508 : : {}
1509 : :
1510 : 4229 : bool can_eq (const BaseType *other) override
1511 : : {
1512 : 4229 : if (!base->can_resolve ())
1513 : 4159 : return BaseCmp::can_eq (other);
1514 : :
1515 : 70 : BaseType *lookup = base->resolve ();
1516 : 70 : return lookup->can_eq (other, emit_error_flag);
1517 : : }
1518 : :
1519 : 0 : void visit (const TupleType &) override { ok = true; }
1520 : :
1521 : 29 : void visit (const ADTType &) override { ok = true; }
1522 : :
1523 : 0 : void visit (const InferType &) override { ok = true; }
1524 : :
1525 : 0 : void visit (const FnType &) override { ok = true; }
1526 : :
1527 : 0 : void visit (const FnPtr &) override { ok = true; }
1528 : :
1529 : 0 : void visit (const ArrayType &) override { ok = true; }
1530 : :
1531 : 54 : void visit (const BoolType &) override { ok = true; }
1532 : :
1533 : 261 : void visit (const IntType &) override { ok = true; }
1534 : :
1535 : 1462 : void visit (const UintType &) override { ok = true; }
1536 : :
1537 : 303 : void visit (const USizeType &) override { ok = true; }
1538 : :
1539 : 76 : void visit (const ISizeType &) override { ok = true; }
1540 : :
1541 : 1060 : void visit (const FloatType &) override { ok = true; }
1542 : :
1543 : 0 : void visit (const ErrorType &) override { ok = true; }
1544 : :
1545 : 54 : void visit (const CharType &) override { ok = true; }
1546 : :
1547 : 2 : void visit (const ReferenceType &) override { ok = true; }
1548 : :
1549 : 649 : void visit (const ParamType &) override { ok = true; }
1550 : :
1551 : 0 : void visit (const StrType &) override { ok = true; }
1552 : :
1553 : 29 : void visit (const NeverType &) override { ok = true; }
1554 : :
1555 : 105 : void visit (const SliceType &) override { ok = true; }
1556 : :
1557 : : private:
1558 : 0 : 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 : 13540 : DynamicCmp (const DynamicObjectType *base, bool emit_errors)
1569 : 13540 : : BaseCmp (base, emit_errors), base (base)
1570 : : {}
1571 : :
1572 : 7 : void visit (const DynamicObjectType &type) override
1573 : : {
1574 : 7 : if (base->num_specified_bounds () != type.num_specified_bounds ())
1575 : : {
1576 : 0 : BaseCmp::visit (type);
1577 : 0 : return;
1578 : : }
1579 : :
1580 : 7 : location_t ref_locus = mappings.lookup_location (type.get_ref ());
1581 : 7 : ok = base->bounds_compatible (type, ref_locus, false);
1582 : : }
1583 : :
1584 : : private:
1585 : 0 : 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 : 0 : OpaqueCmp (const OpaqueType *base, bool emit_errors)
1596 : 0 : : BaseCmp (base, emit_errors), base (base)
1597 : : {}
1598 : :
1599 : : // TODO
1600 : :
1601 : : private:
1602 : 0 : 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
|