LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-tyty.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 86.2 % 2073 1786
Test Date: 2025-11-22 14:42:49 Functions: 90.8 % 346 314
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #include "rust-system.h"
      20                 :             : #include "rust-tyty.h"
      21                 :             : #include "rust-tyty-subst.h"
      22                 :             : #include "rust-tyty-visitor.h"
      23                 :             : #include "rust-hir-map.h"
      24                 :             : #include "rust-location.h"
      25                 :             : #include "rust-type-util.h"
      26                 :             : #include "rust-hir-type-bounds.h"
      27                 :             : #include "rust-substitution-mapper.h"
      28                 :             : #include "rust-hir-trait-reference.h"
      29                 :             : #include "rust-hir-trait-resolve.h"
      30                 :             : #include "tree-pretty-print.h"
      31                 :             : 
      32                 :             : #include "optional.h"
      33                 :             : #include "options.h"
      34                 :             : #include "tree.h"
      35                 :             : #include "fold-const.h"
      36                 :             : 
      37                 :             : namespace Rust {
      38                 :             : namespace TyTy {
      39                 :             : 
      40                 :             : std::string
      41                 :       29603 : TypeKindFormat::to_string (TypeKind kind)
      42                 :             : {
      43                 :       29603 :   switch (kind)
      44                 :             :     {
      45                 :           0 :     case TypeKind::INFER:
      46                 :           0 :       return "Infer";
      47                 :             : 
      48                 :           8 :     case TypeKind::ADT:
      49                 :           8 :       return "ADT";
      50                 :             : 
      51                 :           0 :     case TypeKind::STR:
      52                 :           0 :       return "STR";
      53                 :             : 
      54                 :           9 :     case TypeKind::REF:
      55                 :           9 :       return "REF";
      56                 :             : 
      57                 :           0 :     case TypeKind::POINTER:
      58                 :           0 :       return "POINTER";
      59                 :             : 
      60                 :           0 :     case TypeKind::PARAM:
      61                 :           0 :       return "PARAM";
      62                 :             : 
      63                 :           2 :     case TypeKind::ARRAY:
      64                 :           2 :       return "ARRAY";
      65                 :             : 
      66                 :           0 :     case TypeKind::SLICE:
      67                 :           0 :       return "SLICE";
      68                 :             : 
      69                 :       29495 :     case TypeKind::FNDEF:
      70                 :       29495 :       return "FnDef";
      71                 :             : 
      72                 :           0 :     case TypeKind::FNPTR:
      73                 :           0 :       return "FnPtr";
      74                 :             : 
      75                 :           5 :     case TypeKind::TUPLE:
      76                 :           5 :       return "Tuple";
      77                 :             : 
      78                 :           0 :     case TypeKind::BOOL:
      79                 :           0 :       return "Bool";
      80                 :             : 
      81                 :           0 :     case TypeKind::CHAR:
      82                 :           0 :       return "Char";
      83                 :             : 
      84                 :          23 :     case TypeKind::INT:
      85                 :          23 :       return "Int";
      86                 :             : 
      87                 :           0 :     case TypeKind::UINT:
      88                 :           0 :       return "Uint";
      89                 :             : 
      90                 :           0 :     case TypeKind::FLOAT:
      91                 :           0 :       return "Float";
      92                 :             : 
      93                 :           1 :     case TypeKind::USIZE:
      94                 :           1 :       return "Usize";
      95                 :             : 
      96                 :           0 :     case TypeKind::ISIZE:
      97                 :           0 :       return "Isize";
      98                 :             : 
      99                 :           0 :     case TypeKind::NEVER:
     100                 :           0 :       return "Never";
     101                 :             : 
     102                 :           0 :     case TypeKind::PLACEHOLDER:
     103                 :           0 :       return "Placeholder";
     104                 :             : 
     105                 :           0 :     case TypeKind::PROJECTION:
     106                 :           0 :       return "Projection";
     107                 :             : 
     108                 :           0 :     case TypeKind::DYNAMIC:
     109                 :           0 :       return "Dynamic";
     110                 :             : 
     111                 :          60 :     case TypeKind::CLOSURE:
     112                 :          60 :       return "Closure";
     113                 :             : 
     114                 :           0 :     case TypeKind::OPAQUE:
     115                 :           0 :       return "Opaque";
     116                 :             : 
     117                 :           0 :     case TypeKind::CONST:
     118                 :           0 :       return "Const";
     119                 :             : 
     120                 :           0 :     case TypeKind::ERROR:
     121                 :           0 :       return "ERROR";
     122                 :             :     }
     123                 :           0 :   rust_unreachable ();
     124                 :             : }
     125                 :             : 
     126                 :             : bool
     127                 :           0 : is_primitive_type_kind (TypeKind kind)
     128                 :             : {
     129                 :           0 :   switch (kind)
     130                 :             :     {
     131                 :             :     case TypeKind::BOOL:
     132                 :             :     case TypeKind::CHAR:
     133                 :             :     case TypeKind::INT:
     134                 :             :     case TypeKind::UINT:
     135                 :             :     case TypeKind::ISIZE:
     136                 :             :     case TypeKind::USIZE:
     137                 :             :     case TypeKind::FLOAT:
     138                 :             :     case TypeKind::NEVER:
     139                 :             :     case TypeKind::STR:
     140                 :             :       return true;
     141                 :           0 :     default:
     142                 :           0 :       return false;
     143                 :             :     }
     144                 :             : }
     145                 :             : 
     146                 :             : // BASE TYPE
     147                 :             : 
     148                 :     1589663 : BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
     149                 :     1589663 :                     std::set<HirId> refs)
     150                 :     1589663 :   : TypeBoundsMappings ({}), kind (kind), ref (ref), ty_ref (ty_ref),
     151                 :     1589663 :     orig_ref (ref), combined (refs), ident (ident),
     152                 :     3179326 :     mappings (Analysis::Mappings::get ())
     153                 :     1589663 : {}
     154                 :             : 
     155                 :    85831523 : BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
     156                 :             :                     std::vector<TypeBoundPredicate> specified_bounds,
     157                 :    85831523 :                     std::set<HirId> refs)
     158                 :    85831523 :   : TypeBoundsMappings (specified_bounds), kind (kind), ref (ref),
     159                 :    85831523 :     ty_ref (ty_ref), orig_ref (ref), combined (refs), ident (ident),
     160                 :   171663046 :     mappings (Analysis::Mappings::get ())
     161                 :    85831523 : {}
     162                 :             : 
     163                 :       96034 : BaseType::~BaseType () {}
     164                 :             : 
     165                 :             : HirId
     166                 :    98685041 : BaseType::get_ref () const
     167                 :             : {
     168                 :    98685041 :   return ref;
     169                 :             : }
     170                 :             : 
     171                 :             : void
     172                 :      375560 : BaseType::set_ref (HirId id)
     173                 :             : {
     174                 :      375560 :   if (id != ref)
     175                 :      225313 :     append_reference (ref);
     176                 :      375560 :   ref = id;
     177                 :      375560 : }
     178                 :             : 
     179                 :             : HirId
     180                 :    96376739 : BaseType::get_ty_ref () const
     181                 :             : {
     182                 :    96376739 :   return ty_ref;
     183                 :             : }
     184                 :             : 
     185                 :             : void
     186                 :      334436 : BaseType::set_ty_ref (HirId id)
     187                 :             : {
     188                 :      334436 :   ty_ref = id;
     189                 :      334436 : }
     190                 :             : HirId
     191                 :        6668 : BaseType::get_orig_ref () const
     192                 :             : {
     193                 :        6668 :   return orig_ref;
     194                 :             : }
     195                 :             : 
     196                 :             : bool
     197                 :      254559 : BaseType::is_equal (const BaseType &other) const
     198                 :             : {
     199                 :      254559 :   return get_kind () == other.get_kind ();
     200                 :             : }
     201                 :             : 
     202                 :             : bool
     203                 :      777634 : BaseType::is_unit () const
     204                 :             : {
     205                 :      777634 :   const TyTy::BaseType *x = destructure ();
     206                 :      777634 :   switch (x->get_kind ())
     207                 :             :     {
     208                 :             :     case PARAM:
     209                 :             :     case PROJECTION:
     210                 :             :     case PLACEHOLDER:
     211                 :             :     case FNPTR:
     212                 :             :     case FNDEF:
     213                 :             :     case ARRAY:
     214                 :             :     case SLICE:
     215                 :             :     case POINTER:
     216                 :             :     case REF:
     217                 :             :     case CLOSURE:
     218                 :             :     case INFER:
     219                 :             :     case BOOL:
     220                 :             :     case CHAR:
     221                 :             :     case INT:
     222                 :             :     case UINT:
     223                 :             :     case FLOAT:
     224                 :             :     case USIZE:
     225                 :             :     case ISIZE:
     226                 :             :     case OPAQUE:
     227                 :             :     case STR:
     228                 :             :     case DYNAMIC:
     229                 :             :     case CONST:
     230                 :             :     case ERROR:
     231                 :             :       return false;
     232                 :             : 
     233                 :             :       // FIXME ! is coerceable to () so we need to fix that
     234                 :             :     case NEVER:
     235                 :             :       return true;
     236                 :             : 
     237                 :       10656 :     case TUPLE:
     238                 :       10656 :       {
     239                 :       10656 :         return x->as<const TupleType> ()->num_fields () == 0;
     240                 :             :       }
     241                 :             : 
     242                 :      740418 :     case ADT:
     243                 :      740418 :       {
     244                 :      740418 :         auto adt = x->as<const ADTType> ();
     245                 :      740418 :         if (adt->is_enum ())
     246                 :             :           return false;
     247                 :             : 
     248                 :      775070 :         for (const auto &variant : adt->get_variants ())
     249                 :             :           {
     250                 :      639839 :             if (variant->num_fields () > 0)
     251                 :      631723 :               return false;
     252                 :             :           }
     253                 :             : 
     254                 :             :         return true;
     255                 :             :       }
     256                 :             :     }
     257                 :             :   return false;
     258                 :             : }
     259                 :             : 
     260                 :             : TypeKind
     261                 :   198640918 : BaseType::get_kind () const
     262                 :             : {
     263                 :   198640918 :   return kind;
     264                 :             : }
     265                 :             : 
     266                 :             : std::set<HirId>
     267                 :    87292815 : BaseType::get_combined_refs () const
     268                 :             : {
     269                 :    87292815 :   return combined;
     270                 :             : }
     271                 :             : 
     272                 :             : void
     273                 :     3185230 : BaseType::append_reference (HirId id)
     274                 :             : {
     275                 :     3185230 :   combined.insert (id);
     276                 :     3185230 : }
     277                 :             : 
     278                 :             : const RustIdent &
     279                 :       85515 : BaseType::get_ident () const
     280                 :             : {
     281                 :       85515 :   return ident;
     282                 :             : }
     283                 :             : 
     284                 :             : location_t
     285                 :       43049 : BaseType::get_locus () const
     286                 :             : {
     287                 :       43049 :   return ident.locus;
     288                 :             : }
     289                 :             : 
     290                 :             : // FIXME this is missing locus
     291                 :             : bool
     292                 :       76794 : BaseType::satisfies_bound (const TypeBoundPredicate &predicate, bool emit_error)
     293                 :             : {
     294                 :       76794 :   const Resolver::TraitReference *query = predicate.get ();
     295                 :       83026 :   for (const auto &bound : specified_bounds)
     296                 :             :     {
     297                 :       28280 :       const Resolver::TraitReference *item = bound.get ();
     298                 :       28280 :       if (item->satisfies_bound (*query))
     299                 :       22771 :         return true;
     300                 :             :     }
     301                 :             : 
     302                 :       54746 :   if (destructure ()->is<InferType> ())
     303                 :             :     return true;
     304                 :             : 
     305                 :       54023 :   bool satisfied = false;
     306                 :       54023 :   auto probed = Resolver::TypeBoundsProbe::Probe (this);
     307                 :      138856 :   for (const auto &b : probed)
     308                 :             :     {
     309                 :      130982 :       const Resolver::TraitReference *bound = b.first;
     310                 :      130982 :       if (bound->satisfies_bound (*query))
     311                 :             :         {
     312                 :             :           satisfied = true;
     313                 :             :           break;
     314                 :             :         }
     315                 :             :     }
     316                 :             : 
     317                 :       54023 :   if (!satisfied)
     318                 :             :     return false;
     319                 :             : 
     320                 :      143431 :   for (const auto &b : probed)
     321                 :             :     {
     322                 :      143431 :       const Resolver::TraitReference *bound = b.first;
     323                 :      143431 :       if (!bound->is_equal (*query))
     324                 :       97282 :         continue;
     325                 :             : 
     326                 :             :       // builtin ones have no impl-block this needs fixed and use a builtin node
     327                 :             :       // of somekind
     328                 :       46149 :       if (b.second == nullptr)
     329                 :             :         return true;
     330                 :             : 
     331                 :             :       // need to check that associated types can match as well
     332                 :       46149 :       const HIR::ImplBlock &impl = *(b.second);
     333                 :      112249 :       for (const auto &item : impl.get_impl_items ())
     334                 :             :         {
     335                 :       66414 :           bool is_associated_type = item->get_impl_item_type ()
     336                 :       66414 :                                     == HIR::ImplItem::ImplItemType::TYPE_ALIAS;
     337                 :       66414 :           if (!is_associated_type)
     338                 :       51094 :             continue;
     339                 :             : 
     340                 :       15320 :           TyTy::BaseType *impl_item_ty = nullptr;
     341                 :       15320 :           Analysis::NodeMapping i = item->get_impl_mappings ();
     342                 :       15320 :           bool query_ok = Resolver::query_type (i.get_hirid (), &impl_item_ty);
     343                 :       15320 :           if (!query_ok)
     344                 :         314 :             return false;
     345                 :             : 
     346                 :       15320 :           std::string item_name = item->get_impl_item_name ();
     347                 :       15320 :           tl::optional<TypeBoundPredicateItem> lookup
     348                 :       15320 :             = predicate.lookup_associated_item (item_name);
     349                 :       15320 :           if (!lookup.has_value ())
     350                 :             :             return false;
     351                 :             : 
     352                 :       15320 :           const auto *item_ref = lookup->get_raw_item ();
     353                 :       15320 :           TyTy::BaseType *bound_ty = item_ref->get_tyty ();
     354                 :             : 
     355                 :       30640 :           if (!Resolver::types_compatable (
     356                 :       15320 :                 TyTy::TyWithLocation (bound_ty, predicate.get_locus ()),
     357                 :       15320 :                 TyTy::TyWithLocation (impl_item_ty, item->get_locus ()),
     358                 :       15320 :                 mappings.lookup_location (get_ref ()), false /*emit-error*/,
     359                 :             :                 false /*check-bounds*/))
     360                 :             :             return false;
     361                 :       15320 :         }
     362                 :             : 
     363                 :             :       return true;
     364                 :             :     }
     365                 :             : 
     366                 :             :   return false;
     367                 :       54023 : }
     368                 :             : 
     369                 :             : bool
     370                 :       70393 : BaseType::bounds_compatible (BaseType &other, location_t locus, bool emit_error)
     371                 :             : {
     372                 :       70393 :   std::vector<std::reference_wrapper<const TypeBoundPredicate>>
     373                 :       70393 :     unsatisfied_bounds;
     374                 :      147187 :   for (auto &bound : get_specified_bounds ())
     375                 :             :     {
     376                 :       76794 :       if (!other.satisfies_bound (bound, emit_error))
     377                 :        8188 :         unsatisfied_bounds.push_back (bound);
     378                 :             :     }
     379                 :             : 
     380                 :             :   // lets emit a single error for this
     381                 :       70393 :   if (unsatisfied_bounds.size () > 0)
     382                 :             :     {
     383                 :        6836 :       rich_location r (line_table, locus);
     384                 :        6836 :       std::string missing_preds;
     385                 :       15024 :       for (size_t i = 0; i < unsatisfied_bounds.size (); i++)
     386                 :             :         {
     387                 :        8188 :           const TypeBoundPredicate &pred = unsatisfied_bounds.at (i);
     388                 :        8188 :           r.add_range (pred.get_locus ());
     389                 :       16376 :           missing_preds += pred.get_name ();
     390                 :             : 
     391                 :        8188 :           bool have_next = (i + 1) < unsatisfied_bounds.size ();
     392                 :        8188 :           if (have_next)
     393                 :        1352 :             missing_preds += ", ";
     394                 :             :         }
     395                 :             : 
     396                 :        6836 :       if (emit_error)
     397                 :             :         {
     398                 :          14 :           rust_error_at (r, ErrorCode::E0277,
     399                 :             :                          "bounds not satisfied for %s %qs is not satisfied",
     400                 :          28 :                          other.get_name ().c_str (), missing_preds.c_str ());
     401                 :             :           // rust_assert (!emit_error);
     402                 :             :         }
     403                 :        6836 :     }
     404                 :             : 
     405                 :       70393 :   return unsatisfied_bounds.size () == 0;
     406                 :       70393 : }
     407                 :             : 
     408                 :             : void
     409                 :       12107 : BaseType::inherit_bounds (const BaseType &other)
     410                 :             : {
     411                 :       12107 :   inherit_bounds (other.get_specified_bounds ());
     412                 :       12107 : }
     413                 :             : 
     414                 :             : void
     415                 :       61079 : BaseType::inherit_bounds (
     416                 :             :   const std::vector<TyTy::TypeBoundPredicate> &specified_bounds)
     417                 :             : {
     418                 :      121007 :   for (auto &bound : specified_bounds)
     419                 :             :     {
     420                 :       59928 :       add_bound (bound);
     421                 :             :     }
     422                 :       61079 : }
     423                 :             : 
     424                 :             : BaseType *
     425                 :       11168 : BaseType::get_root ()
     426                 :             : {
     427                 :       13139 :   TyTy::BaseType *root = this;
     428                 :             : 
     429                 :       13139 :   if (const auto r = root->try_as<const ReferenceType> ())
     430                 :             :     {
     431                 :        1467 :       root = r->get_base ()->get_root ();
     432                 :             :     }
     433                 :       11672 :   else if (const auto r = root->try_as<const PointerType> ())
     434                 :             :     {
     435                 :         284 :       root = r->get_base ()->get_root ();
     436                 :             :     }
     437                 :             :   // these are an unsize
     438                 :       11388 :   else if (const auto r = root->try_as<const SliceType> ())
     439                 :             :     {
     440                 :         220 :       root = r->get_element_type ()->get_root ();
     441                 :             :     }
     442                 :             :   //  else if (const auto r = root->try_as<const ArrayType> ())
     443                 :             :   //    {
     444                 :             :   //      root = r->get_element_type ()->get_root ();
     445                 :             :   //    }
     446                 :             : 
     447                 :       11168 :   return root;
     448                 :             : }
     449                 :             : 
     450                 :             : BaseType *
     451                 :     5159587 : BaseType::destructure ()
     452                 :             : {
     453                 :     5159587 :   int recurisve_ops = 0;
     454                 :     5159587 :   BaseType *x = this;
     455                 :     5401363 :   while (true)
     456                 :             :     {
     457                 :     5401363 :       if (recurisve_ops++ >= rust_max_recursion_depth)
     458                 :             :         {
     459                 :           0 :           rust_error_at (
     460                 :             :             UNDEF_LOCATION,
     461                 :             :             "%<recursion depth%> count exceeds limit of %i (use "
     462                 :             :             "%<frust-max-recursion-depth=%> to increase the limit)",
     463                 :             :             rust_max_recursion_depth);
     464                 :           0 :           return new ErrorType (get_ref ());
     465                 :             :         }
     466                 :             : 
     467                 :     5401363 :       if (auto p = x->try_as<ParamType> ())
     468                 :             :         {
     469                 :      363696 :           auto pr = p->resolve ();
     470                 :      363696 :           if (pr == x)
     471                 :             :             return pr;
     472                 :             : 
     473                 :             :           x = pr;
     474                 :             :         }
     475                 :     5037667 :       else if (x->get_kind () == TypeKind::CONST)
     476                 :             :         {
     477                 :        7316 :           auto p = x->as_const_type ();
     478                 :        7316 :           if (p->const_kind () == BaseConstType::ConstKind::Decl)
     479                 :             :             {
     480                 :         741 :               auto decl = static_cast<ConstParamType *> (p);
     481                 :         741 :               auto pr = decl->resolve ();
     482                 :         741 :               if (pr == x)
     483                 :             :                 return pr;
     484                 :             : 
     485                 :             :               x = pr;
     486                 :             :             }
     487                 :             :           else
     488                 :             :             {
     489                 :             :               return x;
     490                 :             :             }
     491                 :             :         }
     492                 :     5030351 :       else if (auto p = x->try_as<PlaceholderType> ())
     493                 :             :         {
     494                 :       22317 :           if (!p->can_resolve ())
     495                 :             :             return p;
     496                 :             : 
     497                 :        6313 :           x = p->resolve ();
     498                 :             :         }
     499                 :     5008034 :       else if (auto p = x->try_as<ProjectionType> ())
     500                 :             :         {
     501                 :       20460 :           x = p->get ();
     502                 :             :         }
     503                 :             :       else
     504                 :             :         {
     505                 :             :           return x;
     506                 :             :         }
     507                 :             :     }
     508                 :             : 
     509                 :             :   return x;
     510                 :             : }
     511                 :             : 
     512                 :             : const BaseType *
     513                 :     8273229 : BaseType::destructure () const
     514                 :             : {
     515                 :     8273229 :   int recurisve_ops = 0;
     516                 :     8273229 :   const BaseType *x = this;
     517                 :     9303509 :   while (true)
     518                 :             :     {
     519                 :     9303509 :       if (recurisve_ops++ >= rust_max_recursion_depth)
     520                 :             :         {
     521                 :           0 :           rust_error_at (
     522                 :             :             UNDEF_LOCATION,
     523                 :             :             "%<recursion depth%> count exceeds limit of %i (use "
     524                 :             :             "%<frust-max-recursion-depth=%> to increase the limit)",
     525                 :             :             rust_max_recursion_depth);
     526                 :           0 :           return new ErrorType (get_ref ());
     527                 :             :         }
     528                 :             : 
     529                 :     9303509 :       if (auto p = x->try_as<const ParamType> ())
     530                 :             :         {
     531                 :     1682411 :           auto pr = p->resolve ();
     532                 :     1682411 :           if (pr == x)
     533                 :             :             return pr;
     534                 :             : 
     535                 :             :           x = pr;
     536                 :             :         }
     537                 :     7621098 :       else if (x->get_kind () == TypeKind::CONST)
     538                 :             :         {
     539                 :       18706 :           auto p = x->as_const_type ();
     540                 :       18706 :           if (p->const_kind () == BaseConstType::ConstKind::Decl)
     541                 :             :             {
     542                 :        1414 :               auto decl = static_cast<const ConstParamType *> (p);
     543                 :        1414 :               auto pr = decl->resolve ();
     544                 :        1414 :               if (pr == x)
     545                 :             :                 return pr;
     546                 :             : 
     547                 :             :               x = pr;
     548                 :             :             }
     549                 :             :           else
     550                 :             :             {
     551                 :             :               return x;
     552                 :             :             }
     553                 :             :         }
     554                 :     7602392 :       else if (auto p = x->try_as<const PlaceholderType> ())
     555                 :             :         {
     556                 :       20845 :           if (!p->can_resolve ())
     557                 :             :             return p;
     558                 :             : 
     559                 :        4096 :           x = p->resolve ();
     560                 :             :         }
     561                 :     7581547 :       else if (auto p = x->try_as<const ProjectionType> ())
     562                 :             :         {
     563                 :        4781 :           x = p->get ();
     564                 :             :         }
     565                 :     7576766 :       else if (auto p = x->try_as<const OpaqueType> ())
     566                 :             :         {
     567                 :         254 :           auto pr = p->resolve ();
     568                 :         254 :           if (pr == x)
     569                 :             :             return pr;
     570                 :             : 
     571                 :             :           x = pr;
     572                 :             :         }
     573                 :             :       else
     574                 :             :         {
     575                 :             :           return x;
     576                 :             :         }
     577                 :             :     }
     578                 :             : 
     579                 :             :   return x;
     580                 :             : }
     581                 :             : 
     582                 :             : BaseType *
     583                 :       17423 : BaseType::monomorphized_clone () const
     584                 :             : {
     585                 :       17423 :   const TyTy::BaseType *x = destructure ();
     586                 :             : 
     587                 :       17423 :   if (auto arr = x->try_as<const ArrayType> ())
     588                 :             :     {
     589                 :          80 :       TyVar elm = arr->get_var_element_type ().monomorphized_clone ();
     590                 :         160 :       return new ArrayType (arr->get_ref (), arr->get_ty_ref (), ident.locus,
     591                 :          80 :                             arr->get_capacity_var (), elm,
     592                 :          80 :                             arr->get_combined_refs ());
     593                 :             :     }
     594                 :       17343 :   else if (auto slice = x->try_as<const SliceType> ())
     595                 :             :     {
     596                 :         161 :       TyVar elm = slice->get_var_element_type ().monomorphized_clone ();
     597                 :         161 :       return new SliceType (slice->get_ref (), slice->get_ty_ref (),
     598                 :         161 :                             ident.locus, elm, slice->get_combined_refs ());
     599                 :             :     }
     600                 :       17182 :   else if (auto ptr = x->try_as<const PointerType> ())
     601                 :             :     {
     602                 :         653 :       TyVar elm = ptr->get_var_element_type ().monomorphized_clone ();
     603                 :         653 :       return new PointerType (ptr->get_ref (), ptr->get_ty_ref (), elm,
     604                 :         653 :                               ptr->mutability (), ptr->get_combined_refs ());
     605                 :             :     }
     606                 :       16529 :   else if (auto ref = x->try_as<const ReferenceType> ())
     607                 :             :     {
     608                 :         279 :       TyVar elm = ref->get_var_element_type ().monomorphized_clone ();
     609                 :         279 :       return new ReferenceType (ref->get_ref (), ref->get_ty_ref (), elm,
     610                 :         558 :                                 ref->mutability (), ref->get_region (),
     611                 :         837 :                                 ref->get_combined_refs ());
     612                 :             :     }
     613                 :       16250 :   else if (auto tuple = x->try_as<const TupleType> ())
     614                 :             :     {
     615                 :        4591 :       std::vector<TyVar> cloned_fields;
     616                 :        5253 :       for (const auto &f : tuple->get_fields ())
     617                 :         662 :         cloned_fields.push_back (f.monomorphized_clone ());
     618                 :             : 
     619                 :        4591 :       return new TupleType (tuple->get_ref (), tuple->get_ty_ref (),
     620                 :        4591 :                             ident.locus, cloned_fields,
     621                 :        4591 :                             tuple->get_combined_refs ());
     622                 :        4591 :     }
     623                 :       11659 :   else if (auto fn = x->try_as<const FnType> ())
     624                 :             :     {
     625                 :           0 :       std::vector<TyTy::FnParam> cloned_params;
     626                 :           0 :       for (auto &p : fn->get_params ())
     627                 :           0 :         cloned_params.push_back (p.monomorphized_clone ());
     628                 :             : 
     629                 :           0 :       BaseType *retty = fn->get_return_type ()->monomorphized_clone ();
     630                 :           0 :       return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (),
     631                 :           0 :                          fn->get_identifier (), fn->ident, fn->get_flags (),
     632                 :             :                          fn->get_abi (), std::move (cloned_params), retty,
     633                 :           0 :                          fn->clone_substs (), fn->get_substitution_arguments (),
     634                 :             :                          fn->get_region_constraints (),
     635                 :           0 :                          fn->get_combined_refs ());
     636                 :           0 :     }
     637                 :       11659 :   else if (auto fn = x->try_as<const FnPtr> ())
     638                 :             :     {
     639                 :           0 :       std::vector<TyVar> cloned_params;
     640                 :           0 :       for (auto &p : fn->get_params ())
     641                 :           0 :         cloned_params.push_back (p.monomorphized_clone ());
     642                 :             : 
     643                 :           0 :       TyVar retty = fn->get_var_return_type ().monomorphized_clone ();
     644                 :           0 :       return new FnPtr (fn->get_ref (), fn->get_ty_ref (), ident.locus,
     645                 :             :                         std::move (cloned_params), retty, fn->get_abi (),
     646                 :           0 :                         fn->get_unsafety (), fn->get_combined_refs ());
     647                 :           0 :     }
     648                 :       11659 :   else if (auto adt = x->try_as<const ADTType> ())
     649                 :             :     {
     650                 :        2102 :       std::vector<VariantDef *> cloned_variants;
     651                 :        6261 :       for (auto &variant : adt->get_variants ())
     652                 :        4159 :         cloned_variants.push_back (variant->monomorphized_clone ());
     653                 :             : 
     654                 :        2102 :       return new ADTType (adt->get_id (), adt->get_ref (), adt->get_ty_ref (),
     655                 :        4204 :                           adt->get_identifier (), adt->ident,
     656                 :             :                           adt->get_adt_kind (), cloned_variants,
     657                 :        2102 :                           adt->clone_substs (), adt->get_repr_options (),
     658                 :             :                           adt->get_used_arguments (),
     659                 :             :                           adt->get_region_constraints (),
     660                 :        8408 :                           adt->get_combined_refs ());
     661                 :        2102 :     }
     662                 :             :   else
     663                 :             :     {
     664                 :        9557 :       return x->clone ();
     665                 :             :     }
     666                 :             : 
     667                 :             :   rust_unreachable ();
     668                 :             :   return nullptr;
     669                 :             : }
     670                 :             : 
     671                 :             : std::string
     672                 :       29603 : BaseType::mappings_str () const
     673                 :             : {
     674                 :       59206 :   std::string buffer = "Ref: " + std::to_string (get_ref ())
     675                 :       88809 :                        + " TyRef: " + std::to_string (get_ty_ref ());
     676                 :       29603 :   buffer += "[";
     677                 :       43726 :   for (auto &ref : combined)
     678                 :       42369 :     buffer += std::to_string (ref) + ",";
     679                 :       29603 :   buffer += "]";
     680                 :       59206 :   return "(" + buffer + ")";
     681                 :       29603 : }
     682                 :             : 
     683                 :             : std::string
     684                 :    13279618 : BaseType::debug_str () const
     685                 :             : {
     686                 :             :   // return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":"
     687                 :             :   //        + mappings_str () + ":" + bounds_as_string ();
     688                 :    13279618 :   return get_name ();
     689                 :             : }
     690                 :             : 
     691                 :             : void
     692                 :         539 : BaseType::debug () const
     693                 :             : {
     694                 :         539 :   rust_debug ("[%p] %s", static_cast<const void *> (this),
     695                 :             :               debug_str ().c_str ());
     696                 :         539 : }
     697                 :             : 
     698                 :             : const TyTy::BaseType *
     699                 :        6377 : BaseType::contains_infer () const
     700                 :             : {
     701                 :        6553 :   const TyTy::BaseType *x = destructure ();
     702                 :             : 
     703                 :        6553 :   if (auto fn = x->try_as<const FnType> ())
     704                 :             :     {
     705                 :           0 :       for (const auto &param : fn->get_params ())
     706                 :             :         {
     707                 :           0 :           auto infer = param.get_type ()->contains_infer ();
     708                 :           0 :           if (infer)
     709                 :        6377 :             return infer;
     710                 :             :         }
     711                 :           0 :       return fn->get_return_type ()->contains_infer ();
     712                 :             :     }
     713                 :        6553 :   else if (auto fn = x->try_as<const FnPtr> ())
     714                 :             :     {
     715                 :           0 :       for (const auto &param : fn->get_params ())
     716                 :             :         {
     717                 :           0 :           auto infer = param.get_tyty ()->contains_infer ();
     718                 :           0 :           if (infer)
     719                 :        6377 :             return infer;
     720                 :             :         }
     721                 :           0 :       return fn->get_return_type ()->contains_infer ();
     722                 :             :     }
     723                 :        6553 :   else if (auto adt = x->try_as<const ADTType> ())
     724                 :             :     {
     725                 :         231 :       for (auto &variant : adt->get_variants ())
     726                 :             :         {
     727                 :         141 :           bool is_num_variant
     728                 :         141 :             = variant->get_variant_type () == VariantDef::VariantType::NUM;
     729                 :         141 :           if (is_num_variant)
     730                 :          58 :             continue;
     731                 :             : 
     732                 :         175 :           for (auto &field : variant->get_fields ())
     733                 :             :             {
     734                 :          93 :               const BaseType *field_type = field->get_field_type ();
     735                 :          93 :               auto infer = (field_type->contains_infer ());
     736                 :          93 :               if (infer)
     737                 :        6377 :                 return infer;
     738                 :             :             }
     739                 :             :         }
     740                 :             :       return nullptr;
     741                 :             :     }
     742                 :        6462 :   else if (auto arr = x->try_as<const ArrayType> ())
     743                 :             :     {
     744                 :          32 :       return arr->get_element_type ()->contains_infer ();
     745                 :             :     }
     746                 :        6430 :   else if (auto slice = x->try_as<const SliceType> ())
     747                 :             :     {
     748                 :          49 :       return slice->get_element_type ()->contains_infer ();
     749                 :             :     }
     750                 :        6381 :   else if (auto ptr = x->try_as<const PointerType> ())
     751                 :             :     {
     752                 :          49 :       return ptr->get_base ()->contains_infer ();
     753                 :             :     }
     754                 :        6332 :   else if (auto ref = x->try_as<const ReferenceType> ())
     755                 :             :     {
     756                 :          46 :       return ref->get_base ()->contains_infer ();
     757                 :             :     }
     758                 :        6286 :   else if (auto tuple = x->try_as<const TupleType> ())
     759                 :             :     {
     760                 :        3237 :       for (size_t i = 0; i < tuple->num_fields (); i++)
     761                 :             :         {
     762                 :          14 :           auto infer = (tuple->get_field (i)->contains_infer ());
     763                 :          14 :           if (infer)
     764                 :             :             return infer;
     765                 :             :         }
     766                 :             :       return nullptr;
     767                 :             :     }
     768                 :        3063 :   else if (auto closure = x->try_as<const ClosureType> ())
     769                 :             :     {
     770                 :           0 :       auto infer = (closure->get_parameters ().contains_infer ());
     771                 :           0 :       if (infer)
     772                 :             :         return infer;
     773                 :           0 :       return closure->get_result_type ().contains_infer ();
     774                 :             :     }
     775                 :        3063 :   else if (x->is<InferType> ())
     776                 :             :     {
     777                 :             :       return x;
     778                 :             :     }
     779                 :             : 
     780                 :             :   return nullptr;
     781                 :             : }
     782                 :             : 
     783                 :             : bool
     784                 :     5688456 : BaseType::is_concrete () const
     785                 :             : {
     786                 :     5940150 :   const TyTy::BaseType *x = destructure ();
     787                 :             : 
     788                 :    11586819 :   if (x->is<ParamType> () || x->is<ProjectionType> ())
     789                 :             :     {
     790                 :      293481 :       return false;
     791                 :             :     }
     792                 :     5646669 :   else if (x->get_kind () == TyTy::TypeKind::CONST)
     793                 :             :     {
     794                 :       17984 :       auto p = x->as_const_type ();
     795                 :       17984 :       if (p->const_kind () == BaseConstType::ConstKind::Decl)
     796                 :             :         return false;
     797                 :             : 
     798                 :             :       return true;
     799                 :             :     }
     800                 :             :   // placeholder is a special case for this case when it is not resolvable
     801                 :             :   // it means we its just an empty placeholder associated type which is
     802                 :             :   // concrete
     803                 :     5628685 :   else if (x->is<PlaceholderType> ())
     804                 :             :     {
     805                 :             :       return true;
     806                 :             :     }
     807                 :     5612666 :   else if (auto fn = x->try_as<const FnType> ())
     808                 :             :     {
     809                 :       19352 :       for (const auto &param : fn->get_params ())
     810                 :             :         {
     811                 :       12400 :           if (!param.get_type ()->is_concrete ())
     812                 :      339220 :             return false;
     813                 :             :         }
     814                 :        6952 :       return fn->get_return_type ()->is_concrete ();
     815                 :             :     }
     816                 :     5604306 :   else if (auto fn = x->try_as<const FnPtr> ())
     817                 :             :     {
     818                 :        4276 :       for (const auto &param : fn->get_params ())
     819                 :             :         {
     820                 :          95 :           if (!param.get_tyty ()->is_concrete ())
     821                 :      339220 :             return false;
     822                 :             :         }
     823                 :        4181 :       return fn->get_return_type ()->is_concrete ();
     824                 :             :     }
     825                 :     5600121 :   else if (auto adt = x->try_as<const ADTType> ())
     826                 :             :     {
     827                 :      655649 :       if (adt->is_unit ())
     828                 :      116807 :         return !adt->needs_substitution ();
     829                 :             : 
     830                 :     1179114 :       for (auto &variant : adt->get_variants ())
     831                 :             :         {
     832                 :      683249 :           bool is_num_variant
     833                 :      683249 :             = variant->get_variant_type () == VariantDef::VariantType::NUM;
     834                 :      683249 :           if (is_num_variant)
     835                 :      137765 :             continue;
     836                 :             : 
     837                 :     1804729 :           for (auto &field : variant->get_fields ())
     838                 :             :             {
     839                 :     1302222 :               const BaseType *field_type = field->get_field_type ();
     840                 :     1302222 :               if (!field_type->is_concrete ())
     841                 :      339220 :                 return false;
     842                 :             :             }
     843                 :             :         }
     844                 :             :       return true;
     845                 :             :     }
     846                 :     4944472 :   else if (auto arr = x->try_as<const ArrayType> ())
     847                 :             :     {
     848                 :       14971 :       return arr->get_element_type ()->is_concrete ()
     849                 :       14971 :              && arr->get_capacity ()->is_concrete ();
     850                 :             :     }
     851                 :     4929501 :   else if (auto slice = x->try_as<const SliceType> ())
     852                 :             :     {
     853                 :       12688 :       return slice->get_element_type ()->is_concrete ();
     854                 :             :     }
     855                 :     4916813 :   else if (auto ptr = x->try_as<const PointerType> ())
     856                 :             :     {
     857                 :       17881 :       return ptr->get_base ()->is_concrete ();
     858                 :             :     }
     859                 :     4898932 :   else if (auto ref = x->try_as<const ReferenceType> ())
     860                 :             :     {
     861                 :      209992 :       return ref->get_base ()->is_concrete ();
     862                 :             :     }
     863                 :     4688940 :   else if (auto tuple = x->try_as<const TupleType> ())
     864                 :             :     {
     865                 :       18812 :       for (size_t i = 0; i < tuple->num_fields (); i++)
     866                 :             :         {
     867                 :        2630 :           if (!tuple->get_field (i)->is_concrete ())
     868                 :             :             return false;
     869                 :             :         }
     870                 :             :       return true;
     871                 :             :     }
     872                 :     4672453 :   else if (auto closure = x->try_as<const ClosureType> ())
     873                 :             :     {
     874                 :         259 :       if (closure->get_parameters ().is_concrete ())
     875                 :             :         return false;
     876                 :           0 :       return closure->get_result_type ().is_concrete ();
     877                 :             :     }
     878                 :    13833809 :   else if (x->is<InferType> () || x->is<BoolType> () || x->is<CharType> ()
     879                 :     8823095 :            || x->is<IntType> () || x->is<UintType> () || x->is<FloatType> ()
     880                 :     1185691 :            || x->is<USizeType> () || x->is<ISizeType> () || x->is<NeverType> ()
     881                 :       32544 :            || x->is<StrType> () || x->is<DynamicObjectType> ()
     882                 :     4672195 :            || x->is<ErrorType> ())
     883                 :             :     {
     884                 :     4672194 :       return true;
     885                 :             :     }
     886                 :             : 
     887                 :             :   return false;
     888                 :             : }
     889                 :             : 
     890                 :             : bool
     891                 :      205061 : BaseType::has_substitutions_defined () const
     892                 :             : {
     893                 :      205061 :   const TyTy::BaseType *x = destructure ();
     894                 :      205061 :   switch (x->get_kind ())
     895                 :             :     {
     896                 :             :     case INFER:
     897                 :             :     case BOOL:
     898                 :             :     case CHAR:
     899                 :             :     case INT:
     900                 :             :     case UINT:
     901                 :             :     case FLOAT:
     902                 :             :     case USIZE:
     903                 :             :     case ISIZE:
     904                 :             :     case NEVER:
     905                 :             :     case STR:
     906                 :             :     case DYNAMIC:
     907                 :             :     case ERROR:
     908                 :             :     case FNPTR:
     909                 :             :     case ARRAY:
     910                 :             :     case SLICE:
     911                 :             :     case POINTER:
     912                 :             :     case REF:
     913                 :             :     case TUPLE:
     914                 :             :     case PARAM:
     915                 :             :     case PLACEHOLDER:
     916                 :             :     case CONST:
     917                 :             :     case OPAQUE:
     918                 :             :       return false;
     919                 :             : 
     920                 :           0 :     case PROJECTION:
     921                 :           0 :       {
     922                 :           0 :         const ProjectionType &p = *static_cast<const ProjectionType *> (x);
     923                 :           0 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p);
     924                 :           0 :         return ref.has_substitutions ();
     925                 :             :       }
     926                 :       71861 :       break;
     927                 :             : 
     928                 :       71861 :     case FNDEF:
     929                 :       71861 :       {
     930                 :       71861 :         const FnType &fn = *static_cast<const FnType *> (x);
     931                 :       71861 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn);
     932                 :       71861 :         return ref.has_substitutions ();
     933                 :             :       }
     934                 :      112650 :       break;
     935                 :             : 
     936                 :      112650 :     case ADT:
     937                 :      112650 :       {
     938                 :      112650 :         const ADTType &adt = *static_cast<const ADTType *> (x);
     939                 :      112650 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt);
     940                 :      112650 :         return ref.has_substitutions ();
     941                 :             :       }
     942                 :           0 :       break;
     943                 :             : 
     944                 :           0 :     case CLOSURE:
     945                 :           0 :       {
     946                 :           0 :         const ClosureType &closure = *static_cast<const ClosureType *> (x);
     947                 :           0 :         const SubstitutionRef &ref
     948                 :             :           = static_cast<const SubstitutionRef &> (closure);
     949                 :           0 :         return ref.has_substitutions ();
     950                 :             :       }
     951                 :             :       break;
     952                 :             :     }
     953                 :             : 
     954                 :             :   return false;
     955                 :             : }
     956                 :             : 
     957                 :             : bool
     958                 :      117134 : BaseType::needs_generic_substitutions () const
     959                 :             : {
     960                 :      117134 :   const TyTy::BaseType *x = destructure ();
     961                 :      117134 :   switch (x->get_kind ())
     962                 :             :     {
     963                 :             :     case INFER:
     964                 :             :     case BOOL:
     965                 :             :     case CHAR:
     966                 :             :     case INT:
     967                 :             :     case UINT:
     968                 :             :     case FLOAT:
     969                 :             :     case USIZE:
     970                 :             :     case ISIZE:
     971                 :             :     case NEVER:
     972                 :             :     case STR:
     973                 :             :     case DYNAMIC:
     974                 :             :     case ERROR:
     975                 :             :     case FNPTR:
     976                 :             :     case ARRAY:
     977                 :             :     case SLICE:
     978                 :             :     case POINTER:
     979                 :             :     case REF:
     980                 :             :     case TUPLE:
     981                 :             :     case PARAM:
     982                 :             :     case PLACEHOLDER:
     983                 :             :     case CONST:
     984                 :             :     case OPAQUE:
     985                 :             :       return false;
     986                 :             : 
     987                 :           0 :     case PROJECTION:
     988                 :           0 :       {
     989                 :           0 :         const ProjectionType &p = *static_cast<const ProjectionType *> (x);
     990                 :           0 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p);
     991                 :           0 :         return ref.needs_substitution ();
     992                 :             :       }
     993                 :       13461 :       break;
     994                 :             : 
     995                 :       13461 :     case FNDEF:
     996                 :       13461 :       {
     997                 :       13461 :         const FnType &fn = *static_cast<const FnType *> (x);
     998                 :       13461 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn);
     999                 :       13461 :         return ref.needs_substitution ();
    1000                 :             :       }
    1001                 :       25879 :       break;
    1002                 :             : 
    1003                 :       25879 :     case ADT:
    1004                 :       25879 :       {
    1005                 :       25879 :         const ADTType &adt = *static_cast<const ADTType *> (x);
    1006                 :       25879 :         const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt);
    1007                 :       25879 :         return ref.needs_substitution ();
    1008                 :             :       }
    1009                 :          53 :       break;
    1010                 :             : 
    1011                 :          53 :     case CLOSURE:
    1012                 :          53 :       {
    1013                 :          53 :         const ClosureType &closure = *static_cast<const ClosureType *> (x);
    1014                 :          53 :         const SubstitutionRef &ref
    1015                 :             :           = static_cast<const SubstitutionRef &> (closure);
    1016                 :          53 :         return ref.needs_substitution ();
    1017                 :             :       }
    1018                 :             :       break;
    1019                 :             :     }
    1020                 :             : 
    1021                 :             :   return false;
    1022                 :             : }
    1023                 :             : 
    1024                 :             : const SubstitutionArgumentMappings &
    1025                 :         253 : BaseType::get_subst_argument_mappings () const
    1026                 :             : {
    1027                 :         461 :   static auto empty = SubstitutionArgumentMappings::empty ();
    1028                 :         253 :   const TyTy::BaseType *x = destructure ();
    1029                 :         253 :   switch (x->get_kind ())
    1030                 :             :     {
    1031                 :           0 :     case PROJECTION:
    1032                 :           0 :       {
    1033                 :           0 :         const auto &p = *static_cast<const ProjectionType *> (x);
    1034                 :           0 :         const auto &ref = static_cast<const SubstitutionRef &> (p);
    1035                 :           0 :         return ref.get_substitution_arguments ();
    1036                 :             :       }
    1037                 :           0 :       break;
    1038                 :             : 
    1039                 :           0 :     case FNDEF:
    1040                 :           0 :       {
    1041                 :           0 :         const auto &fn = *static_cast<const FnType *> (x);
    1042                 :           0 :         const auto &ref = static_cast<const SubstitutionRef &> (fn);
    1043                 :           0 :         return ref.get_substitution_arguments ();
    1044                 :             :       }
    1045                 :         253 :       break;
    1046                 :             : 
    1047                 :         253 :     case ADT:
    1048                 :         253 :       {
    1049                 :         253 :         const auto &adt = *static_cast<const ADTType *> (x);
    1050                 :         253 :         const auto &ref = static_cast<const SubstitutionRef &> (adt);
    1051                 :         253 :         return ref.get_substitution_arguments ();
    1052                 :             :       }
    1053                 :           0 :       break;
    1054                 :             : 
    1055                 :           0 :     case CLOSURE:
    1056                 :           0 :       {
    1057                 :           0 :         const auto &closure = *static_cast<const ClosureType *> (x);
    1058                 :           0 :         const auto &ref = static_cast<const SubstitutionRef &> (closure);
    1059                 :           0 :         return ref.get_substitution_arguments ();
    1060                 :             :       }
    1061                 :             :       break;
    1062                 :             : 
    1063                 :             :     default:
    1064                 :             :       return empty;
    1065                 :             :     }
    1066                 :             : 
    1067                 :             :   return empty;
    1068                 :             : }
    1069                 :             : 
    1070                 :             : // InferType
    1071                 :             : 
    1072                 :      116393 : InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
    1073                 :      116393 :                       location_t locus, std::set<HirId> refs)
    1074                 :      116393 :   : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
    1075                 :             :               refs),
    1076                 :      232786 :     infer_kind (infer_kind), default_hint (hint)
    1077                 :      116393 : {}
    1078                 :             : 
    1079                 :           0 : InferType::InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind,
    1080                 :           0 :                       TypeHint hint, location_t locus, std::set<HirId> refs)
    1081                 :             :   : BaseType (ref, ty_ref, KIND,
    1082                 :           0 :               {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1083                 :           0 :     infer_kind (infer_kind), default_hint (hint)
    1084                 :           0 : {}
    1085                 :             : 
    1086                 :             : InferType::InferTypeKind
    1087                 :      190681 : InferType::get_infer_kind () const
    1088                 :             : {
    1089                 :      190681 :   return infer_kind;
    1090                 :             : }
    1091                 :             : 
    1092                 :             : std::string
    1093                 :      354486 : InferType::get_name () const
    1094                 :             : {
    1095                 :      354486 :   return as_string ();
    1096                 :             : }
    1097                 :             : 
    1098                 :             : void
    1099                 :           0 : InferType::accept_vis (TyVisitor &vis)
    1100                 :             : {
    1101                 :           0 :   vis.visit (*this);
    1102                 :           0 : }
    1103                 :             : 
    1104                 :             : void
    1105                 :         568 : InferType::accept_vis (TyConstVisitor &vis) const
    1106                 :             : {
    1107                 :         568 :   vis.visit (*this);
    1108                 :         568 : }
    1109                 :             : 
    1110                 :             : std::string
    1111                 :      379927 : InferType::as_string () const
    1112                 :             : {
    1113                 :      379927 :   switch (infer_kind)
    1114                 :             :     {
    1115                 :      260814 :     case GENERAL:
    1116                 :      260814 :       return "T?";
    1117                 :      116957 :     case INTEGRAL:
    1118                 :      116957 :       return "<integer>";
    1119                 :        2156 :     case FLOAT:
    1120                 :        2156 :       return "<float>";
    1121                 :             :     }
    1122                 :           0 :   return "<infer::error>";
    1123                 :             : }
    1124                 :             : 
    1125                 :             : BaseType *
    1126                 :        7247 : InferType::clone () const
    1127                 :             : {
    1128                 :             :   // clones for inference variables are special in that they _must_ exist within
    1129                 :             :   // the type check context and we must ensure we don't loose the chain
    1130                 :             :   // otherwise we will end up in the missing type annotations case
    1131                 :             :   //
    1132                 :             :   // This means we cannot simply take over the same reference we must generate a
    1133                 :             :   // new ref just like the get_implicit_infer_var code then we can setup the
    1134                 :             :   // chain of references accordingly to ensure we don't loose the ability to
    1135                 :             :   // update the inference variables when we solve the type
    1136                 :             : 
    1137                 :        7247 :   auto &mappings = Analysis::Mappings::get ();
    1138                 :        7247 :   auto context = Resolver::TypeCheckContext::get ();
    1139                 :             : 
    1140                 :        7247 :   InferType *clone
    1141                 :             :     = new InferType (mappings.get_next_hir_id (), get_infer_kind (),
    1142                 :        7247 :                      default_hint, get_ident ().locus, get_combined_refs ());
    1143                 :             : 
    1144                 :        7247 :   context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
    1145                 :             :                                                UNKNOWN_NODEID,
    1146                 :             :                                                clone->get_ref (),
    1147                 :        7247 :                                                UNKNOWN_LOCAL_DEFID),
    1148                 :             :                         clone);
    1149                 :        7247 :   mappings.insert_location (clone->get_ref (),
    1150                 :             :                             mappings.lookup_location (get_ref ()));
    1151                 :             : 
    1152                 :             :   // setup the chain to reference this
    1153                 :        7247 :   clone->append_reference (get_ref ());
    1154                 :             : 
    1155                 :        7247 :   return clone;
    1156                 :             : }
    1157                 :             : 
    1158                 :             : bool
    1159                 :        1833 : InferType::default_type (BaseType **type) const
    1160                 :             : {
    1161                 :        1833 :   auto context = Resolver::TypeCheckContext::get ();
    1162                 :        1833 :   bool ok = false;
    1163                 :             : 
    1164                 :             :   // NOTE: Calling this error is misleading.
    1165                 :        1833 :   if (default_hint.kind == TypeKind::ERROR)
    1166                 :             :     {
    1167                 :        1833 :       switch (infer_kind)
    1168                 :             :         {
    1169                 :             :         case GENERAL:
    1170                 :             :           return false;
    1171                 :             : 
    1172                 :        1778 :         case INTEGRAL:
    1173                 :        1778 :           {
    1174                 :        1778 :             ok = context->lookup_builtin ("i32", type);
    1175                 :        1778 :             rust_assert (ok);
    1176                 :             :             return ok;
    1177                 :             :           }
    1178                 :             : 
    1179                 :          43 :         case FLOAT:
    1180                 :          43 :           {
    1181                 :          43 :             ok = context->lookup_builtin ("f64", type);
    1182                 :          43 :             rust_assert (ok);
    1183                 :             :             return ok;
    1184                 :             :           }
    1185                 :             :         }
    1186                 :             :       return false;
    1187                 :             :     }
    1188                 :             : 
    1189                 :           0 :   switch (default_hint.kind)
    1190                 :             :     {
    1191                 :           0 :     case ISIZE:
    1192                 :           0 :       ok = context->lookup_builtin ("isize", type);
    1193                 :           0 :       rust_assert (ok);
    1194                 :             :       return ok;
    1195                 :             : 
    1196                 :           0 :     case USIZE:
    1197                 :           0 :       ok = context->lookup_builtin ("usize", type);
    1198                 :           0 :       rust_assert (ok);
    1199                 :             :       return ok;
    1200                 :             : 
    1201                 :           0 :     case INT:
    1202                 :           0 :       switch (default_hint.szhint)
    1203                 :             :         {
    1204                 :           0 :         case TypeHint::SizeHint::S8:
    1205                 :           0 :           ok = context->lookup_builtin ("i8", type);
    1206                 :           0 :           rust_assert (ok);
    1207                 :             :           return ok;
    1208                 :             : 
    1209                 :           0 :         case TypeHint::SizeHint::S16:
    1210                 :           0 :           ok = context->lookup_builtin ("i16", type);
    1211                 :           0 :           rust_assert (ok);
    1212                 :             :           return ok;
    1213                 :             : 
    1214                 :           0 :         case TypeHint::SizeHint::S32:
    1215                 :           0 :           ok = context->lookup_builtin ("i32", type);
    1216                 :           0 :           rust_assert (ok);
    1217                 :             :           return ok;
    1218                 :             : 
    1219                 :           0 :         case TypeHint::SizeHint::S64:
    1220                 :           0 :           ok = context->lookup_builtin ("i64", type);
    1221                 :           0 :           rust_assert (ok);
    1222                 :             :           return ok;
    1223                 :             : 
    1224                 :           0 :         case TypeHint::SizeHint::S128:
    1225                 :           0 :           ok = context->lookup_builtin ("i128", type);
    1226                 :           0 :           rust_assert (ok);
    1227                 :             :           return ok;
    1228                 :             : 
    1229                 :             :         default:
    1230                 :             :           return false;
    1231                 :             :         }
    1232                 :           0 :       break;
    1233                 :             : 
    1234                 :           0 :     case UINT:
    1235                 :           0 :       switch (default_hint.szhint)
    1236                 :             :         {
    1237                 :           0 :         case TypeHint::SizeHint::S8:
    1238                 :           0 :           ok = context->lookup_builtin ("u8", type);
    1239                 :           0 :           rust_assert (ok);
    1240                 :             :           return ok;
    1241                 :             : 
    1242                 :           0 :         case TypeHint::SizeHint::S16:
    1243                 :           0 :           ok = context->lookup_builtin ("u16", type);
    1244                 :           0 :           rust_assert (ok);
    1245                 :             :           return ok;
    1246                 :             : 
    1247                 :           0 :         case TypeHint::SizeHint::S32:
    1248                 :           0 :           ok = context->lookup_builtin ("u32", type);
    1249                 :           0 :           rust_assert (ok);
    1250                 :             :           return ok;
    1251                 :             : 
    1252                 :           0 :         case TypeHint::SizeHint::S64:
    1253                 :           0 :           ok = context->lookup_builtin ("u64", type);
    1254                 :           0 :           rust_assert (ok);
    1255                 :             :           return ok;
    1256                 :             : 
    1257                 :           0 :         case TypeHint::SizeHint::S128:
    1258                 :           0 :           ok = context->lookup_builtin ("u128", type);
    1259                 :           0 :           rust_assert (ok);
    1260                 :             :           return ok;
    1261                 :             : 
    1262                 :             :         default:
    1263                 :             :           return false;
    1264                 :             :         }
    1265                 :           0 :       break;
    1266                 :             : 
    1267                 :           0 :     case TypeKind::FLOAT:
    1268                 :           0 :       switch (default_hint.szhint)
    1269                 :             :         {
    1270                 :           0 :         case TypeHint::SizeHint::S32:
    1271                 :           0 :           ok = context->lookup_builtin ("f32", type);
    1272                 :           0 :           rust_assert (ok);
    1273                 :             :           return ok;
    1274                 :             : 
    1275                 :           0 :         case TypeHint::SizeHint::S64:
    1276                 :           0 :           ok = context->lookup_builtin ("f64", type);
    1277                 :           0 :           rust_assert (ok);
    1278                 :             :           return ok;
    1279                 :             : 
    1280                 :             :         default:
    1281                 :             :           return false;
    1282                 :             :         }
    1283                 :             :       break;
    1284                 :             : 
    1285                 :             :     default:
    1286                 :             :       return false;
    1287                 :             :     }
    1288                 :             : 
    1289                 :             :   return false;
    1290                 :             : }
    1291                 :             : 
    1292                 :             : void
    1293                 :        5445 : InferType::apply_primitive_type_hint (const BaseType &hint)
    1294                 :             : {
    1295                 :        5445 :   switch (hint.get_kind ())
    1296                 :             :     {
    1297                 :        1310 :     case ISIZE:
    1298                 :        1310 :     case USIZE:
    1299                 :        1310 :       infer_kind = INTEGRAL;
    1300                 :        1310 :       default_hint.kind = hint.get_kind ();
    1301                 :        1310 :       break;
    1302                 :             : 
    1303                 :        3365 :     case INT:
    1304                 :        3365 :       {
    1305                 :        3365 :         infer_kind = INTEGRAL;
    1306                 :        3365 :         default_hint.kind = hint.get_kind ();
    1307                 :        3365 :         default_hint.shint = TypeHint::SignedHint::SIGNED;
    1308                 :        3365 :         switch (hint.as<const IntType> ()->get_int_kind ())
    1309                 :             :           {
    1310                 :          16 :           case IntType::I8:
    1311                 :          16 :             default_hint.szhint = TypeHint::SizeHint::S8;
    1312                 :          16 :             break;
    1313                 :          14 :           case IntType::I16:
    1314                 :          14 :             default_hint.szhint = TypeHint::SizeHint::S16;
    1315                 :          14 :             break;
    1316                 :        3307 :           case IntType::I32:
    1317                 :        3307 :             default_hint.szhint = TypeHint::SizeHint::S32;
    1318                 :        3307 :             break;
    1319                 :          14 :           case IntType::I64:
    1320                 :          14 :             default_hint.szhint = TypeHint::SizeHint::S64;
    1321                 :          14 :             break;
    1322                 :          14 :           case IntType::I128:
    1323                 :          14 :             default_hint.szhint = TypeHint::SizeHint::S128;
    1324                 :          14 :             break;
    1325                 :             :           }
    1326                 :             :       }
    1327                 :             :       break;
    1328                 :             : 
    1329                 :         653 :     case UINT:
    1330                 :         653 :       {
    1331                 :         653 :         infer_kind = INTEGRAL;
    1332                 :         653 :         default_hint.kind = hint.get_kind ();
    1333                 :         653 :         default_hint.shint = TypeHint::SignedHint::UNSIGNED;
    1334                 :         653 :         switch (hint.as<const UintType> ()->get_uint_kind ())
    1335                 :             :           {
    1336                 :         318 :           case UintType::U8:
    1337                 :         318 :             default_hint.szhint = TypeHint::SizeHint::S8;
    1338                 :         318 :             break;
    1339                 :          45 :           case UintType::U16:
    1340                 :          45 :             default_hint.szhint = TypeHint::SizeHint::S16;
    1341                 :          45 :             break;
    1342                 :         196 :           case UintType::U32:
    1343                 :         196 :             default_hint.szhint = TypeHint::SizeHint::S32;
    1344                 :         196 :             break;
    1345                 :          73 :           case UintType::U64:
    1346                 :          73 :             default_hint.szhint = TypeHint::SizeHint::S64;
    1347                 :          73 :             break;
    1348                 :          21 :           case UintType::U128:
    1349                 :          21 :             default_hint.szhint = TypeHint::SizeHint::S128;
    1350                 :          21 :             break;
    1351                 :             :           }
    1352                 :             :       }
    1353                 :             :       break;
    1354                 :             : 
    1355                 :         117 :     case TypeKind::FLOAT:
    1356                 :         117 :       {
    1357                 :         117 :         infer_kind = FLOAT;
    1358                 :         117 :         default_hint.shint = TypeHint::SignedHint::SIGNED;
    1359                 :         117 :         default_hint.kind = hint.get_kind ();
    1360                 :         117 :         switch (hint.as<const FloatType> ()->get_float_kind ())
    1361                 :             :           {
    1362                 :          57 :           case FloatType::F32:
    1363                 :          57 :             default_hint.szhint = TypeHint::SizeHint::S32;
    1364                 :          57 :             break;
    1365                 :             : 
    1366                 :          60 :           case FloatType::F64:
    1367                 :          60 :             default_hint.szhint = TypeHint::SizeHint::S64;
    1368                 :          60 :             break;
    1369                 :             :           }
    1370                 :             :       }
    1371                 :             :       break;
    1372                 :             : 
    1373                 :             :     default:
    1374                 :             :       // TODO bool, char, never??
    1375                 :             :       break;
    1376                 :             :     }
    1377                 :        5445 : }
    1378                 :             : 
    1379                 :             : // ErrorType
    1380                 :             : 
    1381                 :      211366 : ErrorType::ErrorType (HirId ref, std::set<HirId> refs)
    1382                 :             :   : BaseType (ref, ref, KIND,
    1383                 :      211366 :               {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
    1384                 :      211366 : {}
    1385                 :             : 
    1386                 :          18 : ErrorType::ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    1387                 :             :   : BaseType (ref, ty_ref, KIND,
    1388                 :          18 :               {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
    1389                 :          18 : {}
    1390                 :             : 
    1391                 :             : std::string
    1392                 :          53 : ErrorType::get_name () const
    1393                 :             : {
    1394                 :          53 :   return as_string ();
    1395                 :             : }
    1396                 :             : 
    1397                 :             : void
    1398                 :           0 : ErrorType::accept_vis (TyVisitor &vis)
    1399                 :             : {
    1400                 :           0 :   vis.visit (*this);
    1401                 :           0 : }
    1402                 :             : 
    1403                 :             : void
    1404                 :           0 : ErrorType::accept_vis (TyConstVisitor &vis) const
    1405                 :             : {
    1406                 :           0 :   vis.visit (*this);
    1407                 :           0 : }
    1408                 :             : 
    1409                 :             : std::string
    1410                 :          53 : ErrorType::as_string () const
    1411                 :             : {
    1412                 :          53 :   return "<tyty::error>";
    1413                 :             : }
    1414                 :             : 
    1415                 :             : BaseType *
    1416                 :          18 : ErrorType::clone () const
    1417                 :             : {
    1418                 :          18 :   return new ErrorType (get_ref (), get_ty_ref (), get_combined_refs ());
    1419                 :             : }
    1420                 :             : 
    1421                 :             : // Struct Field type
    1422                 :             : 
    1423                 :      247977 : StructFieldType::StructFieldType (HirId ref, std::string name, BaseType *ty,
    1424                 :      247977 :                                   location_t locus)
    1425                 :      247977 :   : ref (ref), name (name), ty (ty), locus (locus)
    1426                 :      247977 : {}
    1427                 :             : 
    1428                 :             : HirId
    1429                 :      247134 : StructFieldType::get_ref () const
    1430                 :             : {
    1431                 :      247134 :   return ref;
    1432                 :             : }
    1433                 :             : 
    1434                 :             : std::string
    1435                 :      473385 : StructFieldType::get_name () const
    1436                 :             : {
    1437                 :      473385 :   return name;
    1438                 :             : }
    1439                 :             : 
    1440                 :             : BaseType *
    1441                 :     1991611 : StructFieldType::get_field_type () const
    1442                 :             : {
    1443                 :     1991611 :   return ty;
    1444                 :             : }
    1445                 :             : 
    1446                 :             : void
    1447                 :        3377 : StructFieldType::set_field_type (BaseType *fty)
    1448                 :             : {
    1449                 :        3377 :   ty = fty;
    1450                 :        3377 : }
    1451                 :             : 
    1452                 :             : void
    1453                 :           0 : StructFieldType::debug () const
    1454                 :             : {
    1455                 :           0 :   rust_debug ("%s", as_string ().c_str ());
    1456                 :           0 : }
    1457                 :             : 
    1458                 :             : location_t
    1459                 :        2275 : StructFieldType::get_locus () const
    1460                 :             : {
    1461                 :        2275 :   return locus;
    1462                 :             : }
    1463                 :             : 
    1464                 :             : std::string
    1465                 :       62205 : StructFieldType::as_string () const
    1466                 :             : {
    1467                 :       62205 :   return name + ":" + get_field_type ()->debug_str ();
    1468                 :             : }
    1469                 :             : 
    1470                 :             : bool
    1471                 :       49753 : StructFieldType::is_equal (const StructFieldType &other) const
    1472                 :             : {
    1473                 :       49753 :   bool names_eq = get_name () == other.get_name ();
    1474                 :             : 
    1475                 :       49753 :   TyTy::BaseType *o = other.get_field_type ();
    1476                 :       49753 :   if (auto op = o->try_as<ParamType> ())
    1477                 :        3785 :     o = op->resolve ();
    1478                 :             : 
    1479                 :       49753 :   bool types_eq = get_field_type ()->is_equal (*o);
    1480                 :             : 
    1481                 :       49753 :   return names_eq && types_eq;
    1482                 :             : }
    1483                 :             : 
    1484                 :             : StructFieldType *
    1485                 :      241519 : StructFieldType::clone () const
    1486                 :             : {
    1487                 :      241519 :   return new StructFieldType (get_ref (), get_name (),
    1488                 :      483038 :                               get_field_type ()->clone (), locus);
    1489                 :             : }
    1490                 :             : 
    1491                 :             : StructFieldType *
    1492                 :        2193 : StructFieldType::monomorphized_clone () const
    1493                 :             : {
    1494                 :        2193 :   return new StructFieldType (get_ref (), get_name (),
    1495                 :        4386 :                               get_field_type ()->monomorphized_clone (), locus);
    1496                 :             : }
    1497                 :             : 
    1498                 :             : // VariantDef
    1499                 :             : 
    1500                 :             : std::string
    1501                 :           5 : VariantDef::variant_type_string (VariantType type)
    1502                 :             : {
    1503                 :           5 :   switch (type)
    1504                 :             :     {
    1505                 :           0 :     case NUM:
    1506                 :           0 :       return "enumeral";
    1507                 :           3 :     case TUPLE:
    1508                 :           3 :       return "tuple";
    1509                 :           2 :     case STRUCT:
    1510                 :           2 :       return "struct";
    1511                 :             :     }
    1512                 :           0 :   rust_unreachable ();
    1513                 :             :   return "";
    1514                 :             : }
    1515                 :             : 
    1516                 :        3555 : VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
    1517                 :             :                         RustIdent ident,
    1518                 :        3555 :                         tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant)
    1519                 :        7110 :   : id (id), defid (defid), identifier (identifier), ident (ident),
    1520                 :        7110 :     discriminant (std::move (discriminant))
    1521                 :             : 
    1522                 :             : {
    1523                 :        3555 :   type = VariantType::NUM;
    1524                 :        3555 :   fields = {};
    1525                 :        3555 : }
    1526                 :             : 
    1527                 :      215300 : VariantDef::VariantDef (HirId id, DefId defid, std::string identifier,
    1528                 :             :                         RustIdent ident, VariantType type,
    1529                 :             :                         tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
    1530                 :      215300 :                         std::vector<StructFieldType *> fields)
    1531                 :      430600 :   : id (id), defid (defid), identifier (identifier), ident (ident), type (type),
    1532                 :      430600 :     discriminant (std::move (discriminant)), fields (fields)
    1533                 :             : {
    1534                 :      215300 :   rust_assert ((type == VariantType::NUM && fields.empty ())
    1535                 :             :                || (type == VariantType::TUPLE || type == VariantType::STRUCT));
    1536                 :      215300 : }
    1537                 :             : 
    1538                 :             : VariantDef &
    1539                 :       12323 : VariantDef::get_error_node ()
    1540                 :             : {
    1541                 :       12323 :   static VariantDef node
    1542                 :        5710 :     = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "",
    1543                 :        2855 :                   {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
    1544                 :       20888 :                   tl::nullopt);
    1545                 :             : 
    1546                 :       12323 :   return node;
    1547                 :             : }
    1548                 :             : 
    1549                 :             : bool
    1550                 :        1802 : VariantDef::is_error () const
    1551                 :             : {
    1552                 :        1802 :   return get_id () == UNKNOWN_HIRID;
    1553                 :             : }
    1554                 :             : 
    1555                 :             : HirId
    1556                 :       17511 : VariantDef::get_id () const
    1557                 :             : {
    1558                 :       17511 :   return id;
    1559                 :             : }
    1560                 :             : 
    1561                 :             : DefId
    1562                 :           0 : VariantDef::get_defid () const
    1563                 :             : {
    1564                 :           0 :   return defid;
    1565                 :             : }
    1566                 :             : 
    1567                 :             : VariantDef::VariantType
    1568                 :      702954 : VariantDef::get_variant_type () const
    1569                 :             : {
    1570                 :      702954 :   return type;
    1571                 :             : }
    1572                 :             : 
    1573                 :             : bool
    1574                 :           0 : VariantDef::is_data_variant () const
    1575                 :             : {
    1576                 :           0 :   return type != VariantType::NUM;
    1577                 :             : }
    1578                 :             : 
    1579                 :             : bool
    1580                 :       12740 : VariantDef::is_dataless_variant () const
    1581                 :             : {
    1582                 :       12740 :   return type == VariantType::NUM;
    1583                 :             : }
    1584                 :             : 
    1585                 :             : std::string
    1586                 :       16104 : VariantDef::get_identifier () const
    1587                 :             : {
    1588                 :       16104 :   return identifier;
    1589                 :             : }
    1590                 :             : 
    1591                 :             : size_t
    1592                 :     1093482 : VariantDef::num_fields () const
    1593                 :             : {
    1594                 :     1093482 :   return fields.size ();
    1595                 :             : }
    1596                 :             : 
    1597                 :             : StructFieldType *
    1598                 :      250219 : VariantDef::get_field_at_index (size_t index)
    1599                 :             : {
    1600                 :      250219 :   rust_assert (index < fields.size ());
    1601                 :      250219 :   return fields.at (index);
    1602                 :             : }
    1603                 :             : 
    1604                 :             : std::vector<StructFieldType *> &
    1605                 :      562918 : VariantDef::get_fields ()
    1606                 :             : {
    1607                 :      562918 :   rust_assert (type != NUM);
    1608                 :      562918 :   return fields;
    1609                 :             : }
    1610                 :             : 
    1611                 :             : bool
    1612                 :       17391 : VariantDef::lookup_field (const std::string &lookup,
    1613                 :             :                           StructFieldType **field_lookup, size_t *index) const
    1614                 :             : {
    1615                 :       17391 :   size_t i = 0;
    1616                 :       88141 :   for (auto &field : fields)
    1617                 :             :     {
    1618                 :       88133 :       if (field->get_name ().compare (lookup) == 0)
    1619                 :             :         {
    1620                 :       17383 :           if (index != nullptr)
    1621                 :       12205 :             *index = i;
    1622                 :             : 
    1623                 :       17383 :           if (field_lookup != nullptr)
    1624                 :       10887 :             *field_lookup = field;
    1625                 :             : 
    1626                 :       17383 :           return true;
    1627                 :             :         }
    1628                 :       70750 :       i++;
    1629                 :             :     }
    1630                 :             :   return false;
    1631                 :             : }
    1632                 :             : 
    1633                 :             : HIR::Expr &
    1634                 :        3566 : VariantDef::get_discriminant ()
    1635                 :             : {
    1636                 :        3566 :   return *discriminant.value ();
    1637                 :             : }
    1638                 :             : 
    1639                 :             : const HIR::Expr &
    1640                 :      107257 : VariantDef::get_discriminant () const
    1641                 :             : {
    1642                 :      107257 :   return *discriminant.value ();
    1643                 :             : }
    1644                 :             : 
    1645                 :             : bool
    1646                 :      229627 : VariantDef::has_discriminant () const
    1647                 :             : {
    1648                 :      229627 :   return discriminant.has_value ();
    1649                 :             : }
    1650                 :             : 
    1651                 :             : std::string
    1652                 :       59664 : VariantDef::as_string () const
    1653                 :             : {
    1654                 :       59664 :   if (type == VariantType::NUM)
    1655                 :       34716 :     return identifier
    1656                 :       34716 :            + (has_discriminant () ? " = " + get_discriminant ().as_string ()
    1657                 :       17358 :                                   : "");
    1658                 :             : 
    1659                 :       42306 :   std::string buffer;
    1660                 :      104511 :   for (size_t i = 0; i < fields.size (); ++i)
    1661                 :             :     {
    1662                 :      124410 :       buffer += fields.at (i)->as_string ();
    1663                 :       62205 :       if ((i + 1) < fields.size ())
    1664                 :       23549 :         buffer += ", ";
    1665                 :             :     }
    1666                 :             : 
    1667                 :       42306 :   if (type == VariantType::TUPLE)
    1668                 :       42632 :     return identifier + " (" + buffer + ")";
    1669                 :             :   else
    1670                 :       41980 :     return identifier + " {" + buffer + "}";
    1671                 :       42306 : }
    1672                 :             : 
    1673                 :             : bool
    1674                 :       55236 : VariantDef::is_equal (const VariantDef &other) const
    1675                 :             : {
    1676                 :       55236 :   if (type != other.type)
    1677                 :             :     return false;
    1678                 :             : 
    1679                 :       55236 :   if (identifier.compare (other.identifier) != 0)
    1680                 :             :     return false;
    1681                 :             : 
    1682                 :       54393 :   if (fields.size () != other.fields.size ())
    1683                 :             :     return false;
    1684                 :             : 
    1685                 :      100870 :   for (size_t i = 0; i < fields.size (); i++)
    1686                 :             :     {
    1687                 :       49753 :       if (!fields.at (i)->is_equal (*other.fields.at (i)))
    1688                 :             :         return false;
    1689                 :             :     }
    1690                 :             : 
    1691                 :             :   return true;
    1692                 :             : }
    1693                 :             : 
    1694                 :             : VariantDef *
    1695                 :      208110 : VariantDef::clone () const
    1696                 :             : {
    1697                 :      208110 :   std::vector<StructFieldType *> cloned_fields;
    1698                 :      449629 :   for (auto &f : fields)
    1699                 :      241519 :     cloned_fields.push_back ((StructFieldType *) f->clone ());
    1700                 :             : 
    1701                 :      208110 :   auto &&discriminant_opt = has_discriminant ()
    1702                 :      208110 :                               ? tl::optional<std::unique_ptr<HIR::Expr>> (
    1703                 :       86460 :                                 get_discriminant ().clone_expr ())
    1704                 :      208110 :                               : tl::nullopt;
    1705                 :             : 
    1706                 :      208110 :   return new VariantDef (id, defid, identifier, ident, type,
    1707                 :      624330 :                          std::move (discriminant_opt), cloned_fields);
    1708                 :      208110 : }
    1709                 :             : 
    1710                 :             : VariantDef *
    1711                 :        4159 : VariantDef::monomorphized_clone () const
    1712                 :             : {
    1713                 :        4159 :   std::vector<StructFieldType *> cloned_fields;
    1714                 :        6352 :   for (auto &f : fields)
    1715                 :        2193 :     cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ());
    1716                 :             : 
    1717                 :        4159 :   auto discriminant_opt = has_discriminant ()
    1718                 :        4159 :                             ? tl::optional<std::unique_ptr<HIR::Expr>> (
    1719                 :        3439 :                               get_discriminant ().clone_expr ())
    1720                 :        4159 :                             : tl::nullopt;
    1721                 :             : 
    1722                 :        4159 :   return new VariantDef (id, defid, identifier, ident, type,
    1723                 :       12477 :                          std::move (discriminant_opt), cloned_fields);
    1724                 :        4159 : }
    1725                 :             : 
    1726                 :             : const RustIdent &
    1727                 :       32120 : VariantDef::get_ident () const
    1728                 :             : {
    1729                 :       32120 :   return ident;
    1730                 :             : }
    1731                 :             : 
    1732                 :             : // ADTType
    1733                 :             : 
    1734                 :           0 : ADTType::ADTType (DefId id, HirId ref, std::string identifier, RustIdent ident,
    1735                 :             :                   ADTKind adt_kind, std::vector<VariantDef *> variants,
    1736                 :             :                   std::vector<SubstitutionParamMapping> subst_refs,
    1737                 :             :                   SubstitutionArgumentMappings generic_arguments,
    1738                 :           0 :                   RegionConstraints region_constraints, std::set<HirId> refs)
    1739                 :             :   : BaseType (ref, ref, TypeKind::ADT, ident, refs),
    1740                 :             :     SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
    1741                 :             :                      region_constraints),
    1742                 :           0 :     id (id), identifier (identifier), variants (variants), adt_kind (adt_kind)
    1743                 :           0 : {}
    1744                 :             : 
    1745                 :         100 : ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
    1746                 :             :                   RustIdent ident, ADTKind adt_kind,
    1747                 :             :                   std::vector<VariantDef *> variants,
    1748                 :             :                   std::vector<SubstitutionParamMapping> subst_refs,
    1749                 :             :                   SubstitutionArgumentMappings generic_arguments,
    1750                 :         100 :                   RegionConstraints region_constraints, std::set<HirId> refs)
    1751                 :             :   : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs),
    1752                 :             :     SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
    1753                 :             :                      region_constraints),
    1754                 :         300 :     id (id), identifier (identifier), variants (variants), adt_kind (adt_kind)
    1755                 :         100 : {}
    1756                 :             : 
    1757                 :      160809 : ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
    1758                 :             :                   RustIdent ident, ADTKind adt_kind,
    1759                 :             :                   std::vector<VariantDef *> variants,
    1760                 :             :                   std::vector<SubstitutionParamMapping> subst_refs,
    1761                 :             :                   ReprOptions repr,
    1762                 :             :                   SubstitutionArgumentMappings generic_arguments,
    1763                 :      160809 :                   RegionConstraints region_constraints, std::set<HirId> refs)
    1764                 :             :   : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs),
    1765                 :             :     SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
    1766                 :             :                      region_constraints),
    1767                 :      160809 :     id (id), identifier (identifier), variants (variants), adt_kind (adt_kind),
    1768                 :      321618 :     repr (repr)
    1769                 :      160809 : {}
    1770                 :             : 
    1771                 :             : void
    1772                 :       10386 : ADTType::accept_vis (TyVisitor &vis)
    1773                 :             : {
    1774                 :       10386 :   vis.visit (*this);
    1775                 :       10386 : }
    1776                 :             : 
    1777                 :             : void
    1778                 :       31598 : ADTType::accept_vis (TyConstVisitor &vis) const
    1779                 :             : {
    1780                 :       31598 :   vis.visit (*this);
    1781                 :       31598 : }
    1782                 :             : 
    1783                 :             : std::string
    1784                 :       42361 : ADTType::as_string () const
    1785                 :             : {
    1786                 :       42361 :   std::string variants_buffer;
    1787                 :      102025 :   for (size_t i = 0; i < number_of_variants (); ++i)
    1788                 :             :     {
    1789                 :       59664 :       TyTy::VariantDef *variant = variants.at (i);
    1790                 :      119328 :       variants_buffer += variant->as_string ();
    1791                 :       59664 :       if ((i + 1) < number_of_variants ())
    1792                 :       17324 :         variants_buffer += ", ";
    1793                 :             :     }
    1794                 :             : 
    1795                 :      127083 :   return identifier + subst_as_string () + "{" + variants_buffer + "}";
    1796                 :       42361 : }
    1797                 :             : 
    1798                 :             : bool
    1799                 :       50050 : ADTType::is_equal (const BaseType &other) const
    1800                 :             : {
    1801                 :       50050 :   if (get_kind () != other.get_kind ())
    1802                 :             :     return false;
    1803                 :             : 
    1804                 :       37814 :   auto other2 = other.as<const ADTType> ();
    1805                 :       37814 :   if (get_adt_kind () != other2->get_adt_kind ())
    1806                 :             :     return false;
    1807                 :             : 
    1808                 :       36747 :   if (number_of_variants () != other2->number_of_variants ())
    1809                 :             :     return false;
    1810                 :             : 
    1811                 :       36747 :   if (has_substitutions_defined () != other2->has_substitutions_defined ())
    1812                 :             :     return false;
    1813                 :             : 
    1814                 :       36321 :   if (has_substitutions_defined ())
    1815                 :             :     {
    1816                 :       11164 :       if (get_num_substitutions () != other2->get_num_substitutions ())
    1817                 :             :         return false;
    1818                 :             : 
    1819                 :       22246 :       for (size_t i = 0; i < get_num_substitutions (); i++)
    1820                 :             :         {
    1821                 :       11707 :           const SubstitutionParamMapping &a = substitutions.at (i);
    1822                 :       11707 :           const SubstitutionParamMapping &b = other2->substitutions.at (i);
    1823                 :             : 
    1824                 :       11707 :           const auto &aa = a.get_param_ty ();
    1825                 :       11707 :           const auto &bb = b.get_param_ty ();
    1826                 :       11707 :           if (!aa->is_equal (*bb))
    1827                 :             :             return false;
    1828                 :             :         }
    1829                 :             :     }
    1830                 :             : 
    1831                 :       86813 :   for (size_t i = 0; i < number_of_variants (); i++)
    1832                 :             :     {
    1833                 :       55236 :       const TyTy::VariantDef *a = get_variants ().at (i);
    1834                 :       55236 :       const TyTy::VariantDef *b = other2->get_variants ().at (i);
    1835                 :             : 
    1836                 :       55236 :       if (!a->is_equal (*b))
    1837                 :             :         return false;
    1838                 :             :     }
    1839                 :             : 
    1840                 :             :   return true;
    1841                 :             : }
    1842                 :             : 
    1843                 :             : DefId
    1844                 :      157863 : ADTType::get_id () const
    1845                 :             : {
    1846                 :      157863 :   return id;
    1847                 :             : }
    1848                 :             : 
    1849                 :             : BaseType *
    1850                 :      155761 : ADTType::clone () const
    1851                 :             : {
    1852                 :      155761 :   std::vector<VariantDef *> cloned_variants;
    1853                 :      362069 :   for (auto &variant : variants)
    1854                 :      206308 :     cloned_variants.push_back (variant->clone ());
    1855                 :             : 
    1856                 :      155761 :   return new ADTType (get_id (), get_ref (), get_ty_ref (), identifier, ident,
    1857                 :      155761 :                       get_adt_kind (), cloned_variants, clone_substs (),
    1858                 :      155761 :                       get_repr_options (), used_arguments,
    1859                 :      467283 :                       get_region_constraints (), get_combined_refs ());
    1860                 :      155761 : }
    1861                 :             : 
    1862                 :             : static bool
    1863                 :       10773 : handle_substitions (SubstitutionArgumentMappings &subst_mappings,
    1864                 :             :                     StructFieldType *field)
    1865                 :             : {
    1866                 :       10773 :   auto fty = field->get_field_type ();
    1867                 :       10773 :   if (auto p = fty->try_as<ParamType> ())
    1868                 :             :     {
    1869                 :        7537 :       SubstitutionArg arg = SubstitutionArg::error ();
    1870                 :        7537 :       bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
    1871                 :        7537 :       if (ok)
    1872                 :             :         {
    1873                 :        7500 :           auto argt = arg.get_tyty ();
    1874                 :        7500 :           bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
    1875                 :        7500 :           bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
    1876                 :             : 
    1877                 :        7500 :           if (arg_is_param || arg_is_concrete)
    1878                 :             :             {
    1879                 :        2306 :               auto new_field = argt->clone ();
    1880                 :        2306 :               new_field->set_ref (fty->get_ref ());
    1881                 :        2306 :               field->set_field_type (new_field);
    1882                 :             :             }
    1883                 :             :           else
    1884                 :             :             {
    1885                 :        5194 :               field->get_field_type ()->set_ty_ref (argt->get_ref ());
    1886                 :             :             }
    1887                 :             :         }
    1888                 :             :     }
    1889                 :        3236 :   else if (fty->has_substitutions_defined () || !fty->is_concrete ())
    1890                 :             :     {
    1891                 :        1071 :       BaseType *concrete
    1892                 :        1071 :         = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
    1893                 :             : 
    1894                 :        1071 :       if (concrete->get_kind () == TyTy::TypeKind::ERROR)
    1895                 :             :         {
    1896                 :           0 :           rust_error_at (subst_mappings.get_locus (),
    1897                 :             :                          "Failed to resolve field substitution type: %s",
    1898                 :           0 :                          fty->as_string ().c_str ());
    1899                 :           0 :           return false;
    1900                 :             :         }
    1901                 :             : 
    1902                 :        1071 :       auto new_field = concrete->clone ();
    1903                 :        1071 :       new_field->set_ref (fty->get_ref ());
    1904                 :        1071 :       field->set_field_type (new_field);
    1905                 :             :     }
    1906                 :             : 
    1907                 :             :   return true;
    1908                 :             : }
    1909                 :             : 
    1910                 :             : ADTType *
    1911                 :        8147 : ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
    1912                 :             : {
    1913                 :        8147 :   auto adt = clone ()->as<ADTType> ();
    1914                 :        8147 :   adt->set_ty_ref (mappings.get_next_hir_id ());
    1915                 :        8147 :   adt->used_arguments = subst_mappings;
    1916                 :             : 
    1917                 :       17059 :   for (auto &sub : adt->get_substs ())
    1918                 :             :     {
    1919                 :        8912 :       SubstitutionArg arg = SubstitutionArg::error ();
    1920                 :        8912 :       bool ok
    1921                 :        8912 :         = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
    1922                 :        8912 :       if (ok)
    1923                 :        8857 :         sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
    1924                 :             :     }
    1925                 :             : 
    1926                 :       20887 :   for (auto &variant : adt->get_variants ())
    1927                 :             :     {
    1928                 :       12740 :       if (variant->is_dataless_variant ())
    1929                 :        4114 :         continue;
    1930                 :             : 
    1931                 :       19399 :       for (auto &field : variant->get_fields ())
    1932                 :             :         {
    1933                 :       10773 :           bool ok = ::Rust::TyTy::handle_substitions (subst_mappings, field);
    1934                 :       10773 :           if (!ok)
    1935                 :        8147 :             return adt;
    1936                 :             :         }
    1937                 :             :     }
    1938                 :             : 
    1939                 :             :   return adt;
    1940                 :             : }
    1941                 :             : 
    1942                 :             : // TupleType
    1943                 :             : 
    1944                 :        5716 : TupleType::TupleType (HirId ref, location_t locus, std::vector<TyVar> fields,
    1945                 :        5716 :                       std::set<HirId> refs)
    1946                 :        5716 :   : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
    1947                 :             :               refs),
    1948                 :       11432 :     fields (fields)
    1949                 :        5716 : {}
    1950                 :             : 
    1951                 :       14646 : TupleType::TupleType (HirId ref, HirId ty_ref, location_t locus,
    1952                 :       14646 :                       std::vector<TyVar> fields, std::set<HirId> refs)
    1953                 :             :   : BaseType (ref, ty_ref, KIND,
    1954                 :       14646 :               {Resolver::CanonicalPath::create_empty (), locus}, refs),
    1955                 :       29292 :     fields (fields)
    1956                 :       14646 : {}
    1957                 :             : 
    1958                 :             : TupleType *
    1959                 :       36761 : TupleType::get_unit_type ()
    1960                 :             : {
    1961                 :       36761 :   static TupleType *ret = nullptr;
    1962                 :       36761 :   if (ret == nullptr)
    1963                 :        8898 :     ret = new TupleType (Analysis::Mappings::get ().get_next_hir_id (),
    1964                 :        8898 :                          BUILTINS_LOCATION);
    1965                 :       36761 :   return ret;
    1966                 :             : }
    1967                 :             : 
    1968                 :             : size_t
    1969                 :      136230 : TupleType::num_fields () const
    1970                 :             : {
    1971                 :      136230 :   return fields.size ();
    1972                 :             : }
    1973                 :             : 
    1974                 :             : const std::vector<TyVar> &
    1975                 :      170579 : TupleType::get_fields () const
    1976                 :             : {
    1977                 :      170579 :   return fields;
    1978                 :             : }
    1979                 :             : 
    1980                 :             : void
    1981                 :         412 : TupleType::accept_vis (TyVisitor &vis)
    1982                 :             : {
    1983                 :         412 :   vis.visit (*this);
    1984                 :         412 : }
    1985                 :             : 
    1986                 :             : void
    1987                 :       21141 : TupleType::accept_vis (TyConstVisitor &vis) const
    1988                 :             : {
    1989                 :       21141 :   vis.visit (*this);
    1990                 :       21141 : }
    1991                 :             : 
    1992                 :             : std::string
    1993                 :       22331 : TupleType::as_string () const
    1994                 :             : {
    1995                 :       22331 :   size_t i = 0;
    1996                 :       22331 :   std::string fields_buffer;
    1997                 :       29646 :   for (const TyVar &field : get_fields ())
    1998                 :             :     {
    1999                 :       14630 :       fields_buffer += field.get_tyty ()->as_string ();
    2000                 :        7315 :       bool has_next = (i + 1) < get_fields ().size ();
    2001                 :        7315 :       fields_buffer += has_next ? ", " : "";
    2002                 :        7315 :       i++;
    2003                 :             :     }
    2004                 :       44662 :   return "(" + fields_buffer + ")";
    2005                 :       22331 : }
    2006                 :             : 
    2007                 :             : std::string
    2008                 :      114505 : TupleType::get_name () const
    2009                 :             : {
    2010                 :      114505 :   size_t i = 0;
    2011                 :      114505 :   std::string fields_buffer;
    2012                 :      135524 :   for (const TyVar &field : get_fields ())
    2013                 :             :     {
    2014                 :       42038 :       fields_buffer += field.get_tyty ()->get_name ();
    2015                 :       21019 :       bool has_next = (i + 1) < get_fields ().size ();
    2016                 :       21019 :       fields_buffer += has_next ? ", " : "";
    2017                 :       21019 :       i++;
    2018                 :             :     }
    2019                 :      229010 :   return "(" + fields_buffer + ")";
    2020                 :      114505 : }
    2021                 :             : 
    2022                 :             : BaseType *
    2023                 :       15577 : TupleType::get_field (size_t index) const
    2024                 :             : {
    2025                 :       15577 :   return fields.at (index).get_tyty ();
    2026                 :             : }
    2027                 :             : 
    2028                 :             : bool
    2029                 :       12655 : TupleType::is_equal (const BaseType &other) const
    2030                 :             : {
    2031                 :       12655 :   if (get_kind () != other.get_kind ())
    2032                 :             :     return false;
    2033                 :             : 
    2034                 :       12620 :   auto other2 = other.as<const TupleType> ();
    2035                 :       12620 :   if (num_fields () != other2->num_fields ())
    2036                 :             :     return false;
    2037                 :             : 
    2038                 :       14643 :   for (size_t i = 0; i < num_fields (); i++)
    2039                 :             :     {
    2040                 :        2194 :       if (!get_field (i)->is_equal (*other2->get_field (i)))
    2041                 :             :         return false;
    2042                 :             :     }
    2043                 :             :   return true;
    2044                 :             : }
    2045                 :             : 
    2046                 :             : BaseType *
    2047                 :       10055 : TupleType::clone () const
    2048                 :             : {
    2049                 :       10055 :   std::vector<TyVar> cloned_fields;
    2050                 :       17752 :   for (const auto &f : fields)
    2051                 :        7697 :     cloned_fields.push_back (f.clone ());
    2052                 :             : 
    2053                 :       10055 :   return new TupleType (get_ref (), get_ty_ref (), get_ident ().locus,
    2054                 :       20110 :                         cloned_fields, get_combined_refs ());
    2055                 :       10055 : }
    2056                 :             : 
    2057                 :             : TupleType *
    2058                 :         264 : TupleType::handle_substitions (SubstitutionArgumentMappings &mappings)
    2059                 :             : {
    2060                 :         264 :   auto &mappings_table = Analysis::Mappings::get ();
    2061                 :             : 
    2062                 :         264 :   auto tuple = clone ()->as<TupleType> ();
    2063                 :         264 :   tuple->set_ref (mappings_table.get_next_hir_id ());
    2064                 :         264 :   tuple->set_ty_ref (mappings_table.get_next_hir_id ());
    2065                 :             : 
    2066                 :         767 :   for (size_t i = 0; i < tuple->fields.size (); i++)
    2067                 :             :     {
    2068                 :         503 :       TyVar &field = fields.at (i);
    2069                 :         503 :       if (!field.get_tyty ()->is_concrete ())
    2070                 :             :         {
    2071                 :         311 :           BaseType *concrete
    2072                 :         311 :             = Resolver::SubstMapperInternal::Resolve (field.get_tyty (),
    2073                 :             :                                                       mappings);
    2074                 :         311 :           tuple->fields[i]
    2075                 :         311 :             = TyVar::subst_covariant_var (field.get_tyty (), concrete);
    2076                 :             :         }
    2077                 :             :     }
    2078                 :             : 
    2079                 :         264 :   return tuple;
    2080                 :             : }
    2081                 :             : 
    2082                 :             : void
    2083                 :       20400 : FnType::accept_vis (TyVisitor &vis)
    2084                 :             : {
    2085                 :       20400 :   vis.visit (*this);
    2086                 :       20400 : }
    2087                 :             : 
    2088                 :             : void
    2089                 :       16183 : FnType::accept_vis (TyConstVisitor &vis) const
    2090                 :             : {
    2091                 :       16183 :   vis.visit (*this);
    2092                 :       16183 : }
    2093                 :             : 
    2094                 :             : std::string
    2095                 :       92949 : FnType::as_string () const
    2096                 :             : {
    2097                 :       92949 :   std::string params_str = "";
    2098                 :      216132 :   for (auto &param : params)
    2099                 :             :     {
    2100                 :      123183 :       auto &pattern = param.get_pattern ();
    2101                 :      123183 :       auto ty = param.get_type ();
    2102                 :      369549 :       params_str += pattern.as_string () + " " + ty->as_string ();
    2103                 :      123183 :       params_str += ",";
    2104                 :             :     }
    2105                 :             : 
    2106                 :       92949 :   std::string ret_str = type->as_string ();
    2107                 :      278847 :   return "fn" + subst_as_string () + " (" + params_str + ") -> " + ret_str;
    2108                 :       92949 : }
    2109                 :             : 
    2110                 :             : bool
    2111                 :        8570 : FnType::is_equal (const BaseType &other) const
    2112                 :             : {
    2113                 :        8570 :   if (get_kind () != other.get_kind ())
    2114                 :             :     return false;
    2115                 :             : 
    2116                 :        8570 :   auto &other2 = static_cast<const FnType &> (other);
    2117                 :       25710 :   if (get_identifier ().compare (other2.get_identifier ()) != 0)
    2118                 :             :     return false;
    2119                 :             : 
    2120                 :        8570 :   if (!get_return_type ()->is_equal (*other2.get_return_type ()))
    2121                 :             :     return false;
    2122                 :             : 
    2123                 :        7036 :   if (has_substitutions_defined () != other2.has_substitutions_defined ())
    2124                 :             :     return false;
    2125                 :             : 
    2126                 :        4189 :   if (has_substitutions_defined ())
    2127                 :             :     {
    2128                 :         609 :       if (get_num_substitutions () != other2.get_num_substitutions ())
    2129                 :             :         return false;
    2130                 :             : 
    2131                 :             :       const FnType &ofn = static_cast<const FnType &> (other);
    2132                 :         911 :       for (size_t i = 0; i < get_num_substitutions (); i++)
    2133                 :             :         {
    2134                 :         590 :           const SubstitutionParamMapping &a = get_substs ().at (i);
    2135                 :         590 :           const SubstitutionParamMapping &b = ofn.get_substs ().at (i);
    2136                 :             : 
    2137                 :         590 :           const auto *pa = a.get_param_ty ();
    2138                 :         590 :           const auto *pb = b.get_param_ty ();
    2139                 :         590 :           if (!pa->is_equal (*pb))
    2140                 :             :             return false;
    2141                 :             :         }
    2142                 :             :     }
    2143                 :             : 
    2144                 :        3901 :   if (num_params () != other2.num_params ())
    2145                 :             :     return false;
    2146                 :             : 
    2147                 :       10406 :   for (size_t i = 0; i < num_params (); i++)
    2148                 :             :     {
    2149                 :        6506 :       auto lhs = param_at (i).get_type ();
    2150                 :        6506 :       auto rhs = other2.param_at (i).get_type ();
    2151                 :        6506 :       if (!lhs->is_equal (*rhs))
    2152                 :             :         return false;
    2153                 :             :     }
    2154                 :             :   return true;
    2155                 :             : }
    2156                 :             : 
    2157                 :             : BaseType *
    2158                 :       12887 : FnType::clone () const
    2159                 :             : {
    2160                 :       12887 :   std::vector<TyTy::FnParam> cloned_params;
    2161                 :       32265 :   for (auto &p : params)
    2162                 :       19378 :     cloned_params.push_back (p.clone ());
    2163                 :             : 
    2164                 :       25774 :   return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (),
    2165                 :       12887 :                      ident, flags, abi, std::move (cloned_params),
    2166                 :       12887 :                      get_return_type ()->clone (), clone_substs (),
    2167                 :             :                      get_substitution_arguments (), get_region_constraints (),
    2168                 :       64435 :                      get_combined_refs ());
    2169                 :       12887 : }
    2170                 :             : 
    2171                 :             : FnType *
    2172                 :       11476 : FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
    2173                 :             : {
    2174                 :       11476 :   FnType *fn = static_cast<FnType *> (clone ());
    2175                 :       11476 :   fn->set_ty_ref (mappings.get_next_hir_id ());
    2176                 :       11476 :   fn->used_arguments = subst_mappings;
    2177                 :             : 
    2178                 :       27283 :   for (auto &sub : fn->get_substs ())
    2179                 :             :     {
    2180                 :       15807 :       SubstitutionArg arg = SubstitutionArg::error ();
    2181                 :             : 
    2182                 :       15807 :       bool ok
    2183                 :       15807 :         = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
    2184                 :       15807 :       if (ok)
    2185                 :             :         {
    2186                 :       15338 :           sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
    2187                 :             :         }
    2188                 :             :     }
    2189                 :             : 
    2190                 :       11476 :   auto fty = fn->get_return_type ();
    2191                 :       11476 :   bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
    2192                 :       11476 :   if (is_param_ty)
    2193                 :             :     {
    2194                 :        2882 :       ParamType *p = static_cast<ParamType *> (fty);
    2195                 :             : 
    2196                 :        2882 :       SubstitutionArg arg = SubstitutionArg::error ();
    2197                 :        2882 :       bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
    2198                 :        2882 :       if (ok)
    2199                 :             :         {
    2200                 :        2875 :           auto argt = arg.get_tyty ();
    2201                 :        2875 :           bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
    2202                 :        2875 :           bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
    2203                 :             : 
    2204                 :        2875 :           if (arg_is_param || arg_is_concrete)
    2205                 :             :             {
    2206                 :        1413 :               auto new_field = argt->clone ();
    2207                 :        1413 :               new_field->set_ref (fty->get_ref ());
    2208                 :        1413 :               fn->type = new_field;
    2209                 :             :             }
    2210                 :             :           else
    2211                 :             :             {
    2212                 :        1462 :               fty->set_ty_ref (argt->get_ref ());
    2213                 :             :             }
    2214                 :             :         }
    2215                 :             :     }
    2216                 :        8594 :   else if (fty->needs_generic_substitutions () || !fty->is_concrete ())
    2217                 :             :     {
    2218                 :        1840 :       BaseType *concrete
    2219                 :        1840 :         = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
    2220                 :             : 
    2221                 :        1840 :       if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR)
    2222                 :             :         {
    2223                 :           0 :           rust_error_at (subst_mappings.get_locus (),
    2224                 :             :                          "Failed to resolve field substitution type: %s",
    2225                 :           0 :                          fty->as_string ().c_str ());
    2226                 :           0 :           return nullptr;
    2227                 :             :         }
    2228                 :             : 
    2229                 :        1840 :       auto new_field = concrete->clone ();
    2230                 :        1840 :       new_field->set_ref (fty->get_ref ());
    2231                 :        1840 :       fn->type = new_field;
    2232                 :             :     }
    2233                 :             : 
    2234                 :       28656 :   for (auto &param : fn->get_params ())
    2235                 :             :     {
    2236                 :       17180 :       auto fty = param.get_type ();
    2237                 :             : 
    2238                 :       17180 :       bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
    2239                 :       17180 :       if (is_param_ty)
    2240                 :             :         {
    2241                 :        5125 :           ParamType *p = static_cast<ParamType *> (fty);
    2242                 :             : 
    2243                 :        5125 :           SubstitutionArg arg = SubstitutionArg::error ();
    2244                 :        5125 :           bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
    2245                 :        5125 :           if (ok)
    2246                 :             :             {
    2247                 :        5074 :               auto argt = arg.get_tyty ();
    2248                 :        5074 :               bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
    2249                 :        5074 :               bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
    2250                 :             : 
    2251                 :        5074 :               if (arg_is_param || arg_is_concrete)
    2252                 :             :                 {
    2253                 :        2112 :                   auto new_field = argt->clone ();
    2254                 :        2112 :                   new_field->set_ref (fty->get_ref ());
    2255                 :        2112 :                   param.set_type (new_field);
    2256                 :             :                 }
    2257                 :             :               else
    2258                 :             :                 {
    2259                 :        2962 :                   fty->set_ty_ref (argt->get_ref ());
    2260                 :             :                 }
    2261                 :             :             }
    2262                 :             :         }
    2263                 :       12055 :       else if (fty->has_substitutions_defined () || !fty->is_concrete ())
    2264                 :             :         {
    2265                 :       10898 :           BaseType *concrete
    2266                 :       10898 :             = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
    2267                 :             : 
    2268                 :       10898 :           if (concrete == nullptr
    2269                 :       10898 :               || concrete->get_kind () == TyTy::TypeKind::ERROR)
    2270                 :             :             {
    2271                 :           0 :               rust_error_at (subst_mappings.get_locus (),
    2272                 :             :                              "Failed to resolve field substitution type: %s",
    2273                 :           0 :                              fty->as_string ().c_str ());
    2274                 :           0 :               return nullptr;
    2275                 :             :             }
    2276                 :             : 
    2277                 :       10898 :           auto new_field = concrete->clone ();
    2278                 :       10898 :           new_field->set_ref (fty->get_ref ());
    2279                 :       10898 :           param.set_type (new_field);
    2280                 :             :         }
    2281                 :             :     }
    2282                 :             : 
    2283                 :             :   return fn;
    2284                 :             : }
    2285                 :             : 
    2286                 :             : void
    2287                 :          46 : FnPtr::accept_vis (TyVisitor &vis)
    2288                 :             : {
    2289                 :          46 :   vis.visit (*this);
    2290                 :          46 : }
    2291                 :             : 
    2292                 :             : void
    2293                 :         186 : FnPtr::accept_vis (TyConstVisitor &vis) const
    2294                 :             : {
    2295                 :         186 :   vis.visit (*this);
    2296                 :         186 : }
    2297                 :             : 
    2298                 :             : std::string
    2299                 :        9412 : FnPtr::as_string () const
    2300                 :             : {
    2301                 :        9412 :   std::string params_str;
    2302                 :             : 
    2303                 :        9412 :   auto &params = get_params ();
    2304                 :       10128 :   for (auto &p : params)
    2305                 :             :     {
    2306                 :        2148 :       params_str += p.get_tyty ()->as_string () + " ,";
    2307                 :             :     }
    2308                 :             : 
    2309                 :        9412 :   std::string unsafety = "";
    2310                 :        9412 :   if (get_unsafety () == Unsafety::Unsafe)
    2311                 :        3832 :     unsafety = "unsafe ";
    2312                 :             : 
    2313                 :        9412 :   std::string abi = get_string_from_abi (get_abi ());
    2314                 :       37648 :   return unsafety + "abi:" + abi + " " + "fnptr (" + params_str + ") -> "
    2315                 :       28236 :          + get_return_type ()->as_string ();
    2316                 :        9412 : }
    2317                 :             : 
    2318                 :             : bool
    2319                 :         322 : FnPtr::is_equal (const BaseType &other) const
    2320                 :             : {
    2321                 :         322 :   if (get_kind () != other.get_kind ())
    2322                 :             :     return false;
    2323                 :             : 
    2324                 :         110 :   auto other2 = static_cast<const FnPtr &> (other);
    2325                 :         110 :   auto this_ret_type = get_return_type ();
    2326                 :         110 :   auto other_ret_type = other2.get_return_type ();
    2327                 :         110 :   if (this_ret_type->is_equal (*other_ret_type))
    2328                 :             :     return false;
    2329                 :             : 
    2330                 :          27 :   if (num_params () != other2.num_params ())
    2331                 :             :     return false;
    2332                 :             : 
    2333                 :          27 :   for (size_t i = 0; i < num_params (); i++)
    2334                 :             :     {
    2335                 :           0 :       if (!get_param_type_at (i)->is_equal (*other2.get_param_type_at (i)))
    2336                 :             :         return false;
    2337                 :             :     }
    2338                 :             :   return true;
    2339                 :         110 : }
    2340                 :             : 
    2341                 :             : BaseType *
    2342                 :        1384 : FnPtr::clone () const
    2343                 :             : {
    2344                 :        1384 :   std::vector<TyVar> cloned_params;
    2345                 :        1384 :   cloned_params.reserve (params.size ());
    2346                 :             : 
    2347                 :        1415 :   for (auto &p : params)
    2348                 :          31 :     cloned_params.emplace_back (p.get_ref ());
    2349                 :             : 
    2350                 :        1384 :   return new FnPtr (get_ref (), get_ty_ref (), ident.locus,
    2351                 :             :                     std::move (cloned_params), result_type, get_abi (),
    2352                 :        1384 :                     get_unsafety (), get_combined_refs ());
    2353                 :        1384 : }
    2354                 :             : 
    2355                 :             : FnPtr *
    2356                 :           8 : FnPtr::handle_substitions (SubstitutionArgumentMappings &mappings)
    2357                 :             : {
    2358                 :           8 :   auto &mappings_table = Analysis::Mappings::get ();
    2359                 :             : 
    2360                 :           8 :   auto fn = clone ()->as<FnPtr> ();
    2361                 :           8 :   fn->set_ref (mappings_table.get_next_hir_id ());
    2362                 :           8 :   fn->set_ty_ref (mappings_table.get_next_hir_id ());
    2363                 :             : 
    2364                 :           8 :   if (!fn->result_type.get_tyty ()->is_concrete ())
    2365                 :             :     {
    2366                 :           4 :       BaseType *concrete
    2367                 :           4 :         = Resolver::SubstMapperInternal::Resolve (fn->result_type.get_tyty (),
    2368                 :             :                                                   mappings);
    2369                 :           4 :       fn->result_type
    2370                 :           4 :         = TyVar::subst_covariant_var (fn->result_type.get_tyty (), concrete);
    2371                 :             :     }
    2372                 :             : 
    2373                 :          16 :   for (size_t i = 0; i < fn->params.size (); i++)
    2374                 :             :     {
    2375                 :           8 :       TyVar &field = fn->params.at (i);
    2376                 :           8 :       if (!field.get_tyty ()->is_concrete ())
    2377                 :             :         {
    2378                 :           4 :           BaseType *concrete
    2379                 :           4 :             = Resolver::SubstMapperInternal::Resolve (field.get_tyty (),
    2380                 :             :                                                       mappings);
    2381                 :           4 :           fn->params[i]
    2382                 :           4 :             = TyVar::subst_covariant_var (field.get_tyty (), concrete);
    2383                 :             :         }
    2384                 :             :     }
    2385                 :             : 
    2386                 :           8 :   return fn;
    2387                 :             : }
    2388                 :             : 
    2389                 :             : void
    2390                 :           0 : ClosureType::accept_vis (TyVisitor &vis)
    2391                 :             : {
    2392                 :           0 :   vis.visit (*this);
    2393                 :           0 : }
    2394                 :             : 
    2395                 :             : void
    2396                 :         243 : ClosureType::accept_vis (TyConstVisitor &vis) const
    2397                 :             : {
    2398                 :         243 :   vis.visit (*this);
    2399                 :         243 : }
    2400                 :             : 
    2401                 :             : std::string
    2402                 :        2275 : ClosureType::as_string () const
    2403                 :             : {
    2404                 :        2275 :   std::string params_buf = parameters->as_string ();
    2405                 :        6825 :   return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string () + "}";
    2406                 :        2275 : }
    2407                 :             : 
    2408                 :             : bool
    2409                 :         161 : ClosureType::is_equal (const BaseType &other) const
    2410                 :             : {
    2411                 :         161 :   if (other.get_kind () != TypeKind::CLOSURE)
    2412                 :             :     return false;
    2413                 :             : 
    2414                 :         161 :   const ClosureType &other2 = static_cast<const ClosureType &> (other);
    2415                 :         161 :   if (get_def_id () != other2.get_def_id ())
    2416                 :             :     return false;
    2417                 :             : 
    2418                 :         161 :   if (!get_parameters ().is_equal (other2.get_parameters ()))
    2419                 :             :     return false;
    2420                 :             : 
    2421                 :         161 :   return get_result_type ().is_equal (other2.get_result_type ());
    2422                 :             : }
    2423                 :             : 
    2424                 :             : BaseType *
    2425                 :         319 : ClosureType::clone () const
    2426                 :             : {
    2427                 :         319 :   return new ClosureType (get_ref (), get_ty_ref (), ident, id,
    2428                 :         319 :                           (TyTy::TupleType *) parameters->clone (), result_type,
    2429                 :         638 :                           clone_substs (), captures, get_combined_refs (),
    2430                 :        1276 :                           specified_bounds);
    2431                 :             : }
    2432                 :             : 
    2433                 :             : ClosureType *
    2434                 :           0 : ClosureType::handle_substitions (SubstitutionArgumentMappings &mappings)
    2435                 :             : {
    2436                 :           0 :   rust_unreachable ();
    2437                 :             :   return nullptr;
    2438                 :             : }
    2439                 :             : 
    2440                 :             : void
    2441                 :         100 : ClosureType::setup_fn_once_output () const
    2442                 :             : {
    2443                 :             :   // lookup the lang items
    2444                 :         100 :   auto fn_once_lookup = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE);
    2445                 :         100 :   auto fn_once_output_lookup
    2446                 :         100 :     = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE_OUTPUT);
    2447                 :         100 :   if (!fn_once_lookup)
    2448                 :             :     {
    2449                 :           0 :       rust_fatal_error (UNKNOWN_LOCATION,
    2450                 :             :                         "Missing required %<fn_once%> lang item");
    2451                 :             :       return;
    2452                 :             :     }
    2453                 :         100 :   if (!fn_once_output_lookup)
    2454                 :             :     {
    2455                 :           0 :       rust_fatal_error (UNKNOWN_LOCATION,
    2456                 :             :                         "Missing required %<fn_once_ouput%> lang item");
    2457                 :             :       return;
    2458                 :             :     }
    2459                 :             : 
    2460                 :         100 :   DefId &trait_id = fn_once_lookup.value ();
    2461                 :         100 :   DefId &trait_item_id = fn_once_output_lookup.value ();
    2462                 :             : 
    2463                 :             :   // resolve to the trait
    2464                 :         100 :   HIR::Item *item = mappings.lookup_defid (trait_id).value ();
    2465                 :         100 :   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
    2466                 :         100 :   HIR::Trait *trait = static_cast<HIR::Trait *> (item);
    2467                 :             : 
    2468                 :         100 :   Resolver::TraitReference *trait_ref
    2469                 :         100 :     = Resolver::TraitResolver::Resolve (*trait);
    2470                 :         100 :   rust_assert (!trait_ref->is_error ());
    2471                 :             : 
    2472                 :             :   // resolve to trait item
    2473                 :         100 :   HIR::TraitItem *trait_item
    2474                 :         100 :     = mappings.lookup_trait_item_defid (trait_item_id).value ();
    2475                 :         100 :   rust_assert (trait_item->get_item_kind ()
    2476                 :             :                == HIR::TraitItem::TraitItemKind::TYPE);
    2477                 :         100 :   std::string item_identifier = trait_item->trait_identifier ();
    2478                 :             : 
    2479                 :             :   // setup associated types  #[lang = "fn_once_output"]
    2480                 :         100 :   Resolver::TraitItemReference *item_reference = nullptr;
    2481                 :         100 :   bool found = trait_ref->lookup_trait_item_by_type (
    2482                 :             :     item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
    2483                 :             :     &item_reference);
    2484                 :         100 :   rust_assert (found);
    2485                 :             : 
    2486                 :             :   // setup
    2487                 :         100 :   item_reference->associated_type_set (&get_result_type ());
    2488                 :         100 : }
    2489                 :             : 
    2490                 :             : void
    2491                 :         107 : ArrayType::accept_vis (TyVisitor &vis)
    2492                 :             : {
    2493                 :         107 :   vis.visit (*this);
    2494                 :         107 : }
    2495                 :             : 
    2496                 :             : void
    2497                 :        4092 : ArrayType::accept_vis (TyConstVisitor &vis) const
    2498                 :             : {
    2499                 :        4092 :   vis.visit (*this);
    2500                 :        4092 : }
    2501                 :             : 
    2502                 :             : std::string
    2503                 :       56839 : ArrayType::as_string () const
    2504                 :             : {
    2505                 :       56839 :   auto cap = get_capacity ();
    2506                 :       56839 :   std::string capacity_str = cap->as_string ();
    2507                 :             : 
    2508                 :      170517 :   return "[" + get_element_type ()->as_string () + "; " + capacity_str + "]";
    2509                 :       56839 : }
    2510                 :             : 
    2511                 :             : bool
    2512                 :        2405 : ArrayType::is_equal (const BaseType &other) const
    2513                 :             : {
    2514                 :        2405 :   if (get_kind () != other.get_kind ())
    2515                 :             :     return false;
    2516                 :             : 
    2517                 :        2352 :   auto other2 = static_cast<const ArrayType &> (other);
    2518                 :             : 
    2519                 :        2352 :   auto this_element_type = get_element_type ();
    2520                 :        2352 :   auto other_element_type = other2.get_element_type ();
    2521                 :             : 
    2522                 :        2352 :   return this_element_type->is_equal (*other_element_type);
    2523                 :        2352 : }
    2524                 :             : 
    2525                 :             : BaseType *
    2526                 :       84772 : ArrayType::get_element_type () const
    2527                 :             : {
    2528                 :       84772 :   return element_type.get_tyty ();
    2529                 :             : }
    2530                 :             : 
    2531                 :             : const TyVar &
    2532                 :          80 : ArrayType::get_var_element_type () const
    2533                 :             : {
    2534                 :          80 :   return element_type;
    2535                 :             : }
    2536                 :             : 
    2537                 :             : BaseType *
    2538                 :       79303 : ArrayType::get_capacity () const
    2539                 :             : {
    2540                 :       79303 :   return capacity.get_tyty ();
    2541                 :             : }
    2542                 :             : 
    2543                 :             : BaseType *
    2544                 :         784 : ArrayType::clone () const
    2545                 :             : {
    2546                 :        1568 :   return new ArrayType (get_ref (), get_ty_ref (), ident.locus, capacity,
    2547                 :         784 :                         element_type, get_combined_refs ());
    2548                 :             : }
    2549                 :             : 
    2550                 :             : ArrayType *
    2551                 :          57 : ArrayType::handle_substitions (SubstitutionArgumentMappings &mappings)
    2552                 :             : {
    2553                 :          57 :   auto &mappings_table = Analysis::Mappings::get ();
    2554                 :             : 
    2555                 :          57 :   ArrayType *ref = static_cast<ArrayType *> (clone ());
    2556                 :          57 :   ref->set_ty_ref (mappings_table.get_next_hir_id ());
    2557                 :             : 
    2558                 :             :   // might be &T or &ADT so this needs to be recursive
    2559                 :          57 :   auto base = ref->get_element_type ();
    2560                 :          57 :   BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
    2561                 :          57 :   ref->element_type = TyVar::subst_covariant_var (base, concrete);
    2562                 :             : 
    2563                 :             :   // handle capacity type
    2564                 :          57 :   auto cap = ref->get_capacity ();
    2565                 :          57 :   BaseType *concrete_cap
    2566                 :          57 :     = Resolver::SubstMapperInternal::Resolve (cap, mappings);
    2567                 :          57 :   rust_assert (concrete_cap->get_kind () == TyTy::TypeKind::CONST);
    2568                 :          57 :   ref->capacity = TyVar::subst_covariant_var (cap, concrete_cap);
    2569                 :             : 
    2570                 :          57 :   return ref;
    2571                 :             : }
    2572                 :             : 
    2573                 :             : void
    2574                 :        1418 : SliceType::accept_vis (TyVisitor &vis)
    2575                 :             : {
    2576                 :        1418 :   vis.visit (*this);
    2577                 :        1418 : }
    2578                 :             : 
    2579                 :             : void
    2580                 :         602 : SliceType::accept_vis (TyConstVisitor &vis) const
    2581                 :             : {
    2582                 :         602 :   vis.visit (*this);
    2583                 :         602 : }
    2584                 :             : 
    2585                 :             : std::string
    2586                 :       50684 : SliceType::as_string () const
    2587                 :             : {
    2588                 :      101368 :   return "[" + get_element_type ()->as_string () + "]";
    2589                 :             : }
    2590                 :             : 
    2591                 :             : bool
    2592                 :        4711 : SliceType::is_equal (const BaseType &other) const
    2593                 :             : {
    2594                 :        4711 :   if (get_kind () != other.get_kind ())
    2595                 :             :     return false;
    2596                 :             : 
    2597                 :        3668 :   auto other2 = static_cast<const SliceType &> (other);
    2598                 :             : 
    2599                 :        3668 :   auto this_element_type = get_element_type ();
    2600                 :        3668 :   auto other_element_type = other2.get_element_type ();
    2601                 :             : 
    2602                 :        3668 :   return this_element_type->is_equal (*other_element_type);
    2603                 :        3668 : }
    2604                 :             : 
    2605                 :             : BaseType *
    2606                 :       80812 : SliceType::get_element_type () const
    2607                 :             : {
    2608                 :       80812 :   return element_type.get_tyty ();
    2609                 :             : }
    2610                 :             : 
    2611                 :             : const TyVar &
    2612                 :         161 : SliceType::get_var_element_type () const
    2613                 :             : {
    2614                 :         161 :   return element_type;
    2615                 :             : }
    2616                 :             : 
    2617                 :             : BaseType *
    2618                 :       49901 : SliceType::clone () const
    2619                 :             : {
    2620                 :       99802 :   return new SliceType (get_ref (), get_ty_ref (), ident.locus,
    2621                 :       49901 :                         element_type.clone (), get_combined_refs ());
    2622                 :             : }
    2623                 :             : 
    2624                 :             : SliceType *
    2625                 :        1128 : SliceType::handle_substitions (SubstitutionArgumentMappings &mappings)
    2626                 :             : {
    2627                 :        1128 :   auto &mappings_table = Analysis::Mappings::get ();
    2628                 :             : 
    2629                 :        1128 :   SliceType *ref = static_cast<SliceType *> (clone ());
    2630                 :        1128 :   ref->set_ty_ref (mappings_table.get_next_hir_id ());
    2631                 :             : 
    2632                 :             :   // might be &T or &ADT so this needs to be recursive
    2633                 :        1128 :   auto base = ref->get_element_type ();
    2634                 :        1128 :   BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
    2635                 :        1128 :   ref->element_type = TyVar::subst_covariant_var (base, concrete);
    2636                 :             : 
    2637                 :        1128 :   return ref;
    2638                 :             : }
    2639                 :             : 
    2640                 :             : // BoolType
    2641                 :             : 
    2642                 :        8893 : BoolType::BoolType (HirId ref, std::set<HirId> refs)
    2643                 :             :   : BaseType (ref, ref, KIND,
    2644                 :        8893 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2645                 :       17786 :               refs)
    2646                 :        8893 : {}
    2647                 :             : 
    2648                 :       37293 : BoolType::BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    2649                 :             :   : BaseType (ref, ty_ref, KIND,
    2650                 :       37293 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2651                 :       74586 :               refs)
    2652                 :       37293 : {}
    2653                 :             : 
    2654                 :             : std::string
    2655                 :      277414 : BoolType::get_name () const
    2656                 :             : {
    2657                 :      277414 :   return as_string ();
    2658                 :             : }
    2659                 :             : 
    2660                 :             : void
    2661                 :         206 : BoolType::accept_vis (TyVisitor &vis)
    2662                 :             : {
    2663                 :         206 :   vis.visit (*this);
    2664                 :         206 : }
    2665                 :             : 
    2666                 :             : void
    2667                 :       14610 : BoolType::accept_vis (TyConstVisitor &vis) const
    2668                 :             : {
    2669                 :       14610 :   vis.visit (*this);
    2670                 :       14610 : }
    2671                 :             : 
    2672                 :             : std::string
    2673                 :      326624 : BoolType::as_string () const
    2674                 :             : {
    2675                 :      326624 :   return "bool";
    2676                 :             : }
    2677                 :             : 
    2678                 :             : BaseType *
    2679                 :       37293 : BoolType::clone () const
    2680                 :             : {
    2681                 :       37293 :   return new BoolType (get_ref (), get_ty_ref (), get_combined_refs ());
    2682                 :             : }
    2683                 :             : 
    2684                 :             : // IntType
    2685                 :             : 
    2686                 :       44465 : IntType::IntType (HirId ref, IntKind kind, std::set<HirId> refs)
    2687                 :             :   : BaseType (ref, ref, KIND,
    2688                 :       44465 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2689                 :             :               refs),
    2690                 :       88930 :     int_kind (kind)
    2691                 :       44465 : {}
    2692                 :             : 
    2693                 :      300334 : IntType::IntType (HirId ref, HirId ty_ref, IntKind kind, std::set<HirId> refs)
    2694                 :             :   : BaseType (ref, ty_ref, KIND,
    2695                 :      300334 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2696                 :             :               refs),
    2697                 :      600668 :     int_kind (kind)
    2698                 :      300334 : {}
    2699                 :             : 
    2700                 :             : std::string
    2701                 :     2320497 : IntType::get_name () const
    2702                 :             : {
    2703                 :     2320497 :   return as_string ();
    2704                 :             : }
    2705                 :             : 
    2706                 :             : IntType::IntKind
    2707                 :      795572 : IntType::get_int_kind () const
    2708                 :             : {
    2709                 :      795572 :   return int_kind;
    2710                 :             : }
    2711                 :             : 
    2712                 :             : void
    2713                 :        1984 : IntType::accept_vis (TyVisitor &vis)
    2714                 :             : {
    2715                 :        1984 :   vis.visit (*this);
    2716                 :        1984 : }
    2717                 :             : 
    2718                 :             : void
    2719                 :      101191 : IntType::accept_vis (TyConstVisitor &vis) const
    2720                 :             : {
    2721                 :      101191 :   vis.visit (*this);
    2722                 :      101191 : }
    2723                 :             : 
    2724                 :             : std::string
    2725                 :     2557851 : IntType::as_string () const
    2726                 :             : {
    2727                 :     2557851 :   switch (int_kind)
    2728                 :             :     {
    2729                 :      405076 :     case I8:
    2730                 :      405076 :       return "i8";
    2731                 :      339349 :     case I16:
    2732                 :      339349 :       return "i16";
    2733                 :     1424438 :     case I32:
    2734                 :     1424438 :       return "i32";
    2735                 :      338323 :     case I64:
    2736                 :      338323 :       return "i64";
    2737                 :       50665 :     case I128:
    2738                 :       50665 :       return "i128";
    2739                 :             :     }
    2740                 :           0 :   rust_unreachable ();
    2741                 :             :   return "__unknown_int_type";
    2742                 :             : }
    2743                 :             : 
    2744                 :             : BaseType *
    2745                 :      300334 : IntType::clone () const
    2746                 :             : {
    2747                 :      300334 :   return new IntType (get_ref (), get_ty_ref (), get_int_kind (),
    2748                 :      300334 :                       get_combined_refs ());
    2749                 :             : }
    2750                 :             : 
    2751                 :             : bool
    2752                 :       87685 : IntType::is_equal (const BaseType &other) const
    2753                 :             : {
    2754                 :       87685 :   if (!BaseType::is_equal (other))
    2755                 :             :     return false;
    2756                 :             : 
    2757                 :       67351 :   const IntType &o = static_cast<const IntType &> (other);
    2758                 :       67351 :   return get_int_kind () == o.get_int_kind ();
    2759                 :             : }
    2760                 :             : 
    2761                 :             : // UintType
    2762                 :             : 
    2763                 :       44465 : UintType::UintType (HirId ref, UintKind kind, std::set<HirId> refs)
    2764                 :             :   : BaseType (ref, ref, KIND,
    2765                 :       44465 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2766                 :             :               refs),
    2767                 :       88930 :     uint_kind (kind)
    2768                 :       44465 : {}
    2769                 :             : 
    2770                 :      139192 : UintType::UintType (HirId ref, HirId ty_ref, UintKind kind,
    2771                 :      139192 :                     std::set<HirId> refs)
    2772                 :             :   : BaseType (ref, ty_ref, KIND,
    2773                 :      139192 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2774                 :             :               refs),
    2775                 :      278384 :     uint_kind (kind)
    2776                 :      139192 : {}
    2777                 :             : 
    2778                 :             : std::string
    2779                 :     4466026 : UintType::get_name () const
    2780                 :             : {
    2781                 :     4466026 :   return as_string ();
    2782                 :             : }
    2783                 :             : 
    2784                 :             : UintType::UintKind
    2785                 :      974674 : UintType::get_uint_kind () const
    2786                 :             : {
    2787                 :      974674 :   return uint_kind;
    2788                 :             : }
    2789                 :             : 
    2790                 :             : void
    2791                 :         665 : UintType::accept_vis (TyVisitor &vis)
    2792                 :             : {
    2793                 :         665 :   vis.visit (*this);
    2794                 :         665 : }
    2795                 :             : 
    2796                 :             : void
    2797                 :       84408 : UintType::accept_vis (TyConstVisitor &vis) const
    2798                 :             : {
    2799                 :       84408 :   vis.visit (*this);
    2800                 :       84408 : }
    2801                 :             : 
    2802                 :             : std::string
    2803                 :     4746573 : UintType::as_string () const
    2804                 :             : {
    2805                 :     4746573 :   switch (uint_kind)
    2806                 :             :     {
    2807                 :     1272818 :     case U8:
    2808                 :     1272818 :       return "u8";
    2809                 :     1079270 :     case U16:
    2810                 :     1079270 :       return "u16";
    2811                 :     1153470 :     case U32:
    2812                 :     1153470 :       return "u32";
    2813                 :     1185945 :     case U64:
    2814                 :     1185945 :       return "u64";
    2815                 :       55070 :     case U128:
    2816                 :       55070 :       return "u128";
    2817                 :             :     }
    2818                 :           0 :   rust_unreachable ();
    2819                 :             :   return "__unknown_uint_type";
    2820                 :             : }
    2821                 :             : 
    2822                 :             : BaseType *
    2823                 :      139192 : UintType::clone () const
    2824                 :             : {
    2825                 :      139192 :   return new UintType (get_ref (), get_ty_ref (), get_uint_kind (),
    2826                 :      139192 :                        get_combined_refs ());
    2827                 :             : }
    2828                 :             : 
    2829                 :             : bool
    2830                 :       74708 : UintType::is_equal (const BaseType &other) const
    2831                 :             : {
    2832                 :       74708 :   if (!BaseType::is_equal (other))
    2833                 :             :     return false;
    2834                 :             : 
    2835                 :       59947 :   const UintType &o = static_cast<const UintType &> (other);
    2836                 :       59947 :   return get_uint_kind () == o.get_uint_kind ();
    2837                 :             : }
    2838                 :             : 
    2839                 :             : // FloatType
    2840                 :             : 
    2841                 :       17786 : FloatType::FloatType (HirId ref, FloatKind kind, std::set<HirId> refs)
    2842                 :             :   : BaseType (ref, ref, KIND,
    2843                 :       17786 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2844                 :             :               refs),
    2845                 :       35572 :     float_kind (kind)
    2846                 :       17786 : {}
    2847                 :             : 
    2848                 :       45095 : FloatType::FloatType (HirId ref, HirId ty_ref, FloatKind kind,
    2849                 :       45095 :                       std::set<HirId> refs)
    2850                 :             :   : BaseType (ref, ty_ref, KIND,
    2851                 :       45095 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2852                 :             :               refs),
    2853                 :       90190 :     float_kind (kind)
    2854                 :       45095 : {}
    2855                 :             : 
    2856                 :             : std::string
    2857                 :     1000382 : FloatType::get_name () const
    2858                 :             : {
    2859                 :     1000382 :   return as_string ();
    2860                 :             : }
    2861                 :             : 
    2862                 :             : FloatType::FloatKind
    2863                 :      115988 : FloatType::get_float_kind () const
    2864                 :             : {
    2865                 :      115988 :   return float_kind;
    2866                 :             : }
    2867                 :             : 
    2868                 :             : void
    2869                 :         188 : FloatType::accept_vis (TyVisitor &vis)
    2870                 :             : {
    2871                 :         188 :   vis.visit (*this);
    2872                 :         188 : }
    2873                 :             : 
    2874                 :             : void
    2875                 :       25053 : FloatType::accept_vis (TyConstVisitor &vis) const
    2876                 :             : {
    2877                 :       25053 :   vis.visit (*this);
    2878                 :       25053 : }
    2879                 :             : 
    2880                 :             : std::string
    2881                 :     1067878 : FloatType::as_string () const
    2882                 :             : {
    2883                 :     1067878 :   switch (float_kind)
    2884                 :             :     {
    2885                 :      538322 :     case F32:
    2886                 :      538322 :       return "f32";
    2887                 :      529556 :     case F64:
    2888                 :      529556 :       return "f64";
    2889                 :             :     }
    2890                 :           0 :   rust_unreachable ();
    2891                 :             :   return "__unknown_float_type";
    2892                 :             : }
    2893                 :             : 
    2894                 :             : BaseType *
    2895                 :       45095 : FloatType::clone () const
    2896                 :             : {
    2897                 :       45095 :   return new FloatType (get_ref (), get_ty_ref (), get_float_kind (),
    2898                 :       45095 :                         get_combined_refs ());
    2899                 :             : }
    2900                 :             : 
    2901                 :             : bool
    2902                 :       11829 : FloatType::is_equal (const BaseType &other) const
    2903                 :             : {
    2904                 :       11829 :   if (!BaseType::is_equal (other))
    2905                 :             :     return false;
    2906                 :             : 
    2907                 :        9975 :   const FloatType &o = static_cast<const FloatType &> (other);
    2908                 :        9975 :   return get_float_kind () == o.get_float_kind ();
    2909                 :             : }
    2910                 :             : 
    2911                 :             : // UsizeType
    2912                 :             : 
    2913                 :        8893 : USizeType::USizeType (HirId ref, std::set<HirId> refs)
    2914                 :             :   : BaseType (ref, ref, KIND,
    2915                 :        8893 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2916                 :       17786 :               refs)
    2917                 :        8893 : {}
    2918                 :             : 
    2919                 :       53417 : USizeType::USizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    2920                 :             :   : BaseType (ref, ty_ref, KIND,
    2921                 :       53417 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2922                 :      106834 :               refs)
    2923                 :       53417 : {}
    2924                 :             : 
    2925                 :             : std::string
    2926                 :     1848863 : USizeType::get_name () const
    2927                 :             : {
    2928                 :     1848863 :   return as_string ();
    2929                 :             : }
    2930                 :             : 
    2931                 :             : void
    2932                 :         250 : USizeType::accept_vis (TyVisitor &vis)
    2933                 :             : {
    2934                 :         250 :   vis.visit (*this);
    2935                 :         250 : }
    2936                 :             : 
    2937                 :             : void
    2938                 :       45109 : USizeType::accept_vis (TyConstVisitor &vis) const
    2939                 :             : {
    2940                 :       45109 :   vis.visit (*this);
    2941                 :       45109 : }
    2942                 :             : 
    2943                 :             : std::string
    2944                 :     1901360 : USizeType::as_string () const
    2945                 :             : {
    2946                 :     1901360 :   return "usize";
    2947                 :             : }
    2948                 :             : 
    2949                 :             : BaseType *
    2950                 :       53417 : USizeType::clone () const
    2951                 :             : {
    2952                 :       53417 :   return new USizeType (get_ref (), get_ty_ref (), get_combined_refs ());
    2953                 :             : }
    2954                 :             : 
    2955                 :             : // ISizeType
    2956                 :             : 
    2957                 :        9162 : ISizeType::ISizeType (HirId ref, std::set<HirId> refs)
    2958                 :             :   : BaseType (ref, ref, KIND,
    2959                 :        9162 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2960                 :       18324 :               refs)
    2961                 :        9162 : {}
    2962                 :             : 
    2963                 :       58709 : ISizeType::ISizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    2964                 :             :   : BaseType (ref, ty_ref, KIND,
    2965                 :       58709 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    2966                 :      117418 :               refs)
    2967                 :       58709 : {}
    2968                 :             : 
    2969                 :             : std::string
    2970                 :      624868 : ISizeType::get_name () const
    2971                 :             : {
    2972                 :      624868 :   return as_string ();
    2973                 :             : }
    2974                 :             : 
    2975                 :             : void
    2976                 :          73 : ISizeType::accept_vis (TyVisitor &vis)
    2977                 :             : {
    2978                 :          73 :   vis.visit (*this);
    2979                 :          73 : }
    2980                 :             : 
    2981                 :             : void
    2982                 :       26462 : ISizeType::accept_vis (TyConstVisitor &vis) const
    2983                 :             : {
    2984                 :       26462 :   vis.visit (*this);
    2985                 :       26462 : }
    2986                 :             : 
    2987                 :             : std::string
    2988                 :      644437 : ISizeType::as_string () const
    2989                 :             : {
    2990                 :      644437 :   return "isize";
    2991                 :             : }
    2992                 :             : 
    2993                 :             : BaseType *
    2994                 :       58709 : ISizeType::clone () const
    2995                 :             : {
    2996                 :       58709 :   return new ISizeType (get_ref (), get_ty_ref (), get_combined_refs ());
    2997                 :             : }
    2998                 :             : 
    2999                 :             : // Char Type
    3000                 :             : 
    3001                 :        8893 : CharType::CharType (HirId ref, std::set<HirId> refs)
    3002                 :             :   : BaseType (ref, ref, KIND,
    3003                 :        8893 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3004                 :       17786 :               refs)
    3005                 :        8893 : {}
    3006                 :             : 
    3007                 :        8928 : CharType::CharType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    3008                 :             :   : BaseType (ref, ty_ref, KIND,
    3009                 :        8928 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3010                 :       17856 :               refs)
    3011                 :        8928 : {}
    3012                 :             : 
    3013                 :             : std::string
    3014                 :      203460 : CharType::get_name () const
    3015                 :             : {
    3016                 :      203460 :   return as_string ();
    3017                 :             : }
    3018                 :             : 
    3019                 :             : void
    3020                 :          47 : CharType::accept_vis (TyVisitor &vis)
    3021                 :             : {
    3022                 :          47 :   vis.visit (*this);
    3023                 :          47 : }
    3024                 :             : 
    3025                 :             : void
    3026                 :        9751 : CharType::accept_vis (TyConstVisitor &vis) const
    3027                 :             : {
    3028                 :        9751 :   vis.visit (*this);
    3029                 :        9751 : }
    3030                 :             : 
    3031                 :             : std::string
    3032                 :      207902 : CharType::as_string () const
    3033                 :             : {
    3034                 :      207902 :   return "char";
    3035                 :             : }
    3036                 :             : 
    3037                 :             : BaseType *
    3038                 :        8928 : CharType::clone () const
    3039                 :             : {
    3040                 :        8928 :   return new CharType (get_ref (), get_ty_ref (), get_combined_refs ());
    3041                 :             : }
    3042                 :             : 
    3043                 :             : // Reference Type
    3044                 :             : 
    3045                 :       46244 : ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut,
    3046                 :       46244 :                               Region region, std::set<HirId> refs)
    3047                 :             :   : BaseType (ref, ref, KIND,
    3048                 :       46244 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3049                 :             :               std::move (refs)),
    3050                 :       92488 :     base (base), mut (mut), region (region)
    3051                 :       46244 : {}
    3052                 :             : 
    3053                 :       75873 : ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base,
    3054                 :             :                               Mutability mut, Region region,
    3055                 :       75873 :                               std::set<HirId> refs)
    3056                 :             :   : BaseType (ref, ty_ref, KIND,
    3057                 :       75873 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3058                 :             :               std::move (refs)),
    3059                 :      151746 :     base (base), mut (mut), region (region)
    3060                 :       75873 : {}
    3061                 :             : 
    3062                 :             : Mutability
    3063                 :      212625 : ReferenceType::mutability () const
    3064                 :             : {
    3065                 :      212625 :   return mut;
    3066                 :             : }
    3067                 :             : 
    3068                 :             : bool
    3069                 :      893954 : ReferenceType::is_mutable () const
    3070                 :             : {
    3071                 :      893954 :   return mut == Mutability::Mut;
    3072                 :             : }
    3073                 :             : Region
    3074                 :       76126 : ReferenceType::get_region () const
    3075                 :             : {
    3076                 :       76126 :   return region;
    3077                 :             : }
    3078                 :             : 
    3079                 :             : bool
    3080                 :        1941 : ReferenceType::is_dyn_object () const
    3081                 :             : {
    3082                 :        1941 :   return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
    3083                 :             : }
    3084                 :             : 
    3085                 :             : bool
    3086                 :       20098 : ReferenceType::is_dyn_slice_type (const TyTy::SliceType **slice) const
    3087                 :             : {
    3088                 :       20098 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3089                 :       20098 :   if (element->get_kind () != TyTy::TypeKind::SLICE)
    3090                 :             :     return false;
    3091                 :         648 :   if (slice == nullptr)
    3092                 :             :     return true;
    3093                 :             : 
    3094                 :         648 :   *slice = static_cast<const TyTy::SliceType *> (element);
    3095                 :         648 :   return true;
    3096                 :             : }
    3097                 :             : 
    3098                 :             : bool
    3099                 :       19493 : ReferenceType::is_dyn_str_type (const TyTy::StrType **str) const
    3100                 :             : {
    3101                 :       19493 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3102                 :       19493 :   if (element->get_kind () != TyTy::TypeKind::STR)
    3103                 :             :     return false;
    3104                 :        4054 :   if (str == nullptr)
    3105                 :             :     return true;
    3106                 :             : 
    3107                 :        4045 :   *str = static_cast<const TyTy::StrType *> (element);
    3108                 :        4045 :   return true;
    3109                 :             : }
    3110                 :             : 
    3111                 :             : bool
    3112                 :       15405 : ReferenceType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const
    3113                 :             : {
    3114                 :       15405 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3115                 :       15405 :   if (element->get_kind () != TyTy::TypeKind::DYNAMIC)
    3116                 :             :     return false;
    3117                 :         556 :   if (dyn == nullptr)
    3118                 :             :     return true;
    3119                 :             : 
    3120                 :         549 :   *dyn = static_cast<const TyTy::DynamicObjectType *> (element);
    3121                 :         549 :   return true;
    3122                 :             : }
    3123                 :             : 
    3124                 :             : void
    3125                 :       10210 : ReferenceType::accept_vis (TyVisitor &vis)
    3126                 :             : {
    3127                 :       10210 :   vis.visit (*this);
    3128                 :       10210 : }
    3129                 :             : 
    3130                 :             : void
    3131                 :       19401 : ReferenceType::accept_vis (TyConstVisitor &vis) const
    3132                 :             : {
    3133                 :       19401 :   vis.visit (*this);
    3134                 :       19401 : }
    3135                 :             : 
    3136                 :             : std::string
    3137                 :       71095 : ReferenceType::as_string () const
    3138                 :             : {
    3139                 :      280836 :   return std::string ("&") + (is_mutable () ? "mut" : "") + " "
    3140                 :      142190 :          + get_base ()->as_string ();
    3141                 :             : }
    3142                 :             : 
    3143                 :             : std::string
    3144                 :      780580 : ReferenceType::get_name () const
    3145                 :             : {
    3146                 :     3085047 :   return std::string ("&") + (is_mutable () ? "mut" : "") + " "
    3147                 :     1561160 :          + get_base ()->get_name ();
    3148                 :             : }
    3149                 :             : 
    3150                 :             : bool
    3151                 :       36361 : ReferenceType::is_equal (const BaseType &other) const
    3152                 :             : {
    3153                 :       36361 :   if (get_kind () != other.get_kind ())
    3154                 :             :     return false;
    3155                 :             : 
    3156                 :       35728 :   auto other2 = static_cast<const ReferenceType &> (other);
    3157                 :       35728 :   if (mutability () != other2.mutability ())
    3158                 :             :     return false;
    3159                 :             : 
    3160                 :       32480 :   return get_base ()->is_equal (*other2.get_base ());
    3161                 :       35728 : }
    3162                 :             : 
    3163                 :             : BaseType *
    3164                 :     1318893 : ReferenceType::get_base () const
    3165                 :             : {
    3166                 :     1318893 :   return base.get_tyty ();
    3167                 :             : }
    3168                 :             : 
    3169                 :             : const TyVar &
    3170                 :        1438 : ReferenceType::get_var_element_type () const
    3171                 :             : {
    3172                 :        1438 :   return base;
    3173                 :             : }
    3174                 :             : 
    3175                 :             : BaseType *
    3176                 :       75594 : ReferenceType::clone () const
    3177                 :             : {
    3178                 :       75594 :   return new ReferenceType (get_ref (), get_ty_ref (), base, mutability (),
    3179                 :       75594 :                             get_region (), get_combined_refs ());
    3180                 :             : }
    3181                 :             : 
    3182                 :             : ReferenceType *
    3183                 :        9957 : ReferenceType::handle_substitions (SubstitutionArgumentMappings &mappings)
    3184                 :             : {
    3185                 :        9957 :   auto &mappings_table = Analysis::Mappings::get ();
    3186                 :             : 
    3187                 :        9957 :   ReferenceType *ref = static_cast<ReferenceType *> (clone ());
    3188                 :        9957 :   ref->set_ty_ref (mappings_table.get_next_hir_id ());
    3189                 :             : 
    3190                 :             :   // might be &T or &ADT so this needs to be recursive
    3191                 :        9957 :   auto base = ref->get_base ();
    3192                 :        9957 :   BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
    3193                 :        9957 :   ref->base = TyVar::subst_covariant_var (base, concrete);
    3194                 :             : 
    3195                 :        9957 :   return ref;
    3196                 :             : }
    3197                 :             : 
    3198                 :             : // PointerType
    3199                 :             : 
    3200                 :       13841 : PointerType::PointerType (HirId ref, TyVar base, Mutability mut,
    3201                 :       13841 :                           std::set<HirId> refs)
    3202                 :             :   : BaseType (ref, ref, KIND,
    3203                 :       13841 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3204                 :             :               refs),
    3205                 :       27682 :     base (base), mut (mut)
    3206                 :       13841 : {}
    3207                 :             : 
    3208                 :       17174 : PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
    3209                 :       17174 :                           std::set<HirId> refs)
    3210                 :             :   : BaseType (ref, ty_ref, KIND,
    3211                 :       17174 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    3212                 :             :               refs),
    3213                 :       34348 :     base (base), mut (mut)
    3214                 :       17174 : {}
    3215                 :             : 
    3216                 :             : Mutability
    3217                 :       64751 : PointerType::mutability () const
    3218                 :             : {
    3219                 :       64751 :   return mut;
    3220                 :             : }
    3221                 :             : 
    3222                 :             : bool
    3223                 :      205725 : PointerType::is_mutable () const
    3224                 :             : {
    3225                 :      205725 :   return mut == Mutability::Mut;
    3226                 :             : }
    3227                 :             : 
    3228                 :             : bool
    3229                 :           0 : PointerType::is_const () const
    3230                 :             : {
    3231                 :           0 :   return mut == Mutability::Imm;
    3232                 :             : }
    3233                 :             : 
    3234                 :             : bool
    3235                 :        4034 : PointerType::is_dyn_object () const
    3236                 :             : {
    3237                 :        4034 :   return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type ();
    3238                 :             : }
    3239                 :             : 
    3240                 :             : bool
    3241                 :       14204 : PointerType::is_dyn_slice_type (const TyTy::SliceType **slice) const
    3242                 :             : {
    3243                 :       14204 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3244                 :       14204 :   if (element->get_kind () != TyTy::TypeKind::SLICE)
    3245                 :             :     return false;
    3246                 :         533 :   if (slice == nullptr)
    3247                 :             :     return true;
    3248                 :             : 
    3249                 :         533 :   *slice = static_cast<const TyTy::SliceType *> (element);
    3250                 :         533 :   return true;
    3251                 :             : }
    3252                 :             : 
    3253                 :             : bool
    3254                 :       13671 : PointerType::is_dyn_str_type (const TyTy::StrType **str) const
    3255                 :             : {
    3256                 :       13671 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3257                 :       13671 :   if (element->get_kind () != TyTy::TypeKind::STR)
    3258                 :             :     return false;
    3259                 :        2578 :   if (str == nullptr)
    3260                 :             :     return true;
    3261                 :             : 
    3262                 :        2578 :   *str = static_cast<const TyTy::StrType *> (element);
    3263                 :        2578 :   return true;
    3264                 :             : }
    3265                 :             : 
    3266                 :             : bool
    3267                 :       11093 : PointerType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const
    3268                 :             : {
    3269                 :       11093 :   const TyTy::BaseType *element = get_base ()->destructure ();
    3270                 :       11093 :   if (element->get_kind () != TyTy::TypeKind::DYNAMIC)
    3271                 :             :     return false;
    3272                 :           0 :   if (dyn == nullptr)
    3273                 :             :     return true;
    3274                 :             : 
    3275                 :           0 :   *dyn = static_cast<const TyTy::DynamicObjectType *> (element);
    3276                 :           0 :   return true;
    3277                 :             : }
    3278                 :             : 
    3279                 :             : void
    3280                 :        3149 : PointerType::accept_vis (TyVisitor &vis)
    3281                 :             : {
    3282                 :        3149 :   vis.visit (*this);
    3283                 :        3149 : }
    3284                 :             : 
    3285                 :             : void
    3286                 :       11317 : PointerType::accept_vis (TyConstVisitor &vis) const
    3287                 :             : {
    3288                 :       11317 :   vis.visit (*this);
    3289                 :       11317 : }
    3290                 :             : 
    3291                 :             : std::string
    3292                 :       14298 : PointerType::as_string () const
    3293                 :             : {
    3294                 :       53590 :   return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
    3295                 :       28596 :          + get_base ()->as_string ();
    3296                 :             : }
    3297                 :             : 
    3298                 :             : std::string
    3299                 :      164396 : PointerType::get_name () const
    3300                 :             : {
    3301                 :      630063 :   return std::string ("* ") + (is_mutable () ? "mut" : "const") + " "
    3302                 :      328792 :          + get_base ()->get_name ();
    3303                 :             : }
    3304                 :             : 
    3305                 :             : bool
    3306                 :       16075 : PointerType::is_equal (const BaseType &other) const
    3307                 :             : {
    3308                 :       16075 :   if (get_kind () != other.get_kind ())
    3309                 :             :     return false;
    3310                 :             : 
    3311                 :       15747 :   auto other2 = static_cast<const PointerType &> (other);
    3312                 :       15747 :   if (mutability () != other2.mutability ())
    3313                 :             :     return false;
    3314                 :             : 
    3315                 :       15663 :   return get_base ()->is_equal (*other2.get_base ());
    3316                 :       15747 : }
    3317                 :             : 
    3318                 :             : BaseType *
    3319                 :      324090 : PointerType::get_base () const
    3320                 :             : {
    3321                 :      324090 :   return base.get_tyty ();
    3322                 :             : }
    3323                 :             : 
    3324                 :             : const TyVar &
    3325                 :         653 : PointerType::get_var_element_type () const
    3326                 :             : {
    3327                 :         653 :   return base;
    3328                 :             : }
    3329                 :             : 
    3330                 :             : BaseType *
    3331                 :       16521 : PointerType::clone () const
    3332                 :             : {
    3333                 :       16521 :   return new PointerType (get_ref (), get_ty_ref (), base, mutability (),
    3334                 :       16521 :                           get_combined_refs ());
    3335                 :             : }
    3336                 :             : 
    3337                 :             : PointerType *
    3338                 :        2930 : PointerType::handle_substitions (SubstitutionArgumentMappings &mappings)
    3339                 :             : {
    3340                 :        2930 :   auto &mappings_table = Analysis::Mappings::get ();
    3341                 :             : 
    3342                 :        2930 :   PointerType *ref = static_cast<PointerType *> (clone ());
    3343                 :        2930 :   ref->set_ty_ref (mappings_table.get_next_hir_id ());
    3344                 :             : 
    3345                 :             :   // might be &T or &ADT so this needs to be recursive
    3346                 :        2930 :   auto base = ref->get_base ();
    3347                 :        2930 :   BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings);
    3348                 :        2930 :   ref->base = TyVar::subst_covariant_var (base, concrete);
    3349                 :             : 
    3350                 :        2930 :   return ref;
    3351                 :             : }
    3352                 :             : 
    3353                 :             : // PARAM Type
    3354                 :             : 
    3355                 :       14128 : ParamType::ParamType (std::string symbol, location_t locus, HirId ref,
    3356                 :             :                       std::vector<TypeBoundPredicate> specified_bounds,
    3357                 :       14128 :                       std::set<HirId> refs)
    3358                 :             :   : BaseGeneric (ref, ref, KIND,
    3359                 :       28256 :                  {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
    3360                 :             :                   locus},
    3361                 :             :                  specified_bounds, refs),
    3362                 :       42384 :     is_trait_self (false), symbol (symbol)
    3363                 :       14128 : {}
    3364                 :             : 
    3365                 :    85811801 : ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus,
    3366                 :             :                       HirId ref, HirId ty_ref,
    3367                 :             :                       std::vector<TypeBoundPredicate> specified_bounds,
    3368                 :    85811801 :                       std::set<HirId> refs)
    3369                 :             :   : BaseGeneric (ref, ty_ref, KIND,
    3370                 :   171623602 :                  {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
    3371                 :             :                   locus},
    3372                 :             :                  specified_bounds, refs),
    3373                 :   257435403 :     is_trait_self (is_trait_self), symbol (symbol)
    3374                 :    85811801 : {}
    3375                 :             : 
    3376                 :             : bool
    3377                 :     3514605 : ParamType::can_resolve () const
    3378                 :             : {
    3379                 :     3514605 :   return get_ref () != get_ty_ref ();
    3380                 :             : }
    3381                 :             : 
    3382                 :             : void
    3383                 :       60485 : ParamType::accept_vis (TyVisitor &vis)
    3384                 :             : {
    3385                 :       60485 :   vis.visit (*this);
    3386                 :       60485 : }
    3387                 :             : 
    3388                 :             : void
    3389                 :         330 : ParamType::accept_vis (TyConstVisitor &vis) const
    3390                 :             : {
    3391                 :         330 :   vis.visit (*this);
    3392                 :         330 : }
    3393                 :             : 
    3394                 :             : std::string
    3395                 :      165032 : ParamType::as_string () const
    3396                 :             : {
    3397                 :      165032 :   if (!can_resolve ())
    3398                 :             :     {
    3399                 :      115906 :       return get_symbol () + " REF: " + std::to_string (get_ref ());
    3400                 :             :     }
    3401                 :             : 
    3402                 :      107079 :   BaseType *lookup = resolve ();
    3403                 :      214158 :   return get_symbol () + "=" + lookup->as_string ();
    3404                 :             : }
    3405                 :             : 
    3406                 :             : std::string
    3407                 :     1555219 : ParamType::get_name () const
    3408                 :             : {
    3409                 :     1555219 :   if (!can_resolve ())
    3410                 :      758407 :     return get_symbol ();
    3411                 :             : 
    3412                 :      796812 :   return destructure ()->get_name ();
    3413                 :             : }
    3414                 :             : 
    3415                 :             : BaseType *
    3416                 :    85811801 : ParamType::clone () const
    3417                 :             : {
    3418                 :    85811801 :   return new ParamType (is_trait_self, get_symbol (), ident.locus, get_ref (),
    3419                 :             :                         get_ty_ref (), get_specified_bounds (),
    3420                 :   171623602 :                         get_combined_refs ());
    3421                 :             : }
    3422                 :             : 
    3423                 :             : std::string
    3424                 :    87128023 : ParamType::get_symbol () const
    3425                 :             : {
    3426                 :    87128023 :   return symbol;
    3427                 :             : }
    3428                 :             : 
    3429                 :             : BaseType *
    3430                 :     2293707 : ParamType::resolve () const
    3431                 :             : {
    3432                 :     2293707 :   TyVar var (get_ty_ref ());
    3433                 :     2293707 :   BaseType *r = var.get_tyty ();
    3434                 :             : 
    3435                 :     4691746 :   while (r->get_kind () == TypeKind::PARAM)
    3436                 :             :     {
    3437                 :     1657878 :       ParamType *rr = static_cast<ParamType *> (r);
    3438                 :     1657878 :       if (!rr->can_resolve ())
    3439                 :             :         break;
    3440                 :             : 
    3441                 :      104332 :       TyVar v (rr->get_ty_ref ());
    3442                 :      104332 :       BaseType *n = v.get_tyty ();
    3443                 :             : 
    3444                 :             :       // fix infinite loop
    3445                 :      104332 :       if (r == n)
    3446                 :             :         break;
    3447                 :             : 
    3448                 :      104332 :       r = n;
    3449                 :             :     }
    3450                 :             : 
    3451                 :     2293707 :   if (r->get_kind () == TypeKind::PARAM && (r->get_ref () == r->get_ty_ref ()))
    3452                 :     1553546 :     return TyVar (r->get_ty_ref ()).get_tyty ();
    3453                 :             : 
    3454                 :             :   return r;
    3455                 :             : }
    3456                 :             : 
    3457                 :             : bool
    3458                 :       48789 : ParamType::is_equal (const BaseType &other) const
    3459                 :             : {
    3460                 :       48789 :   if (get_kind () != other.get_kind ())
    3461                 :             :     {
    3462                 :       14608 :       if (!can_resolve ())
    3463                 :             :         return false;
    3464                 :             : 
    3465                 :        9992 :       return resolve ()->is_equal (other);
    3466                 :             :     }
    3467                 :             : 
    3468                 :       34181 :   auto other2 = static_cast<const ParamType &> (other);
    3469                 :       34181 :   if (can_resolve () != other2.can_resolve ())
    3470                 :             :     return false;
    3471                 :             : 
    3472                 :       32737 :   if (can_resolve ())
    3473                 :       22776 :     return Resolver::types_compatable (TyTy::TyWithLocation (resolve ()),
    3474                 :       22776 :                                        TyTy::TyWithLocation (other2.resolve ()),
    3475                 :             :                                        UNKNOWN_LOCATION, false, false);
    3476                 :             : 
    3477                 :        9961 :   return get_symbol ().compare (other2.get_symbol ()) == 0;
    3478                 :       34181 : }
    3479                 :             : 
    3480                 :             : ParamType *
    3481                 :       59040 : ParamType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
    3482                 :             : {
    3483                 :       59040 :   SubstitutionArg arg = SubstitutionArg::error ();
    3484                 :       59040 :   bool ok = subst_mappings.get_argument_for_symbol (this, &arg);
    3485                 :       59040 :   if (!ok || arg.is_error ())
    3486                 :       14129 :     return this;
    3487                 :             : 
    3488                 :       44911 :   ParamType *p = static_cast<ParamType *> (clone ());
    3489                 :       44911 :   subst_mappings.on_param_subst (*p, arg);
    3490                 :             : 
    3491                 :       44911 :   const BaseType *resolved = arg.get_tyty ();
    3492                 :       44911 :   if (resolved->get_kind () == TyTy::TypeKind::PARAM)
    3493                 :             :     {
    3494                 :        6312 :       const ParamType &pp = *static_cast<const ParamType *> (resolved);
    3495                 :        6312 :       if (pp.can_resolve ())
    3496                 :        5771 :         pp.resolve ();
    3497                 :             :     }
    3498                 :             : 
    3499                 :             :   // this is the new subst that this needs to pass
    3500                 :       44911 :   p->set_ref (mappings.get_next_hir_id ());
    3501                 :       44911 :   p->set_ty_ref (arg.get_tyty ()->get_ref ());
    3502                 :             : 
    3503                 :       44911 :   return p;
    3504                 :             : }
    3505                 :             : 
    3506                 :             : void
    3507                 :        3670 : ParamType::set_implicit_self_trait ()
    3508                 :             : {
    3509                 :        3670 :   is_trait_self = true;
    3510                 :        3670 : }
    3511                 :             : 
    3512                 :             : bool
    3513                 :       29922 : ParamType::is_implicit_self_trait () const
    3514                 :             : {
    3515                 :       29922 :   return is_trait_self;
    3516                 :             : }
    3517                 :             : 
    3518                 :             : static std::string
    3519                 :       62449 : generate_tree_str (tree value)
    3520                 :             : {
    3521                 :       62449 :   char *buf = nullptr;
    3522                 :       62449 :   size_t size = 0;
    3523                 :             : 
    3524                 :       62449 :   FILE *stream = open_memstream (&buf, &size);
    3525                 :       62449 :   if (!stream)
    3526                 :           0 :     return "<error>";
    3527                 :             : 
    3528                 :       62449 :   print_generic_stmt (stream, value, TDF_NONE);
    3529                 :       62449 :   fclose (stream);
    3530                 :             : 
    3531                 :       62449 :   std::string result = (buf ? std::string (buf, size) : "<error>");
    3532                 :       62449 :   free (buf);
    3533                 :             : 
    3534                 :      124898 :   if (!result.empty () && result.back () == '\n')
    3535                 :       62449 :     result.pop_back ();
    3536                 :             : 
    3537                 :       62449 :   return result;
    3538                 :       62449 : }
    3539                 :             : 
    3540                 :             : // ---
    3541                 :             : 
    3542                 :         807 : ConstParamType::ConstParamType (std::string symbol, location_t locus,
    3543                 :             :                                 BaseType *type, HirId ref, HirId ty_ref,
    3544                 :         807 :                                 std::set<HirId> refs)
    3545                 :             :   : BaseConstType (type),
    3546                 :             :     BaseGeneric (ref, ty_ref, KIND,
    3547                 :        1614 :                  {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
    3548                 :             :                   locus},
    3549                 :             :                  {}, refs),
    3550                 :        2421 :     symbol (symbol)
    3551                 :         807 : {}
    3552                 :             : 
    3553                 :             : BaseConstType::ConstKind
    3554                 :        5052 : ConstParamType::const_kind () const
    3555                 :             : {
    3556                 :        5052 :   return BaseConstType::ConstKind::Decl;
    3557                 :             : }
    3558                 :             : 
    3559                 :             : std::string
    3560                 :        2805 : ConstParamType::get_symbol () const
    3561                 :             : {
    3562                 :        2805 :   return symbol;
    3563                 :             : }
    3564                 :             : 
    3565                 :             : bool
    3566                 :        4508 : ConstParamType::can_resolve () const
    3567                 :             : {
    3568                 :        4508 :   return get_ref () != get_ty_ref ();
    3569                 :             : }
    3570                 :             : 
    3571                 :             : BaseType *
    3572                 :        5331 : ConstParamType::resolve () const
    3573                 :             : {
    3574                 :        5331 :   TyVar var (get_ty_ref ());
    3575                 :        5331 :   BaseType *r = var.get_tyty ();
    3576                 :             : 
    3577                 :       10802 :   while (r->get_kind () == TypeKind::CONST)
    3578                 :             :     {
    3579                 :        5471 :       TyVar v (r->get_ty_ref ());
    3580                 :        5471 :       BaseType *n = v.get_tyty ();
    3581                 :             : 
    3582                 :             :       // fix infinite loop
    3583                 :        5471 :       if (r == n)
    3584                 :             :         break;
    3585                 :             : 
    3586                 :         140 :       r = n;
    3587                 :             :     }
    3588                 :             : 
    3589                 :        5331 :   if (r->get_kind () == TypeKind::CONST && (r->get_ref () == r->get_ty_ref ()))
    3590                 :             :     {
    3591                 :        5331 :       auto *const_type = r->as_const_type ();
    3592                 :        5331 :       if (const_type->const_kind () != BaseConstType::ConstKind::Value)
    3593                 :        1489 :         return TyVar (r->get_ty_ref ()).get_tyty ();
    3594                 :             :     }
    3595                 :             : 
    3596                 :             :   return r;
    3597                 :             : }
    3598                 :             : 
    3599                 :             : void
    3600                 :          57 : ConstParamType::accept_vis (TyVisitor &vis)
    3601                 :             : {
    3602                 :          57 :   vis.visit (*this);
    3603                 :          57 : }
    3604                 :             : 
    3605                 :             : void
    3606                 :           0 : ConstParamType::accept_vis (TyConstVisitor &vis) const
    3607                 :             : {
    3608                 :           0 :   vis.visit (*this);
    3609                 :           0 : }
    3610                 :             : 
    3611                 :             : std::string
    3612                 :         165 : ConstParamType::as_string () const
    3613                 :             : {
    3614                 :         165 :   if (!can_resolve ())
    3615                 :             :     {
    3616                 :         330 :       return get_symbol () + " CONST_REF: " + std::to_string (get_ref ());
    3617                 :             :     }
    3618                 :             : 
    3619                 :           0 :   BaseType *lookup = resolve ();
    3620                 :             :   // Avoid infinite recursion if resolve() returns this same type
    3621                 :           0 :   if (lookup == this->as_base_type ())
    3622                 :             :     {
    3623                 :           0 :       return get_symbol () + " CONST_REF: " + std::to_string (get_ref ());
    3624                 :             :     }
    3625                 :             : 
    3626                 :           0 :   return get_symbol () + "=" + lookup->as_string ();
    3627                 :             : }
    3628                 :             : 
    3629                 :             : BaseType *
    3630                 :         682 : ConstParamType::clone () const
    3631                 :             : {
    3632                 :        1364 :   return new ConstParamType (get_symbol (), ident.locus, specified_type,
    3633                 :        1364 :                              get_ref (), get_ty_ref (), get_combined_refs ());
    3634                 :             : }
    3635                 :             : 
    3636                 :             : std::string
    3637                 :        3304 : ConstParamType::get_name () const
    3638                 :             : {
    3639                 :        3304 :   if (!can_resolve ())
    3640                 :         655 :     return get_symbol ();
    3641                 :             : 
    3642                 :        2649 :   BaseType *lookup = resolve ();
    3643                 :        2649 :   if (lookup == this->as_base_type ())
    3644                 :           0 :     return get_symbol () + ":" + get_specified_type ()->get_name ();
    3645                 :             : 
    3646                 :        2649 :   return lookup->get_name ();
    3647                 :             : }
    3648                 :             : 
    3649                 :             : bool
    3650                 :         330 : ConstParamType::is_equal (const BaseType &other) const
    3651                 :             : {
    3652                 :         330 :   if (get_kind () != other.get_kind ())
    3653                 :             :     {
    3654                 :           0 :       if (!can_resolve ())
    3655                 :             :         return false;
    3656                 :             : 
    3657                 :           0 :       return resolve ()->is_equal (other);
    3658                 :             :     }
    3659                 :             : 
    3660                 :         330 :   auto other_const = other.as_const_type ();
    3661                 :         330 :   if (other_const->const_kind () != BaseConstType::ConstKind::Decl)
    3662                 :             :     return false;
    3663                 :             : 
    3664                 :         323 :   auto &other2 = static_cast<const ConstParamType &> (*other_const);
    3665                 :         323 :   if (can_resolve () != other2.can_resolve ())
    3666                 :             :     return false;
    3667                 :             : 
    3668                 :         274 :   if (can_resolve ())
    3669                 :             :     {
    3670                 :             :       // Compare the resolved ty_ref values to avoid infinite recursion
    3671                 :             :       // through types_compatable/unification
    3672                 :         232 :       BaseType *lhs = resolve ();
    3673                 :         232 :       BaseType *rhs = other2.resolve ();
    3674                 :             : 
    3675                 :             :       // If they resolve to the same type (same ty_ref), they're equal
    3676                 :         232 :       if (lhs->get_ty_ref () == rhs->get_ty_ref ())
    3677                 :             :         return true;
    3678                 :             : 
    3679                 :             :       // Otherwise check if the resolved types are equal
    3680                 :             :       // Avoid recursion by checking if we'd be comparing ConstParamTypes again
    3681                 :         199 :       if (lhs->get_kind () == TypeKind::CONST
    3682                 :         199 :           && lhs->as_const_type ()->const_kind ()
    3683                 :             :                == BaseConstType::ConstKind::Decl)
    3684                 :             :         return false; // Would cause recursion, so not equal
    3685                 :             : 
    3686                 :         199 :       return lhs->is_equal (*rhs);
    3687                 :             :     }
    3688                 :             : 
    3689                 :          42 :   return get_symbol ().compare (other2.get_symbol ()) == 0;
    3690                 :             : }
    3691                 :             : 
    3692                 :             : BaseType *
    3693                 :          57 : ConstParamType::handle_substitions (
    3694                 :             :   SubstitutionArgumentMappings &subst_mappings)
    3695                 :             : {
    3696                 :          57 :   SubstitutionArg arg = SubstitutionArg::error ();
    3697                 :          57 :   bool ok = subst_mappings.get_argument_for_symbol (this, &arg);
    3698                 :          57 :   if (!ok || arg.is_error ())
    3699                 :           0 :     return this;
    3700                 :             : 
    3701                 :          57 :   ConstParamType *p = static_cast<ConstParamType *> (clone ());
    3702                 :          57 :   const BaseType *resolved = arg.get_tyty ();
    3703                 :             : 
    3704                 :             :   // this is the new subst that this needs to pass
    3705                 :          57 :   p->set_ref (mappings.get_next_hir_id ());
    3706                 :          57 :   p->set_ty_ref (resolved->get_ref ());
    3707                 :             : 
    3708                 :          57 :   return p;
    3709                 :             : }
    3710                 :             : 
    3711                 :             : // --- ConstValueType
    3712                 :             : 
    3713                 :        2942 : ConstValueType::ConstValueType (tree value, BaseType *type, HirId ref,
    3714                 :        2942 :                                 HirId ty_ref, std::set<HirId> refs)
    3715                 :             :   : BaseType (ref, ty_ref, KIND,
    3716                 :        2942 :               {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
    3717                 :             :               refs),
    3718                 :        5884 :     BaseConstType (type), folded_val (value)
    3719                 :        2942 : {}
    3720                 :             : 
    3721                 :             : BaseConstType::ConstKind
    3722                 :       73626 : ConstValueType::const_kind () const
    3723                 :             : {
    3724                 :       73626 :   return BaseConstType::ConstKind::Value;
    3725                 :             : }
    3726                 :             : 
    3727                 :             : void
    3728                 :           0 : ConstValueType::accept_vis (TyVisitor &vis)
    3729                 :             : {
    3730                 :           0 :   vis.visit (*this);
    3731                 :           0 : }
    3732                 :             : 
    3733                 :             : void
    3734                 :           0 : ConstValueType::accept_vis (TyConstVisitor &vis) const
    3735                 :             : {
    3736                 :           0 :   vis.visit (*this);
    3737                 :           0 : }
    3738                 :             : 
    3739                 :             : std::string
    3740                 :       62449 : ConstValueType::as_string () const
    3741                 :             : {
    3742                 :       62449 :   return generate_tree_str (folded_val);
    3743                 :             : }
    3744                 :             : 
    3745                 :             : BaseType *
    3746                 :         170 : ConstValueType::clone () const
    3747                 :             : {
    3748                 :         340 :   return new ConstValueType (folded_val, specified_type, get_ref (),
    3749                 :         170 :                              get_ty_ref (), get_combined_refs ());
    3750                 :             : }
    3751                 :             : 
    3752                 :             : std::string
    3753                 :        5943 : ConstValueType::get_name () const
    3754                 :             : {
    3755                 :        5943 :   return as_string ();
    3756                 :             : }
    3757                 :             : 
    3758                 :             : bool
    3759                 :        1888 : ConstValueType::is_equal (const BaseType &other) const
    3760                 :             : {
    3761                 :        1888 :   if (get_kind () != other.get_kind ())
    3762                 :             :     return false;
    3763                 :             : 
    3764                 :        1888 :   auto other_const = other.as_const_type ();
    3765                 :        1888 :   if (other_const->const_kind () != BaseConstType::ConstKind::Value)
    3766                 :             :     return false;
    3767                 :             : 
    3768                 :        1805 :   auto &other2 = static_cast<const ConstValueType &> (*other_const);
    3769                 :        1805 :   return folded_val == other2.folded_val;
    3770                 :             : }
    3771                 :             : 
    3772                 :             : tree
    3773                 :        7624 : ConstValueType::get_value () const
    3774                 :             : {
    3775                 :        7624 :   return folded_val;
    3776                 :             : }
    3777                 :             : 
    3778                 :             : // --- ConstInferType
    3779                 :             : 
    3780                 :         106 : ConstInferType::ConstInferType (BaseType *type, HirId ref, HirId ty_ref,
    3781                 :         106 :                                 std::set<HirId> refs)
    3782                 :             :   : BaseType (ref, ty_ref, KIND,
    3783                 :         106 :               {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
    3784                 :             :               refs),
    3785                 :         212 :     BaseConstType (type)
    3786                 :         106 : {}
    3787                 :             : 
    3788                 :             : BaseConstType::ConstKind
    3789                 :        1026 : ConstInferType::const_kind () const
    3790                 :             : {
    3791                 :        1026 :   return BaseConstType::ConstKind::Infer;
    3792                 :             : }
    3793                 :             : 
    3794                 :             : void
    3795                 :           0 : ConstInferType::accept_vis (TyVisitor &vis)
    3796                 :             : {
    3797                 :           0 :   vis.visit (*this);
    3798                 :           0 : }
    3799                 :             : 
    3800                 :             : void
    3801                 :           0 : ConstInferType::accept_vis (TyConstVisitor &vis) const
    3802                 :             : {
    3803                 :           0 :   vis.visit (*this);
    3804                 :           0 : }
    3805                 :             : 
    3806                 :             : std::string
    3807                 :         338 : ConstInferType::as_string () const
    3808                 :             : {
    3809                 :         676 :   return specified_type->get_name () + "-?";
    3810                 :             : }
    3811                 :             : 
    3812                 :             : BaseType *
    3813                 :           0 : ConstInferType::clone () const
    3814                 :             : {
    3815                 :           0 :   auto &mappings = Analysis::Mappings::get ();
    3816                 :           0 :   auto context = Resolver::TypeCheckContext::get ();
    3817                 :             : 
    3818                 :           0 :   ConstInferType *clone
    3819                 :           0 :     = new ConstInferType (specified_type, mappings.get_next_hir_id (),
    3820                 :           0 :                           get_ty_ref (), get_combined_refs ());
    3821                 :             : 
    3822                 :           0 :   context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
    3823                 :             :                                                UNKNOWN_NODEID,
    3824                 :             :                                                clone->get_ref (),
    3825                 :           0 :                                                UNKNOWN_LOCAL_DEFID),
    3826                 :             :                         clone);
    3827                 :           0 :   mappings.insert_location (clone->get_ref (),
    3828                 :             :                             mappings.lookup_location (get_ref ()));
    3829                 :             : 
    3830                 :           0 :   clone->append_reference (get_ref ());
    3831                 :             : 
    3832                 :           0 :   return clone;
    3833                 :             : }
    3834                 :             : 
    3835                 :             : std::string
    3836                 :         173 : ConstInferType::get_name () const
    3837                 :             : {
    3838                 :         173 :   return as_string ();
    3839                 :             : }
    3840                 :             : 
    3841                 :             : bool
    3842                 :          46 : ConstInferType::is_equal (const BaseType &other) const
    3843                 :             : {
    3844                 :          46 :   if (get_kind () != other.get_kind ())
    3845                 :             :     return false;
    3846                 :             : 
    3847                 :          46 :   auto other_const = other.as_const_type ();
    3848                 :          46 :   if (other_const->const_kind () != BaseConstType::ConstKind::Infer)
    3849                 :             :     return false;
    3850                 :             : 
    3851                 :           0 :   return get_ref () == other.get_ref ();
    3852                 :             : }
    3853                 :             : 
    3854                 :             : // --- ConstErrorType
    3855                 :             : 
    3856                 :           1 : ConstErrorType::ConstErrorType (BaseType *type, HirId ref, HirId ty_ref,
    3857                 :           1 :                                 std::set<HirId> refs)
    3858                 :             :   : BaseType (ref, ty_ref, KIND,
    3859                 :           1 :               {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION},
    3860                 :             :               refs),
    3861                 :           2 :     BaseConstType (type)
    3862                 :           1 : {}
    3863                 :             : 
    3864                 :             : BaseConstType::ConstKind
    3865                 :           0 : ConstErrorType::const_kind () const
    3866                 :             : {
    3867                 :           0 :   return BaseConstType::ConstKind::Error;
    3868                 :             : }
    3869                 :             : 
    3870                 :             : void
    3871                 :           0 : ConstErrorType::accept_vis (TyVisitor &vis)
    3872                 :             : {
    3873                 :           0 :   vis.visit (*this);
    3874                 :           0 : }
    3875                 :             : 
    3876                 :             : void
    3877                 :           0 : ConstErrorType::accept_vis (TyConstVisitor &vis) const
    3878                 :             : {
    3879                 :           0 :   vis.visit (*this);
    3880                 :           0 : }
    3881                 :             : 
    3882                 :             : std::string
    3883                 :           0 : ConstErrorType::as_string () const
    3884                 :             : {
    3885                 :           0 :   return "<const_error>";
    3886                 :             : }
    3887                 :             : 
    3888                 :             : BaseType *
    3889                 :           0 : ConstErrorType::clone () const
    3890                 :             : {
    3891                 :           0 :   return new ConstErrorType (specified_type, get_ref (), get_ty_ref (),
    3892                 :           0 :                              get_combined_refs ());
    3893                 :             : }
    3894                 :             : 
    3895                 :             : std::string
    3896                 :           0 : ConstErrorType::get_name () const
    3897                 :             : {
    3898                 :           0 :   return as_string ();
    3899                 :             : }
    3900                 :             : 
    3901                 :             : bool
    3902                 :           0 : ConstErrorType::is_equal (const BaseType &other) const
    3903                 :             : {
    3904                 :           0 :   if (get_kind () != other.get_kind ())
    3905                 :             :     return false;
    3906                 :             : 
    3907                 :           0 :   auto other_const = other.as_const_type ();
    3908                 :           0 :   return other_const->const_kind () == BaseConstType::ConstKind::Error;
    3909                 :             : }
    3910                 :             : 
    3911                 :             : // OpaqueType
    3912                 :             : 
    3913                 :          29 : OpaqueType::OpaqueType (location_t locus, HirId ref,
    3914                 :             :                         std::vector<TypeBoundPredicate> specified_bounds,
    3915                 :          29 :                         std::set<HirId> refs)
    3916                 :             :   : BaseType (ref, ref, KIND,
    3917                 :          58 :               {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"),
    3918                 :             :                locus},
    3919                 :          87 :               specified_bounds, refs)
    3920                 :          29 : {}
    3921                 :             : 
    3922                 :          29 : OpaqueType::OpaqueType (location_t locus, HirId ref, HirId ty_ref,
    3923                 :             :                         std::vector<TypeBoundPredicate> specified_bounds,
    3924                 :          29 :                         std::set<HirId> refs)
    3925                 :             :   : BaseType (ref, ty_ref, KIND,
    3926                 :          58 :               {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"),
    3927                 :             :                locus},
    3928                 :          87 :               specified_bounds, refs)
    3929                 :          29 : {}
    3930                 :             : 
    3931                 :             : bool
    3932                 :         252 : OpaqueType::can_resolve () const
    3933                 :             : {
    3934                 :         252 :   return get_ref () != get_ty_ref ();
    3935                 :             : }
    3936                 :             : 
    3937                 :             : void
    3938                 :           0 : OpaqueType::accept_vis (TyVisitor &vis)
    3939                 :             : {
    3940                 :           0 :   vis.visit (*this);
    3941                 :           0 : }
    3942                 :             : 
    3943                 :             : void
    3944                 :           0 : OpaqueType::accept_vis (TyConstVisitor &vis) const
    3945                 :             : {
    3946                 :           0 :   vis.visit (*this);
    3947                 :           0 : }
    3948                 :             : 
    3949                 :             : std::string
    3950                 :         112 : OpaqueType::as_string () const
    3951                 :             : {
    3952                 :         112 :   return get_name ();
    3953                 :             : }
    3954                 :             : 
    3955                 :             : std::string
    3956                 :         505 : OpaqueType::get_name () const
    3957                 :             : {
    3958                 :         505 :   return "impl " + raw_bounds_as_name ();
    3959                 :             : }
    3960                 :             : 
    3961                 :             : BaseType *
    3962                 :          29 : OpaqueType::clone () const
    3963                 :             : {
    3964                 :          58 :   return new OpaqueType (ident.locus, get_ref (), get_ty_ref (),
    3965                 :          29 :                          get_specified_bounds (), get_combined_refs ());
    3966                 :             : }
    3967                 :             : 
    3968                 :             : BaseType *
    3969                 :         310 : OpaqueType::resolve () const
    3970                 :             : {
    3971                 :         310 :   TyVar var (get_ty_ref ());
    3972                 :         310 :   return var.get_tyty ();
    3973                 :             : }
    3974                 :             : 
    3975                 :             : bool
    3976                 :          84 : OpaqueType::is_equal (const BaseType &other) const
    3977                 :             : {
    3978                 :          84 :   auto other2 = static_cast<const OpaqueType &> (other);
    3979                 :          84 :   if (can_resolve () != other2.can_resolve ())
    3980                 :             :     return false;
    3981                 :             : 
    3982                 :          56 :   if (num_specified_bounds () != other.num_specified_bounds ())
    3983                 :             :     return false;
    3984                 :             : 
    3985                 :         112 :   for (const auto &pred : specified_bounds)
    3986                 :             :     {
    3987                 :          56 :       bool found = false;
    3988                 :          56 :       for (const auto &opred : other.get_specified_bounds ())
    3989                 :             :         {
    3990                 :          56 :           found = pred.is_equal (opred);
    3991                 :          56 :           if (found)
    3992                 :             :             break;
    3993                 :             :         }
    3994                 :             : 
    3995                 :          56 :       if (!found)
    3996                 :          28 :         return false;
    3997                 :             :     }
    3998                 :             : 
    3999                 :             :   return true;
    4000                 :          84 : }
    4001                 :             : 
    4002                 :             : // StrType
    4003                 :             : 
    4004                 :        8893 : StrType::StrType (HirId ref, std::set<HirId> refs)
    4005                 :             :   : BaseType (ref, ref, KIND,
    4006                 :        8893 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4007                 :       17786 :               refs)
    4008                 :        8893 : {}
    4009                 :             : 
    4010                 :        3716 : StrType::StrType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    4011                 :             :   : BaseType (ref, ty_ref, KIND,
    4012                 :        3716 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4013                 :        7432 :               refs)
    4014                 :        3716 : {}
    4015                 :             : 
    4016                 :             : std::string
    4017                 :       96439 : StrType::get_name () const
    4018                 :             : {
    4019                 :       96439 :   return as_string ();
    4020                 :             : }
    4021                 :             : 
    4022                 :             : BaseType *
    4023                 :        3716 : StrType::clone () const
    4024                 :             : {
    4025                 :        3716 :   return new StrType (get_ref (), get_ty_ref (), get_combined_refs ());
    4026                 :             : }
    4027                 :             : 
    4028                 :             : void
    4029                 :           8 : StrType::accept_vis (TyVisitor &vis)
    4030                 :             : {
    4031                 :           8 :   vis.visit (*this);
    4032                 :           8 : }
    4033                 :             : 
    4034                 :             : void
    4035                 :        8583 : StrType::accept_vis (TyConstVisitor &vis) const
    4036                 :             : {
    4037                 :        8583 :   vis.visit (*this);
    4038                 :        8583 : }
    4039                 :             : 
    4040                 :             : std::string
    4041                 :      103481 : StrType::as_string () const
    4042                 :             : {
    4043                 :      103481 :   return "str";
    4044                 :             : }
    4045                 :             : 
    4046                 :             : bool
    4047                 :       13967 : StrType::is_equal (const BaseType &other) const
    4048                 :             : {
    4049                 :       13967 :   return get_kind () == other.get_kind ();
    4050                 :             : }
    4051                 :             : 
    4052                 :             : // Never Type
    4053                 :             : 
    4054                 :        9945 : NeverType::NeverType (HirId ref, std::set<HirId> refs)
    4055                 :             :   : BaseType (ref, ref, KIND,
    4056                 :        9945 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4057                 :       19890 :               refs)
    4058                 :        9945 : {}
    4059                 :             : 
    4060                 :         693 : NeverType::NeverType (HirId ref, HirId ty_ref, std::set<HirId> refs)
    4061                 :             :   : BaseType (ref, ty_ref, KIND,
    4062                 :         693 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4063                 :        1386 :               refs)
    4064                 :         693 : {}
    4065                 :             : 
    4066                 :             : std::string
    4067                 :       16362 : NeverType::get_name () const
    4068                 :             : {
    4069                 :       16362 :   return as_string ();
    4070                 :             : }
    4071                 :             : 
    4072                 :             : void
    4073                 :           0 : NeverType::accept_vis (TyVisitor &vis)
    4074                 :             : {
    4075                 :           0 :   vis.visit (*this);
    4076                 :           0 : }
    4077                 :             : 
    4078                 :             : void
    4079                 :        8779 : NeverType::accept_vis (TyConstVisitor &vis) const
    4080                 :             : {
    4081                 :        8779 :   vis.visit (*this);
    4082                 :        8779 : }
    4083                 :             : 
    4084                 :             : std::string
    4085                 :       17186 : NeverType::as_string () const
    4086                 :             : {
    4087                 :       17186 :   return "!";
    4088                 :             : }
    4089                 :             : 
    4090                 :             : BaseType *
    4091                 :         693 : NeverType::clone () const
    4092                 :             : {
    4093                 :         693 :   return new NeverType (get_ref (), get_ty_ref (), get_combined_refs ());
    4094                 :             : }
    4095                 :             : 
    4096                 :             : // placeholder type
    4097                 :             : 
    4098                 :         726 : PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref,
    4099                 :         726 :                                   std::set<HirId> refs)
    4100                 :             :   : BaseType (ref, ref, KIND,
    4101                 :         726 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4102                 :             :               refs),
    4103                 :        1452 :     symbol (symbol), defId (id)
    4104                 :         726 : {}
    4105                 :             : 
    4106                 :        3476 : PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref,
    4107                 :        3476 :                                   HirId ty_ref, std::set<HirId> refs)
    4108                 :             :   : BaseType (ref, ty_ref, KIND,
    4109                 :        3476 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4110                 :             :               refs),
    4111                 :        6952 :     symbol (symbol), defId (id)
    4112                 :        3476 : {}
    4113                 :             : 
    4114                 :             : std::string
    4115                 :       57915 : PlaceholderType::get_name () const
    4116                 :             : {
    4117                 :       57915 :   return as_string ();
    4118                 :             : }
    4119                 :             : 
    4120                 :             : std::string
    4121                 :        4944 : PlaceholderType::get_symbol () const
    4122                 :             : {
    4123                 :        4944 :   return symbol;
    4124                 :             : }
    4125                 :             : 
    4126                 :             : void
    4127                 :         481 : PlaceholderType::accept_vis (TyVisitor &vis)
    4128                 :             : {
    4129                 :         481 :   vis.visit (*this);
    4130                 :         481 : }
    4131                 :             : 
    4132                 :             : void
    4133                 :           0 : PlaceholderType::accept_vis (TyConstVisitor &vis) const
    4134                 :             : {
    4135                 :           0 :   vis.visit (*this);
    4136                 :           0 : }
    4137                 :             : 
    4138                 :             : std::string
    4139                 :       62679 : PlaceholderType::as_string () const
    4140                 :             : {
    4141                 :      125358 :   return "<placeholder:" + (can_resolve () ? resolve ()->as_string () : "")
    4142                 :       62679 :          + ">";
    4143                 :             : }
    4144                 :             : 
    4145                 :             : BaseType *
    4146                 :        3476 : PlaceholderType::clone () const
    4147                 :             : {
    4148                 :        6952 :   return new PlaceholderType (get_symbol (), get_def_id (), get_ref (),
    4149                 :        6952 :                               get_ty_ref (), get_combined_refs ());
    4150                 :             : }
    4151                 :             : 
    4152                 :             : void
    4153                 :        3045 : PlaceholderType::set_associated_type (HirId ref)
    4154                 :             : {
    4155                 :        3045 :   auto context = Resolver::TypeCheckContext::get ();
    4156                 :        3045 :   context->insert_associated_type_mapping (get_ty_ref (), ref);
    4157                 :        3045 : }
    4158                 :             : 
    4159                 :             : void
    4160                 :        1182 : PlaceholderType::clear_associated_type ()
    4161                 :             : {
    4162                 :        1182 :   auto context = Resolver::TypeCheckContext::get ();
    4163                 :        1182 :   context->clear_associated_type_mapping (get_ty_ref ());
    4164                 :        1182 : }
    4165                 :             : 
    4166                 :             : bool
    4167                 :      108214 : PlaceholderType::can_resolve () const
    4168                 :             : {
    4169                 :      108214 :   auto context = Resolver::TypeCheckContext::get ();
    4170                 :             : 
    4171                 :      108214 :   BaseType *lookup = nullptr;
    4172                 :      108214 :   HirId mapping;
    4173                 :             : 
    4174                 :      108214 :   if (!context->lookup_associated_type_mapping (get_ty_ref (), &mapping))
    4175                 :             :     return false;
    4176                 :             : 
    4177                 :       24742 :   if (!context->lookup_type (mapping, &lookup))
    4178                 :             :     return false;
    4179                 :             : 
    4180                 :       24742 :   return lookup != nullptr;
    4181                 :             : }
    4182                 :             : 
    4183                 :             : BaseType *
    4184                 :       24742 : PlaceholderType::resolve () const
    4185                 :             : {
    4186                 :       24742 :   auto context = Resolver::TypeCheckContext::get ();
    4187                 :             : 
    4188                 :       24742 :   HirId mapping;
    4189                 :       24742 :   bool ok = context->lookup_associated_type_mapping (get_ty_ref (), &mapping);
    4190                 :       24742 :   rust_assert (ok);
    4191                 :             : 
    4192                 :       24742 :   return TyVar (mapping).get_tyty ();
    4193                 :             : }
    4194                 :             : 
    4195                 :             : bool
    4196                 :        2450 : PlaceholderType::is_equal (const BaseType &other) const
    4197                 :             : {
    4198                 :        2450 :   if (get_kind () != other.get_kind ())
    4199                 :             :     {
    4200                 :        1716 :       if (!can_resolve ())
    4201                 :             :         return false;
    4202                 :             : 
    4203                 :         593 :       return resolve ()->is_equal (other);
    4204                 :             :     }
    4205                 :             : 
    4206                 :         734 :   auto other2 = static_cast<const PlaceholderType &> (other);
    4207                 :         734 :   return get_symbol ().compare (other2.get_symbol ()) == 0;
    4208                 :         734 : }
    4209                 :             : 
    4210                 :             : DefId
    4211                 :        3579 : PlaceholderType::get_def_id () const
    4212                 :             : {
    4213                 :        3579 :   return defId;
    4214                 :             : }
    4215                 :             : 
    4216                 :             : // Projection type
    4217                 :             : 
    4218                 :        1166 : ProjectionType::ProjectionType (
    4219                 :             :   HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item,
    4220                 :             :   std::vector<SubstitutionParamMapping> subst_refs,
    4221                 :             :   SubstitutionArgumentMappings generic_arguments,
    4222                 :        1166 :   RegionConstraints region_constraints, std::set<HirId> refs)
    4223                 :             :   : BaseType (ref, ref, KIND,
    4224                 :        1166 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4225                 :             :               std::move (refs)),
    4226                 :             :     SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
    4227                 :             :                      std::move (region_constraints)),
    4228                 :        2332 :     base (base), trait (trait), item (item)
    4229                 :        1166 : {}
    4230                 :             : 
    4231                 :         961 : ProjectionType::ProjectionType (
    4232                 :             :   HirId ref, HirId ty_ref, BaseType *base,
    4233                 :             :   const Resolver::TraitReference *trait, DefId item,
    4234                 :             :   std::vector<SubstitutionParamMapping> subst_refs,
    4235                 :             :   SubstitutionArgumentMappings generic_arguments,
    4236                 :         961 :   RegionConstraints region_constraints, std::set<HirId> refs)
    4237                 :             :   : BaseType (ref, ty_ref, KIND,
    4238                 :         961 :               {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
    4239                 :             :               refs),
    4240                 :             :     SubstitutionRef (std::move (subst_refs), std::move (generic_arguments),
    4241                 :             :                      std::move (region_constraints)),
    4242                 :        1922 :     base (base), trait (trait), item (item)
    4243                 :         961 : {}
    4244                 :             : 
    4245                 :             : std::string
    4246                 :       21369 : ProjectionType::get_name () const
    4247                 :             : {
    4248                 :       21369 :   return as_string ();
    4249                 :             : }
    4250                 :             : 
    4251                 :             : const BaseType *
    4252                 :        7571 : ProjectionType::get () const
    4253                 :             : {
    4254                 :        7571 :   return base;
    4255                 :             : }
    4256                 :             : 
    4257                 :             : BaseType *
    4258                 :       20460 : ProjectionType::get ()
    4259                 :             : {
    4260                 :       20460 :   return base;
    4261                 :             : }
    4262                 :             : 
    4263                 :             : void
    4264                 :         731 : ProjectionType::accept_vis (TyVisitor &vis)
    4265                 :             : {
    4266                 :         731 :   vis.visit (*this);
    4267                 :         731 : }
    4268                 :             : 
    4269                 :             : void
    4270                 :           0 : ProjectionType::accept_vis (TyConstVisitor &vis) const
    4271                 :             : {
    4272                 :           0 :   vis.visit (*this);
    4273                 :           0 : }
    4274                 :             : 
    4275                 :             : std::string
    4276                 :       42514 : ProjectionType::as_string () const
    4277                 :             : {
    4278                 :      127542 :   return "<Projection=" + subst_as_string () + "::" + base->as_string () + ">";
    4279                 :             : }
    4280                 :             : 
    4281                 :             : BaseType *
    4282                 :         961 : ProjectionType::clone () const
    4283                 :             : {
    4284                 :        1922 :   return new ProjectionType (get_ref (), get_ty_ref (), base->clone (), trait,
    4285                 :         961 :                              item, clone_substs (), used_arguments,
    4286                 :        2883 :                              region_constraints, get_combined_refs ());
    4287                 :             : }
    4288                 :             : 
    4289                 :             : ProjectionType *
    4290                 :         731 : ProjectionType::handle_substitions (
    4291                 :             :   SubstitutionArgumentMappings &subst_mappings)
    4292                 :             : {
    4293                 :             :   // // do we really need to substitute this?
    4294                 :             :   // if (base->needs_generic_substitutions () ||
    4295                 :             :   // base->contains_type_parameters
    4296                 :             :   // ())
    4297                 :             :   //   {
    4298                 :             :   //     return this;
    4299                 :             :   //   }
    4300                 :             : 
    4301                 :         731 :   ProjectionType *projection = static_cast<ProjectionType *> (clone ());
    4302                 :         731 :   projection->set_ty_ref (mappings.get_next_hir_id ());
    4303                 :         731 :   projection->used_arguments = subst_mappings;
    4304                 :             : 
    4305                 :         731 :   auto context = Resolver::TypeCheckContext::get ();
    4306                 :         731 :   context->insert_implicit_type (projection->get_ty_ref (), projection);
    4307                 :             : 
    4308                 :        1106 :   for (auto &sub : projection->get_substs ())
    4309                 :             :     {
    4310                 :         375 :       SubstitutionArg arg = SubstitutionArg::error ();
    4311                 :         375 :       bool ok
    4312                 :         375 :         = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
    4313                 :         375 :       if (ok)
    4314                 :         375 :         sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ());
    4315                 :             :     }
    4316                 :             : 
    4317                 :         731 :   auto fty = projection->base;
    4318                 :         731 :   bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
    4319                 :         731 :   if (is_param_ty)
    4320                 :             :     {
    4321                 :         224 :       ParamType *p = static_cast<ParamType *> (fty);
    4322                 :             : 
    4323                 :         224 :       SubstitutionArg arg = SubstitutionArg::error ();
    4324                 :         224 :       bool ok = subst_mappings.get_argument_for_symbol (p, &arg);
    4325                 :         224 :       if (ok)
    4326                 :             :         {
    4327                 :         224 :           auto argt = arg.get_tyty ();
    4328                 :         224 :           bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM;
    4329                 :         224 :           bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER;
    4330                 :             : 
    4331                 :         224 :           if (arg_is_param || arg_is_concrete)
    4332                 :             :             {
    4333                 :         184 :               auto new_field = argt->clone ();
    4334                 :         184 :               new_field->set_ref (fty->get_ref ());
    4335                 :         184 :               projection->base = new_field;
    4336                 :             :             }
    4337                 :             :           else
    4338                 :             :             {
    4339                 :          40 :               fty->set_ty_ref (argt->get_ref ());
    4340                 :             :             }
    4341                 :             :         }
    4342                 :             :     }
    4343                 :         507 :   else if (fty->needs_generic_substitutions () || !fty->is_concrete ())
    4344                 :             :     {
    4345                 :          70 :       BaseType *concrete
    4346                 :          70 :         = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings);
    4347                 :             : 
    4348                 :          70 :       if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR)
    4349                 :             :         {
    4350                 :           0 :           rust_error_at (subst_mappings.get_locus (),
    4351                 :             :                          "Failed to resolve field substitution type: %s",
    4352                 :           0 :                          fty->as_string ().c_str ());
    4353                 :           0 :           return nullptr;
    4354                 :             :         }
    4355                 :             : 
    4356                 :          70 :       projection->base = concrete;
    4357                 :             :     }
    4358                 :             : 
    4359                 :             :   return projection;
    4360                 :             : }
    4361                 :             : 
    4362                 :             : // DynObjectType
    4363                 :             : 
    4364                 :        4192 : DynamicObjectType::DynamicObjectType (
    4365                 :             :   HirId ref, RustIdent ident, std::vector<TypeBoundPredicate> specified_bounds,
    4366                 :        4192 :   std::set<HirId> refs)
    4367                 :        4192 :   : BaseType (ref, ref, KIND, ident, specified_bounds, refs)
    4368                 :        4192 : {}
    4369                 :             : 
    4370                 :         537 : DynamicObjectType::DynamicObjectType (
    4371                 :             :   HirId ref, HirId ty_ref, RustIdent ident,
    4372                 :         537 :   std::vector<TypeBoundPredicate> specified_bounds, std::set<HirId> refs)
    4373                 :         537 :   : BaseType (ref, ty_ref, KIND, ident, specified_bounds, refs)
    4374                 :         537 : {}
    4375                 :             : 
    4376                 :             : void
    4377                 :           0 : DynamicObjectType::accept_vis (TyVisitor &vis)
    4378                 :             : {
    4379                 :           0 :   vis.visit (*this);
    4380                 :           0 : }
    4381                 :             : 
    4382                 :             : void
    4383                 :          10 : DynamicObjectType::accept_vis (TyConstVisitor &vis) const
    4384                 :             : {
    4385                 :          10 :   vis.visit (*this);
    4386                 :          10 : }
    4387                 :             : 
    4388                 :             : std::string
    4389                 :         948 : DynamicObjectType::as_string () const
    4390                 :             : {
    4391                 :        1896 :   return "dyn [" + raw_bounds_as_string () + "]";
    4392                 :             : }
    4393                 :             : 
    4394                 :             : BaseType *
    4395                 :         537 : DynamicObjectType::clone () const
    4396                 :             : {
    4397                 :         537 :   return new DynamicObjectType (get_ref (), get_ty_ref (), ident,
    4398                 :        1074 :                                 specified_bounds, get_combined_refs ());
    4399                 :             : }
    4400                 :             : 
    4401                 :             : std::string
    4402                 :       49039 : DynamicObjectType::get_name () const
    4403                 :             : {
    4404                 :       98078 :   return "dyn [" + raw_bounds_as_name () + "]";
    4405                 :             : }
    4406                 :             : 
    4407                 :             : bool
    4408                 :       14098 : DynamicObjectType::is_equal (const BaseType &other) const
    4409                 :             : {
    4410                 :       14098 :   if (get_kind () != other.get_kind ())
    4411                 :             :     return false;
    4412                 :             : 
    4413                 :        1087 :   if (num_specified_bounds () != other.num_specified_bounds ())
    4414                 :             :     return false;
    4415                 :             : 
    4416                 :        2181 :   for (const auto &pred : specified_bounds)
    4417                 :             :     {
    4418                 :        1094 :       bool found = false;
    4419                 :        1112 :       for (const auto &opred : other.get_specified_bounds ())
    4420                 :             :         {
    4421                 :        1112 :           found = pred.is_equal (opred);
    4422                 :        1112 :           if (found)
    4423                 :             :             break;
    4424                 :             :         }
    4425                 :             : 
    4426                 :        1094 :       if (!found)
    4427                 :       13011 :         return false;
    4428                 :             :     }
    4429                 :             : 
    4430                 :             :   return true;
    4431                 :             : }
    4432                 :             : 
    4433                 :             : const std::vector<
    4434                 :             :   std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
    4435                 :         846 : DynamicObjectType::get_object_items () const
    4436                 :             : {
    4437                 :         846 :   std::vector<
    4438                 :             :     std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
    4439                 :         846 :     items;
    4440                 :        1704 :   for (const TypeBoundPredicate &bound : get_specified_bounds ())
    4441                 :             :     {
    4442                 :         858 :       const Resolver::TraitReference *trait = bound.get ();
    4443                 :         858 :       std::vector<const Resolver::TraitItemReference *> trait_items;
    4444                 :         858 :       trait->get_trait_items_and_supers (trait_items);
    4445                 :             : 
    4446                 :        2177 :       for (auto &item : trait_items)
    4447                 :             :         {
    4448                 :        1319 :           if (item->get_trait_item_type ()
    4449                 :             :                 == Resolver::TraitItemReference::TraitItemType::FN
    4450                 :        1319 :               && item->is_object_safe ())
    4451                 :        1319 :             items.emplace_back (item, &bound);
    4452                 :             :         }
    4453                 :         858 :     }
    4454                 :         846 :   return items;
    4455                 :             : }
    4456                 :             : 
    4457                 :             : } // namespace TyTy
    4458                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.