1 // Copyright (C) 2020-2025 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-tyty.h"
21 #include "rust-tyty-visitor.h"
22 #include "rust-hir-map.h"
23 #include "rust-location.h"
24 #include "rust-linemap.h"
26 #include "rust-substitution-mapper.h"
27 #include "rust-hir-trait-reference.h"
28 #include "rust-hir-trait-resolve.h"
29 #include "rust-tyty-cmp.h"
30 #include "rust-type-util.h"
31 #include "rust-hir-type-bounds.h"
39 TypeKindFormat::to_string (TypeKind kind
)
55 case TypeKind::POINTER
:
100 case TypeKind::PLACEHOLDER
:
101 return "Placeholder";
103 case TypeKind::PROJECTION
:
106 case TypeKind::DYNAMIC
:
109 case TypeKind::CLOSURE
:
112 case TypeKind::ERROR
:
119 is_primitive_type_kind (TypeKind kind
)
127 case TypeKind::ISIZE
:
128 case TypeKind::USIZE
:
129 case TypeKind::FLOAT
:
130 case TypeKind::NEVER
:
140 BaseType::BaseType (HirId ref
, HirId ty_ref
, TypeKind kind
, RustIdent ident
,
141 std::set
<HirId
> refs
)
142 : TypeBoundsMappings ({}), kind (kind
), ref (ref
), ty_ref (ty_ref
),
143 orig_ref (ref
), combined (refs
), ident (ident
),
144 mappings (Analysis::Mappings::get ())
147 BaseType::BaseType (HirId ref
, HirId ty_ref
, TypeKind kind
, RustIdent ident
,
148 std::vector
<TypeBoundPredicate
> specified_bounds
,
149 std::set
<HirId
> refs
)
150 : TypeBoundsMappings (specified_bounds
), kind (kind
), ref (ref
),
151 ty_ref (ty_ref
), orig_ref (ref
), combined (refs
), ident (ident
),
152 mappings (Analysis::Mappings::get ())
155 BaseType::~BaseType () {}
158 BaseType::get_ref () const
164 BaseType::set_ref (HirId id
)
167 append_reference (ref
);
172 BaseType::get_ty_ref () const
178 BaseType::set_ty_ref (HirId id
)
183 BaseType::get_orig_ref () const
189 BaseType::is_equal (const BaseType
&other
) const
191 return get_kind () == other
.get_kind ();
195 BaseType::is_unit () const
197 const TyTy::BaseType
*x
= destructure ();
198 switch (x
->get_kind ())
224 // FIXME ! is coerceable to () so we need to fix that
229 return x
->as
<const TupleType
> ()->num_fields () == 0;
233 auto adt
= x
->as
<const ADTType
> ();
237 for (const auto &variant
: adt
->get_variants ())
239 if (variant
->num_fields () > 0)
250 BaseType::get_kind () const
256 BaseType::get_combined_refs () const
262 BaseType::append_reference (HirId id
)
264 combined
.insert (id
);
268 BaseType::get_ident () const
274 BaseType::get_locus () const
279 // FIXME this is missing locus
281 BaseType::satisfies_bound (const TypeBoundPredicate
&predicate
,
282 bool emit_error
) const
284 const Resolver::TraitReference
*query
= predicate
.get ();
285 for (const auto &bound
: specified_bounds
)
287 const Resolver::TraitReference
*item
= bound
.get ();
288 if (item
->satisfies_bound (*query
))
292 if (destructure ()->is
<InferType
> ())
295 bool satisfied
= false;
296 auto probed
= Resolver::TypeBoundsProbe::Probe (this);
297 for (const auto &b
: probed
)
299 const Resolver::TraitReference
*bound
= b
.first
;
300 if (bound
->satisfies_bound (*query
))
310 for (const auto &b
: probed
)
312 const Resolver::TraitReference
*bound
= b
.first
;
313 if (!bound
->is_equal (*query
))
316 // builtin ones have no impl-block this needs fixed and use a builtin node
318 if (b
.second
== nullptr)
321 // need to check that associated types can match as well
322 const HIR::ImplBlock
&impl
= *(b
.second
);
323 for (const auto &item
: impl
.get_impl_items ())
325 bool is_associated_type
= item
->get_impl_item_type ()
326 == HIR::ImplItem::ImplItemType::TYPE_ALIAS
;
327 if (!is_associated_type
)
330 TyTy::BaseType
*impl_item_ty
= nullptr;
331 Analysis::NodeMapping i
= item
->get_impl_mappings ();
332 bool query_ok
= Resolver::query_type (i
.get_hirid (), &impl_item_ty
);
336 std::string item_name
= item
->get_impl_item_name ();
337 TypeBoundPredicateItem lookup
338 = predicate
.lookup_associated_item (item_name
);
339 if (lookup
.is_error ())
342 const auto *item_ref
= lookup
.get_raw_item ();
343 TyTy::BaseType
*bound_ty
= item_ref
->get_tyty ();
346 if (!bound_ty
->can_eq (impl_item_ty
, false))
348 if (!impl_item_ty
->can_eq (bound_ty
, false))
352 rich_location
r (line_table
,
353 mappings
->lookup_location (get_ref ()));
354 r
.add_range (predicate
.get_locus ());
355 r
.add_range (mappings
->lookup_location (i
.get_hirid ()));
358 = "expected " + bound_ty
->destructure ()->get_name ()
360 + impl_item_ty
->destructure ()->get_name ();
361 r
.add_fixit_replace (rich_msg
.c_str ());
365 "type mismatch, expected %qs but got %qs",
366 bound_ty
->destructure ()->get_name ().c_str (),
367 impl_item_ty
->destructure ()->get_name ().c_str ());
381 BaseType::bounds_compatible (const BaseType
&other
, location_t locus
,
382 bool emit_error
) const
384 std::vector
<std::reference_wrapper
<const TypeBoundPredicate
>>
386 for (auto &bound
: get_specified_bounds ())
388 if (!other
.satisfies_bound (bound
, emit_error
))
389 unsatisfied_bounds
.push_back (bound
);
392 // lets emit a single error for this
393 if (unsatisfied_bounds
.size () > 0)
395 rich_location
r (line_table
, locus
);
396 std::string missing_preds
;
397 for (size_t i
= 0; i
< unsatisfied_bounds
.size (); i
++)
399 const TypeBoundPredicate
&pred
= unsatisfied_bounds
.at (i
);
400 r
.add_range (pred
.get_locus ());
401 missing_preds
+= pred
.get_name ();
403 bool have_next
= (i
+ 1) < unsatisfied_bounds
.size ();
405 missing_preds
+= ", ";
410 rust_error_at (r
, ErrorCode::E0277
,
411 "bounds not satisfied for %s %qs is not satisfied",
412 other
.get_name ().c_str (), missing_preds
.c_str ());
413 // rust_assert (!emit_error);
417 return unsatisfied_bounds
.size () == 0;
421 BaseType::inherit_bounds (const BaseType
&other
)
423 inherit_bounds (other
.get_specified_bounds ());
427 BaseType::inherit_bounds (
428 const std::vector
<TyTy::TypeBoundPredicate
> &specified_bounds
)
430 for (auto &bound
: specified_bounds
)
437 BaseType::get_root () const
439 // FIXME this needs to be it its own visitor class with a vector adjustments
440 const TyTy::BaseType
*root
= this;
442 if (const auto r
= root
->try_as
<const ReferenceType
> ())
444 root
= r
->get_base ()->get_root ();
446 else if (const auto r
= root
->try_as
<const PointerType
> ())
448 root
= r
->get_base ()->get_root ();
450 // these are an unsize
451 else if (const auto r
= root
->try_as
<const SliceType
> ())
453 root
= r
->get_element_type ()->get_root ();
455 // else if (const auto r = root->try_as<const ArrayType> ())
457 // root = r->get_element_type ()->get_root ();
464 BaseType::destructure ()
466 int recurisve_ops
= 0;
470 if (recurisve_ops
++ >= rust_max_recursion_depth
)
474 "%<recursion depth%> count exceeds limit of %i (use "
475 "%<frust-max-recursion-depth=%> to increase the limit)",
476 rust_max_recursion_depth
);
477 return new ErrorType (get_ref ());
480 if (auto p
= x
->try_as
<ParamType
> ())
482 auto pr
= p
->resolve ();
488 else if (auto p
= x
->try_as
<PlaceholderType
> ())
490 if (!p
->can_resolve ())
495 else if (auto p
= x
->try_as
<ProjectionType
> ())
509 BaseType::destructure () const
511 int recurisve_ops
= 0;
512 const BaseType
*x
= this;
515 if (recurisve_ops
++ >= rust_max_recursion_depth
)
519 "%<recursion depth%> count exceeds limit of %i (use "
520 "%<frust-max-recursion-depth=%> to increase the limit)",
521 rust_max_recursion_depth
);
522 return new ErrorType (get_ref ());
525 if (auto p
= x
->try_as
<const ParamType
> ())
527 auto pr
= p
->resolve ();
533 else if (auto p
= x
->try_as
<const PlaceholderType
> ())
535 if (!p
->can_resolve ())
540 else if (auto p
= x
->try_as
<const ProjectionType
> ())
554 BaseType::monomorphized_clone () const
556 const TyTy::BaseType
*x
= destructure ();
558 if (auto arr
= x
->try_as
<const ArrayType
> ())
560 TyVar elm
= arr
->get_var_element_type ().monomorphized_clone ();
561 return new ArrayType (arr
->get_ref (), arr
->get_ty_ref (), ident
.locus
,
562 arr
->get_capacity_expr (), elm
,
563 arr
->get_combined_refs ());
565 else if (auto slice
= x
->try_as
<const SliceType
> ())
567 TyVar elm
= slice
->get_var_element_type ().monomorphized_clone ();
568 return new SliceType (slice
->get_ref (), slice
->get_ty_ref (),
569 ident
.locus
, elm
, slice
->get_combined_refs ());
571 else if (auto ptr
= x
->try_as
<const PointerType
> ())
573 TyVar elm
= ptr
->get_var_element_type ().monomorphized_clone ();
574 return new PointerType (ptr
->get_ref (), ptr
->get_ty_ref (), elm
,
575 ptr
->mutability (), ptr
->get_combined_refs ());
577 else if (auto ref
= x
->try_as
<const ReferenceType
> ())
579 TyVar elm
= ref
->get_var_element_type ().monomorphized_clone ();
580 return new ReferenceType (ref
->get_ref (), ref
->get_ty_ref (), elm
,
581 ref
->mutability (), ref
->get_region (),
582 ref
->get_combined_refs ());
584 else if (auto tuple
= x
->try_as
<const TupleType
> ())
586 std::vector
<TyVar
> cloned_fields
;
587 for (const auto &f
: tuple
->get_fields ())
588 cloned_fields
.push_back (f
.monomorphized_clone ());
590 return new TupleType (tuple
->get_ref (), tuple
->get_ty_ref (),
591 ident
.locus
, cloned_fields
,
592 tuple
->get_combined_refs ());
594 else if (auto fn
= x
->try_as
<const FnType
> ())
596 std::vector
<std::pair
<HIR::Pattern
*, BaseType
*>> cloned_params
;
597 for (auto &p
: fn
->get_params ())
598 cloned_params
.push_back ({p
.first
, p
.second
->monomorphized_clone ()});
600 BaseType
*retty
= fn
->get_return_type ()->monomorphized_clone ();
601 return new FnType (fn
->get_ref (), fn
->get_ty_ref (), fn
->get_id (),
602 fn
->get_identifier (), fn
->ident
, fn
->get_flags (),
603 fn
->get_abi (), std::move (cloned_params
), retty
,
604 fn
->clone_substs (), fn
->get_substitution_arguments (),
605 fn
->get_region_constraints (),
606 fn
->get_combined_refs ());
608 else if (auto fn
= x
->try_as
<const FnPtr
> ())
610 std::vector
<TyVar
> cloned_params
;
611 for (auto &p
: fn
->get_params ())
612 cloned_params
.push_back (p
.monomorphized_clone ());
614 TyVar retty
= fn
->get_var_return_type ().monomorphized_clone ();
615 return new FnPtr (fn
->get_ref (), fn
->get_ty_ref (), ident
.locus
,
616 std::move (cloned_params
), retty
,
617 fn
->get_combined_refs ());
619 else if (auto adt
= x
->try_as
<const ADTType
> ())
621 std::vector
<VariantDef
*> cloned_variants
;
622 for (auto &variant
: adt
->get_variants ())
623 cloned_variants
.push_back (variant
->monomorphized_clone ());
625 return new ADTType (adt
->get_ref (), adt
->get_ty_ref (),
626 adt
->get_identifier (), adt
->ident
,
627 adt
->get_adt_kind (), cloned_variants
,
628 adt
->clone_substs (), adt
->get_repr_options (),
629 adt
->get_used_arguments (),
630 adt
->get_region_constraints (),
631 adt
->get_combined_refs ());
643 BaseType::mappings_str () const
645 std::string buffer
= "Ref: " + std::to_string (get_ref ())
646 + " TyRef: " + std::to_string (get_ty_ref ());
648 for (auto &ref
: combined
)
649 buffer
+= std::to_string (ref
) + ",";
651 return "(" + buffer
+ ")";
655 BaseType::debug_str () const
657 // return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":"
658 // + mappings_str () + ":" + bounds_as_string ();
663 BaseType::debug () const
665 rust_debug ("[%p] %s", static_cast<const void *> (this),
666 debug_str ().c_str ());
670 BaseType::is_concrete () const
672 const TyTy::BaseType
*x
= destructure ();
674 if (x
->is
<ParamType
> () || x
->is
<ProjectionType
> ())
678 // placeholder is a special case for this case when it is not resolvable
679 // it means we its just an empty placeholder associated type which is
681 else if (x
->is
<PlaceholderType
> ())
685 else if (auto fn
= x
->try_as
<const FnType
> ())
687 for (const auto ¶m
: fn
->get_params ())
689 if (!param
.second
->is_concrete ())
692 return fn
->get_return_type ()->is_concrete ();
694 else if (auto fn
= x
->try_as
<const FnPtr
> ())
696 for (const auto ¶m
: fn
->get_params ())
698 if (!param
.get_tyty ()->is_concrete ())
701 return fn
->get_return_type ()->is_concrete ();
703 else if (auto adt
= x
->try_as
<const ADTType
> ())
706 return !adt
->needs_substitution ();
708 for (auto &variant
: adt
->get_variants ())
711 = variant
->get_variant_type () == VariantDef::VariantType::NUM
;
715 for (auto &field
: variant
->get_fields ())
717 const BaseType
*field_type
= field
->get_field_type ();
718 if (!field_type
->is_concrete ())
724 else if (auto arr
= x
->try_as
<const ArrayType
> ())
726 return arr
->get_element_type ()->is_concrete ();
728 else if (auto slice
= x
->try_as
<const SliceType
> ())
730 return slice
->get_element_type ()->is_concrete ();
732 else if (auto ptr
= x
->try_as
<const PointerType
> ())
734 return ptr
->get_base ()->is_concrete ();
736 else if (auto ref
= x
->try_as
<const ReferenceType
> ())
738 return ref
->get_base ()->is_concrete ();
740 else if (auto tuple
= x
->try_as
<const TupleType
> ())
742 for (size_t i
= 0; i
< tuple
->num_fields (); i
++)
744 if (!tuple
->get_field (i
)->is_concrete ())
749 else if (auto closure
= x
->try_as
<const ClosureType
> ())
751 if (closure
->get_parameters ().is_concrete ())
753 return closure
->get_result_type ().is_concrete ();
755 else if (x
->is
<InferType
> () || x
->is
<BoolType
> () || x
->is
<CharType
> ()
756 || x
->is
<IntType
> () || x
->is
<UintType
> () || x
->is
<FloatType
> ()
757 || x
->is
<USizeType
> () || x
->is
<ISizeType
> () || x
->is
<NeverType
> ()
758 || x
->is
<StrType
> () || x
->is
<DynamicObjectType
> ()
759 || x
->is
<ErrorType
> ())
768 BaseType::has_substitutions_defined () const
770 const TyTy::BaseType
*x
= destructure ();
771 switch (x
->get_kind ())
796 const ProjectionType
&p
= *static_cast<const ProjectionType
*> (x
);
797 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (p
);
798 return ref
.has_substitutions ();
803 const FnType
&fn
= *static_cast<const FnType
*> (x
);
804 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (fn
);
805 return ref
.has_substitutions ();
810 const ADTType
&adt
= *static_cast<const ADTType
*> (x
);
811 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (adt
);
812 return ref
.has_substitutions ();
817 const ClosureType
&closure
= *static_cast<const ClosureType
*> (x
);
818 const SubstitutionRef
&ref
819 = static_cast<const SubstitutionRef
&> (closure
);
820 return ref
.has_substitutions ();
829 BaseType::needs_generic_substitutions () const
831 const TyTy::BaseType
*x
= destructure ();
832 switch (x
->get_kind ())
857 const ProjectionType
&p
= *static_cast<const ProjectionType
*> (x
);
858 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (p
);
859 return ref
.needs_substitution ();
864 const FnType
&fn
= *static_cast<const FnType
*> (x
);
865 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (fn
);
866 return ref
.needs_substitution ();
871 const ADTType
&adt
= *static_cast<const ADTType
*> (x
);
872 const SubstitutionRef
&ref
= static_cast<const SubstitutionRef
&> (adt
);
873 return ref
.needs_substitution ();
878 const ClosureType
&closure
= *static_cast<const ClosureType
*> (x
);
879 const SubstitutionRef
&ref
880 = static_cast<const SubstitutionRef
&> (closure
);
881 return ref
.needs_substitution ();
891 InferType::InferType (HirId ref
, InferTypeKind infer_kind
, TypeHint hint
,
892 location_t locus
, std::set
<HirId
> refs
)
893 : BaseType (ref
, ref
, KIND
, {Resolver::CanonicalPath::create_empty (), locus
},
895 infer_kind (infer_kind
), default_hint (hint
)
898 InferType::InferType (HirId ref
, HirId ty_ref
, InferTypeKind infer_kind
,
899 TypeHint hint
, location_t locus
, std::set
<HirId
> refs
)
900 : BaseType (ref
, ty_ref
, KIND
,
901 {Resolver::CanonicalPath::create_empty (), locus
}, refs
),
902 infer_kind (infer_kind
), default_hint (hint
)
905 InferType::InferTypeKind
906 InferType::get_infer_kind () const
912 InferType::get_name () const
918 InferType::accept_vis (TyVisitor
&vis
)
924 InferType::accept_vis (TyConstVisitor
&vis
) const
930 InferType::as_string () const
941 return "<infer::error>";
945 InferType::can_eq (const BaseType
*other
, bool emit_errors
) const
947 InferCmp
r (this, emit_errors
);
948 return r
.can_eq (other
);
952 InferType::clone () const
954 // clones for inference variables are special in that they _must_ exist within
955 // the type check context and we must ensure we don't loose the chain
956 // otherwise we will end up in the missing type annotations case
958 // This means we cannot simply take over the same reference we must generate a
959 // new ref just like the get_implicit_infer_var code then we can setup the
960 // chain of references accordingly to ensure we don't loose the ability to
961 // update the inference variables when we solve the type
963 auto mappings
= Analysis::Mappings::get ();
964 auto context
= Resolver::TypeCheckContext::get ();
967 = new InferType (mappings
->get_next_hir_id (), get_infer_kind (),
968 default_hint
, get_ident ().locus
, get_combined_refs ());
970 context
->insert_type (Analysis::NodeMapping (mappings
->get_current_crate (),
973 UNKNOWN_LOCAL_DEFID
),
975 mappings
->insert_location (clone
->get_ref (),
976 mappings
->lookup_location (get_ref ()));
978 // setup the chain to reference this
979 clone
->append_reference (get_ref ());
985 InferType::default_type (BaseType
**type
) const
987 auto context
= Resolver::TypeCheckContext::get ();
990 // NOTE: Calling this error is misleading.
991 if (default_hint
.kind
== TypeKind::ERROR
)
999 ok
= context
->lookup_builtin ("i32", type
);
1005 ok
= context
->lookup_builtin ("f64", type
);
1013 switch (default_hint
.kind
)
1016 ok
= context
->lookup_builtin ("isize", type
);
1021 ok
= context
->lookup_builtin ("usize", type
);
1026 switch (default_hint
.szhint
)
1028 case TypeHint::SizeHint::S8
:
1029 ok
= context
->lookup_builtin ("i8", type
);
1033 case TypeHint::SizeHint::S16
:
1034 ok
= context
->lookup_builtin ("i16", type
);
1038 case TypeHint::SizeHint::S32
:
1039 ok
= context
->lookup_builtin ("i32", type
);
1043 case TypeHint::SizeHint::S64
:
1044 ok
= context
->lookup_builtin ("i64", type
);
1048 case TypeHint::SizeHint::S128
:
1049 ok
= context
->lookup_builtin ("i128", type
);
1059 switch (default_hint
.szhint
)
1061 case TypeHint::SizeHint::S8
:
1062 ok
= context
->lookup_builtin ("u8", type
);
1066 case TypeHint::SizeHint::S16
:
1067 ok
= context
->lookup_builtin ("u16", type
);
1071 case TypeHint::SizeHint::S32
:
1072 ok
= context
->lookup_builtin ("u32", type
);
1076 case TypeHint::SizeHint::S64
:
1077 ok
= context
->lookup_builtin ("u64", type
);
1081 case TypeHint::SizeHint::S128
:
1082 ok
= context
->lookup_builtin ("u128", type
);
1091 case TypeKind::FLOAT
:
1092 switch (default_hint
.szhint
)
1094 case TypeHint::SizeHint::S32
:
1095 ok
= context
->lookup_builtin ("f32", type
);
1099 case TypeHint::SizeHint::S64
:
1100 ok
= context
->lookup_builtin ("f64", type
);
1117 InferType::apply_primitive_type_hint (const BaseType
&hint
)
1119 switch (hint
.get_kind ())
1123 infer_kind
= INTEGRAL
;
1124 default_hint
.kind
= hint
.get_kind ();
1128 infer_kind
= INTEGRAL
;
1129 default_hint
.kind
= hint
.get_kind ();
1130 default_hint
.shint
= TypeHint::SignedHint::SIGNED
;
1131 switch (hint
.as
<const IntType
> ()->get_int_kind ())
1134 default_hint
.szhint
= TypeHint::SizeHint::S8
;
1137 default_hint
.szhint
= TypeHint::SizeHint::S16
;
1140 default_hint
.szhint
= TypeHint::SizeHint::S32
;
1143 default_hint
.szhint
= TypeHint::SizeHint::S64
;
1146 default_hint
.szhint
= TypeHint::SizeHint::S128
;
1153 infer_kind
= INTEGRAL
;
1154 default_hint
.kind
= hint
.get_kind ();
1155 default_hint
.shint
= TypeHint::SignedHint::UNSIGNED
;
1156 switch (hint
.as
<const UintType
> ()->get_uint_kind ())
1159 default_hint
.szhint
= TypeHint::SizeHint::S8
;
1162 default_hint
.szhint
= TypeHint::SizeHint::S16
;
1165 default_hint
.szhint
= TypeHint::SizeHint::S32
;
1168 default_hint
.szhint
= TypeHint::SizeHint::S64
;
1170 case UintType::U128
:
1171 default_hint
.szhint
= TypeHint::SizeHint::S128
;
1177 case TypeKind::FLOAT
: {
1179 default_hint
.shint
= TypeHint::SignedHint::SIGNED
;
1180 default_hint
.kind
= hint
.get_kind ();
1181 switch (hint
.as
<const FloatType
> ()->get_float_kind ())
1183 case FloatType::F32
:
1184 default_hint
.szhint
= TypeHint::SizeHint::S32
;
1187 case FloatType::F64
:
1188 default_hint
.szhint
= TypeHint::SizeHint::S64
;
1195 // TODO bool, char, never??
1202 ErrorType::ErrorType (HirId ref
, std::set
<HirId
> refs
)
1203 : BaseType (ref
, ref
, KIND
,
1204 {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION
}, refs
)
1207 ErrorType::ErrorType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
1208 : BaseType (ref
, ty_ref
, KIND
,
1209 {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION
}, refs
)
1213 ErrorType::get_name () const
1215 return as_string ();
1219 ErrorType::accept_vis (TyVisitor
&vis
)
1225 ErrorType::accept_vis (TyConstVisitor
&vis
) const
1231 ErrorType::as_string () const
1233 return "<tyty::error>";
1237 ErrorType::can_eq (const BaseType
*other
, bool emit_errors
) const
1239 return get_kind () == other
->get_kind ();
1243 ErrorType::clone () const
1245 return new ErrorType (get_ref (), get_ty_ref (), get_combined_refs ());
1248 // Struct Field type
1250 StructFieldType::StructFieldType (HirId ref
, std::string name
, BaseType
*ty
,
1252 : ref (ref
), name (name
), ty (ty
), locus (locus
)
1256 StructFieldType::get_ref () const
1262 StructFieldType::get_name () const
1268 StructFieldType::get_field_type () const
1274 StructFieldType::set_field_type (BaseType
*fty
)
1280 StructFieldType::debug () const
1282 rust_debug ("%s", as_string ().c_str ());
1286 StructFieldType::get_locus () const
1292 StructFieldType::as_string () const
1294 return name
+ ":" + get_field_type ()->debug_str ();
1298 StructFieldType::is_equal (const StructFieldType
&other
) const
1300 bool names_eq
= get_name () == other
.get_name ();
1302 TyTy::BaseType
*o
= other
.get_field_type ();
1303 if (auto op
= o
->try_as
<ParamType
> ())
1306 bool types_eq
= get_field_type ()->is_equal (*o
);
1308 return names_eq
&& types_eq
;
1312 StructFieldType::clone () const
1314 return new StructFieldType (get_ref (), get_name (),
1315 get_field_type ()->clone (), locus
);
1319 StructFieldType::monomorphized_clone () const
1321 return new StructFieldType (get_ref (), get_name (),
1322 get_field_type ()->monomorphized_clone (), locus
);
1328 VariantDef::variant_type_string (VariantType type
)
1339 rust_unreachable ();
1343 VariantDef::VariantDef (HirId id
, DefId defid
, std::string identifier
,
1344 RustIdent ident
, HIR::Expr
*discriminant
)
1345 : id (id
), defid (defid
), identifier (identifier
), ident (ident
),
1346 discriminant (discriminant
)
1349 type
= VariantType::NUM
;
1353 VariantDef::VariantDef (HirId id
, DefId defid
, std::string identifier
,
1354 RustIdent ident
, VariantType type
,
1355 HIR::Expr
*discriminant
,
1356 std::vector
<StructFieldType
*> fields
)
1357 : id (id
), defid (defid
), identifier (identifier
), ident (ident
), type (type
),
1358 discriminant (discriminant
), fields (fields
)
1360 rust_assert ((type
== VariantType::NUM
&& fields
.empty ())
1361 || (type
== VariantType::TUPLE
|| type
== VariantType::STRUCT
));
1364 VariantDef::VariantDef (const VariantDef
&other
)
1365 : id (other
.id
), defid (other
.defid
), identifier (other
.identifier
),
1366 ident (other
.ident
), type (other
.type
), discriminant (other
.discriminant
),
1367 fields (other
.fields
)
1371 VariantDef::operator= (const VariantDef
&other
)
1374 identifier
= other
.identifier
;
1376 discriminant
= other
.discriminant
;
1377 fields
= other
.fields
;
1378 ident
= other
.ident
;
1384 VariantDef::get_error_node ()
1386 static VariantDef node
1387 = VariantDef (UNKNOWN_HIRID
, UNKNOWN_DEFID
, "",
1388 {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION
},
1395 VariantDef::is_error () const
1397 return get_id () == UNKNOWN_HIRID
;
1401 VariantDef::get_id () const
1407 VariantDef::get_defid () const
1412 VariantDef::VariantType
1413 VariantDef::get_variant_type () const
1419 VariantDef::is_data_variant () const
1421 return type
!= VariantType::NUM
;
1425 VariantDef::is_dataless_variant () const
1427 return type
== VariantType::NUM
;
1431 VariantDef::get_identifier () const
1437 VariantDef::num_fields () const
1439 return fields
.size ();
1443 VariantDef::get_field_at_index (size_t index
)
1445 rust_assert (index
< fields
.size ());
1446 return fields
.at (index
);
1449 std::vector
<StructFieldType
*> &
1450 VariantDef::get_fields ()
1452 rust_assert (type
!= NUM
);
1457 VariantDef::lookup_field (const std::string
&lookup
,
1458 StructFieldType
**field_lookup
, size_t *index
) const
1461 for (auto &field
: fields
)
1463 if (field
->get_name ().compare (lookup
) == 0)
1465 if (index
!= nullptr)
1468 if (field_lookup
!= nullptr)
1469 *field_lookup
= field
;
1479 VariantDef::get_discriminant () const
1481 rust_assert (discriminant
!= nullptr);
1482 return discriminant
;
1486 VariantDef::as_string () const
1488 if (type
== VariantType::NUM
)
1489 return identifier
+ " = " + discriminant
->as_string ();
1492 for (size_t i
= 0; i
< fields
.size (); ++i
)
1494 buffer
+= fields
.at (i
)->as_string ();
1495 if ((i
+ 1) < fields
.size ())
1499 if (type
== VariantType::TUPLE
)
1500 return identifier
+ " (" + buffer
+ ")";
1502 return identifier
+ " {" + buffer
+ "}";
1506 VariantDef::is_equal (const VariantDef
&other
) const
1508 if (type
!= other
.type
)
1511 if (identifier
.compare (other
.identifier
) != 0)
1514 if (discriminant
!= other
.discriminant
)
1517 if (fields
.size () != other
.fields
.size ())
1520 for (size_t i
= 0; i
< fields
.size (); i
++)
1522 if (!fields
.at (i
)->is_equal (*other
.fields
.at (i
)))
1530 VariantDef::clone () const
1532 std::vector
<StructFieldType
*> cloned_fields
;
1533 for (auto &f
: fields
)
1534 cloned_fields
.push_back ((StructFieldType
*) f
->clone ());
1536 return new VariantDef (id
, defid
, identifier
, ident
, type
, discriminant
,
1541 VariantDef::monomorphized_clone () const
1543 std::vector
<StructFieldType
*> cloned_fields
;
1544 for (auto &f
: fields
)
1545 cloned_fields
.push_back ((StructFieldType
*) f
->monomorphized_clone ());
1547 return new VariantDef (id
, defid
, identifier
, ident
, type
, discriminant
,
1552 VariantDef::get_ident () const
1560 ADTType::accept_vis (TyVisitor
&vis
)
1566 ADTType::accept_vis (TyConstVisitor
&vis
) const
1572 ADTType::as_string () const
1574 std::string variants_buffer
;
1575 for (size_t i
= 0; i
< number_of_variants (); ++i
)
1577 TyTy::VariantDef
*variant
= variants
.at (i
);
1578 variants_buffer
+= variant
->as_string ();
1579 if ((i
+ 1) < number_of_variants ())
1580 variants_buffer
+= ", ";
1583 return identifier
+ subst_as_string () + "{" + variants_buffer
+ "}";
1587 ADTType::can_eq (const BaseType
*other
, bool emit_errors
) const
1589 ADTCmp
r (this, emit_errors
);
1590 return r
.can_eq (other
);
1594 ADTType::is_equal (const BaseType
&other
) const
1596 if (get_kind () != other
.get_kind ())
1599 auto other2
= other
.as
<const ADTType
> ();
1600 if (get_adt_kind () != other2
->get_adt_kind ())
1603 if (number_of_variants () != other2
->number_of_variants ())
1606 if (has_substitutions_defined () != other2
->has_substitutions_defined ())
1609 if (has_substitutions_defined ())
1611 if (get_num_substitutions () != other2
->get_num_substitutions ())
1614 for (size_t i
= 0; i
< get_num_substitutions (); i
++)
1616 const SubstitutionParamMapping
&a
= substitutions
.at (i
);
1617 const SubstitutionParamMapping
&b
= other2
->substitutions
.at (i
);
1619 const ParamType
*aa
= a
.get_param_ty ();
1620 const ParamType
*bb
= b
.get_param_ty ();
1621 BaseType
*aaa
= aa
->resolve ();
1622 BaseType
*bbb
= bb
->resolve ();
1623 if (!aaa
->is_equal (*bbb
))
1628 for (size_t i
= 0; i
< number_of_variants (); i
++)
1630 const TyTy::VariantDef
*a
= get_variants ().at (i
);
1631 const TyTy::VariantDef
*b
= other2
->get_variants ().at (i
);
1633 if (!a
->is_equal (*b
))
1641 ADTType::clone () const
1643 std::vector
<VariantDef
*> cloned_variants
;
1644 for (auto &variant
: variants
)
1645 cloned_variants
.push_back (variant
->clone ());
1647 return new ADTType (get_ref (), get_ty_ref (), identifier
, ident
,
1648 get_adt_kind (), cloned_variants
, clone_substs (),
1649 get_repr_options (), used_arguments
,
1650 get_region_constraints (), get_combined_refs ());
1654 handle_substitions (SubstitutionArgumentMappings
&subst_mappings
,
1655 StructFieldType
*field
)
1657 auto fty
= field
->get_field_type ();
1658 if (auto p
= fty
->try_as
<ParamType
> ())
1660 SubstitutionArg arg
= SubstitutionArg::error ();
1661 bool ok
= subst_mappings
.get_argument_for_symbol (p
, &arg
);
1664 auto argt
= arg
.get_tyty ();
1665 bool arg_is_param
= argt
->get_kind () == TyTy::TypeKind::PARAM
;
1666 bool arg_is_concrete
= argt
->get_kind () != TyTy::TypeKind::INFER
;
1668 if (arg_is_param
|| arg_is_concrete
)
1670 auto new_field
= argt
->clone ();
1671 new_field
->set_ref (fty
->get_ref ());
1672 field
->set_field_type (new_field
);
1676 field
->get_field_type ()->set_ty_ref (argt
->get_ref ());
1680 else if (fty
->has_substitutions_defined () || !fty
->is_concrete ())
1683 = Resolver::SubstMapperInternal::Resolve (fty
, subst_mappings
);
1685 if (concrete
->get_kind () == TyTy::TypeKind::ERROR
)
1687 rust_error_at (subst_mappings
.get_locus (),
1688 "Failed to resolve field substitution type: %s",
1689 fty
->as_string ().c_str ());
1693 auto new_field
= concrete
->clone ();
1694 new_field
->set_ref (fty
->get_ref ());
1695 field
->set_field_type (new_field
);
1702 ADTType::handle_substitions (SubstitutionArgumentMappings
&subst_mappings
)
1704 auto adt
= clone ()->as
<ADTType
> ();
1705 adt
->set_ty_ref (mappings
->get_next_hir_id ());
1706 adt
->used_arguments
= subst_mappings
;
1708 for (auto &sub
: adt
->get_substs ())
1710 SubstitutionArg arg
= SubstitutionArg::error ();
1712 = subst_mappings
.get_argument_for_symbol (sub
.get_param_ty (), &arg
);
1714 sub
.fill_param_ty (subst_mappings
, subst_mappings
.get_locus ());
1717 for (auto &variant
: adt
->get_variants ())
1719 if (variant
->is_dataless_variant ())
1722 for (auto &field
: variant
->get_fields ())
1724 bool ok
= ::Rust::TyTy::handle_substitions (subst_mappings
, field
);
1735 TupleType::TupleType (HirId ref
, location_t locus
, std::vector
<TyVar
> fields
,
1736 std::set
<HirId
> refs
)
1737 : BaseType (ref
, ref
, KIND
, {Resolver::CanonicalPath::create_empty (), locus
},
1742 TupleType::TupleType (HirId ref
, HirId ty_ref
, location_t locus
,
1743 std::vector
<TyVar
> fields
, std::set
<HirId
> refs
)
1744 : BaseType (ref
, ty_ref
, KIND
,
1745 {Resolver::CanonicalPath::create_empty (), locus
}, refs
),
1750 TupleType::get_unit_type (HirId ref
)
1752 return new TupleType (ref
, BUILTINS_LOCATION
);
1756 TupleType::num_fields () const
1758 return fields
.size ();
1761 const std::vector
<TyVar
> &
1762 TupleType::get_fields () const
1768 TupleType::accept_vis (TyVisitor
&vis
)
1774 TupleType::accept_vis (TyConstVisitor
&vis
) const
1780 TupleType::as_string () const
1783 std::string fields_buffer
;
1784 for (const TyVar
&field
: get_fields ())
1786 fields_buffer
+= field
.get_tyty ()->as_string ();
1787 bool has_next
= (i
+ 1) < get_fields ().size ();
1788 fields_buffer
+= has_next
? ", " : "";
1791 return "(" + fields_buffer
+ ")";
1795 TupleType::get_name () const
1798 std::string fields_buffer
;
1799 for (const TyVar
&field
: get_fields ())
1801 fields_buffer
+= field
.get_tyty ()->as_string ();
1802 bool has_next
= (i
+ 1) < get_fields ().size ();
1803 fields_buffer
+= has_next
? ", " : "";
1806 return "(" + fields_buffer
+ ")";
1810 TupleType::get_field (size_t index
) const
1812 return fields
.at (index
).get_tyty ();
1816 TupleType::can_eq (const BaseType
*other
, bool emit_errors
) const
1818 TupleCmp
r (this, emit_errors
);
1819 return r
.can_eq (other
);
1823 TupleType::is_equal (const BaseType
&other
) const
1825 if (get_kind () != other
.get_kind ())
1828 auto other2
= other
.as
<const TupleType
> ();
1829 if (num_fields () != other2
->num_fields ())
1832 for (size_t i
= 0; i
< num_fields (); i
++)
1834 if (!get_field (i
)->is_equal (*other2
->get_field (i
)))
1841 TupleType::clone () const
1843 std::vector
<TyVar
> cloned_fields
;
1844 for (const auto &f
: fields
)
1845 cloned_fields
.push_back (f
.clone ());
1847 return new TupleType (get_ref (), get_ty_ref (), get_ident ().locus
,
1848 cloned_fields
, get_combined_refs ());
1852 TupleType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
1854 auto mappings_table
= Analysis::Mappings::get ();
1856 auto tuple
= clone ()->as
<TupleType
> ();
1857 tuple
->set_ref (mappings_table
->get_next_hir_id ());
1858 tuple
->set_ty_ref (mappings_table
->get_next_hir_id ());
1860 for (size_t i
= 0; i
< tuple
->fields
.size (); i
++)
1862 TyVar
&field
= fields
.at (i
);
1863 if (!field
.get_tyty ()->is_concrete ())
1866 = Resolver::SubstMapperInternal::Resolve (field
.get_tyty (),
1869 = TyVar::subst_covariant_var (field
.get_tyty (), concrete
);
1877 FnType::accept_vis (TyVisitor
&vis
)
1883 FnType::accept_vis (TyConstVisitor
&vis
) const
1889 FnType::as_string () const
1891 std::string params_str
= "";
1892 for (auto ¶m
: params
)
1894 auto pattern
= param
.first
;
1895 auto ty
= param
.second
;
1896 params_str
+= pattern
->as_string () + " " + ty
->as_string ();
1900 std::string ret_str
= type
->as_string ();
1901 return "fn" + subst_as_string () + " (" + params_str
+ ") -> " + ret_str
;
1905 FnType::can_eq (const BaseType
*other
, bool emit_errors
) const
1907 FnCmp
r (this, emit_errors
);
1908 return r
.can_eq (other
);
1912 FnType::is_equal (const BaseType
&other
) const
1914 if (get_kind () != other
.get_kind ())
1917 auto other2
= static_cast<const FnType
&> (other
);
1918 if (get_identifier ().compare (other2
.get_identifier ()) != 0)
1921 if (!get_return_type ()->is_equal (*other2
.get_return_type ()))
1924 if (has_substitutions_defined () != other2
.has_substitutions_defined ())
1927 if (has_substitutions_defined ())
1929 if (get_num_substitutions () != other2
.get_num_substitutions ())
1932 const FnType
&ofn
= static_cast<const FnType
&> (other
);
1933 for (size_t i
= 0; i
< get_num_substitutions (); i
++)
1935 const SubstitutionParamMapping
&a
= get_substs ().at (i
);
1936 const SubstitutionParamMapping
&b
= ofn
.get_substs ().at (i
);
1938 const ParamType
*pa
= a
.get_param_ty ();
1939 const ParamType
*pb
= b
.get_param_ty ();
1941 if (!pa
->is_equal (*pb
))
1946 if (num_params () != other2
.num_params ())
1949 for (size_t i
= 0; i
< num_params (); i
++)
1951 auto lhs
= param_at (i
).second
;
1952 auto rhs
= other2
.param_at (i
).second
;
1953 if (!lhs
->is_equal (*rhs
))
1960 FnType::clone () const
1962 std::vector
<std::pair
<HIR::Pattern
*, BaseType
*>> cloned_params
;
1963 for (auto &p
: params
)
1964 cloned_params
.push_back ({p
.first
, p
.second
->clone ()});
1966 return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (),
1967 ident
, flags
, abi
, std::move (cloned_params
),
1968 get_return_type ()->clone (), clone_substs (),
1969 get_substitution_arguments (), get_region_constraints (),
1970 get_combined_refs ());
1974 FnType::handle_substitions (SubstitutionArgumentMappings
&subst_mappings
)
1976 FnType
*fn
= static_cast<FnType
*> (clone ());
1977 fn
->set_ty_ref (mappings
->get_next_hir_id ());
1978 fn
->used_arguments
= subst_mappings
;
1980 for (auto &sub
: fn
->get_substs ())
1982 SubstitutionArg arg
= SubstitutionArg::error ();
1985 = subst_mappings
.get_argument_for_symbol (sub
.get_param_ty (), &arg
);
1988 sub
.fill_param_ty (subst_mappings
, subst_mappings
.get_locus ());
1992 auto fty
= fn
->get_return_type ();
1993 bool is_param_ty
= fty
->get_kind () == TypeKind::PARAM
;
1996 ParamType
*p
= static_cast<ParamType
*> (fty
);
1998 SubstitutionArg arg
= SubstitutionArg::error ();
1999 bool ok
= subst_mappings
.get_argument_for_symbol (p
, &arg
);
2002 auto argt
= arg
.get_tyty ();
2003 bool arg_is_param
= argt
->get_kind () == TyTy::TypeKind::PARAM
;
2004 bool arg_is_concrete
= argt
->get_kind () != TyTy::TypeKind::INFER
;
2006 if (arg_is_param
|| arg_is_concrete
)
2008 auto new_field
= argt
->clone ();
2009 new_field
->set_ref (fty
->get_ref ());
2010 fn
->type
= new_field
;
2014 fty
->set_ty_ref (argt
->get_ref ());
2018 else if (fty
->needs_generic_substitutions () || !fty
->is_concrete ())
2021 = Resolver::SubstMapperInternal::Resolve (fty
, subst_mappings
);
2023 if (concrete
== nullptr || concrete
->get_kind () == TyTy::TypeKind::ERROR
)
2025 rust_error_at (subst_mappings
.get_locus (),
2026 "Failed to resolve field substitution type: %s",
2027 fty
->as_string ().c_str ());
2031 auto new_field
= concrete
->clone ();
2032 new_field
->set_ref (fty
->get_ref ());
2033 fn
->type
= new_field
;
2036 for (auto ¶m
: fn
->get_params ())
2038 auto fty
= param
.second
;
2040 bool is_param_ty
= fty
->get_kind () == TypeKind::PARAM
;
2043 ParamType
*p
= static_cast<ParamType
*> (fty
);
2045 SubstitutionArg arg
= SubstitutionArg::error ();
2046 bool ok
= subst_mappings
.get_argument_for_symbol (p
, &arg
);
2049 auto argt
= arg
.get_tyty ();
2050 bool arg_is_param
= argt
->get_kind () == TyTy::TypeKind::PARAM
;
2051 bool arg_is_concrete
= argt
->get_kind () != TyTy::TypeKind::INFER
;
2053 if (arg_is_param
|| arg_is_concrete
)
2055 auto new_field
= argt
->clone ();
2056 new_field
->set_ref (fty
->get_ref ());
2057 param
.second
= new_field
;
2061 fty
->set_ty_ref (argt
->get_ref ());
2065 else if (fty
->has_substitutions_defined () || !fty
->is_concrete ())
2068 = Resolver::SubstMapperInternal::Resolve (fty
, subst_mappings
);
2070 if (concrete
== nullptr
2071 || concrete
->get_kind () == TyTy::TypeKind::ERROR
)
2073 rust_error_at (subst_mappings
.get_locus (),
2074 "Failed to resolve field substitution type: %s",
2075 fty
->as_string ().c_str ());
2079 auto new_field
= concrete
->clone ();
2080 new_field
->set_ref (fty
->get_ref ());
2081 param
.second
= new_field
;
2089 FnPtr::accept_vis (TyVisitor
&vis
)
2095 FnPtr::accept_vis (TyConstVisitor
&vis
) const
2101 FnPtr::as_string () const
2103 std::string params_str
;
2105 auto ¶ms
= get_params ();
2106 for (auto &p
: params
)
2108 params_str
+= p
.get_tyty ()->as_string () + " ,";
2111 return "fnptr (" + params_str
+ ") -> " + get_return_type ()->as_string ();
2115 FnPtr::can_eq (const BaseType
*other
, bool emit_errors
) const
2117 FnptrCmp
r (this, emit_errors
);
2118 return r
.can_eq (other
);
2122 FnPtr::is_equal (const BaseType
&other
) const
2124 if (get_kind () != other
.get_kind ())
2127 auto other2
= static_cast<const FnPtr
&> (other
);
2128 auto this_ret_type
= get_return_type ();
2129 auto other_ret_type
= other2
.get_return_type ();
2130 if (this_ret_type
->is_equal (*other_ret_type
))
2133 if (num_params () != other2
.num_params ())
2136 for (size_t i
= 0; i
< num_params (); i
++)
2138 if (!get_param_type_at (i
)->is_equal (*other2
.get_param_type_at (i
)))
2145 FnPtr::clone () const
2147 std::vector
<TyVar
> cloned_params
;
2148 for (auto &p
: params
)
2149 cloned_params
.push_back (TyVar (p
.get_ref ()));
2151 return new FnPtr (get_ref (), get_ty_ref (), ident
.locus
,
2152 std::move (cloned_params
), result_type
,
2153 get_combined_refs ());
2157 ClosureType::accept_vis (TyVisitor
&vis
)
2163 ClosureType::accept_vis (TyConstVisitor
&vis
) const
2169 ClosureType::as_string () const
2171 std::string params_buf
= parameters
->as_string ();
2172 return "|" + params_buf
+ "| {" + result_type
.get_tyty ()->as_string () + "}";
2176 ClosureType::can_eq (const BaseType
*other
, bool emit_errors
) const
2178 ClosureCmp
r (this, emit_errors
);
2179 return r
.can_eq (other
);
2183 ClosureType::is_equal (const BaseType
&other
) const
2185 if (other
.get_kind () != TypeKind::CLOSURE
)
2188 const ClosureType
&other2
= static_cast<const ClosureType
&> (other
);
2189 if (get_def_id () != other2
.get_def_id ())
2192 if (!get_parameters ().is_equal (other2
.get_parameters ()))
2195 return get_result_type ().is_equal (other2
.get_result_type ());
2199 ClosureType::clone () const
2201 return new ClosureType (get_ref (), get_ty_ref (), ident
, id
,
2202 (TyTy::TupleType
*) parameters
->clone (), result_type
,
2203 clone_substs (), captures
, get_combined_refs (),
2208 ClosureType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
2210 rust_unreachable ();
2215 ClosureType::setup_fn_once_output () const
2217 // lookup the lang items
2218 auto fn_once_lang_item
= LangItem::Kind::FN_ONCE
;
2219 auto fn_once_output_lang_item
= LangItem::Kind::FN_ONCE_OUTPUT
;
2221 DefId trait_id
= UNKNOWN_DEFID
;
2222 bool trait_lang_item_defined
2223 = mappings
->lookup_lang_item (fn_once_lang_item
, &trait_id
);
2224 rust_assert (trait_lang_item_defined
);
2226 DefId trait_item_id
= UNKNOWN_DEFID
;
2227 bool trait_item_lang_item_defined
2228 = mappings
->lookup_lang_item (fn_once_output_lang_item
, &trait_item_id
);
2229 rust_assert (trait_item_lang_item_defined
);
2231 // resolve to the trait
2232 HIR::Item
*item
= mappings
->lookup_defid (trait_id
);
2233 rust_assert (item
->get_item_kind () == HIR::Item::ItemKind::Trait
);
2234 HIR::Trait
*trait
= static_cast<HIR::Trait
*> (item
);
2236 Resolver::TraitReference
*trait_ref
2237 = Resolver::TraitResolver::Resolve (*trait
);
2238 rust_assert (!trait_ref
->is_error ());
2240 // resolve to trait item
2241 HIR::TraitItem
*trait_item
2242 = mappings
->lookup_trait_item_defid (trait_item_id
);
2243 rust_assert (trait_item
!= nullptr);
2244 rust_assert (trait_item
->get_item_kind ()
2245 == HIR::TraitItem::TraitItemKind::TYPE
);
2246 std::string item_identifier
= trait_item
->trait_identifier ();
2248 // setup associated types #[lang = "fn_once_output"]
2249 Resolver::TraitItemReference
*item_reference
= nullptr;
2250 bool found
= trait_ref
->lookup_trait_item_by_type (
2251 item_identifier
, Resolver::TraitItemReference::TraitItemType::TYPE
,
2253 rust_assert (found
);
2256 item_reference
->associated_type_set (&get_result_type ());
2260 ArrayType::accept_vis (TyVisitor
&vis
)
2266 ArrayType::accept_vis (TyConstVisitor
&vis
) const
2272 ArrayType::as_string () const
2274 return "[" + get_element_type ()->as_string () + ":" + "CAPACITY" + "]";
2278 ArrayType::can_eq (const BaseType
*other
, bool emit_errors
) const
2280 ArrayCmp
r (this, emit_errors
);
2281 return r
.can_eq (other
);
2285 ArrayType::is_equal (const BaseType
&other
) const
2287 if (get_kind () != other
.get_kind ())
2290 auto other2
= static_cast<const ArrayType
&> (other
);
2292 auto this_element_type
= get_element_type ();
2293 auto other_element_type
= other2
.get_element_type ();
2295 return this_element_type
->is_equal (*other_element_type
);
2299 ArrayType::get_element_type () const
2301 return element_type
.get_tyty ();
2305 ArrayType::get_var_element_type () const
2307 return element_type
;
2311 ArrayType::clone () const
2313 return new ArrayType (get_ref (), get_ty_ref (), ident
.locus
, capacity_expr
,
2314 element_type
, get_combined_refs ());
2318 ArrayType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
2320 auto mappings_table
= Analysis::Mappings::get ();
2322 ArrayType
*ref
= static_cast<ArrayType
*> (clone ());
2323 ref
->set_ty_ref (mappings_table
->get_next_hir_id ());
2325 // might be &T or &ADT so this needs to be recursive
2326 auto base
= ref
->get_element_type ();
2327 BaseType
*concrete
= Resolver::SubstMapperInternal::Resolve (base
, mappings
);
2328 ref
->element_type
= TyVar::subst_covariant_var (base
, concrete
);
2334 SliceType::accept_vis (TyVisitor
&vis
)
2340 SliceType::accept_vis (TyConstVisitor
&vis
) const
2346 SliceType::as_string () const
2348 return "[" + get_element_type ()->as_string () + "]";
2352 SliceType::can_eq (const BaseType
*other
, bool emit_errors
) const
2354 SliceCmp
r (this, emit_errors
);
2355 return r
.can_eq (other
);
2359 SliceType::is_equal (const BaseType
&other
) const
2361 if (get_kind () != other
.get_kind ())
2364 auto other2
= static_cast<const SliceType
&> (other
);
2366 auto this_element_type
= get_element_type ();
2367 auto other_element_type
= other2
.get_element_type ();
2369 return this_element_type
->is_equal (*other_element_type
);
2373 SliceType::get_element_type () const
2375 return element_type
.get_tyty ();
2379 SliceType::get_var_element_type () const
2381 return element_type
;
2385 SliceType::clone () const
2387 return new SliceType (get_ref (), get_ty_ref (), ident
.locus
,
2388 element_type
.clone (), get_combined_refs ());
2392 SliceType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
2394 auto mappings_table
= Analysis::Mappings::get ();
2396 SliceType
*ref
= static_cast<SliceType
*> (clone ());
2397 ref
->set_ty_ref (mappings_table
->get_next_hir_id ());
2399 // might be &T or &ADT so this needs to be recursive
2400 auto base
= ref
->get_element_type ();
2401 BaseType
*concrete
= Resolver::SubstMapperInternal::Resolve (base
, mappings
);
2402 ref
->element_type
= TyVar::subst_covariant_var (base
, concrete
);
2409 BoolType::BoolType (HirId ref
, std::set
<HirId
> refs
)
2410 : BaseType (ref
, ref
, KIND
,
2411 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2415 BoolType::BoolType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
2416 : BaseType (ref
, ty_ref
, KIND
,
2417 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2422 BoolType::get_name () const
2424 return as_string ();
2428 BoolType::accept_vis (TyVisitor
&vis
)
2434 BoolType::accept_vis (TyConstVisitor
&vis
) const
2440 BoolType::as_string () const
2446 BoolType::can_eq (const BaseType
*other
, bool emit_errors
) const
2448 BoolCmp
r (this, emit_errors
);
2449 return r
.can_eq (other
);
2453 BoolType::clone () const
2455 return new BoolType (get_ref (), get_ty_ref (), get_combined_refs ());
2460 IntType::IntType (HirId ref
, IntKind kind
, std::set
<HirId
> refs
)
2461 : BaseType (ref
, ref
, KIND
,
2462 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2467 IntType::IntType (HirId ref
, HirId ty_ref
, IntKind kind
, std::set
<HirId
> refs
)
2468 : BaseType (ref
, ty_ref
, KIND
,
2469 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2475 IntType::get_name () const
2477 return as_string ();
2481 IntType::get_int_kind () const
2487 IntType::accept_vis (TyVisitor
&vis
)
2493 IntType::accept_vis (TyConstVisitor
&vis
) const
2499 IntType::as_string () const
2514 rust_unreachable ();
2515 return "__unknown_int_type";
2519 IntType::can_eq (const BaseType
*other
, bool emit_errors
) const
2521 IntCmp
r (this, emit_errors
);
2522 return r
.can_eq (other
);
2526 IntType::clone () const
2528 return new IntType (get_ref (), get_ty_ref (), get_int_kind (),
2529 get_combined_refs ());
2533 IntType::is_equal (const BaseType
&other
) const
2535 if (!BaseType::is_equal (other
))
2538 const IntType
&o
= static_cast<const IntType
&> (other
);
2539 return get_int_kind () == o
.get_int_kind ();
2544 UintType::UintType (HirId ref
, UintKind kind
, std::set
<HirId
> refs
)
2545 : BaseType (ref
, ref
, KIND
,
2546 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2551 UintType::UintType (HirId ref
, HirId ty_ref
, UintKind kind
,
2552 std::set
<HirId
> refs
)
2553 : BaseType (ref
, ty_ref
, KIND
,
2554 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2560 UintType::get_name () const
2562 return as_string ();
2566 UintType::get_uint_kind () const
2572 UintType::accept_vis (TyVisitor
&vis
)
2578 UintType::accept_vis (TyConstVisitor
&vis
) const
2584 UintType::as_string () const
2599 rust_unreachable ();
2600 return "__unknown_uint_type";
2604 UintType::can_eq (const BaseType
*other
, bool emit_errors
) const
2606 UintCmp
r (this, emit_errors
);
2607 return r
.can_eq (other
);
2611 UintType::clone () const
2613 return new UintType (get_ref (), get_ty_ref (), get_uint_kind (),
2614 get_combined_refs ());
2618 UintType::is_equal (const BaseType
&other
) const
2620 if (!BaseType::is_equal (other
))
2623 const UintType
&o
= static_cast<const UintType
&> (other
);
2624 return get_uint_kind () == o
.get_uint_kind ();
2629 FloatType::FloatType (HirId ref
, FloatKind kind
, std::set
<HirId
> refs
)
2630 : BaseType (ref
, ref
, KIND
,
2631 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2636 FloatType::FloatType (HirId ref
, HirId ty_ref
, FloatKind kind
,
2637 std::set
<HirId
> refs
)
2638 : BaseType (ref
, ty_ref
, KIND
,
2639 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2645 FloatType::get_name () const
2647 return as_string ();
2650 FloatType::FloatKind
2651 FloatType::get_float_kind () const
2657 FloatType::accept_vis (TyVisitor
&vis
)
2663 FloatType::accept_vis (TyConstVisitor
&vis
) const
2669 FloatType::as_string () const
2678 rust_unreachable ();
2679 return "__unknown_float_type";
2683 FloatType::can_eq (const BaseType
*other
, bool emit_errors
) const
2685 FloatCmp
r (this, emit_errors
);
2686 return r
.can_eq (other
);
2690 FloatType::clone () const
2692 return new FloatType (get_ref (), get_ty_ref (), get_float_kind (),
2693 get_combined_refs ());
2697 FloatType::is_equal (const BaseType
&other
) const
2699 if (!BaseType::is_equal (other
))
2702 const FloatType
&o
= static_cast<const FloatType
&> (other
);
2703 return get_float_kind () == o
.get_float_kind ();
2708 USizeType::USizeType (HirId ref
, std::set
<HirId
> refs
)
2709 : BaseType (ref
, ref
, KIND
,
2710 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2714 USizeType::USizeType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
2715 : BaseType (ref
, ty_ref
, KIND
,
2716 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2721 USizeType::get_name () const
2723 return as_string ();
2727 USizeType::accept_vis (TyVisitor
&vis
)
2733 USizeType::accept_vis (TyConstVisitor
&vis
) const
2739 USizeType::as_string () const
2745 USizeType::can_eq (const BaseType
*other
, bool emit_errors
) const
2747 USizeCmp
r (this, emit_errors
);
2748 return r
.can_eq (other
);
2752 USizeType::clone () const
2754 return new USizeType (get_ref (), get_ty_ref (), get_combined_refs ());
2759 ISizeType::ISizeType (HirId ref
, std::set
<HirId
> refs
)
2760 : BaseType (ref
, ref
, KIND
,
2761 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2765 ISizeType::ISizeType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
2766 : BaseType (ref
, ty_ref
, KIND
,
2767 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2772 ISizeType::get_name () const
2774 return as_string ();
2778 ISizeType::accept_vis (TyVisitor
&vis
)
2784 ISizeType::accept_vis (TyConstVisitor
&vis
) const
2790 ISizeType::as_string () const
2796 ISizeType::can_eq (const BaseType
*other
, bool emit_errors
) const
2798 ISizeCmp
r (this, emit_errors
);
2799 return r
.can_eq (other
);
2803 ISizeType::clone () const
2805 return new ISizeType (get_ref (), get_ty_ref (), get_combined_refs ());
2810 CharType::CharType (HirId ref
, std::set
<HirId
> refs
)
2811 : BaseType (ref
, ref
, KIND
,
2812 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2816 CharType::CharType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
2817 : BaseType (ref
, ty_ref
, KIND
,
2818 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2823 CharType::get_name () const
2825 return as_string ();
2829 CharType::accept_vis (TyVisitor
&vis
)
2835 CharType::accept_vis (TyConstVisitor
&vis
) const
2841 CharType::as_string () const
2847 CharType::can_eq (const BaseType
*other
, bool emit_errors
) const
2849 CharCmp
r (this, emit_errors
);
2850 return r
.can_eq (other
);
2854 CharType::clone () const
2856 return new CharType (get_ref (), get_ty_ref (), get_combined_refs ());
2861 ReferenceType::ReferenceType (HirId ref
, TyVar base
, Mutability mut
,
2862 Region region
, std::set
<HirId
> refs
)
2863 : BaseType (ref
, ref
, KIND
,
2864 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2866 base (base
), mut (mut
), region (region
)
2869 ReferenceType::ReferenceType (HirId ref
, HirId ty_ref
, TyVar base
,
2870 Mutability mut
, Region region
,
2871 std::set
<HirId
> refs
)
2872 : BaseType (ref
, ty_ref
, KIND
,
2873 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
2875 base (base
), mut (mut
), region (region
)
2879 ReferenceType::mutability () const
2885 ReferenceType::is_mutable () const
2887 return mut
== Mutability::Mut
;
2890 ReferenceType::get_region () const
2896 ReferenceType::is_dyn_object () const
2898 return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
2902 ReferenceType::is_dyn_slice_type (const TyTy::SliceType
**slice
) const
2904 const TyTy::BaseType
*element
= get_base ()->destructure ();
2905 if (element
->get_kind () != TyTy::TypeKind::SLICE
)
2907 if (slice
== nullptr)
2910 *slice
= static_cast<const TyTy::SliceType
*> (element
);
2915 ReferenceType::is_dyn_str_type (const TyTy::StrType
**str
) const
2917 const TyTy::BaseType
*element
= get_base ()->destructure ();
2918 if (element
->get_kind () != TyTy::TypeKind::STR
)
2923 *str
= static_cast<const TyTy::StrType
*> (element
);
2928 ReferenceType::is_dyn_obj_type (const TyTy::DynamicObjectType
**dyn
) const
2930 const TyTy::BaseType
*element
= get_base ()->destructure ();
2931 if (element
->get_kind () != TyTy::TypeKind::DYNAMIC
)
2936 *dyn
= static_cast<const TyTy::DynamicObjectType
*> (element
);
2941 ReferenceType::accept_vis (TyVisitor
&vis
)
2947 ReferenceType::accept_vis (TyConstVisitor
&vis
) const
2953 ReferenceType::as_string () const
2955 return std::string ("&") + (is_mutable () ? "mut" : "") + " "
2956 + get_base ()->as_string ();
2960 ReferenceType::get_name () const
2962 return std::string ("&") + (is_mutable () ? "mut" : "") + " "
2963 + get_base ()->get_name ();
2967 ReferenceType::can_eq (const BaseType
*other
, bool emit_errors
) const
2969 ReferenceCmp
r (this, emit_errors
);
2970 return r
.can_eq (other
);
2974 ReferenceType::is_equal (const BaseType
&other
) const
2976 if (get_kind () != other
.get_kind ())
2979 auto other2
= static_cast<const ReferenceType
&> (other
);
2980 if (mutability () != other2
.mutability ())
2983 return get_base ()->is_equal (*other2
.get_base ());
2987 ReferenceType::get_base () const
2989 return base
.get_tyty ();
2993 ReferenceType::get_var_element_type () const
2999 ReferenceType::clone () const
3001 return new ReferenceType (get_ref (), get_ty_ref (), base
, mutability (),
3002 get_region (), get_combined_refs ());
3006 ReferenceType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
3008 auto mappings_table
= Analysis::Mappings::get ();
3010 ReferenceType
*ref
= static_cast<ReferenceType
*> (clone ());
3011 ref
->set_ty_ref (mappings_table
->get_next_hir_id ());
3013 // might be &T or &ADT so this needs to be recursive
3014 auto base
= ref
->get_base ();
3015 BaseType
*concrete
= Resolver::SubstMapperInternal::Resolve (base
, mappings
);
3016 ref
->base
= TyVar::subst_covariant_var (base
, concrete
);
3023 PointerType::PointerType (HirId ref
, TyVar base
, Mutability mut
,
3024 std::set
<HirId
> refs
)
3025 : BaseType (ref
, ref
, KIND
,
3026 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3028 base (base
), mut (mut
)
3031 PointerType::PointerType (HirId ref
, HirId ty_ref
, TyVar base
, Mutability mut
,
3032 std::set
<HirId
> refs
)
3033 : BaseType (ref
, ty_ref
, KIND
,
3034 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3036 base (base
), mut (mut
)
3040 PointerType::mutability () const
3046 PointerType::is_mutable () const
3048 return mut
== Mutability::Mut
;
3052 PointerType::is_const () const
3054 return mut
== Mutability::Imm
;
3058 PointerType::is_dyn_object () const
3060 return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
3064 PointerType::is_dyn_slice_type (const TyTy::SliceType
**slice
) const
3066 const TyTy::BaseType
*element
= get_base ()->destructure ();
3067 if (element
->get_kind () != TyTy::TypeKind::SLICE
)
3069 if (slice
== nullptr)
3072 *slice
= static_cast<const TyTy::SliceType
*> (element
);
3077 PointerType::is_dyn_str_type (const TyTy::StrType
**str
) const
3079 const TyTy::BaseType
*element
= get_base ()->destructure ();
3080 if (element
->get_kind () != TyTy::TypeKind::STR
)
3085 *str
= static_cast<const TyTy::StrType
*> (element
);
3090 PointerType::is_dyn_obj_type (const TyTy::DynamicObjectType
**dyn
) const
3092 const TyTy::BaseType
*element
= get_base ()->destructure ();
3093 if (element
->get_kind () != TyTy::TypeKind::DYNAMIC
)
3098 *dyn
= static_cast<const TyTy::DynamicObjectType
*> (element
);
3103 PointerType::accept_vis (TyVisitor
&vis
)
3109 PointerType::accept_vis (TyConstVisitor
&vis
) const
3115 PointerType::as_string () const
3117 return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
3118 + get_base ()->as_string ();
3122 PointerType::get_name () const
3124 return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
3125 + get_base ()->get_name ();
3129 PointerType::can_eq (const BaseType
*other
, bool emit_errors
) const
3131 PointerCmp
r (this, emit_errors
);
3132 return r
.can_eq (other
);
3136 PointerType::is_equal (const BaseType
&other
) const
3138 if (get_kind () != other
.get_kind ())
3141 auto other2
= static_cast<const PointerType
&> (other
);
3142 if (mutability () != other2
.mutability ())
3145 return get_base ()->is_equal (*other2
.get_base ());
3149 PointerType::get_base () const
3151 return base
.get_tyty ();
3155 PointerType::get_var_element_type () const
3161 PointerType::clone () const
3163 return new PointerType (get_ref (), get_ty_ref (), base
, mutability (),
3164 get_combined_refs ());
3168 PointerType::handle_substitions (SubstitutionArgumentMappings
&mappings
)
3170 auto mappings_table
= Analysis::Mappings::get ();
3172 PointerType
*ref
= static_cast<PointerType
*> (clone ());
3173 ref
->set_ty_ref (mappings_table
->get_next_hir_id ());
3175 // might be &T or &ADT so this needs to be recursive
3176 auto base
= ref
->get_base ();
3177 BaseType
*concrete
= Resolver::SubstMapperInternal::Resolve (base
, mappings
);
3178 ref
->base
= TyVar::subst_covariant_var (base
, concrete
);
3185 ParamType::ParamType (std::string symbol
, location_t locus
, HirId ref
,
3186 HIR::GenericParam
¶m
,
3187 std::vector
<TypeBoundPredicate
> specified_bounds
,
3188 std::set
<HirId
> refs
)
3189 : BaseType (ref
, ref
, KIND
,
3190 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID
, symbol
),
3192 specified_bounds
, refs
),
3193 is_trait_self (false), symbol (symbol
), param (param
)
3196 ParamType::ParamType (bool is_trait_self
, std::string symbol
, location_t locus
,
3197 HirId ref
, HirId ty_ref
, HIR::GenericParam
¶m
,
3198 std::vector
<TypeBoundPredicate
> specified_bounds
,
3199 std::set
<HirId
> refs
)
3200 : BaseType (ref
, ty_ref
, KIND
,
3201 {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID
, symbol
),
3203 specified_bounds
, refs
),
3204 is_trait_self (is_trait_self
), symbol (symbol
), param (param
)
3208 ParamType::get_generic_param ()
3214 ParamType::can_resolve () const
3216 return get_ref () != get_ty_ref ();
3220 ParamType::accept_vis (TyVisitor
&vis
)
3226 ParamType::accept_vis (TyConstVisitor
&vis
) const
3232 ParamType::as_string () const
3234 if (!can_resolve ())
3236 return get_symbol () + " REF: " + std::to_string (get_ref ());
3239 BaseType
*lookup
= resolve ();
3240 return get_symbol () + "=" + lookup
->as_string ();
3244 ParamType::get_name () const
3246 if (!can_resolve ())
3247 return get_symbol ();
3249 return destructure ()->get_name ();
3253 ParamType::can_eq (const BaseType
*other
, bool emit_errors
) const
3255 ParamCmp
r (this, emit_errors
);
3256 return r
.can_eq (other
);
3260 ParamType::clone () const
3262 return new ParamType (is_trait_self
, get_symbol (), ident
.locus
, get_ref (),
3263 get_ty_ref (), param
, get_specified_bounds (),
3264 get_combined_refs ());
3268 ParamType::get_symbol () const
3274 ParamType::resolve () const
3276 TyVar
var (get_ty_ref ());
3277 BaseType
*r
= var
.get_tyty ();
3279 while (r
->get_kind () == TypeKind::PARAM
)
3281 ParamType
*rr
= static_cast<ParamType
*> (r
);
3282 if (!rr
->can_resolve ())
3285 TyVar
v (rr
->get_ty_ref ());
3286 BaseType
*n
= v
.get_tyty ();
3288 // fix infinite loop
3295 if (r
->get_kind () == TypeKind::PARAM
&& (r
->get_ref () == r
->get_ty_ref ()))
3296 return TyVar (r
->get_ty_ref ()).get_tyty ();
3302 ParamType::is_equal (const BaseType
&other
) const
3304 if (get_kind () != other
.get_kind ())
3306 if (!can_resolve ())
3309 return resolve ()->is_equal (other
);
3312 auto other2
= static_cast<const ParamType
&> (other
);
3313 if (can_resolve () != other2
.can_resolve ())
3317 return resolve ()->can_eq (other2
.resolve (), false);
3319 return get_symbol ().compare (other2
.get_symbol ()) == 0;
3323 ParamType::handle_substitions (SubstitutionArgumentMappings
&subst_mappings
)
3325 SubstitutionArg arg
= SubstitutionArg::error ();
3326 bool ok
= subst_mappings
.get_argument_for_symbol (this, &arg
);
3327 if (!ok
|| arg
.is_error ())
3330 ParamType
*p
= static_cast<ParamType
*> (clone ());
3331 subst_mappings
.on_param_subst (*p
, arg
);
3333 // there are two cases one where we substitute directly to a new PARAM and
3335 if (arg
.get_tyty ()->get_kind () == TyTy::TypeKind::PARAM
)
3337 p
->set_ty_ref (arg
.get_tyty ()->get_ref ());
3341 // this is the new subst that this needs to pass
3342 p
->set_ref (mappings
->get_next_hir_id ());
3343 p
->set_ty_ref (arg
.get_tyty ()->get_ref ());
3349 ParamType::set_implicit_self_trait ()
3351 is_trait_self
= true;
3355 ParamType::is_implicit_self_trait () const
3357 return is_trait_self
;
3362 StrType::StrType (HirId ref
, std::set
<HirId
> refs
)
3363 : BaseType (ref
, ref
, KIND
,
3364 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3368 StrType::StrType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
3369 : BaseType (ref
, ty_ref
, KIND
,
3370 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3375 StrType::get_name () const
3377 return as_string ();
3381 StrType::clone () const
3383 return new StrType (get_ref (), get_ty_ref (), get_combined_refs ());
3387 StrType::accept_vis (TyVisitor
&vis
)
3393 StrType::accept_vis (TyConstVisitor
&vis
) const
3399 StrType::as_string () const
3405 StrType::can_eq (const BaseType
*other
, bool emit_errors
) const
3407 StrCmp
r (this, emit_errors
);
3408 return r
.can_eq (other
);
3412 StrType::is_equal (const BaseType
&other
) const
3414 return get_kind () == other
.get_kind ();
3419 NeverType::NeverType (HirId ref
, std::set
<HirId
> refs
)
3420 : BaseType (ref
, ref
, KIND
,
3421 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3425 NeverType::NeverType (HirId ref
, HirId ty_ref
, std::set
<HirId
> refs
)
3426 : BaseType (ref
, ty_ref
, KIND
,
3427 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3432 NeverType::get_name () const
3434 return as_string ();
3438 NeverType::accept_vis (TyVisitor
&vis
)
3444 NeverType::accept_vis (TyConstVisitor
&vis
) const
3450 NeverType::as_string () const
3456 NeverType::can_eq (const BaseType
*other
, bool emit_errors
) const
3458 NeverCmp
r (this, emit_errors
);
3459 return r
.can_eq (other
);
3463 NeverType::clone () const
3465 return new NeverType (get_ref (), get_ty_ref (), get_combined_refs ());
3470 PlaceholderType::PlaceholderType (std::string symbol
, HirId ref
,
3471 std::set
<HirId
> refs
)
3472 : BaseType (ref
, ref
, KIND
,
3473 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3478 PlaceholderType::PlaceholderType (std::string symbol
, HirId ref
, HirId ty_ref
,
3479 std::set
<HirId
> refs
)
3480 : BaseType (ref
, ty_ref
, KIND
,
3481 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3487 PlaceholderType::get_name () const
3489 return as_string ();
3493 PlaceholderType::get_symbol () const
3499 PlaceholderType::accept_vis (TyVisitor
&vis
)
3505 PlaceholderType::accept_vis (TyConstVisitor
&vis
) const
3511 PlaceholderType::as_string () const
3513 return "<placeholder:" + (can_resolve () ? resolve ()->as_string () : "")
3518 PlaceholderType::can_eq (const BaseType
*other
, bool emit_errors
) const
3520 PlaceholderCmp
r (this, emit_errors
);
3521 return r
.can_eq (other
);
3525 PlaceholderType::clone () const
3527 return new PlaceholderType (get_symbol (), get_ref (), get_ty_ref (),
3528 get_combined_refs ());
3532 PlaceholderType::set_associated_type (HirId ref
)
3534 auto context
= Resolver::TypeCheckContext::get ();
3535 context
->insert_associated_type_mapping (get_ty_ref (), ref
);
3539 PlaceholderType::clear_associated_type ()
3541 auto context
= Resolver::TypeCheckContext::get ();
3542 context
->clear_associated_type_mapping (get_ty_ref ());
3546 PlaceholderType::can_resolve () const
3548 auto context
= Resolver::TypeCheckContext::get ();
3549 return context
->lookup_associated_type_mapping (get_ty_ref (), nullptr);
3553 PlaceholderType::resolve () const
3555 auto context
= Resolver::TypeCheckContext::get ();
3558 bool ok
= context
->lookup_associated_type_mapping (get_ty_ref (), &mapping
);
3561 return TyVar (mapping
).get_tyty ();
3565 PlaceholderType::is_equal (const BaseType
&other
) const
3567 if (get_kind () != other
.get_kind ())
3569 if (!can_resolve ())
3572 return resolve ()->is_equal (other
);
3575 auto other2
= static_cast<const PlaceholderType
&> (other
);
3576 return get_symbol ().compare (other2
.get_symbol ()) == 0;
3581 ProjectionType::ProjectionType (
3582 HirId ref
, BaseType
*base
, const Resolver::TraitReference
*trait
, DefId item
,
3583 std::vector
<SubstitutionParamMapping
> subst_refs
,
3584 SubstitutionArgumentMappings generic_arguments
,
3585 RegionConstraints region_constraints
, std::set
<HirId
> refs
)
3586 : BaseType (ref
, ref
, KIND
,
3587 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3589 SubstitutionRef (std::move (subst_refs
), std::move (generic_arguments
),
3590 std::move (region_constraints
)),
3591 base (base
), trait (trait
), item (item
)
3594 ProjectionType::ProjectionType (
3595 HirId ref
, HirId ty_ref
, BaseType
*base
,
3596 const Resolver::TraitReference
*trait
, DefId item
,
3597 std::vector
<SubstitutionParamMapping
> subst_refs
,
3598 SubstitutionArgumentMappings generic_arguments
,
3599 RegionConstraints region_constraints
, std::set
<HirId
> refs
)
3600 : BaseType (ref
, ty_ref
, KIND
,
3601 {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION
},
3603 SubstitutionRef (std::move (subst_refs
), std::move (generic_arguments
),
3604 std::move (region_constraints
)),
3605 base (base
), trait (trait
), item (item
)
3609 ProjectionType::get_name () const
3611 return as_string ();
3615 ProjectionType::get () const
3621 ProjectionType::get ()
3627 ProjectionType::accept_vis (TyVisitor
&vis
)
3633 ProjectionType::accept_vis (TyConstVisitor
&vis
) const
3639 ProjectionType::as_string () const
3641 return "<Projection=" + subst_as_string () + "::" + base
->as_string () + ">";
3645 ProjectionType::can_eq (const BaseType
*other
, bool emit_errors
) const
3647 return base
->can_eq (other
, emit_errors
);
3651 ProjectionType::clone () const
3653 return new ProjectionType (get_ref (), get_ty_ref (), base
->clone (), trait
,
3654 item
, clone_substs (), used_arguments
,
3655 region_constraints
, get_combined_refs ());
3659 ProjectionType::handle_substitions (
3660 SubstitutionArgumentMappings
&subst_mappings
)
3662 // // do we really need to substitute this?
3663 // if (base->needs_generic_substitutions () ||
3664 // base->contains_type_parameters
3670 ProjectionType
*projection
= static_cast<ProjectionType
*> (clone ());
3671 projection
->set_ty_ref (mappings
->get_next_hir_id ());
3672 projection
->used_arguments
= subst_mappings
;
3674 auto context
= Resolver::TypeCheckContext::get ();
3675 context
->insert_implicit_type (projection
->get_ty_ref (), projection
);
3677 for (auto &sub
: projection
->get_substs ())
3679 SubstitutionArg arg
= SubstitutionArg::error ();
3681 = subst_mappings
.get_argument_for_symbol (sub
.get_param_ty (), &arg
);
3683 sub
.fill_param_ty (subst_mappings
, subst_mappings
.get_locus ());
3686 auto fty
= projection
->base
;
3687 bool is_param_ty
= fty
->get_kind () == TypeKind::PARAM
;
3690 ParamType
*p
= static_cast<ParamType
*> (fty
);
3692 SubstitutionArg arg
= SubstitutionArg::error ();
3693 bool ok
= subst_mappings
.get_argument_for_symbol (p
, &arg
);
3696 auto argt
= arg
.get_tyty ();
3697 bool arg_is_param
= argt
->get_kind () == TyTy::TypeKind::PARAM
;
3698 bool arg_is_concrete
= argt
->get_kind () != TyTy::TypeKind::INFER
;
3700 if (arg_is_param
|| arg_is_concrete
)
3702 auto new_field
= argt
->clone ();
3703 new_field
->set_ref (fty
->get_ref ());
3704 projection
->base
= new_field
;
3708 fty
->set_ty_ref (argt
->get_ref ());
3712 else if (fty
->needs_generic_substitutions () || !fty
->is_concrete ())
3715 = Resolver::SubstMapperInternal::Resolve (fty
, subst_mappings
);
3717 if (concrete
== nullptr || concrete
->get_kind () == TyTy::TypeKind::ERROR
)
3719 rust_error_at (subst_mappings
.get_locus (),
3720 "Failed to resolve field substitution type: %s",
3721 fty
->as_string ().c_str ());
3725 projection
->base
= concrete
;
3733 DynamicObjectType::DynamicObjectType (
3734 HirId ref
, RustIdent ident
, std::vector
<TypeBoundPredicate
> specified_bounds
,
3735 std::set
<HirId
> refs
)
3736 : BaseType (ref
, ref
, KIND
, ident
, specified_bounds
, refs
)
3739 DynamicObjectType::DynamicObjectType (
3740 HirId ref
, HirId ty_ref
, RustIdent ident
,
3741 std::vector
<TypeBoundPredicate
> specified_bounds
, std::set
<HirId
> refs
)
3742 : BaseType (ref
, ty_ref
, KIND
, ident
, specified_bounds
, refs
)
3746 DynamicObjectType::accept_vis (TyVisitor
&vis
)
3752 DynamicObjectType::accept_vis (TyConstVisitor
&vis
) const
3758 DynamicObjectType::as_string () const
3760 return "dyn [" + raw_bounds_as_string () + "]";
3764 DynamicObjectType::can_eq (const BaseType
*other
, bool emit_errors
) const
3766 DynamicCmp
r (this, emit_errors
);
3767 return r
.can_eq (other
);
3771 DynamicObjectType::clone () const
3773 return new DynamicObjectType (get_ref (), get_ty_ref (), ident
,
3774 specified_bounds
, get_combined_refs ());
3778 DynamicObjectType::get_name () const
3780 return "dyn [" + raw_bounds_as_name () + "]";
3784 DynamicObjectType::is_equal (const BaseType
&other
) const
3786 if (get_kind () != other
.get_kind ())
3789 if (num_specified_bounds () != other
.num_specified_bounds ())
3792 return bounds_compatible (other
, UNDEF_LOCATION
, false);
3796 std::pair
<const Resolver::TraitItemReference
*, const TypeBoundPredicate
*>>
3797 DynamicObjectType::get_object_items () const
3800 std::pair
<const Resolver::TraitItemReference
*, const TypeBoundPredicate
*>>
3802 for (auto &bound
: get_specified_bounds ())
3804 const Resolver::TraitReference
*trait
= bound
.get ();
3805 std::vector
<const Resolver::TraitItemReference
*> trait_items
;
3806 trait
->get_trait_items_and_supers (trait_items
);
3808 for (auto &item
: trait_items
)
3810 if (item
->get_trait_item_type ()
3811 == Resolver::TraitItemReference::TraitItemType::FN
3812 && item
->is_object_safe ())
3813 items
.push_back ({item
, &bound
});