Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / rust / typecheck / rust-unify.cc
blobcbbff8c44204670f075dd305ce015ce83e2cc17e
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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.
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.
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/>.
19 #include "rust-unify.h"
21 namespace Rust {
22 namespace Resolver {
24 UnifyRules::UnifyRules (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
25 Location locus, bool commit_flag, bool emit_error)
26 : lhs (lhs), rhs (rhs), locus (locus), commit_flag (commit_flag),
27 emit_error (emit_error), mappings (*Analysis::Mappings::get ()),
28 context (*TypeCheckContext::get ())
31 TyTy::BaseType *
32 UnifyRules::Resolve (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
33 Location locus, bool commit_flag, bool emit_error)
35 UnifyRules r (lhs, rhs, locus, commit_flag, emit_error);
36 TyTy::BaseType *result = r.go ();
38 if (r.commit_flag)
39 r.commit (result);
41 bool failed = result->get_kind () == TyTy::TypeKind::ERROR;
42 if (failed && r.emit_error)
43 r.emit_type_mismatch ();
45 return result;
48 TyTy::BaseType *
49 UnifyRules::get_base ()
51 return lhs.get_ty ()->destructure ();
54 TyTy::BaseType *
55 UnifyRules::get_other ()
57 return rhs.get_ty ()->destructure ();
60 void
61 UnifyRules::commit (TyTy::BaseType *resolved)
63 resolved->append_reference (get_base ()->get_ref ());
64 resolved->append_reference (get_other ()->get_ref ());
65 for (auto ref : get_base ()->get_combined_refs ())
66 resolved->append_reference (ref);
67 for (auto ref : get_other ()->get_combined_refs ())
68 resolved->append_reference (ref);
70 get_other ()->append_reference (resolved->get_ref ());
71 get_other ()->append_reference (get_base ()->get_ref ());
72 get_base ()->append_reference (resolved->get_ref ());
73 get_base ()->append_reference (get_other ()->get_ref ());
75 bool result_resolved = resolved->get_kind () != TyTy::TypeKind::INFER;
76 bool result_is_infer_var = resolved->get_kind () == TyTy::TypeKind::INFER;
77 bool results_is_non_general_infer_var
78 = (result_is_infer_var
79 && (static_cast<TyTy::InferType *> (resolved))->get_infer_kind ()
80 != TyTy::InferType::GENERAL);
81 if (result_resolved || results_is_non_general_infer_var)
83 for (auto &ref : resolved->get_combined_refs ())
85 TyTy::BaseType *ref_tyty = nullptr;
86 bool ok = context.lookup_type (ref, &ref_tyty);
87 if (!ok)
88 continue;
90 // if any of the types are inference variables lets fix them
91 if (ref_tyty->get_kind () == TyTy::TypeKind::INFER)
93 auto node = Analysis::NodeMapping (mappings.get_current_crate (),
94 UNKNOWN_NODEID, ref,
95 UNKNOWN_LOCAL_DEFID);
96 context.insert_type (node, resolved->clone ());
102 void
103 UnifyRules::emit_type_mismatch () const
105 TyTy::BaseType *expected = lhs.get_ty ();
106 TyTy::BaseType *expr = rhs.get_ty ();
108 RichLocation r (locus);
109 r.add_range (lhs.get_locus ());
110 r.add_range (rhs.get_locus ());
111 rust_error_at (r, "expected %<%s%> got %<%s%>",
112 expected->get_name ().c_str (), expr->get_name ().c_str ());
115 TyTy::BaseType *
116 UnifyRules::go ()
118 TyTy::BaseType *ltype = lhs.get_ty ();
119 TyTy::BaseType *rtype = rhs.get_ty ();
121 ltype = lhs.get_ty ()->destructure ();
122 rtype = rhs.get_ty ()->destructure ();
124 rust_debug ("unify::go ltype={%s} rtype={%s}", ltype->debug_str ().c_str (),
125 rtype->debug_str ().c_str ());
127 // check bounds
128 if (ltype->num_specified_bounds () > 0)
130 if (!ltype->bounds_compatible (*rtype, locus, true))
132 // already emitted an error
133 emit_error = false;
134 return new TyTy::ErrorType (0);
138 switch (ltype->get_kind ())
140 case TyTy::INFER:
141 return expect_inference_variable (static_cast<TyTy::InferType *> (ltype),
142 rtype);
144 case TyTy::ADT:
145 return expect_adt (static_cast<TyTy::ADTType *> (ltype), rtype);
147 case TyTy::STR:
148 return expect_str (static_cast<TyTy::StrType *> (ltype), rtype);
150 case TyTy::REF:
151 return expect_reference (static_cast<TyTy::ReferenceType *> (ltype),
152 rtype);
154 case TyTy::POINTER:
155 return expect_pointer (static_cast<TyTy::PointerType *> (ltype), rtype);
157 case TyTy::PARAM:
158 return expect_param (static_cast<TyTy::ParamType *> (ltype), rtype);
160 case TyTy::ARRAY:
161 return expect_array (static_cast<TyTy::ArrayType *> (ltype), rtype);
163 case TyTy::SLICE:
164 return expect_slice (static_cast<TyTy::SliceType *> (ltype), rtype);
166 case TyTy::FNDEF:
167 return expect_fndef (static_cast<TyTy::FnType *> (ltype), rtype);
169 case TyTy::FNPTR:
170 return expect_fnptr (static_cast<TyTy::FnPtr *> (ltype), rtype);
172 case TyTy::TUPLE:
173 return expect_tuple (static_cast<TyTy::TupleType *> (ltype), rtype);
175 case TyTy::BOOL:
176 return expect_bool (static_cast<TyTy::BoolType *> (ltype), rtype);
178 case TyTy::CHAR:
179 return expect_char (static_cast<TyTy::CharType *> (ltype), rtype);
181 case TyTy::INT:
182 return expect_int (static_cast<TyTy::IntType *> (ltype), rtype);
184 case TyTy::UINT:
185 return expect_uint (static_cast<TyTy::UintType *> (ltype), rtype);
187 case TyTy::FLOAT:
188 return expect_float (static_cast<TyTy::FloatType *> (ltype), rtype);
190 case TyTy::USIZE:
191 return expect_usize (static_cast<TyTy::USizeType *> (ltype), rtype);
193 case TyTy::ISIZE:
194 return expect_isize (static_cast<TyTy::ISizeType *> (ltype), rtype);
196 case TyTy::NEVER:
197 return expect_never (static_cast<TyTy::NeverType *> (ltype), rtype);
199 case TyTy::PLACEHOLDER:
200 return expect_placeholder (static_cast<TyTy::PlaceholderType *> (ltype),
201 rtype);
203 case TyTy::PROJECTION:
204 return expect_projection (static_cast<TyTy::ProjectionType *> (ltype),
205 rtype);
207 case TyTy::DYNAMIC:
208 return expect_dyn (static_cast<TyTy::DynamicObjectType *> (ltype), rtype);
210 case TyTy::CLOSURE:
211 return expect_closure (static_cast<TyTy::ClosureType *> (ltype), rtype);
213 case TyTy::ERROR:
214 return new TyTy::ErrorType (0);
217 return new TyTy::ErrorType (0);
220 TyTy::BaseType *
221 UnifyRules::expect_inference_variable (TyTy::InferType *ltype,
222 TyTy::BaseType *rtype)
224 switch (rtype->get_kind ())
226 case TyTy::INFER: {
227 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
228 switch (ltype->get_infer_kind ())
230 case TyTy::InferType::InferTypeKind::GENERAL:
231 return rtype->clone ();
233 case TyTy::InferType::InferTypeKind::INTEGRAL: {
234 bool is_valid = r->get_infer_kind ()
235 == TyTy::InferType::InferTypeKind::INTEGRAL
236 || r->get_infer_kind ()
237 == TyTy::InferType::InferTypeKind::GENERAL;
238 if (is_valid)
239 return rtype->clone ();
241 break;
243 case TyTy::InferType::InferTypeKind::FLOAT: {
244 bool is_valid
245 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT
246 || r->get_infer_kind ()
247 == TyTy::InferType::InferTypeKind::GENERAL;
248 if (is_valid)
249 return rtype->clone ();
251 break;
254 break;
256 case TyTy::INT:
257 case TyTy::UINT:
258 case TyTy::USIZE:
259 case TyTy::ISIZE: {
260 bool is_valid = (ltype->get_infer_kind ()
261 == TyTy::InferType::InferTypeKind::GENERAL)
262 || (ltype->get_infer_kind ()
263 == TyTy::InferType::InferTypeKind::INTEGRAL);
264 if (is_valid)
265 return rtype->clone ();
267 break;
269 case TyTy::FLOAT: {
270 bool is_valid = (ltype->get_infer_kind ()
271 == TyTy::InferType::InferTypeKind::GENERAL)
272 || (ltype->get_infer_kind ()
273 == TyTy::InferType::InferTypeKind::FLOAT);
274 if (is_valid)
275 return rtype->clone ();
277 break;
279 case TyTy::ADT:
280 case TyTy::STR:
281 case TyTy::REF:
282 case TyTy::POINTER:
283 case TyTy::PARAM:
284 case TyTy::ARRAY:
285 case TyTy::SLICE:
286 case TyTy::FNDEF:
287 case TyTy::FNPTR:
288 case TyTy::TUPLE:
289 case TyTy::BOOL:
290 case TyTy::CHAR:
291 case TyTy::NEVER:
292 case TyTy::PLACEHOLDER:
293 case TyTy::PROJECTION:
294 case TyTy::DYNAMIC:
295 case TyTy::CLOSURE: {
296 bool is_valid = (ltype->get_infer_kind ()
297 == TyTy::InferType::InferTypeKind::GENERAL);
298 if (is_valid)
299 return rtype->clone ();
301 break;
303 case TyTy::ERROR:
304 return new TyTy::ErrorType (0);
307 return new TyTy::ErrorType (0);
310 TyTy::BaseType *
311 UnifyRules::expect_adt (TyTy::ADTType *ltype, TyTy::BaseType *rtype)
313 switch (rtype->get_kind ())
315 case TyTy::INFER: {
316 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
317 bool is_valid
318 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
319 if (is_valid)
320 return ltype->clone ();
322 break;
324 case TyTy::ADT: {
325 TyTy::ADTType &type = *static_cast<TyTy::ADTType *> (rtype);
326 if (ltype->get_adt_kind () != type.get_adt_kind ())
328 return new TyTy::ErrorType (0);
331 if (ltype->get_identifier ().compare (type.get_identifier ()) != 0)
333 return new TyTy::ErrorType (0);
336 if (ltype->number_of_variants () != type.number_of_variants ())
338 return new TyTy::ErrorType (0);
341 for (size_t i = 0; i < type.number_of_variants (); ++i)
343 TyTy::VariantDef *a = ltype->get_variants ().at (i);
344 TyTy::VariantDef *b = type.get_variants ().at (i);
346 if (a->num_fields () != b->num_fields ())
348 return new TyTy::ErrorType (0);
351 for (size_t j = 0; j < a->num_fields (); j++)
353 TyTy::StructFieldType *base_field = a->get_field_at_index (j);
354 TyTy::StructFieldType *other_field = b->get_field_at_index (j);
356 TyTy::BaseType *this_field_ty = base_field->get_field_type ();
357 TyTy::BaseType *other_field_ty = other_field->get_field_type ();
359 TyTy::BaseType *unified_ty
360 = UnifyRules::Resolve (TyTy::TyWithLocation (this_field_ty),
361 TyTy::TyWithLocation (other_field_ty),
362 locus, commit_flag,
363 false /* emit_error */);
364 if (unified_ty->get_kind () == TyTy::TypeKind::ERROR)
366 return new TyTy::ErrorType (0);
371 // generic args for the unit-struct case
372 if (type.is_unit () && ltype->is_unit ())
374 rust_assert (type.get_num_substitutions ()
375 == ltype->get_num_substitutions ());
377 for (size_t i = 0; i < type.get_num_substitutions (); i++)
379 auto &a = ltype->get_substs ().at (i);
380 auto &b = type.get_substs ().at (i);
382 auto pa = a.get_param_ty ();
383 auto pb = b.get_param_ty ();
385 auto res
386 = UnifyRules::Resolve (TyTy::TyWithLocation (pa),
387 TyTy::TyWithLocation (pb), locus,
388 commit_flag, false /* emit_error */);
389 if (res->get_kind () == TyTy::TypeKind::ERROR)
391 return new TyTy::ErrorType (0);
396 return type.clone ();
398 break;
400 case TyTy::STR:
401 case TyTy::REF:
402 case TyTy::POINTER:
403 case TyTy::PARAM:
404 case TyTy::ARRAY:
405 case TyTy::SLICE:
406 case TyTy::FNDEF:
407 case TyTy::FNPTR:
408 case TyTy::TUPLE:
409 case TyTy::BOOL:
410 case TyTy::CHAR:
411 case TyTy::INT:
412 case TyTy::UINT:
413 case TyTy::FLOAT:
414 case TyTy::USIZE:
415 case TyTy::ISIZE:
416 case TyTy::NEVER:
417 case TyTy::PLACEHOLDER:
418 case TyTy::PROJECTION:
419 case TyTy::DYNAMIC:
420 case TyTy::CLOSURE:
421 case TyTy::ERROR:
422 return new TyTy::ErrorType (0);
424 return new TyTy::ErrorType (0);
427 TyTy::BaseType *
428 UnifyRules::expect_str (TyTy::StrType *ltype, TyTy::BaseType *rtype)
430 switch (rtype->get_kind ())
432 case TyTy::INFER: {
433 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
434 bool is_valid
435 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
436 if (is_valid)
437 return ltype->clone ();
439 break;
441 case TyTy::STR:
442 return rtype->clone ();
444 case TyTy::ADT:
445 case TyTy::REF:
446 case TyTy::POINTER:
447 case TyTy::PARAM:
448 case TyTy::ARRAY:
449 case TyTy::SLICE:
450 case TyTy::FNDEF:
451 case TyTy::FNPTR:
452 case TyTy::TUPLE:
453 case TyTy::BOOL:
454 case TyTy::CHAR:
455 case TyTy::INT:
456 case TyTy::UINT:
457 case TyTy::FLOAT:
458 case TyTy::USIZE:
459 case TyTy::ISIZE:
460 case TyTy::NEVER:
461 case TyTy::PLACEHOLDER:
462 case TyTy::PROJECTION:
463 case TyTy::DYNAMIC:
464 case TyTy::CLOSURE:
465 case TyTy::ERROR:
466 return new TyTy::ErrorType (0);
468 return new TyTy::ErrorType (0);
471 TyTy::BaseType *
472 UnifyRules::expect_reference (TyTy::ReferenceType *ltype, TyTy::BaseType *rtype)
474 switch (rtype->get_kind ())
476 case TyTy::INFER: {
477 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
478 bool is_valid
479 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
480 if (is_valid)
481 return ltype->clone ();
483 break;
485 case TyTy::REF: {
486 TyTy::ReferenceType &type = *static_cast<TyTy::ReferenceType *> (rtype);
487 auto base_type = ltype->get_base ();
488 auto other_base_type = type.get_base ();
490 TyTy::BaseType *base_resolved
491 = UnifyRules::Resolve (TyTy::TyWithLocation (base_type),
492 TyTy::TyWithLocation (other_base_type), locus,
493 commit_flag, false /* emit_error */);
494 if (base_resolved->get_kind () == TyTy::TypeKind::ERROR)
496 return new TyTy::ErrorType (0);
499 // rust is permissive about mutablity here you can always go from
500 // mutable to immutable but not the otherway round
501 bool mutability_ok = ltype->is_mutable () ? type.is_mutable () : true;
502 if (!mutability_ok)
504 return new TyTy::ErrorType (0);
507 return new TyTy::ReferenceType (ltype->get_ref (), ltype->get_ty_ref (),
508 TyTy::TyVar (base_resolved->get_ref ()),
509 ltype->mutability ());
511 break;
513 case TyTy::STR:
514 case TyTy::ADT:
515 case TyTy::POINTER:
516 case TyTy::PARAM:
517 case TyTy::ARRAY:
518 case TyTy::SLICE:
519 case TyTy::FNDEF:
520 case TyTy::FNPTR:
521 case TyTy::TUPLE:
522 case TyTy::BOOL:
523 case TyTy::CHAR:
524 case TyTy::INT:
525 case TyTy::UINT:
526 case TyTy::FLOAT:
527 case TyTy::USIZE:
528 case TyTy::ISIZE:
529 case TyTy::NEVER:
530 case TyTy::PLACEHOLDER:
531 case TyTy::PROJECTION:
532 case TyTy::DYNAMIC:
533 case TyTy::CLOSURE:
534 case TyTy::ERROR:
535 return new TyTy::ErrorType (0);
537 return new TyTy::ErrorType (0);
540 TyTy::BaseType *
541 UnifyRules::expect_pointer (TyTy::PointerType *ltype, TyTy::BaseType *rtype)
543 switch (rtype->get_kind ())
545 case TyTy::INFER: {
546 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
547 bool is_valid
548 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
549 if (is_valid)
550 return ltype->clone ();
552 break;
554 case TyTy::POINTER: {
555 TyTy::PointerType &type = *static_cast<TyTy::PointerType *> (rtype);
556 auto base_type = ltype->get_base ();
557 auto other_base_type = type.get_base ();
559 TyTy::BaseType *base_resolved
560 = UnifyRules::Resolve (TyTy::TyWithLocation (base_type),
561 TyTy::TyWithLocation (other_base_type), locus,
562 commit_flag, false /* emit_error */);
563 if (base_resolved->get_kind () == TyTy::TypeKind::ERROR)
565 return new TyTy::ErrorType (0);
568 // rust is permissive about mutablity here you can always go from
569 // mutable to immutable but not the otherway round
570 bool mutability_ok = ltype->is_mutable () ? type.is_mutable () : true;
571 if (!mutability_ok)
573 return new TyTy::ErrorType (0);
576 return new TyTy::PointerType (ltype->get_ref (), ltype->get_ty_ref (),
577 TyTy::TyVar (base_resolved->get_ref ()),
578 ltype->mutability ());
580 break;
582 case TyTy::STR:
583 case TyTy::ADT:
584 case TyTy::REF:
585 case TyTy::PARAM:
586 case TyTy::ARRAY:
587 case TyTy::SLICE:
588 case TyTy::FNDEF:
589 case TyTy::FNPTR:
590 case TyTy::TUPLE:
591 case TyTy::BOOL:
592 case TyTy::CHAR:
593 case TyTy::INT:
594 case TyTy::UINT:
595 case TyTy::FLOAT:
596 case TyTy::USIZE:
597 case TyTy::ISIZE:
598 case TyTy::NEVER:
599 case TyTy::PLACEHOLDER:
600 case TyTy::PROJECTION:
601 case TyTy::DYNAMIC:
602 case TyTy::CLOSURE:
603 case TyTy::ERROR:
604 return new TyTy::ErrorType (0);
606 return new TyTy::ErrorType (0);
609 TyTy::BaseType *
610 UnifyRules::expect_param (TyTy::ParamType *ltype, TyTy::BaseType *rtype)
612 switch (rtype->get_kind ())
614 case TyTy::INFER: {
615 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
616 bool is_valid
617 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
618 if (is_valid)
619 return ltype->clone ();
621 break;
623 case TyTy::PARAM: {
624 TyTy::ParamType &type = *static_cast<TyTy::ParamType *> (rtype);
625 // bool symbol_matches
626 // = ltype->get_symbol ().compare (type.get_symbol ()) == 0;
627 // // TODO
628 // // I think rustc checks a debruinj index
629 // if (symbol_matches)
630 // {
631 // return type.clone ();
632 // }
634 // matching symbol is not going to work when we mix symbol's and have
635 // nested generics
637 // bounds match? FIXME
639 return type.clone ();
641 break;
643 case TyTy::POINTER:
644 case TyTy::STR:
645 case TyTy::ADT:
646 case TyTy::REF:
647 case TyTy::ARRAY:
648 case TyTy::SLICE:
649 case TyTy::FNDEF:
650 case TyTy::FNPTR:
651 case TyTy::TUPLE:
652 case TyTy::BOOL:
653 case TyTy::CHAR:
654 case TyTy::INT:
655 case TyTy::UINT:
656 case TyTy::FLOAT:
657 case TyTy::USIZE:
658 case TyTy::ISIZE:
659 case TyTy::NEVER:
660 case TyTy::PLACEHOLDER:
661 case TyTy::PROJECTION:
662 case TyTy::DYNAMIC:
663 case TyTy::CLOSURE:
664 case TyTy::ERROR:
665 return new TyTy::ErrorType (0);
667 return new TyTy::ErrorType (0);
670 TyTy::BaseType *
671 UnifyRules::expect_array (TyTy::ArrayType *ltype, TyTy::BaseType *rtype)
673 switch (rtype->get_kind ())
675 case TyTy::INFER: {
676 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
677 bool is_valid
678 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
679 if (is_valid)
680 return ltype->clone ();
682 break;
684 case TyTy::ARRAY: {
685 TyTy::ArrayType &type = *static_cast<TyTy::ArrayType *> (rtype);
686 TyTy::BaseType *element_unify = UnifyRules::Resolve (
687 TyTy::TyWithLocation (ltype->get_element_type ()),
688 TyTy::TyWithLocation (type.get_element_type ()), locus, commit_flag,
689 false /* emit_error*/);
691 if (element_unify->get_kind () != TyTy::TypeKind::ERROR)
693 return new TyTy::ArrayType (type.get_ref (), type.get_ty_ref (),
694 type.get_ident ().locus,
695 type.get_capacity_expr (),
696 TyTy::TyVar (
697 element_unify->get_ref ()));
700 break;
702 case TyTy::PARAM:
703 case TyTy::POINTER:
704 case TyTy::STR:
705 case TyTy::ADT:
706 case TyTy::REF:
707 case TyTy::SLICE:
708 case TyTy::FNDEF:
709 case TyTy::FNPTR:
710 case TyTy::TUPLE:
711 case TyTy::BOOL:
712 case TyTy::CHAR:
713 case TyTy::INT:
714 case TyTy::UINT:
715 case TyTy::FLOAT:
716 case TyTy::USIZE:
717 case TyTy::ISIZE:
718 case TyTy::NEVER:
719 case TyTy::PLACEHOLDER:
720 case TyTy::PROJECTION:
721 case TyTy::DYNAMIC:
722 case TyTy::CLOSURE:
723 case TyTy::ERROR:
724 return new TyTy::ErrorType (0);
726 return new TyTy::ErrorType (0);
729 TyTy::BaseType *
730 UnifyRules::expect_slice (TyTy::SliceType *ltype, TyTy::BaseType *rtype)
732 switch (rtype->get_kind ())
734 case TyTy::INFER: {
735 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
736 bool is_valid
737 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
738 if (is_valid)
739 return ltype->clone ();
741 break;
743 case TyTy::SLICE: {
744 TyTy::SliceType &type = *static_cast<TyTy::SliceType *> (rtype);
745 TyTy::BaseType *element_unify = UnifyRules::Resolve (
746 TyTy::TyWithLocation (ltype->get_element_type ()),
747 TyTy::TyWithLocation (type.get_element_type ()), locus, commit_flag,
748 false /* emit_error*/);
750 if (element_unify->get_kind () != TyTy::TypeKind::ERROR)
752 return new TyTy::SliceType (type.get_ref (), type.get_ty_ref (),
753 type.get_ident ().locus,
754 TyTy::TyVar (
755 element_unify->get_ref ()));
758 break;
760 case TyTy::PARAM:
761 case TyTy::POINTER:
762 case TyTy::STR:
763 case TyTy::ADT:
764 case TyTy::REF:
765 case TyTy::ARRAY:
766 case TyTy::FNDEF:
767 case TyTy::FNPTR:
768 case TyTy::TUPLE:
769 case TyTy::BOOL:
770 case TyTy::CHAR:
771 case TyTy::INT:
772 case TyTy::UINT:
773 case TyTy::FLOAT:
774 case TyTy::USIZE:
775 case TyTy::ISIZE:
776 case TyTy::NEVER:
777 case TyTy::PLACEHOLDER:
778 case TyTy::PROJECTION:
779 case TyTy::DYNAMIC:
780 case TyTy::CLOSURE:
781 case TyTy::ERROR:
782 return new TyTy::ErrorType (0);
784 return new TyTy::ErrorType (0);
787 TyTy::BaseType *
788 UnifyRules::expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype)
790 switch (rtype->get_kind ())
792 case TyTy::INFER: {
793 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
794 bool is_valid
795 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
796 if (is_valid)
797 return ltype->clone ();
799 break;
801 case TyTy::FNDEF: {
802 TyTy::FnType &type = *static_cast<TyTy::FnType *> (rtype);
803 if (ltype->num_params () != type.num_params ())
805 return new TyTy::ErrorType (0);
808 for (size_t i = 0; i < ltype->num_params (); i++)
810 auto a = ltype->param_at (i).second;
811 auto b = type.param_at (i).second;
813 auto unified_param
814 = UnifyRules::Resolve (TyTy::TyWithLocation (a),
815 TyTy::TyWithLocation (b), locus,
816 commit_flag, false /* emit_errors */);
817 if (unified_param->get_kind () == TyTy::TypeKind::ERROR)
819 return new TyTy::ErrorType (0);
823 auto unified_return
824 = UnifyRules::Resolve (TyTy::TyWithLocation (
825 ltype->get_return_type ()),
826 TyTy::TyWithLocation (type.get_return_type ()),
827 locus, commit_flag, false /* emit_errors */);
828 if (unified_return->get_kind () == TyTy::TypeKind::ERROR)
830 return new TyTy::ErrorType (0);
833 return ltype->clone ();
835 break;
837 case TyTy::TUPLE:
838 case TyTy::BOOL:
839 case TyTy::CHAR:
840 case TyTy::INT:
841 case TyTy::FLOAT:
842 case TyTy::ISIZE:
843 case TyTy::ADT:
844 case TyTy::STR:
845 case TyTy::REF:
846 case TyTy::POINTER:
847 case TyTy::PARAM:
848 case TyTy::ARRAY:
849 case TyTy::SLICE:
850 case TyTy::FNPTR:
851 case TyTy::UINT:
852 case TyTy::USIZE:
853 case TyTy::NEVER:
854 case TyTy::PLACEHOLDER:
855 case TyTy::PROJECTION:
856 case TyTy::DYNAMIC:
857 case TyTy::CLOSURE:
858 case TyTy::ERROR:
859 return new TyTy::ErrorType (0);
861 return new TyTy::ErrorType (0);
864 TyTy::BaseType *
865 UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype)
867 switch (rtype->get_kind ())
869 case TyTy::INFER: {
870 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
871 bool is_valid
872 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
873 if (is_valid)
874 return ltype->clone ();
876 break;
878 case TyTy::FNPTR: {
879 TyTy::FnPtr &type = *static_cast<TyTy::FnPtr *> (rtype);
880 if (ltype->num_params () != type.num_params ())
882 return new TyTy::ErrorType (0);
885 for (size_t i = 0; i < ltype->num_params (); i++)
887 auto a = ltype->param_at (i);
888 auto b = type.param_at (i);
890 auto unified_param
891 = UnifyRules::Resolve (TyTy::TyWithLocation (a),
892 TyTy::TyWithLocation (b), locus,
893 commit_flag, false /* emit_errors */);
894 if (unified_param->get_kind () == TyTy::TypeKind::ERROR)
896 return new TyTy::ErrorType (0);
900 auto unified_return
901 = UnifyRules::Resolve (TyTy::TyWithLocation (
902 ltype->get_return_type ()),
903 TyTy::TyWithLocation (type.get_return_type ()),
904 locus, commit_flag, false /* emit_errors */);
905 if (unified_return->get_kind () == TyTy::TypeKind::ERROR)
907 return new TyTy::ErrorType (0);
910 return ltype->clone ();
912 break;
914 case TyTy::FNDEF: {
915 TyTy::FnType &type = *static_cast<TyTy::FnType *> (rtype);
916 auto this_ret_type = ltype->get_return_type ();
917 auto other_ret_type = type.get_return_type ();
919 auto unified_result
920 = UnifyRules::Resolve (TyTy::TyWithLocation (this_ret_type),
921 TyTy::TyWithLocation (other_ret_type), locus,
922 commit_flag, false /*emit_errors*/);
923 if (unified_result->get_kind () == TyTy::TypeKind::ERROR)
925 return new TyTy::ErrorType (0);
928 if (ltype->num_params () != type.num_params ())
930 return new TyTy::ErrorType (0);
933 for (size_t i = 0; i < ltype->num_params (); i++)
935 auto this_param = ltype->param_at (i);
936 auto other_param = type.param_at (i).second;
938 auto unified_param
939 = UnifyRules::Resolve (TyTy::TyWithLocation (this_param),
940 TyTy::TyWithLocation (other_param), locus,
941 commit_flag, false /* emit_errors */);
942 if (unified_param->get_kind () == TyTy::TypeKind::ERROR)
944 return new TyTy::ErrorType (0);
948 return ltype->clone ();
950 break;
952 case TyTy::TUPLE:
953 case TyTy::BOOL:
954 case TyTy::CHAR:
955 case TyTy::INT:
956 case TyTy::FLOAT:
957 case TyTy::ISIZE:
958 case TyTy::ADT:
959 case TyTy::STR:
960 case TyTy::REF:
961 case TyTy::POINTER:
962 case TyTy::PARAM:
963 case TyTy::ARRAY:
964 case TyTy::SLICE:
965 case TyTy::UINT:
966 case TyTy::USIZE:
967 case TyTy::NEVER:
968 case TyTy::PLACEHOLDER:
969 case TyTy::PROJECTION:
970 case TyTy::DYNAMIC:
971 case TyTy::CLOSURE:
972 case TyTy::ERROR:
973 return new TyTy::ErrorType (0);
975 return new TyTy::ErrorType (0);
978 TyTy::BaseType *
979 UnifyRules::expect_tuple (TyTy::TupleType *ltype, TyTy::BaseType *rtype)
981 switch (rtype->get_kind ())
983 case TyTy::INFER: {
984 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
985 bool is_valid
986 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
987 if (is_valid)
988 return ltype->clone ();
990 break;
992 case TyTy::TUPLE: {
993 TyTy::TupleType &type = *static_cast<TyTy::TupleType *> (rtype);
994 if (ltype->num_fields () != type.num_fields ())
996 return new TyTy::ErrorType (0);
999 std::vector<TyTy::TyVar> fields;
1000 for (size_t i = 0; i < ltype->num_fields (); i++)
1002 TyTy::BaseType *bo = ltype->get_field (i);
1003 TyTy::BaseType *fo = type.get_field (i);
1005 TyTy::BaseType *unified_ty
1006 = UnifyRules::Resolve (TyTy::TyWithLocation (bo),
1007 TyTy::TyWithLocation (fo), locus,
1008 commit_flag, false /* emit_errors */);
1009 if (unified_ty->get_kind () == TyTy::TypeKind::ERROR)
1010 return new TyTy::ErrorType (0);
1012 fields.push_back (TyTy::TyVar (unified_ty->get_ref ()));
1015 return new TyTy::TupleType (type.get_ref (), type.get_ty_ref (),
1016 type.get_ident ().locus, fields);
1018 break;
1020 case TyTy::BOOL:
1021 case TyTy::CHAR:
1022 case TyTy::INT:
1023 case TyTy::FLOAT:
1024 case TyTy::ISIZE:
1025 case TyTy::ADT:
1026 case TyTy::STR:
1027 case TyTy::REF:
1028 case TyTy::POINTER:
1029 case TyTy::PARAM:
1030 case TyTy::ARRAY:
1031 case TyTy::SLICE:
1032 case TyTy::FNDEF:
1033 case TyTy::FNPTR:
1034 case TyTy::UINT:
1035 case TyTy::USIZE:
1036 case TyTy::NEVER:
1037 case TyTy::PLACEHOLDER:
1038 case TyTy::PROJECTION:
1039 case TyTy::DYNAMIC:
1040 case TyTy::CLOSURE:
1041 case TyTy::ERROR:
1042 return new TyTy::ErrorType (0);
1044 return new TyTy::ErrorType (0);
1047 TyTy::BaseType *
1048 UnifyRules::expect_bool (TyTy::BoolType *ltype, TyTy::BaseType *rtype)
1050 switch (rtype->get_kind ())
1052 case TyTy::INFER: {
1053 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1054 bool is_valid
1055 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1056 if (is_valid)
1057 return ltype->clone ();
1059 break;
1061 case TyTy::BOOL:
1062 return rtype->clone ();
1064 case TyTy::CHAR:
1065 case TyTy::INT:
1066 case TyTy::FLOAT:
1067 case TyTy::ISIZE:
1068 case TyTy::ADT:
1069 case TyTy::STR:
1070 case TyTy::REF:
1071 case TyTy::POINTER:
1072 case TyTy::PARAM:
1073 case TyTy::ARRAY:
1074 case TyTy::SLICE:
1075 case TyTy::FNDEF:
1076 case TyTy::FNPTR:
1077 case TyTy::TUPLE:
1078 case TyTy::UINT:
1079 case TyTy::USIZE:
1080 case TyTy::NEVER:
1081 case TyTy::PLACEHOLDER:
1082 case TyTy::PROJECTION:
1083 case TyTy::DYNAMIC:
1084 case TyTy::CLOSURE:
1085 case TyTy::ERROR:
1086 return new TyTy::ErrorType (0);
1088 return new TyTy::ErrorType (0);
1091 TyTy::BaseType *
1092 UnifyRules::expect_char (TyTy::CharType *ltype, TyTy::BaseType *rtype)
1094 switch (rtype->get_kind ())
1096 case TyTy::INFER: {
1097 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1098 bool is_valid
1099 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1100 if (is_valid)
1101 return ltype->clone ();
1103 break;
1105 case TyTy::CHAR:
1106 return rtype->clone ();
1108 case TyTy::INT:
1109 case TyTy::FLOAT:
1110 case TyTy::ISIZE:
1111 case TyTy::ADT:
1112 case TyTy::STR:
1113 case TyTy::REF:
1114 case TyTy::POINTER:
1115 case TyTy::PARAM:
1116 case TyTy::ARRAY:
1117 case TyTy::SLICE:
1118 case TyTy::FNDEF:
1119 case TyTy::FNPTR:
1120 case TyTy::TUPLE:
1121 case TyTy::BOOL:
1122 case TyTy::UINT:
1123 case TyTy::USIZE:
1124 case TyTy::NEVER:
1125 case TyTy::PLACEHOLDER:
1126 case TyTy::PROJECTION:
1127 case TyTy::DYNAMIC:
1128 case TyTy::CLOSURE:
1129 case TyTy::ERROR:
1130 return new TyTy::ErrorType (0);
1132 return new TyTy::ErrorType (0);
1135 TyTy::BaseType *
1136 UnifyRules::expect_int (TyTy::IntType *ltype, TyTy::BaseType *rtype)
1138 switch (rtype->get_kind ())
1140 case TyTy::INFER: {
1141 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1142 bool is_valid
1143 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1144 || r->get_infer_kind () == TyTy::InferType::InferTypeKind::INTEGRAL;
1145 if (is_valid)
1146 return ltype->clone ();
1148 break;
1150 case TyTy::INT: {
1151 TyTy::IntType &type = *static_cast<TyTy::IntType *> (rtype);
1152 bool is_valid = ltype->get_int_kind () == type.get_int_kind ();
1153 if (is_valid)
1154 return new TyTy::IntType (type.get_ref (), type.get_ty_ref (),
1155 type.get_int_kind ());
1157 break;
1159 case TyTy::FLOAT:
1160 case TyTy::ISIZE:
1161 case TyTy::ADT:
1162 case TyTy::STR:
1163 case TyTy::REF:
1164 case TyTy::POINTER:
1165 case TyTy::PARAM:
1166 case TyTy::ARRAY:
1167 case TyTy::SLICE:
1168 case TyTy::FNDEF:
1169 case TyTy::FNPTR:
1170 case TyTy::TUPLE:
1171 case TyTy::BOOL:
1172 case TyTy::CHAR:
1173 case TyTy::UINT:
1174 case TyTy::USIZE:
1175 case TyTy::NEVER:
1176 case TyTy::PLACEHOLDER:
1177 case TyTy::PROJECTION:
1178 case TyTy::DYNAMIC:
1179 case TyTy::CLOSURE:
1180 case TyTy::ERROR:
1181 return new TyTy::ErrorType (0);
1183 return new TyTy::ErrorType (0);
1186 TyTy::BaseType *
1187 UnifyRules::expect_uint (TyTy::UintType *ltype, TyTy::BaseType *rtype)
1189 switch (rtype->get_kind ())
1191 case TyTy::INFER: {
1192 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1193 bool is_valid
1194 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1195 || r->get_infer_kind () == TyTy::InferType::InferTypeKind::INTEGRAL;
1196 if (is_valid)
1197 return ltype->clone ();
1199 break;
1201 case TyTy::UINT: {
1202 TyTy::UintType &type = *static_cast<TyTy::UintType *> (rtype);
1203 bool is_valid = ltype->get_uint_kind () == type.get_uint_kind ();
1204 if (is_valid)
1205 return new TyTy::UintType (type.get_ref (), type.get_ty_ref (),
1206 type.get_uint_kind ());
1208 break;
1210 case TyTy::FLOAT:
1211 case TyTy::ISIZE:
1212 case TyTy::ADT:
1213 case TyTy::STR:
1214 case TyTy::REF:
1215 case TyTy::POINTER:
1216 case TyTy::PARAM:
1217 case TyTy::ARRAY:
1218 case TyTy::SLICE:
1219 case TyTy::FNDEF:
1220 case TyTy::FNPTR:
1221 case TyTy::TUPLE:
1222 case TyTy::BOOL:
1223 case TyTy::CHAR:
1224 case TyTy::INT:
1225 case TyTy::USIZE:
1226 case TyTy::NEVER:
1227 case TyTy::PLACEHOLDER:
1228 case TyTy::PROJECTION:
1229 case TyTy::DYNAMIC:
1230 case TyTy::CLOSURE:
1231 case TyTy::ERROR:
1232 return new TyTy::ErrorType (0);
1234 return new TyTy::ErrorType (0);
1237 TyTy::BaseType *
1238 UnifyRules::expect_float (TyTy::FloatType *ltype, TyTy::BaseType *rtype)
1240 switch (rtype->get_kind ())
1242 case TyTy::INFER: {
1243 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1244 bool is_valid
1245 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1246 || r->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT;
1247 if (is_valid)
1248 return ltype->clone ();
1250 break;
1252 case TyTy::FLOAT: {
1253 TyTy::FloatType &type = *static_cast<TyTy::FloatType *> (rtype);
1254 bool is_valid = ltype->get_float_kind () == type.get_float_kind ();
1255 if (is_valid)
1256 return new TyTy::FloatType (type.get_ref (), type.get_ty_ref (),
1257 type.get_float_kind ());
1259 break;
1261 case TyTy::ISIZE:
1262 case TyTy::ADT:
1263 case TyTy::STR:
1264 case TyTy::REF:
1265 case TyTy::POINTER:
1266 case TyTy::PARAM:
1267 case TyTy::ARRAY:
1268 case TyTy::SLICE:
1269 case TyTy::FNDEF:
1270 case TyTy::FNPTR:
1271 case TyTy::TUPLE:
1272 case TyTy::BOOL:
1273 case TyTy::CHAR:
1274 case TyTy::INT:
1275 case TyTy::UINT:
1276 case TyTy::USIZE:
1277 case TyTy::NEVER:
1278 case TyTy::PLACEHOLDER:
1279 case TyTy::PROJECTION:
1280 case TyTy::DYNAMIC:
1281 case TyTy::CLOSURE:
1282 case TyTy::ERROR:
1283 return new TyTy::ErrorType (0);
1285 return new TyTy::ErrorType (0);
1288 TyTy::BaseType *
1289 UnifyRules::expect_isize (TyTy::ISizeType *ltype, TyTy::BaseType *rtype)
1291 switch (rtype->get_kind ())
1293 case TyTy::INFER: {
1294 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1295 bool is_valid
1296 = r->get_infer_kind () != TyTy::InferType::InferTypeKind::FLOAT;
1297 if (is_valid)
1298 return ltype->clone ();
1300 break;
1302 case TyTy::ISIZE:
1303 return rtype->clone ();
1305 case TyTy::ADT:
1306 case TyTy::STR:
1307 case TyTy::REF:
1308 case TyTy::POINTER:
1309 case TyTy::PARAM:
1310 case TyTy::ARRAY:
1311 case TyTy::SLICE:
1312 case TyTy::FNDEF:
1313 case TyTy::FNPTR:
1314 case TyTy::TUPLE:
1315 case TyTy::BOOL:
1316 case TyTy::CHAR:
1317 case TyTy::INT:
1318 case TyTy::UINT:
1319 case TyTy::FLOAT:
1320 case TyTy::USIZE:
1321 case TyTy::NEVER:
1322 case TyTy::PLACEHOLDER:
1323 case TyTy::PROJECTION:
1324 case TyTy::DYNAMIC:
1325 case TyTy::CLOSURE:
1326 case TyTy::ERROR:
1327 return new TyTy::ErrorType (0);
1329 return new TyTy::ErrorType (0);
1332 TyTy::BaseType *
1333 UnifyRules::expect_usize (TyTy::USizeType *ltype, TyTy::BaseType *rtype)
1335 switch (rtype->get_kind ())
1337 case TyTy::INFER: {
1338 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1339 bool is_valid
1340 = r->get_infer_kind () != TyTy::InferType::InferTypeKind::FLOAT;
1341 if (is_valid)
1342 return ltype->clone ();
1344 break;
1346 case TyTy::USIZE:
1347 return rtype->clone ();
1349 case TyTy::ADT:
1350 case TyTy::STR:
1351 case TyTy::REF:
1352 case TyTy::POINTER:
1353 case TyTy::PARAM:
1354 case TyTy::ARRAY:
1355 case TyTy::SLICE:
1356 case TyTy::FNDEF:
1357 case TyTy::FNPTR:
1358 case TyTy::TUPLE:
1359 case TyTy::BOOL:
1360 case TyTy::CHAR:
1361 case TyTy::INT:
1362 case TyTy::UINT:
1363 case TyTy::FLOAT:
1364 case TyTy::ISIZE:
1365 case TyTy::NEVER:
1366 case TyTy::PLACEHOLDER:
1367 case TyTy::PROJECTION:
1368 case TyTy::DYNAMIC:
1369 case TyTy::CLOSURE:
1370 case TyTy::ERROR:
1371 return new TyTy::ErrorType (0);
1373 return new TyTy::ErrorType (0);
1376 TyTy::BaseType *
1377 UnifyRules::expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype)
1379 switch (rtype->get_kind ())
1381 case TyTy::INFER: {
1382 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1383 bool is_valid
1384 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1385 if (is_valid)
1386 return ltype->clone ();
1388 break;
1390 case TyTy::NEVER:
1391 return rtype->clone ();
1393 case TyTy::PLACEHOLDER:
1394 case TyTy::PROJECTION:
1395 case TyTy::DYNAMIC:
1396 case TyTy::CLOSURE:
1397 case TyTy::SLICE:
1398 case TyTy::PARAM:
1399 case TyTy::POINTER:
1400 case TyTy::STR:
1401 case TyTy::ADT:
1402 case TyTy::REF:
1403 case TyTy::ARRAY:
1404 case TyTy::FNDEF:
1405 case TyTy::FNPTR:
1406 case TyTy::TUPLE:
1407 case TyTy::BOOL:
1408 case TyTy::CHAR:
1409 case TyTy::INT:
1410 case TyTy::UINT:
1411 case TyTy::FLOAT:
1412 case TyTy::USIZE:
1413 case TyTy::ISIZE:
1414 case TyTy::ERROR:
1415 return new TyTy::ErrorType (0);
1417 return new TyTy::ErrorType (0);
1420 TyTy::BaseType *
1421 UnifyRules::expect_placeholder (TyTy::PlaceholderType *ltype,
1422 TyTy::BaseType *rtype)
1424 switch (rtype->get_kind ())
1426 case TyTy::INFER: {
1427 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1428 bool is_valid
1429 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1430 if (is_valid)
1431 return ltype->clone ();
1433 break;
1435 case TyTy::PLACEHOLDER: {
1436 TyTy::PlaceholderType &type
1437 = *static_cast<TyTy::PlaceholderType *> (rtype);
1438 bool symbol_match
1439 = ltype->get_symbol ().compare (type.get_symbol ()) == 0;
1440 if (symbol_match)
1442 return type.clone ();
1445 break;
1447 case TyTy::PROJECTION:
1448 case TyTy::DYNAMIC:
1449 case TyTy::CLOSURE:
1450 case TyTy::SLICE:
1451 case TyTy::PARAM:
1452 case TyTy::POINTER:
1453 case TyTy::STR:
1454 case TyTy::ADT:
1455 case TyTy::REF:
1456 case TyTy::ARRAY:
1457 case TyTy::FNDEF:
1458 case TyTy::FNPTR:
1459 case TyTy::TUPLE:
1460 case TyTy::BOOL:
1461 case TyTy::CHAR:
1462 case TyTy::INT:
1463 case TyTy::UINT:
1464 case TyTy::FLOAT:
1465 case TyTy::USIZE:
1466 case TyTy::ISIZE:
1467 case TyTy::NEVER:
1468 case TyTy::ERROR:
1469 return new TyTy::ErrorType (0);
1471 return new TyTy::ErrorType (0);
1474 TyTy::BaseType *
1475 UnifyRules::expect_projection (TyTy::ProjectionType *ltype,
1476 TyTy::BaseType *rtype)
1478 switch (rtype->get_kind ())
1480 case TyTy::INFER: {
1481 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1482 bool is_valid
1483 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1484 if (is_valid)
1485 return ltype->clone ();
1487 break;
1489 // FIXME
1490 case TyTy::PROJECTION:
1491 gcc_unreachable ();
1492 break;
1494 case TyTy::DYNAMIC:
1495 case TyTy::CLOSURE:
1496 case TyTy::SLICE:
1497 case TyTy::PARAM:
1498 case TyTy::POINTER:
1499 case TyTy::STR:
1500 case TyTy::ADT:
1501 case TyTy::REF:
1502 case TyTy::ARRAY:
1503 case TyTy::FNDEF:
1504 case TyTy::FNPTR:
1505 case TyTy::TUPLE:
1506 case TyTy::BOOL:
1507 case TyTy::CHAR:
1508 case TyTy::INT:
1509 case TyTy::UINT:
1510 case TyTy::FLOAT:
1511 case TyTy::USIZE:
1512 case TyTy::ISIZE:
1513 case TyTy::NEVER:
1514 case TyTy::PLACEHOLDER:
1515 case TyTy::ERROR:
1516 return new TyTy::ErrorType (0);
1518 return new TyTy::ErrorType (0);
1521 TyTy::BaseType *
1522 UnifyRules::expect_dyn (TyTy::DynamicObjectType *ltype, TyTy::BaseType *rtype)
1524 switch (rtype->get_kind ())
1526 case TyTy::INFER: {
1527 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1528 bool is_valid
1529 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1530 if (is_valid)
1531 return ltype->clone ();
1533 break;
1535 case TyTy::DYNAMIC: {
1536 TyTy::DynamicObjectType &type
1537 = *static_cast<TyTy::DynamicObjectType *> (rtype);
1538 if (ltype->num_specified_bounds () != type.num_specified_bounds ())
1540 return new TyTy::ErrorType (0);
1543 if (!ltype->bounds_compatible (type, locus, true))
1545 return new TyTy::ErrorType (0);
1548 return ltype->clone ();
1550 break;
1552 case TyTy::CLOSURE:
1553 case TyTy::SLICE:
1554 case TyTy::PARAM:
1555 case TyTy::POINTER:
1556 case TyTy::STR:
1557 case TyTy::ADT:
1558 case TyTy::REF:
1559 case TyTy::ARRAY:
1560 case TyTy::FNDEF:
1561 case TyTy::FNPTR:
1562 case TyTy::TUPLE:
1563 case TyTy::BOOL:
1564 case TyTy::CHAR:
1565 case TyTy::INT:
1566 case TyTy::UINT:
1567 case TyTy::FLOAT:
1568 case TyTy::USIZE:
1569 case TyTy::ISIZE:
1570 case TyTy::NEVER:
1571 case TyTy::PLACEHOLDER:
1572 case TyTy::PROJECTION:
1573 case TyTy::ERROR:
1574 return new TyTy::ErrorType (0);
1576 return new TyTy::ErrorType (0);
1579 TyTy::BaseType *
1580 UnifyRules::expect_closure (TyTy::ClosureType *ltype, TyTy::BaseType *rtype)
1582 switch (rtype->get_kind ())
1584 case TyTy::INFER: {
1585 TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
1586 bool is_valid
1587 = r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
1588 if (is_valid)
1589 return ltype->clone ();
1591 break;
1593 case TyTy::CLOSURE: {
1594 TyTy::ClosureType &type = *static_cast<TyTy::ClosureType *> (rtype);
1595 if (ltype->get_def_id () != type.get_def_id ())
1597 return new TyTy::ErrorType (0);
1600 TyTy::BaseType *args_res
1601 = UnifyRules::Resolve (TyTy::TyWithLocation (
1602 &ltype->get_parameters ()),
1603 TyTy::TyWithLocation (&type.get_parameters ()),
1604 locus, commit_flag, false /* emit_error */);
1605 if (args_res->get_kind () == TyTy::TypeKind::ERROR)
1607 return new TyTy::ErrorType (0);
1610 TyTy::BaseType *res = UnifyRules::Resolve (
1611 TyTy::TyWithLocation (&ltype->get_result_type ()),
1612 TyTy::TyWithLocation (&type.get_result_type ()), locus, commit_flag,
1613 false /* emit_error */);
1614 if (res == nullptr || res->get_kind () == TyTy::TypeKind::ERROR)
1616 return new TyTy::ErrorType (0);
1619 return ltype->clone ();
1621 break;
1623 case TyTy::SLICE:
1624 case TyTy::PARAM:
1625 case TyTy::POINTER:
1626 case TyTy::STR:
1627 case TyTy::ADT:
1628 case TyTy::REF:
1629 case TyTy::ARRAY:
1630 case TyTy::FNDEF:
1631 case TyTy::FNPTR:
1632 case TyTy::TUPLE:
1633 case TyTy::BOOL:
1634 case TyTy::CHAR:
1635 case TyTy::INT:
1636 case TyTy::UINT:
1637 case TyTy::FLOAT:
1638 case TyTy::USIZE:
1639 case TyTy::ISIZE:
1640 case TyTy::NEVER:
1641 case TyTy::PLACEHOLDER:
1642 case TyTy::PROJECTION:
1643 case TyTy::DYNAMIC:
1644 case TyTy::ERROR:
1645 return new TyTy::ErrorType (0);
1647 return new TyTy::ErrorType (0);
1650 } // namespace Resolver
1651 } // namespace Rust