LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-typecheck-context.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 87.0 % 578 503
Test Date: 2026-11-14 16:39:50 Functions: 88.7 % 106 94
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 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-hir-type-check.h"
      20              : #include "rust-type-util.h"
      21              : #include "rust-hir-type-check-expr.h"
      22              : 
      23              : namespace Rust {
      24              : namespace Resolver {
      25              : 
      26              : TypeCheckContext *
      27     60377246 : TypeCheckContext::get ()
      28              : {
      29     60377246 :   static TypeCheckContext *instance;
      30     60377246 :   if (instance == nullptr)
      31         5261 :     instance = new TypeCheckContext ();
      32              : 
      33     60377246 :   return instance;
      34              : }
      35              : 
      36         5261 : TypeCheckContext::TypeCheckContext () { lifetime_resolver_stack.emplace (); }
      37              : 
      38            0 : TypeCheckContext::~TypeCheckContext () {}
      39              : 
      40              : bool
      41            0 : TypeCheckContext::lookup_builtin (NodeId id, TyTy::BaseType **type)
      42              : {
      43            0 :   auto ref_it = node_id_refs.find (id);
      44            0 :   if (ref_it == node_id_refs.end ())
      45              :     return false;
      46              : 
      47            0 :   auto it = resolved.find (ref_it->second);
      48            0 :   if (it == resolved.end ())
      49              :     return false;
      50              : 
      51            0 :   *type = it->second;
      52            0 :   return true;
      53              : }
      54              : 
      55              : bool
      56        48461 : TypeCheckContext::lookup_builtin (std::string name, TyTy::BaseType **type)
      57              : {
      58       531353 :   for (auto &builtin : builtins)
      59              :     {
      60       531353 :       if (name.compare (builtin->as_string ()) == 0)
      61              :         {
      62        48461 :           *type = builtin.get ();
      63        48461 :           return true;
      64              :         }
      65              :     }
      66              :   return false;
      67              : }
      68              : 
      69              : void
      70       101270 : TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::BaseType *type)
      71              : {
      72       101270 :   node_id_refs[ref] = id;
      73       101270 :   resolved[id] = type;
      74       101270 :   builtins.push_back (std::unique_ptr<TyTy::BaseType> (type));
      75       101270 : }
      76              : 
      77              : const std::vector<std::unique_ptr<TyTy::BaseType>> &
      78         4973 : TypeCheckContext::get_builtins () const
      79              : {
      80         4973 :   return builtins;
      81              : }
      82              : 
      83              : void
      84      1176490 : TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings,
      85              :                                TyTy::BaseType *type)
      86              : {
      87      1176490 :   rust_assert (type != nullptr);
      88      1176490 :   NodeId ref = mappings.get_nodeid ();
      89      1176490 :   HirId id = mappings.get_hirid ();
      90      1176490 :   node_id_refs[ref] = id;
      91      1176490 :   resolved[id] = type;
      92      1176490 : }
      93              : 
      94              : void
      95       509647 : TypeCheckContext::insert_implicit_type (HirId id, TyTy::BaseType *type)
      96              : {
      97       509647 :   rust_assert (type != nullptr);
      98       509647 :   resolved[id] = type;
      99       509647 : }
     100              : 
     101              : static std::pair<DefId, std::vector<HirId>>
     102        55457 : make_adt_substitution_key (DefId id,
     103              :                            const TyTy::SubstitutionArgumentMappings &mappings)
     104              : {
     105        55457 :   std::vector<HirId> arguments;
     106        55457 :   arguments.reserve (mappings.size ());
     107       115728 :   for (const auto &mapping : mappings.get_mappings ())
     108        60271 :     arguments.push_back (mapping.get_tyty ()->get_ty_ref ());
     109              : 
     110        55457 :   return {id, std::move (arguments)};
     111        55457 : }
     112              : 
     113              : bool
     114        37754 : TypeCheckContext::lookup_adt_substitution (
     115              :   DefId id, const TyTy::SubstitutionArgumentMappings &mappings,
     116              :   TyTy::ADTType **type) const
     117              : {
     118        37754 :   auto it = adt_substitutions.find (make_adt_substitution_key (id, mappings));
     119        37754 :   if (it == adt_substitutions.end ())
     120              :     return false;
     121              : 
     122        20051 :   *type = it->second;
     123        20051 :   return true;
     124              : }
     125              : 
     126              : void
     127        17703 : TypeCheckContext::insert_adt_substitution (
     128              :   DefId id, const TyTy::SubstitutionArgumentMappings &mappings,
     129              :   TyTy::ADTType *type)
     130              : {
     131        17703 :   rust_assert (type != nullptr);
     132        17703 :   auto inserted
     133        17703 :     = adt_substitutions.emplace (make_adt_substitution_key (id, mappings),
     134              :                                  type);
     135        17703 :   rust_assert (inserted.second);
     136        17703 : }
     137              : 
     138              : bool
     139     41082210 : TypeCheckContext::lookup_type (HirId id, TyTy::BaseType **type) const
     140              : {
     141     41082210 :   auto it = resolved.find (id);
     142     41082210 :   if (it == resolved.end ())
     143              :     return false;
     144              : 
     145     40164720 :   *type = it->second;
     146     40164720 :   return true;
     147              : }
     148              : 
     149              : void
     150        17140 : TypeCheckContext::mark_function_body_pending (DefId id)
     151              : {
     152        17140 :   function_bodies_pending.insert (id);
     153        17140 : }
     154              : 
     155              : void
     156        18563 : TypeCheckContext::clear_function_body_pending (DefId id)
     157              : {
     158        18563 :   function_bodies_pending.erase (id);
     159        18563 : }
     160              : 
     161              : bool
     162         4372 : TypeCheckContext::function_body_pending (DefId id) const
     163              : {
     164         4372 :   return function_bodies_pending.find (id) != function_bodies_pending.end ();
     165              : }
     166              : 
     167              : void
     168        44386 : TypeCheckContext::clear_type (TyTy::BaseType *ty)
     169              : {
     170        44386 :   auto it = resolved.find (ty->get_ref ());
     171        44386 :   if (it == resolved.end ())
     172        44386 :     return;
     173              : 
     174        44386 :   resolved.erase (it);
     175              : }
     176              : 
     177              : void
     178            0 : TypeCheckContext::insert_type_by_node_id (NodeId ref, HirId id)
     179              : {
     180            0 :   rust_assert (node_id_refs.find (ref) == node_id_refs.end ());
     181            0 :   node_id_refs[ref] = id;
     182            0 : }
     183              : 
     184              : bool
     185            0 : TypeCheckContext::lookup_type_by_node_id (NodeId ref, HirId *id)
     186              : {
     187            0 :   auto it = node_id_refs.find (ref);
     188            0 :   if (it == node_id_refs.end ())
     189              :     return false;
     190              : 
     191            0 :   *id = it->second;
     192            0 :   return true;
     193              : }
     194              : 
     195              : bool
     196        26147 : TypeCheckContext::have_function_context () const
     197              : {
     198        26147 :   return !return_type_stack.empty ();
     199              : }
     200              : 
     201              : TyTy::BaseType *
     202         1237 : TypeCheckContext::peek_return_type ()
     203              : {
     204         1237 :   rust_assert (!return_type_stack.empty ());
     205         1237 :   return return_type_stack.back ().second;
     206              : }
     207              : 
     208              : void
     209        18563 : TypeCheckContext::push_return_type (TypeCheckContextItem item,
     210              :                                     TyTy::BaseType *return_type)
     211              : {
     212        18563 :   return_type_stack.emplace_back (std::move (item), return_type);
     213              :   // a query can check another function body while an expression in the
     214              :   // caller has an expected type
     215        18563 :   push_expected_type (nullptr);
     216        18563 : }
     217              : 
     218              : void
     219        18472 : TypeCheckContext::pop_return_type ()
     220              : {
     221        18472 :   rust_assert (!return_type_stack.empty ());
     222        18472 :   pop_expected_type ();
     223        18472 :   return_type_stack.pop_back ();
     224        18472 : }
     225              : 
     226              : TypeCheckContextItem
     227        24855 : TypeCheckContext::peek_context ()
     228              : {
     229        24855 :   rust_assert (!return_type_stack.empty ());
     230        24855 :   return return_type_stack.back ().first;
     231              : }
     232              : 
     233              : void
     234        84453 : TypeCheckContext::push_expected_type (TyTy::BaseType *expected)
     235              : {
     236        84453 :   expected_type_stack.push_back (expected);
     237        84453 : }
     238              : 
     239              : void
     240        84236 : TypeCheckContext::pop_expected_type ()
     241              : {
     242        84236 :   rust_assert (!expected_type_stack.empty ());
     243        84236 :   expected_type_stack.pop_back ();
     244        84236 : }
     245              : 
     246              : TyTy::BaseType *
     247        42251 : TypeCheckContext::peek_expected_type () const
     248              : {
     249        42251 :   if (expected_type_stack.empty ())
     250              :     return nullptr;
     251        41954 :   return expected_type_stack.back ();
     252              : }
     253              : 
     254              : StackedContexts<TypeCheckBlockContextItem> &
     255        91440 : TypeCheckContext::block_context ()
     256              : {
     257        91440 :   return block_stack;
     258              : }
     259              : 
     260              : void
     261         4749 : TypeCheckContext::iterate (std::function<bool (HirId, TyTy::BaseType *)> cb)
     262              : {
     263       589576 :   for (auto it = resolved.begin (); it != resolved.end (); it++)
     264              :     {
     265       584827 :       if (!cb (it->first, it->second))
     266         4749 :         return;
     267              :     }
     268              : }
     269              : 
     270              : bool
     271          183 : TypeCheckContext::have_loop_context () const
     272              : {
     273          183 :   return !loop_type_stack.empty ();
     274              : }
     275              : 
     276              : void
     277          190 : TypeCheckContext::push_new_loop_context (HirId id, location_t locus)
     278              : {
     279          190 :   TyTy::BaseType *infer_var
     280              :     = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL,
     281          190 :                            TyTy::InferType::TypeHint::Default (), locus);
     282          190 :   loop_type_stack.push_back (infer_var);
     283          190 : }
     284              : 
     285              : void
     286           95 : TypeCheckContext::push_new_while_loop_context (HirId id)
     287              : {
     288           95 :   TyTy::BaseType *infer_var = new TyTy::ErrorType (id);
     289           95 :   loop_type_stack.push_back (infer_var);
     290           95 : }
     291              : 
     292              : TyTy::BaseType *
     293          310 : TypeCheckContext::peek_loop_context ()
     294              : {
     295          310 :   return loop_type_stack.back ();
     296              : }
     297              : 
     298              : TyTy::BaseType *
     299          285 : TypeCheckContext::pop_loop_context ()
     300              : {
     301          285 :   auto back = peek_loop_context ();
     302          285 :   loop_type_stack.pop_back ();
     303          285 :   return back;
     304              : }
     305              : 
     306              : void
     307           21 : TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val)
     308              : {
     309           21 :   loop_type_stack.pop_back ();
     310           21 :   loop_type_stack.push_back (val);
     311           21 : }
     312              : 
     313              : bool
     314        23244 : TypeCheckContext::find_matching_impl_trait_frame (
     315              :   const TraitReference &tref, TyTy::BaseType &self,
     316              :   struct ImplTraitContextFrame *find) const
     317              : {
     318        23244 :   if (!have_impl_trait_context ())
     319              :     return false;
     320              : 
     321        67400 :   for (auto it = impl_trait_frame_stack.rbegin ();
     322        67400 :        it != impl_trait_frame_stack.rend (); ++it)
     323              :     {
     324        62459 :       const auto &i = *it;
     325        62459 :       if (!i.trait->is_equal (tref))
     326        19757 :         continue;
     327              : 
     328        42702 :       TyTy::BaseType *resolved_self = &self;
     329        42702 :       bool unresolved_trait_self = false;
     330        42702 :       if (auto param = self.try_as<TyTy::ParamType> ())
     331              :         {
     332           85 :           if (param->can_resolve ())
     333           84 :             resolved_self = param->resolve ();
     334              :           else
     335            1 :             unresolved_trait_self = param->is_implicit_self_trait ();
     336              :         }
     337              : 
     338              :       // Select an existing impl context without inferring a new Self
     339              :       // binding
     340        42786 :       bool compatible_self = unresolved_trait_self;
     341           85 :       if (!compatible_self)
     342              :         {
     343        42701 :           auto res
     344        42701 :             = unify_site_and (UNKNOWN_HIRID, TyTy::TyWithLocation (i.self),
     345        42701 :                               TyTy::TyWithLocation (resolved_self),
     346              :                               UNDEF_LOCATION, false /* emit_errors */,
     347              :                               false /* commit */, false /* infer */,
     348              :                               true /* cleanup */, false /* check_bounds */);
     349        42701 :           compatible_self = res->get_kind () != TyTy::TypeKind::ERROR;
     350              :         }
     351              : 
     352        42701 :       if (compatible_self)
     353              :         {
     354        16452 :           *find = i;
     355              :           return true;
     356              :         }
     357              :     }
     358              : 
     359              :   return false;
     360              : }
     361              : 
     362              : bool
     363        23244 : TypeCheckContext::have_impl_trait_context () const
     364              : {
     365        23244 :   return !impl_trait_frame_stack.empty ();
     366              : }
     367              : 
     368              : void
     369        30341 : TypeCheckContext::push_impl_trait_context (struct ImplTraitContextFrame frame)
     370              : {
     371        30341 :   impl_trait_frame_stack.push_back (frame);
     372        30341 : }
     373              : 
     374              : struct ImplTraitContextFrame
     375        30299 : TypeCheckContext::pop_impl_trait_context ()
     376              : {
     377        30299 :   auto back = peek_impl_trait_context ();
     378        30299 :   impl_trait_frame_stack.pop_back ();
     379        30299 :   return back;
     380              : }
     381              : 
     382              : struct ImplTraitContextFrame
     383        30299 : TypeCheckContext::peek_impl_trait_context ()
     384              : {
     385        30299 :   return impl_trait_frame_stack.back ();
     386              : }
     387              : 
     388              : void
     389         5359 : TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref)
     390              : {
     391         5359 :   rust_assert (trait_context.find (id) == trait_context.end ());
     392         5359 :   trait_context.emplace (id, std::move (ref));
     393         5359 : }
     394              : 
     395              : bool
     396      1595940 : TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref)
     397              : {
     398      1595940 :   auto it = trait_context.find (id);
     399      1595940 :   if (it == trait_context.end ())
     400              :     return false;
     401              : 
     402      1572368 :   *ref = &it->second;
     403      1572368 :   return true;
     404              : }
     405              : 
     406              : bool
     407         8167 : TypeCheckContext::insert_associated_trait_impl (
     408              :   HirId id, AssociatedImplTrait &&associated)
     409              : {
     410         8167 :   auto it = associated_impl_traits.find (id);
     411         8167 :   if (it != associated_impl_traits.end ())
     412              :     {
     413              :       return false;
     414              :     }
     415         8138 :   associated_impl_traits.emplace (id, std::move (associated));
     416         8138 :   return true;
     417              : }
     418              : 
     419              : bool
     420        36215 : TypeCheckContext::lookup_associated_trait_impl (
     421              :   HirId id, AssociatedImplTrait **associated)
     422              : {
     423        36215 :   auto it = associated_impl_traits.find (id);
     424        36215 :   if (it == associated_impl_traits.end ())
     425              :     return false;
     426              : 
     427        17317 :   *associated = &it->second;
     428        17317 :   return true;
     429              : }
     430              : 
     431              : void
     432            0 : TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping)
     433              : {
     434            0 :   associated_type_mappings[id] = mapping;
     435            0 : }
     436              : 
     437              : void
     438            0 : TypeCheckContext::clear_associated_type_mapping (HirId id)
     439              : {
     440            0 :   auto it = associated_type_mappings.find (id);
     441            0 :   if (it != associated_type_mappings.end ())
     442            0 :     associated_type_mappings.erase (it);
     443            0 : }
     444              : 
     445              : // lookup any associated type mappings, the out parameter of mapping is
     446              : // allowed to be nullptr which allows this interface to do a simple does exist
     447              : // check
     448              : bool
     449            0 : TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping)
     450              : {
     451            0 :   auto it = associated_type_mappings.find (id);
     452            0 :   if (it == associated_type_mappings.end ())
     453              :     return false;
     454              : 
     455            0 :   if (mapping != nullptr)
     456            0 :     *mapping = it->second;
     457              : 
     458              :   return true;
     459              : }
     460              : 
     461              : void
     462         8167 : TypeCheckContext::insert_associated_impl_mapping (HirId trait_id,
     463              :                                                   TyTy::BaseType *impl_type,
     464              :                                                   HirId impl_id)
     465              : {
     466         8167 :   auto it = associated_traits_to_impls.find (trait_id);
     467         8167 :   if (it == associated_traits_to_impls.end ())
     468              :     {
     469         2172 :       associated_traits_to_impls[trait_id] = {};
     470              :     }
     471              : 
     472         8167 :   associated_traits_to_impls[trait_id].emplace_back (impl_type, impl_id);
     473         8167 : }
     474              : 
     475              : bool
     476            0 : TypeCheckContext::lookup_associated_impl_mapping_for_self (HirId trait_id,
     477              :                                                            TyTy::BaseType *self,
     478              :                                                            HirId *mapping)
     479              : {
     480            0 :   auto it = associated_traits_to_impls.find (trait_id);
     481            0 :   if (it == associated_traits_to_impls.end ())
     482              :     return false;
     483              : 
     484            0 :   for (auto &item : it->second)
     485              :     {
     486            0 :       if (types_compatable (TyTy::TyWithLocation (item.first),
     487            0 :                             TyTy::TyWithLocation (self), UNKNOWN_LOCATION,
     488              :                             false))
     489              :         {
     490            0 :           *mapping = item.second;
     491            0 :           return true;
     492              :         }
     493              :     }
     494              :   return false;
     495              : }
     496              : 
     497              : void
     498        57336 : TypeCheckContext::insert_autoderef_mappings (
     499              :   HirId id, std::vector<Adjustment> &&adjustments)
     500              : {
     501        57336 :   autoderef_mappings.emplace (id, std::move (adjustments));
     502        57336 : }
     503              : 
     504              : bool
     505        56028 : TypeCheckContext::lookup_autoderef_mappings (
     506              :   HirId id, std::vector<Adjustment> **adjustments)
     507              : {
     508        56028 :   auto it = autoderef_mappings.find (id);
     509        56028 :   if (it == autoderef_mappings.end ())
     510              :     return false;
     511              : 
     512        41443 :   *adjustments = &it->second;
     513        41443 :   return true;
     514              : }
     515              : 
     516              : void
     517         6544 : TypeCheckContext::insert_cast_autoderef_mappings (
     518              :   HirId id, std::vector<Adjustment> &&adjustments)
     519              : {
     520         6544 :   cast_autoderef_mappings.emplace (id, std::move (adjustments));
     521         6544 : }
     522              : 
     523              : bool
     524         5503 : TypeCheckContext::lookup_cast_autoderef_mappings (
     525              :   HirId id, std::vector<Adjustment> **adjustments)
     526              : {
     527         5503 :   auto it = cast_autoderef_mappings.find (id);
     528         5503 :   if (it == cast_autoderef_mappings.end ())
     529              :     return false;
     530              : 
     531         5503 :   *adjustments = &it->second;
     532         5503 :   return true;
     533              : }
     534              : 
     535              : void
     536         4774 : TypeCheckContext::insert_variant_definition (HirId id, HirId variant)
     537              : {
     538         4774 :   auto it = variants.find (id);
     539         4774 :   rust_assert (it == variants.end ());
     540              : 
     541         4774 :   variants[id] = variant;
     542         4774 : }
     543              : 
     544              : bool
     545        10318 : TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant)
     546              : {
     547        10318 :   auto it = variants.find (id);
     548        10318 :   if (it == variants.end ())
     549              :     return false;
     550              : 
     551        10311 :   *variant = it->second;
     552        10311 :   return true;
     553              : }
     554              : 
     555              : void
     556         1923 : TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site)
     557              : {
     558         1923 :   auto it = operator_overloads.find (id);
     559         1923 :   rust_assert (it == operator_overloads.end ());
     560              : 
     561         1923 :   operator_overloads[id] = call_site;
     562         1923 : }
     563              : 
     564              : bool
     565        32245 : TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call)
     566              : {
     567        32245 :   auto it = operator_overloads.find (id);
     568        32245 :   if (it == operator_overloads.end ())
     569              :     return false;
     570              : 
     571         2478 :   *call = it->second;
     572         2478 :   return true;
     573              : }
     574              : 
     575              : void
     576         4201 : TypeCheckContext::insert_deferred_operator_overload (
     577              :   DeferredOpOverload deferred)
     578              : {
     579         4201 :   HirId expr_id = deferred.expr_id;
     580         4201 :   deferred_operator_overloads.emplace (std::make_pair (expr_id, deferred));
     581         4201 : }
     582              : 
     583              : bool
     584            0 : TypeCheckContext::lookup_deferred_operator_overload (
     585              :   HirId id, DeferredOpOverload *deferred)
     586              : {
     587            0 :   auto it = deferred_operator_overloads.find (id);
     588            0 :   if (it == deferred_operator_overloads.end ())
     589              :     return false;
     590              : 
     591            0 :   *deferred = it->second;
     592            0 :   return true;
     593              : }
     594              : 
     595              : void
     596            0 : TypeCheckContext::iterate_deferred_operator_overloads (
     597              :   std::function<bool (HirId, DeferredOpOverload &)> cb)
     598              : {
     599            0 :   for (auto it = deferred_operator_overloads.begin ();
     600            0 :        it != deferred_operator_overloads.end (); it++)
     601              :     {
     602            0 :       if (!cb (it->first, it->second))
     603            0 :         return;
     604              :     }
     605              : }
     606              : 
     607              : void
     608        52816 : TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status)
     609              : {
     610        52816 :   unconstrained[id] = status;
     611        52816 : }
     612              : 
     613              : bool
     614        74769 : TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result)
     615              : {
     616        74769 :   auto it = unconstrained.find (id);
     617        74769 :   bool found = it != unconstrained.end ();
     618        74769 :   if (!found)
     619              :     return false;
     620              : 
     621        21953 :   *result = it->second;
     622        21953 :   return true;
     623              : }
     624              : 
     625              : void
     626        83973 : TypeCheckContext::insert_resolved_predicate (
     627              :   HirId id, const TyTy::TypeBoundPredicate &predicate)
     628              : {
     629              :   // auto it = predicates.find (id);
     630              :   // rust_assert (it == predicates.end ());
     631              : 
     632        83973 :   predicates.emplace (id, predicate);
     633        83973 : }
     634              : 
     635              : bool
     636       130590 : TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result)
     637              : {
     638       130590 :   auto it = predicates.find (id);
     639       130590 :   bool found = it != predicates.end ();
     640       130590 :   if (!found)
     641              :     return false;
     642              : 
     643        42181 :   *result = it->second;
     644        42181 :   return true;
     645              : }
     646              : 
     647              : std::vector<TyTy::TypeBoundPredicate>
     648        16863 : TypeCheckContext::predicates_for_type (const TyTy::BaseType *receiver) const
     649              : {
     650        16863 :   std::vector<TyTy::TypeBoundPredicate> result
     651        16863 :     = receiver->get_specified_bounds ();
     652              : 
     653     40279098 :   for (const auto &entry : predicates)
     654              :     {
     655     40262235 :       const auto &predicate = entry.second;
     656     40262235 :       const auto &args = predicate.get_substitution_arguments ();
     657     40262235 :       if (args.get_mappings ().empty ())
     658     40262230 :         continue;
     659              : 
     660     40262235 :       TyTy::BaseType *bound_self = args.get_mappings ().front ().get_tyty ();
     661     40564920 :       if (bound_self == nullptr
     662     40262235 :           || bound_self->get_kind () == TyTy::TypeKind::ERROR)
     663       302685 :         continue;
     664              : 
     665     39959550 :       if (receiver->get_kind () != TyTy::TypeKind::PROJECTION
     666     39959550 :           || bound_self->get_kind () != TyTy::TypeKind::PROJECTION)
     667     39959542 :         continue;
     668              : 
     669            8 :       const auto *receiver_projection
     670              :         = static_cast<const TyTy::ProjectionType *> (receiver);
     671            8 :       const auto *bound_projection
     672              :         = static_cast<const TyTy::ProjectionType *> (bound_self);
     673              : 
     674            8 :       DefId receiver_item_defid = receiver_projection->get_item_defid ();
     675            8 :       DefId bound_item_defid = bound_projection->get_item_defid ();
     676            8 :       bool same_item = receiver_item_defid == bound_item_defid;
     677            8 :       if (!same_item)
     678            1 :         continue;
     679              : 
     680            7 :       DefId receiver_trait_defid
     681            7 :         = receiver_projection->get_trait_ref ()->get_mappings ().get_defid ();
     682            7 :       DefId bound_trait_defid
     683            7 :         = bound_projection->get_trait_ref ()->get_mappings ().get_defid ();
     684            7 :       bool same_trait = receiver_trait_defid == bound_trait_defid;
     685            7 :       if (!same_trait)
     686            0 :         continue;
     687              : 
     688              :       // Compatibility can unify unrelated parameters. Match their declaration
     689              :       // identities instead; copies and synthetic bound parameters retain them.
     690            7 :       const TyTy::BaseType *receiver_self = receiver_projection->get_self ();
     691            7 :       const TyTy::BaseType *bound_self_ty = bound_projection->get_self ();
     692            7 :       const auto *receiver_param
     693            7 :         = receiver_self->try_as<const TyTy::ParamType> ();
     694            7 :       const auto *bound_param = bound_self_ty->try_as<const TyTy::ParamType> ();
     695              : 
     696            7 :       bool either_self_is_param
     697            7 :         = receiver_param != nullptr || bound_param != nullptr;
     698            7 :       if (either_self_is_param)
     699              :         {
     700            7 :           bool both_selfs_are_param
     701              :             = receiver_param != nullptr && bound_param != nullptr;
     702            7 :           bool same_declaration
     703              :             = both_selfs_are_param
     704            7 :               && receiver_param->get_decl_id () == bound_param->get_decl_id ();
     705            2 :           if (!same_declaration)
     706            2 :             continue;
     707              :         }
     708              :       else
     709              :         {
     710            0 :           bool selfs_compatible = types_compatable (
     711            0 :             TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver_self)),
     712            0 :             TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (bound_self_ty)),
     713              :             UNKNOWN_LOCATION, false, false);
     714            0 :           if (!selfs_compatible)
     715            0 :             continue;
     716              :         }
     717              : 
     718            5 :       bool projections_compatible = types_compatable (
     719            5 :         TyTy::TyWithLocation (const_cast<TyTy::BaseType *> (receiver)),
     720            5 :         TyTy::TyWithLocation (bound_self), UNKNOWN_LOCATION, false, false);
     721            5 :       if (projections_compatible)
     722            5 :         result.push_back (predicate);
     723              :     }
     724              : 
     725        16863 :   return result;
     726              : }
     727              : 
     728              : void
     729        67253 : TypeCheckContext::insert_query (HirId id)
     730              : {
     731        67253 :   querys_in_progress.insert (id);
     732        67253 : }
     733              : 
     734              : void
     735        67106 : TypeCheckContext::query_completed (HirId id)
     736              : {
     737        67106 :   querys_in_progress.erase (id);
     738        67106 : }
     739              : 
     740              : bool
     741       117036 : TypeCheckContext::query_in_progress (HirId id) const
     742              : {
     743       117036 :   return querys_in_progress.find (id) != querys_in_progress.end ();
     744              : }
     745              : 
     746              : void
     747         5374 : TypeCheckContext::insert_trait_query (DefId id)
     748              : {
     749         5374 :   trait_queries_in_progress.insert (id);
     750         5374 : }
     751              : 
     752              : void
     753         5374 : TypeCheckContext::trait_query_completed (DefId id)
     754              : {
     755         5374 :   trait_queries_in_progress.erase (id);
     756         5374 : }
     757              : 
     758              : bool
     759         5378 : TypeCheckContext::trait_query_in_progress (DefId id) const
     760              : {
     761         5378 :   return trait_queries_in_progress.find (id)
     762         5378 :          != trait_queries_in_progress.end ();
     763              : }
     764              : 
     765              : Lifetime
     766        10024 : TypeCheckContext::intern_lifetime (const HIR::Lifetime &lifetime)
     767              : {
     768        10024 :   if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
     769              :     {
     770        10023 :       auto maybe_interned = lookup_lifetime (lifetime);
     771        10023 :       if (maybe_interned)
     772         9872 :         return *maybe_interned;
     773              : 
     774          151 :       auto interned = next_lifetime_index.next ();
     775          151 :       lifetime_name_interner[lifetime.get_name ()] = interned;
     776          151 :       return interned;
     777              :     }
     778            1 :   if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
     779              :     {
     780            1 :       return next_lifetime_index.next ();
     781              :     }
     782            0 :   if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
     783              :     {
     784            0 :       return Lifetime::static_lifetime ();
     785              :     }
     786            0 :   rust_unreachable ();
     787              : }
     788              : 
     789              : tl::optional<Lifetime>
     790        55468 : TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const
     791              : {
     792        55468 :   if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
     793              :     {
     794        18461 :       if (lifetime.get_name () == "static")
     795              :         {
     796            1 :           rich_location r (line_table, lifetime.get_locus ());
     797            1 :           r.add_fixit_insert_after (lifetime.get_locus (),
     798              :                                     "static is a reserved lifetime name");
     799            1 :           rust_error_at (r, ErrorCode::E0262,
     800              :                          "invalid lifetime parameter name: %qs",
     801            1 :                          lifetime.get_name ().c_str ());
     802            1 :           return tl::nullopt;
     803            1 :         }
     804        18460 :       const auto name = lifetime.get_name ();
     805        18460 :       auto it = lifetime_name_interner.find (name);
     806        18460 :       if (it == lifetime_name_interner.end ())
     807          155 :         return tl::nullopt;
     808        18305 :       return it->second;
     809        18460 :     }
     810        37007 :   if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
     811              :     {
     812        36849 :       return Lifetime::anonymous_lifetime ();
     813              :     }
     814          158 :   if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
     815              :     {
     816          158 :       return Lifetime::static_lifetime ();
     817              :     }
     818            0 :   rust_unreachable ();
     819              : }
     820              : 
     821              : WARN_UNUSED_RESULT tl::optional<TyTy::Region>
     822        45445 : TypeCheckContext::lookup_and_resolve_lifetime (
     823              :   const HIR::Lifetime &lifetime) const
     824              : {
     825        45445 :   auto maybe_interned = lookup_lifetime (lifetime);
     826        45445 :   if (!maybe_interned)
     827            5 :     return tl::nullopt;
     828              : 
     829        45440 :   return get_lifetime_resolver ().resolve (maybe_interned.value ());
     830              : }
     831              : void
     832          167 : TypeCheckContext::intern_and_insert_lifetime (const HIR::Lifetime &lifetime)
     833              : {
     834          167 :   get_lifetime_resolver ().insert_mapping (intern_lifetime (lifetime));
     835          167 : }
     836              : 
     837              : WARN_UNUSED_RESULT std::vector<TyTy::Region>
     838       113806 : TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
     839              : {
     840       113806 :   std::vector<TyTy::Region> regions;
     841       117985 :   for (const auto &lifetime : args.get_lifetime_args ())
     842              :     {
     843         4180 :       auto resolved = lookup_and_resolve_lifetime (lifetime);
     844         4180 :       if (!resolved)
     845              :         {
     846            1 :           rust_error_at (lifetime.get_locus (), "unresolved lifetime");
     847            1 :           return {};
     848              :         }
     849         4179 :       regions.push_back (*resolved);
     850              :     }
     851       113805 :   return regions;
     852       113806 : }
     853              : 
     854              : bool
     855           15 : TypeCheckContext::compute_ambigious_op_overload (HirId id,
     856              :                                                  DeferredOpOverload &op)
     857              : {
     858           15 :   rust_debug ("attempting resolution of deferred operator overload");
     859              : 
     860           15 :   TyTy::BaseType *lhs = nullptr;
     861           15 :   bool ok = lookup_type (op.op.get_lvalue_mappings ().get_hirid (), &lhs);
     862           15 :   rust_assert (ok);
     863              : 
     864           15 :   TyTy::BaseType *rhs = nullptr;
     865           15 :   if (op.op.has_rvalue_mappings ())
     866              :     {
     867           15 :       bool ok = lookup_type (op.op.get_rvalue_mappings ().get_hirid (), &rhs);
     868           15 :       rust_assert (ok);
     869              :     }
     870              : 
     871           15 :   TyTy::BaseType *current_result = op.result_type.get_tyty ();
     872           15 :   rust_assert (current_result != nullptr);
     873              : 
     874           15 :   rust_debug ("deferred operator expr=%u lhs=%s rhs=%s "
     875              :               "stored-result=%s current-result=%s result-ref=%u ty-ref=%u",
     876              :               id, lhs->debug_str ().c_str (),
     877              :               rhs == nullptr ? "<none>" : rhs->debug_str ().c_str (),
     878              :               current_result->debug_str ().c_str (),
     879              :               current_result->debug_str ().c_str (), op.result_type.get_ref (),
     880              :               current_result->get_ty_ref ());
     881              : 
     882           15 :   TyTy::BaseType *resolved
     883           15 :     = TypeCheckExpr::ResolveOpOverload (op.lang_item_type, op.op, lhs, rhs,
     884           15 :                                         op.specified_segment, current_result);
     885           15 :   if (resolved == nullptr)
     886              :     return false;
     887              : 
     888           15 :   TyTy::BaseType *result
     889           15 :     = unify_site (id, TyTy::TyWithLocation (current_result),
     890           15 :                   TyTy::TyWithLocation (resolved), op.op.get_locus ());
     891           15 :   rust_assert (result != nullptr);
     892           15 :   rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
     893              : 
     894              :   return true;
     895              : }
     896              : 
     897              : void
     898         4749 : TypeCheckContext::compute_inference_variables (bool emit_error)
     899              : {
     900         9498 :   auto resolve_deferred_operator_overloads = [this] () {
     901         9513 :     bool progress;
     902         9513 :     do
     903              :       {
     904         9513 :         progress = false;
     905         9513 :         for (auto it = deferred_operator_overloads.begin ();
     906         9528 :              it != deferred_operator_overloads.end ();)
     907              :           {
     908           15 :             if (compute_ambigious_op_overload (it->first, it->second))
     909              :               {
     910           15 :                 it = deferred_operator_overloads.erase (it);
     911           15 :                 progress = true;
     912              :               }
     913              :             else
     914            0 :               ++it;
     915              :           }
     916              :       }
     917              :     while (progress);
     918        14247 :   };
     919              : 
     920         4749 :   resolve_deferred_operator_overloads ();
     921              : 
     922         4749 :   iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
     923       584827 :     return compute_infer_var (id, ty, emit_error);
     924              :   });
     925              : 
     926         4749 :   resolve_deferred_operator_overloads ();
     927         4749 : }
     928              : 
     929              : bool
     930       584827 : TypeCheckContext::compute_infer_var (HirId id, TyTy::BaseType *ty,
     931              :                                      bool emit_error)
     932              : {
     933       584827 :   auto &mappings = Analysis::Mappings::get ();
     934              : 
     935              :   // nothing to do
     936       584827 :   if (ty->get_kind () != TyTy::TypeKind::INFER)
     937              :     return true;
     938              : 
     939         1671 :   TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
     940         1671 :   TyTy::BaseType *default_type;
     941              : 
     942         1671 :   TyTy::BaseType *current = TyTy::TyVar (ty->get_ref ()).get_tyty ();
     943         1671 :   rust_assert (current != nullptr);
     944              : 
     945         1671 :   rust_debug_loc (mappings.lookup_location (id),
     946              :                   "trying to default infer-var id=%u ref=%u ty-ref=%u "
     947              :                   "stored=%s current=%s",
     948              :                   id, ty->get_ref (), ty->get_ty_ref (),
     949              :                   infer_var->debug_str ().c_str (),
     950              :                   current->debug_str ().c_str ());
     951         1671 :   bool ok = infer_var->default_type (&default_type);
     952         1671 :   if (!ok)
     953              :     {
     954            8 :       if (emit_error)
     955            8 :         rust_error_at (mappings.lookup_location (id), ErrorCode::E0282,
     956              :                        "type annotations needed");
     957              :       return true;
     958              :     }
     959              : 
     960         1663 :   rust_debug_loc (mappings.lookup_location (id), "default type selected: %s",
     961              :                   default_type->debug_str ().c_str ());
     962              : 
     963         1663 :   auto result
     964         1663 :     = unify_site (id, TyTy::TyWithLocation (ty),
     965         1663 :                   TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
     966         1663 :   rust_assert (result);
     967         1663 :   rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
     968         1663 :   result->set_ref (id);
     969         1663 :   insert_implicit_type (id, result);
     970              : 
     971         1663 :   return true;
     972              : }
     973              : 
     974              : TyTy::VarianceAnalysis::CrateCtx &
     975        11209 : TypeCheckContext::get_variance_analysis_ctx ()
     976              : {
     977        11209 :   return variance_analysis_ctx;
     978              : }
     979              : 
     980              : // TypeCheckContextItem
     981              : 
     982        15915 : TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}
     983              : 
     984        10259 : TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block,
     985              :                                   HIR::Function *item)
     986        10259 :   : impl_item ({impl_block, item})
     987        10259 : {}
     988              : 
     989          926 : TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item)
     990          926 :   : trait_item (trait_item)
     991          926 : {}
     992              : 
     993         7378 : TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item)
     994         7378 :   : type (ItemType::ITEM), item (item)
     995         7378 : {}
     996              : 
     997        10259 : TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock &impl_block,
     998              :                                             HIR::Function *item)
     999        10259 :   : type (ItemType::IMPL_ITEM), item (&impl_block, item)
    1000        10259 : {}
    1001              : 
    1002          926 : TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item)
    1003          926 :   : type (ItemType::TRAIT_ITEM), item (trait_item)
    1004          926 : {}
    1005              : 
    1006        43966 : TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other)
    1007        43966 :   : type (other.type), item (other.item)
    1008              : {
    1009        43966 :   switch (other.type)
    1010              :     {
    1011        17252 :     case ITEM:
    1012        17252 :       item.item = other.item.item;
    1013        17252 :       break;
    1014              : 
    1015        25224 :     case IMPL_ITEM:
    1016        25224 :       item.impl_item = other.item.impl_item;
    1017        25224 :       break;
    1018              : 
    1019         1490 :     case TRAIT_ITEM:
    1020         1490 :       item.trait_item = other.item.trait_item;
    1021         1490 :       break;
    1022              : 
    1023            0 :     case ERROR:
    1024            0 :       item.item = nullptr;
    1025            0 :       break;
    1026              :     }
    1027        43966 : }
    1028              : 
    1029         8537 : TypeCheckContextItem::TypeCheckContextItem ()
    1030         8537 :   : type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr))
    1031         8537 : {}
    1032              : 
    1033              : TypeCheckContextItem &
    1034         8501 : TypeCheckContextItem::operator= (const TypeCheckContextItem &other)
    1035              : {
    1036         8501 :   type = other.type;
    1037         8501 :   switch (other.type)
    1038              :     {
    1039          704 :     case ITEM:
    1040          704 :       item.item = other.item.item;
    1041          704 :       break;
    1042              : 
    1043         7683 :     case IMPL_ITEM:
    1044         7683 :       item.impl_item = other.item.impl_item;
    1045         7683 :       break;
    1046              : 
    1047          114 :     case TRAIT_ITEM:
    1048          114 :       item.trait_item = other.item.trait_item;
    1049          114 :       break;
    1050              : 
    1051            0 :     case ERROR:
    1052            0 :       item.item = nullptr;
    1053            0 :       break;
    1054              :     }
    1055              : 
    1056         8501 :   return *this;
    1057              : }
    1058              : 
    1059              : TypeCheckContextItem
    1060         8537 : TypeCheckContextItem::get_error ()
    1061              : {
    1062         8537 :   return TypeCheckContextItem ();
    1063              : }
    1064              : 
    1065              : bool
    1066            0 : TypeCheckContextItem::is_error () const
    1067              : {
    1068            0 :   return type == ERROR;
    1069              : }
    1070              : 
    1071              : HIR::Function *
    1072           63 : TypeCheckContextItem::get_item ()
    1073              : {
    1074           63 :   rust_assert (get_type () == ItemType::ITEM);
    1075           63 :   return item.item;
    1076              : }
    1077              : 
    1078              : std::pair<HIR::ImplBlock *, HIR::Function *> &
    1079         1306 : TypeCheckContextItem::get_impl_item ()
    1080              : {
    1081         1306 :   rust_assert (get_type () == ItemType::IMPL_ITEM);
    1082         1306 :   return item.impl_item;
    1083              : }
    1084              : 
    1085              : HIR::TraitItemFunc *
    1086            1 : TypeCheckContextItem::get_trait_item ()
    1087              : {
    1088            1 :   rust_assert (get_type () == ItemType::TRAIT_ITEM);
    1089            1 :   return item.trait_item;
    1090              : }
    1091              : 
    1092              : TypeCheckContextItem::ItemType
    1093       414714 : TypeCheckContextItem::get_type () const
    1094              : {
    1095       414714 :   return type;
    1096              : }
    1097              : 
    1098              : TyTy::FnType *
    1099           64 : TypeCheckContextItem::get_context_type ()
    1100              : {
    1101           64 :   auto &context = *TypeCheckContext::get ();
    1102              : 
    1103           64 :   HirId reference = UNKNOWN_HIRID;
    1104           64 :   switch (get_type ())
    1105              :     {
    1106           63 :     case ITEM:
    1107           63 :       reference = get_item ()->get_mappings ().get_hirid ();
    1108           63 :       break;
    1109              : 
    1110            0 :     case IMPL_ITEM:
    1111            0 :       reference = get_impl_item ().second->get_mappings ().get_hirid ();
    1112            0 :       break;
    1113              : 
    1114            1 :     case TRAIT_ITEM:
    1115            1 :       reference = get_trait_item ()->get_mappings ().get_hirid ();
    1116            1 :       break;
    1117              : 
    1118            0 :     case ERROR:
    1119            0 :       rust_unreachable ();
    1120              :       return nullptr;
    1121              :     }
    1122              : 
    1123           64 :   rust_assert (reference != UNKNOWN_HIRID);
    1124              : 
    1125           64 :   TyTy::BaseType *lookup = nullptr;
    1126           64 :   bool ok = context.lookup_type (reference, &lookup);
    1127           64 :   rust_assert (ok);
    1128           64 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
    1129           64 :   return static_cast<TyTy::FnType *> (lookup);
    1130              : }
    1131              : 
    1132              : DefId
    1133       394904 : TypeCheckContextItem::get_defid () const
    1134              : {
    1135       394904 :   switch (get_type ())
    1136              :     {
    1137          942 :     case ITEM:
    1138          942 :       return item.item->get_mappings ().get_defid ();
    1139              : 
    1140       393584 :     case IMPL_ITEM:
    1141       393584 :       return item.impl_item.second->get_mappings ().get_defid ();
    1142              : 
    1143          114 :     case TRAIT_ITEM:
    1144          114 :       return item.trait_item->get_mappings ().get_defid ();
    1145              : 
    1146          264 :     case ERROR:
    1147          264 :       return UNKNOWN_DEFID;
    1148              :     }
    1149              : 
    1150            0 :   return UNKNOWN_DEFID;
    1151              : }
    1152              : 
    1153              : // TypeCheckBlockContextItem
    1154              : 
    1155        38502 : TypeCheckBlockContextItem::Item::Item (HIR::ImplBlock *b) : block (b) {}
    1156              : 
    1157         5359 : TypeCheckBlockContextItem::Item::Item (HIR::Trait *t) : trait (t) {}
    1158              : 
    1159        38502 : TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::ImplBlock *block)
    1160        38502 :   : type (TypeCheckBlockContextItem::ItemType::IMPL_BLOCK), item (block)
    1161        38502 : {}
    1162              : 
    1163         5359 : TypeCheckBlockContextItem::TypeCheckBlockContextItem (HIR::Trait *trait)
    1164         5359 :   : type (TypeCheckBlockContextItem::ItemType::TRAIT), item (trait)
    1165         5359 : {}
    1166              : 
    1167              : bool
    1168            0 : TypeCheckBlockContextItem::is_impl_block () const
    1169              : {
    1170            0 :   return type == IMPL_BLOCK;
    1171              : }
    1172              : 
    1173              : bool
    1174         1929 : TypeCheckBlockContextItem::is_trait_block () const
    1175              : {
    1176         1929 :   return type == TRAIT;
    1177              : }
    1178              : 
    1179              : HIR::ImplBlock &
    1180          166 : TypeCheckBlockContextItem::get_impl_block ()
    1181              : {
    1182          166 :   return *(item.block);
    1183              : }
    1184              : 
    1185              : HIR::Trait &
    1186         1763 : TypeCheckBlockContextItem::get_trait ()
    1187              : {
    1188         1763 :   return *(item.trait);
    1189              : }
    1190              : 
    1191              : } // namespace Resolver
    1192              : } // namespace Rust
        

Generated by: LCOV version 2.5.0-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.