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
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
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"
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 ())
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 ();
41 bool failed
= result
->get_kind () == TyTy::TypeKind::ERROR
;
42 if (failed
&& r
.emit_error
)
43 r
.emit_type_mismatch ();
49 UnifyRules::get_base ()
51 return lhs
.get_ty ()->destructure ();
55 UnifyRules::get_other ()
57 return rhs
.get_ty ()->destructure ();
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
);
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 (),
96 context
.insert_type (node
, resolved
->clone ());
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 ());
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 ());
128 if (ltype
->num_specified_bounds () > 0)
130 if (!ltype
->bounds_compatible (*rtype
, locus
, true))
132 // already emitted an error
134 return new TyTy::ErrorType (0);
138 switch (ltype
->get_kind ())
141 return expect_inference_variable (static_cast<TyTy::InferType
*> (ltype
),
145 return expect_adt (static_cast<TyTy::ADTType
*> (ltype
), rtype
);
148 return expect_str (static_cast<TyTy::StrType
*> (ltype
), rtype
);
151 return expect_reference (static_cast<TyTy::ReferenceType
*> (ltype
),
155 return expect_pointer (static_cast<TyTy::PointerType
*> (ltype
), rtype
);
158 return expect_param (static_cast<TyTy::ParamType
*> (ltype
), rtype
);
161 return expect_array (static_cast<TyTy::ArrayType
*> (ltype
), rtype
);
164 return expect_slice (static_cast<TyTy::SliceType
*> (ltype
), rtype
);
167 return expect_fndef (static_cast<TyTy::FnType
*> (ltype
), rtype
);
170 return expect_fnptr (static_cast<TyTy::FnPtr
*> (ltype
), rtype
);
173 return expect_tuple (static_cast<TyTy::TupleType
*> (ltype
), rtype
);
176 return expect_bool (static_cast<TyTy::BoolType
*> (ltype
), rtype
);
179 return expect_char (static_cast<TyTy::CharType
*> (ltype
), rtype
);
182 return expect_int (static_cast<TyTy::IntType
*> (ltype
), rtype
);
185 return expect_uint (static_cast<TyTy::UintType
*> (ltype
), rtype
);
188 return expect_float (static_cast<TyTy::FloatType
*> (ltype
), rtype
);
191 return expect_usize (static_cast<TyTy::USizeType
*> (ltype
), rtype
);
194 return expect_isize (static_cast<TyTy::ISizeType
*> (ltype
), rtype
);
197 return expect_never (static_cast<TyTy::NeverType
*> (ltype
), rtype
);
199 case TyTy::PLACEHOLDER
:
200 return expect_placeholder (static_cast<TyTy::PlaceholderType
*> (ltype
),
203 case TyTy::PROJECTION
:
204 return expect_projection (static_cast<TyTy::ProjectionType
*> (ltype
),
208 return expect_dyn (static_cast<TyTy::DynamicObjectType
*> (ltype
), rtype
);
211 return expect_closure (static_cast<TyTy::ClosureType
*> (ltype
), rtype
);
214 return new TyTy::ErrorType (0);
217 return new TyTy::ErrorType (0);
221 UnifyRules::expect_inference_variable (TyTy::InferType
*ltype
,
222 TyTy::BaseType
*rtype
)
224 switch (rtype
->get_kind ())
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
;
239 return rtype
->clone ();
243 case TyTy::InferType::InferTypeKind::FLOAT
: {
245 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT
246 || r
->get_infer_kind ()
247 == TyTy::InferType::InferTypeKind::GENERAL
;
249 return rtype
->clone ();
260 bool is_valid
= (ltype
->get_infer_kind ()
261 == TyTy::InferType::InferTypeKind::GENERAL
)
262 || (ltype
->get_infer_kind ()
263 == TyTy::InferType::InferTypeKind::INTEGRAL
);
265 return rtype
->clone ();
270 bool is_valid
= (ltype
->get_infer_kind ()
271 == TyTy::InferType::InferTypeKind::GENERAL
)
272 || (ltype
->get_infer_kind ()
273 == TyTy::InferType::InferTypeKind::FLOAT
);
275 return rtype
->clone ();
292 case TyTy::PLACEHOLDER
:
293 case TyTy::PROJECTION
:
295 case TyTy::CLOSURE
: {
296 bool is_valid
= (ltype
->get_infer_kind ()
297 == TyTy::InferType::InferTypeKind::GENERAL
);
299 return rtype
->clone ();
304 return new TyTy::ErrorType (0);
307 return new TyTy::ErrorType (0);
311 UnifyRules::expect_adt (TyTy::ADTType
*ltype
, TyTy::BaseType
*rtype
)
313 switch (rtype
->get_kind ())
316 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
318 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
320 return ltype
->clone ();
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
),
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 ();
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 ();
417 case TyTy::PLACEHOLDER
:
418 case TyTy::PROJECTION
:
422 return new TyTy::ErrorType (0);
424 return new TyTy::ErrorType (0);
428 UnifyRules::expect_str (TyTy::StrType
*ltype
, TyTy::BaseType
*rtype
)
430 switch (rtype
->get_kind ())
433 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
435 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
437 return ltype
->clone ();
442 return rtype
->clone ();
461 case TyTy::PLACEHOLDER
:
462 case TyTy::PROJECTION
:
466 return new TyTy::ErrorType (0);
468 return new TyTy::ErrorType (0);
472 UnifyRules::expect_reference (TyTy::ReferenceType
*ltype
, TyTy::BaseType
*rtype
)
474 switch (rtype
->get_kind ())
477 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
479 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
481 return ltype
->clone ();
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;
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 ());
530 case TyTy::PLACEHOLDER
:
531 case TyTy::PROJECTION
:
535 return new TyTy::ErrorType (0);
537 return new TyTy::ErrorType (0);
541 UnifyRules::expect_pointer (TyTy::PointerType
*ltype
, TyTy::BaseType
*rtype
)
543 switch (rtype
->get_kind ())
546 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
548 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
550 return ltype
->clone ();
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;
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 ());
599 case TyTy::PLACEHOLDER
:
600 case TyTy::PROJECTION
:
604 return new TyTy::ErrorType (0);
606 return new TyTy::ErrorType (0);
610 UnifyRules::expect_param (TyTy::ParamType
*ltype
, TyTy::BaseType
*rtype
)
612 switch (rtype
->get_kind ())
615 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
617 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
619 return ltype
->clone ();
624 TyTy::ParamType
&type
= *static_cast<TyTy::ParamType
*> (rtype
);
625 // bool symbol_matches
626 // = ltype->get_symbol ().compare (type.get_symbol ()) == 0;
628 // // I think rustc checks a debruinj index
629 // if (symbol_matches)
631 // return type.clone ();
634 // matching symbol is not going to work when we mix symbol's and have
637 // bounds match? FIXME
639 return type
.clone ();
660 case TyTy::PLACEHOLDER
:
661 case TyTy::PROJECTION
:
665 return new TyTy::ErrorType (0);
667 return new TyTy::ErrorType (0);
671 UnifyRules::expect_array (TyTy::ArrayType
*ltype
, TyTy::BaseType
*rtype
)
673 switch (rtype
->get_kind ())
676 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
678 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
680 return ltype
->clone ();
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 (),
697 element_unify
->get_ref ()));
719 case TyTy::PLACEHOLDER
:
720 case TyTy::PROJECTION
:
724 return new TyTy::ErrorType (0);
726 return new TyTy::ErrorType (0);
730 UnifyRules::expect_slice (TyTy::SliceType
*ltype
, TyTy::BaseType
*rtype
)
732 switch (rtype
->get_kind ())
735 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
737 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
739 return ltype
->clone ();
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
,
755 element_unify
->get_ref ()));
777 case TyTy::PLACEHOLDER
:
778 case TyTy::PROJECTION
:
782 return new TyTy::ErrorType (0);
784 return new TyTy::ErrorType (0);
788 UnifyRules::expect_fndef (TyTy::FnType
*ltype
, TyTy::BaseType
*rtype
)
790 switch (rtype
->get_kind ())
793 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
795 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
797 return ltype
->clone ();
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
;
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);
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 ();
854 case TyTy::PLACEHOLDER
:
855 case TyTy::PROJECTION
:
859 return new TyTy::ErrorType (0);
861 return new TyTy::ErrorType (0);
865 UnifyRules::expect_fnptr (TyTy::FnPtr
*ltype
, TyTy::BaseType
*rtype
)
867 switch (rtype
->get_kind ())
870 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
872 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
874 return ltype
->clone ();
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
);
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);
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 ();
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 ();
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
;
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 ();
968 case TyTy::PLACEHOLDER
:
969 case TyTy::PROJECTION
:
973 return new TyTy::ErrorType (0);
975 return new TyTy::ErrorType (0);
979 UnifyRules::expect_tuple (TyTy::TupleType
*ltype
, TyTy::BaseType
*rtype
)
981 switch (rtype
->get_kind ())
984 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
986 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
988 return ltype
->clone ();
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
);
1037 case TyTy::PLACEHOLDER
:
1038 case TyTy::PROJECTION
:
1042 return new TyTy::ErrorType (0);
1044 return new TyTy::ErrorType (0);
1048 UnifyRules::expect_bool (TyTy::BoolType
*ltype
, TyTy::BaseType
*rtype
)
1050 switch (rtype
->get_kind ())
1053 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1055 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1057 return ltype
->clone ();
1062 return rtype
->clone ();
1081 case TyTy::PLACEHOLDER
:
1082 case TyTy::PROJECTION
:
1086 return new TyTy::ErrorType (0);
1088 return new TyTy::ErrorType (0);
1092 UnifyRules::expect_char (TyTy::CharType
*ltype
, TyTy::BaseType
*rtype
)
1094 switch (rtype
->get_kind ())
1097 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1099 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1101 return ltype
->clone ();
1106 return rtype
->clone ();
1125 case TyTy::PLACEHOLDER
:
1126 case TyTy::PROJECTION
:
1130 return new TyTy::ErrorType (0);
1132 return new TyTy::ErrorType (0);
1136 UnifyRules::expect_int (TyTy::IntType
*ltype
, TyTy::BaseType
*rtype
)
1138 switch (rtype
->get_kind ())
1141 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1143 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1144 || r
->get_infer_kind () == TyTy::InferType::InferTypeKind::INTEGRAL
;
1146 return ltype
->clone ();
1151 TyTy::IntType
&type
= *static_cast<TyTy::IntType
*> (rtype
);
1152 bool is_valid
= ltype
->get_int_kind () == type
.get_int_kind ();
1154 return new TyTy::IntType (type
.get_ref (), type
.get_ty_ref (),
1155 type
.get_int_kind ());
1176 case TyTy::PLACEHOLDER
:
1177 case TyTy::PROJECTION
:
1181 return new TyTy::ErrorType (0);
1183 return new TyTy::ErrorType (0);
1187 UnifyRules::expect_uint (TyTy::UintType
*ltype
, TyTy::BaseType
*rtype
)
1189 switch (rtype
->get_kind ())
1192 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1194 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1195 || r
->get_infer_kind () == TyTy::InferType::InferTypeKind::INTEGRAL
;
1197 return ltype
->clone ();
1202 TyTy::UintType
&type
= *static_cast<TyTy::UintType
*> (rtype
);
1203 bool is_valid
= ltype
->get_uint_kind () == type
.get_uint_kind ();
1205 return new TyTy::UintType (type
.get_ref (), type
.get_ty_ref (),
1206 type
.get_uint_kind ());
1227 case TyTy::PLACEHOLDER
:
1228 case TyTy::PROJECTION
:
1232 return new TyTy::ErrorType (0);
1234 return new TyTy::ErrorType (0);
1238 UnifyRules::expect_float (TyTy::FloatType
*ltype
, TyTy::BaseType
*rtype
)
1240 switch (rtype
->get_kind ())
1243 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1245 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
1246 || r
->get_infer_kind () == TyTy::InferType::InferTypeKind::FLOAT
;
1248 return ltype
->clone ();
1253 TyTy::FloatType
&type
= *static_cast<TyTy::FloatType
*> (rtype
);
1254 bool is_valid
= ltype
->get_float_kind () == type
.get_float_kind ();
1256 return new TyTy::FloatType (type
.get_ref (), type
.get_ty_ref (),
1257 type
.get_float_kind ());
1278 case TyTy::PLACEHOLDER
:
1279 case TyTy::PROJECTION
:
1283 return new TyTy::ErrorType (0);
1285 return new TyTy::ErrorType (0);
1289 UnifyRules::expect_isize (TyTy::ISizeType
*ltype
, TyTy::BaseType
*rtype
)
1291 switch (rtype
->get_kind ())
1294 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1296 = r
->get_infer_kind () != TyTy::InferType::InferTypeKind::FLOAT
;
1298 return ltype
->clone ();
1303 return rtype
->clone ();
1322 case TyTy::PLACEHOLDER
:
1323 case TyTy::PROJECTION
:
1327 return new TyTy::ErrorType (0);
1329 return new TyTy::ErrorType (0);
1333 UnifyRules::expect_usize (TyTy::USizeType
*ltype
, TyTy::BaseType
*rtype
)
1335 switch (rtype
->get_kind ())
1338 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1340 = r
->get_infer_kind () != TyTy::InferType::InferTypeKind::FLOAT
;
1342 return ltype
->clone ();
1347 return rtype
->clone ();
1366 case TyTy::PLACEHOLDER
:
1367 case TyTy::PROJECTION
:
1371 return new TyTy::ErrorType (0);
1373 return new TyTy::ErrorType (0);
1377 UnifyRules::expect_never (TyTy::NeverType
*ltype
, TyTy::BaseType
*rtype
)
1379 switch (rtype
->get_kind ())
1382 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1384 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1386 return ltype
->clone ();
1391 return rtype
->clone ();
1393 case TyTy::PLACEHOLDER
:
1394 case TyTy::PROJECTION
:
1415 return new TyTy::ErrorType (0);
1417 return new TyTy::ErrorType (0);
1421 UnifyRules::expect_placeholder (TyTy::PlaceholderType
*ltype
,
1422 TyTy::BaseType
*rtype
)
1424 switch (rtype
->get_kind ())
1427 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1429 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1431 return ltype
->clone ();
1435 case TyTy::PLACEHOLDER
: {
1436 TyTy::PlaceholderType
&type
1437 = *static_cast<TyTy::PlaceholderType
*> (rtype
);
1439 = ltype
->get_symbol ().compare (type
.get_symbol ()) == 0;
1442 return type
.clone ();
1447 case TyTy::PROJECTION
:
1469 return new TyTy::ErrorType (0);
1471 return new TyTy::ErrorType (0);
1475 UnifyRules::expect_projection (TyTy::ProjectionType
*ltype
,
1476 TyTy::BaseType
*rtype
)
1478 switch (rtype
->get_kind ())
1481 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1483 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1485 return ltype
->clone ();
1490 case TyTy::PROJECTION
:
1514 case TyTy::PLACEHOLDER
:
1516 return new TyTy::ErrorType (0);
1518 return new TyTy::ErrorType (0);
1522 UnifyRules::expect_dyn (TyTy::DynamicObjectType
*ltype
, TyTy::BaseType
*rtype
)
1524 switch (rtype
->get_kind ())
1527 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1529 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1531 return ltype
->clone ();
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 ();
1571 case TyTy::PLACEHOLDER
:
1572 case TyTy::PROJECTION
:
1574 return new TyTy::ErrorType (0);
1576 return new TyTy::ErrorType (0);
1580 UnifyRules::expect_closure (TyTy::ClosureType
*ltype
, TyTy::BaseType
*rtype
)
1582 switch (rtype
->get_kind ())
1585 TyTy::InferType
*r
= static_cast<TyTy::InferType
*> (rtype
);
1587 = r
->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL
;
1589 return ltype
->clone ();
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 <ype
->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 (<ype
->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 ();
1641 case TyTy::PLACEHOLDER
:
1642 case TyTy::PROJECTION
:
1645 return new TyTy::ErrorType (0);
1647 return new TyTy::ErrorType (0);
1650 } // namespace Resolver