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.8 % 386 339
Test Date: 2024-03-23 14:05:01 Functions: 93.6 % 78 73
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2024 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                 :             : 
      22                 :             : namespace Rust {
      23                 :             : namespace Resolver {
      24                 :             : 
      25                 :             : TypeCheckContext *
      26                 :     3776152 : TypeCheckContext::get ()
      27                 :             : {
      28                 :     3776152 :   static TypeCheckContext *instance;
      29                 :     3776152 :   if (instance == nullptr)
      30                 :        3600 :     instance = new TypeCheckContext ();
      31                 :             : 
      32                 :     3776152 :   return instance;
      33                 :             : }
      34                 :             : 
      35                 :        3600 : TypeCheckContext::TypeCheckContext () { lifetime_resolver_stack.emplace (); }
      36                 :             : 
      37                 :           0 : TypeCheckContext::~TypeCheckContext () {}
      38                 :             : 
      39                 :             : bool
      40                 :         120 : TypeCheckContext::lookup_builtin (NodeId id, TyTy::BaseType **type)
      41                 :             : {
      42                 :         120 :   auto ref_it = node_id_refs.find (id);
      43                 :         120 :   if (ref_it == node_id_refs.end ())
      44                 :             :     return false;
      45                 :             : 
      46                 :         120 :   auto it = resolved.find (ref_it->second);
      47                 :         120 :   if (it == resolved.end ())
      48                 :             :     return false;
      49                 :             : 
      50                 :         120 :   *type = it->second;
      51                 :         120 :   return true;
      52                 :             : }
      53                 :             : 
      54                 :             : bool
      55                 :       49923 : TypeCheckContext::lookup_builtin (std::string name, TyTy::BaseType **type)
      56                 :             : {
      57                 :      494336 :   for (auto &builtin : builtins)
      58                 :             :     {
      59                 :      494336 :       if (name.compare (builtin->as_string ()) == 0)
      60                 :             :         {
      61                 :       49923 :           *type = builtin.get ();
      62                 :       49923 :           return true;
      63                 :             :         }
      64                 :             :     }
      65                 :             :   return false;
      66                 :             : }
      67                 :             : 
      68                 :             : void
      69                 :       68400 : TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::BaseType *type)
      70                 :             : {
      71                 :       68400 :   node_id_refs[ref] = id;
      72                 :       68400 :   resolved[id] = type;
      73                 :       68400 :   builtins.push_back (std::unique_ptr<TyTy::BaseType> (type));
      74                 :       68400 : }
      75                 :             : 
      76                 :             : void
      77                 :      379340 : TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings,
      78                 :             :                                TyTy::BaseType *type)
      79                 :             : {
      80                 :      379340 :   rust_assert (type != nullptr);
      81                 :      379340 :   NodeId ref = mappings.get_nodeid ();
      82                 :      379340 :   HirId id = mappings.get_hirid ();
      83                 :      379340 :   node_id_refs[ref] = id;
      84                 :      379340 :   resolved[id] = type;
      85                 :      379340 : }
      86                 :             : 
      87                 :             : void
      88                 :         100 : TypeCheckContext::insert_implicit_type (TyTy::BaseType *type)
      89                 :             : {
      90                 :         100 :   rust_assert (type != nullptr);
      91                 :         100 :   resolved[type->get_ref ()] = type;
      92                 :         100 : }
      93                 :             : 
      94                 :             : void
      95                 :       27961 : TypeCheckContext::insert_implicit_type (HirId id, TyTy::BaseType *type)
      96                 :             : {
      97                 :       27961 :   rust_assert (type != nullptr);
      98                 :       27961 :   resolved[id] = type;
      99                 :       27961 : }
     100                 :             : 
     101                 :             : bool
     102                 :     8195947 : TypeCheckContext::lookup_type (HirId id, TyTy::BaseType **type) const
     103                 :             : {
     104                 :     8195947 :   auto it = resolved.find (id);
     105                 :     8195947 :   if (it == resolved.end ())
     106                 :             :     return false;
     107                 :             : 
     108                 :     8109597 :   *type = it->second;
     109                 :     8109597 :   return true;
     110                 :             : }
     111                 :             : 
     112                 :             : void
     113                 :        1270 : TypeCheckContext::clear_type (TyTy::BaseType *ty)
     114                 :             : {
     115                 :        1270 :   auto it = resolved.find (ty->get_ref ());
     116                 :        1270 :   if (it == resolved.end ())
     117                 :        1270 :     return;
     118                 :             : 
     119                 :        1270 :   resolved.erase (it);
     120                 :             : }
     121                 :             : 
     122                 :             : void
     123                 :           0 : TypeCheckContext::insert_type_by_node_id (NodeId ref, HirId id)
     124                 :             : {
     125                 :           0 :   rust_assert (node_id_refs.find (ref) == node_id_refs.end ());
     126                 :           0 :   node_id_refs[ref] = id;
     127                 :           0 : }
     128                 :             : 
     129                 :             : bool
     130                 :       62947 : TypeCheckContext::lookup_type_by_node_id (NodeId ref, HirId *id)
     131                 :             : {
     132                 :       62947 :   auto it = node_id_refs.find (ref);
     133                 :       62947 :   if (it == node_id_refs.end ())
     134                 :             :     return false;
     135                 :             : 
     136                 :       62947 :   *id = it->second;
     137                 :       62947 :   return true;
     138                 :             : }
     139                 :             : 
     140                 :             : bool
     141                 :         988 : TypeCheckContext::have_function_context () const
     142                 :             : {
     143                 :         988 :   return !return_type_stack.empty ();
     144                 :             : }
     145                 :             : 
     146                 :             : TyTy::BaseType *
     147                 :         325 : TypeCheckContext::peek_return_type ()
     148                 :             : {
     149                 :         325 :   rust_assert (!return_type_stack.empty ());
     150                 :         325 :   return return_type_stack.back ().second;
     151                 :             : }
     152                 :             : 
     153                 :             : void
     154                 :        8505 : TypeCheckContext::push_return_type (TypeCheckContextItem item,
     155                 :             :                                     TyTy::BaseType *return_type)
     156                 :             : {
     157                 :        8505 :   return_type_stack.push_back ({std::move (item), return_type});
     158                 :        8505 : }
     159                 :             : 
     160                 :             : void
     161                 :        8504 : TypeCheckContext::pop_return_type ()
     162                 :             : {
     163                 :        8504 :   rust_assert (!return_type_stack.empty ());
     164                 :        8504 :   return_type_stack.pop_back ();
     165                 :        8504 : }
     166                 :             : 
     167                 :             : TypeCheckContextItem
     168                 :         715 : TypeCheckContext::peek_context ()
     169                 :             : {
     170                 :         715 :   rust_assert (!return_type_stack.empty ());
     171                 :         715 :   return return_type_stack.back ().first;
     172                 :             : }
     173                 :             : 
     174                 :             : void
     175                 :        3361 : TypeCheckContext::iterate (std::function<bool (HirId, TyTy::BaseType *)> cb)
     176                 :             : {
     177                 :      296834 :   for (auto it = resolved.begin (); it != resolved.end (); it++)
     178                 :             :     {
     179                 :      293473 :       if (!cb (it->first, it->second))
     180                 :        3361 :         return;
     181                 :             :     }
     182                 :             : }
     183                 :             : 
     184                 :             : bool
     185                 :          66 : TypeCheckContext::have_loop_context () const
     186                 :             : {
     187                 :          66 :   return !loop_type_stack.empty ();
     188                 :             : }
     189                 :             : 
     190                 :             : void
     191                 :          88 : TypeCheckContext::push_new_loop_context (HirId id, location_t locus)
     192                 :             : {
     193                 :          88 :   TyTy::BaseType *infer_var
     194                 :             :     = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL,
     195                 :          88 :                            TyTy::InferType::TypeHint::Default (), locus);
     196                 :          88 :   loop_type_stack.push_back (infer_var);
     197                 :          88 : }
     198                 :             : 
     199                 :             : void
     200                 :          35 : TypeCheckContext::push_new_while_loop_context (HirId id)
     201                 :             : {
     202                 :          35 :   TyTy::BaseType *infer_var = new TyTy::ErrorType (id);
     203                 :          35 :   loop_type_stack.push_back (infer_var);
     204                 :          35 : }
     205                 :             : 
     206                 :             : TyTy::BaseType *
     207                 :         140 : TypeCheckContext::peek_loop_context ()
     208                 :             : {
     209                 :         140 :   return loop_type_stack.back ();
     210                 :             : }
     211                 :             : 
     212                 :             : TyTy::BaseType *
     213                 :         123 : TypeCheckContext::pop_loop_context ()
     214                 :             : {
     215                 :         123 :   auto back = peek_loop_context ();
     216                 :         123 :   loop_type_stack.pop_back ();
     217                 :         123 :   return back;
     218                 :             : }
     219                 :             : 
     220                 :             : void
     221                 :          15 : TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val)
     222                 :             : {
     223                 :          15 :   loop_type_stack.pop_back ();
     224                 :          15 :   loop_type_stack.push_back (val);
     225                 :          15 : }
     226                 :             : 
     227                 :             : void
     228                 :        2072 : TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref)
     229                 :             : {
     230                 :        2072 :   rust_assert (trait_context.find (id) == trait_context.end ());
     231                 :        2072 :   trait_context.emplace (id, std::move (ref));
     232                 :        2072 : }
     233                 :             : 
     234                 :             : bool
     235                 :      182603 : TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref)
     236                 :             : {
     237                 :      182603 :   auto it = trait_context.find (id);
     238                 :      182603 :   if (it == trait_context.end ())
     239                 :             :     return false;
     240                 :             : 
     241                 :      168891 :   *ref = &it->second;
     242                 :      168891 :   return true;
     243                 :             : }
     244                 :             : 
     245                 :             : void
     246                 :        3368 : TypeCheckContext::insert_receiver (HirId id, TyTy::BaseType *t)
     247                 :             : {
     248                 :        3368 :   receiver_context[id] = t;
     249                 :        3368 : }
     250                 :             : 
     251                 :             : bool
     252                 :        1199 : TypeCheckContext::lookup_receiver (HirId id, TyTy::BaseType **ref)
     253                 :             : {
     254                 :        1199 :   auto it = receiver_context.find (id);
     255                 :        1199 :   if (it == receiver_context.end ())
     256                 :             :     return false;
     257                 :             : 
     258                 :        1199 :   *ref = it->second;
     259                 :        1199 :   return true;
     260                 :             : }
     261                 :             : 
     262                 :             : void
     263                 :        2317 : TypeCheckContext::insert_associated_trait_impl (
     264                 :             :   HirId id, AssociatedImplTrait &&associated)
     265                 :             : {
     266                 :        2317 :   rust_assert (associated_impl_traits.find (id)
     267                 :             :                == associated_impl_traits.end ());
     268                 :        2317 :   associated_impl_traits.emplace (id, std::move (associated));
     269                 :        2317 : }
     270                 :             : 
     271                 :             : bool
     272                 :       19193 : TypeCheckContext::lookup_associated_trait_impl (
     273                 :             :   HirId id, AssociatedImplTrait **associated)
     274                 :             : {
     275                 :       19193 :   auto it = associated_impl_traits.find (id);
     276                 :       19193 :   if (it == associated_impl_traits.end ())
     277                 :             :     return false;
     278                 :             : 
     279                 :        8711 :   *associated = &it->second;
     280                 :        8711 :   return true;
     281                 :             : }
     282                 :             : 
     283                 :             : void
     284                 :        1798 : TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping)
     285                 :             : {
     286                 :        1798 :   associated_type_mappings[id] = mapping;
     287                 :        1798 : }
     288                 :             : 
     289                 :             : void
     290                 :         725 : TypeCheckContext::clear_associated_type_mapping (HirId id)
     291                 :             : {
     292                 :         725 :   auto it = associated_type_mappings.find (id);
     293                 :         725 :   if (it != associated_type_mappings.end ())
     294                 :         724 :     associated_type_mappings.erase (it);
     295                 :         725 : }
     296                 :             : 
     297                 :             : // lookup any associated type mappings, the out parameter of mapping is
     298                 :             : // allowed to be nullptr which allows this interface to do a simple does exist
     299                 :             : // check
     300                 :             : bool
     301                 :       26920 : TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping)
     302                 :             : {
     303                 :       26920 :   auto it = associated_type_mappings.find (id);
     304                 :       26920 :   if (it == associated_type_mappings.end ())
     305                 :             :     return false;
     306                 :             : 
     307                 :       14666 :   if (mapping != nullptr)
     308                 :        7333 :     *mapping = it->second;
     309                 :             : 
     310                 :             :   return true;
     311                 :             : }
     312                 :             : 
     313                 :             : void
     314                 :        2317 : TypeCheckContext::insert_associated_impl_mapping (
     315                 :             :   HirId trait_id, const TyTy::BaseType *impl_type, HirId impl_id)
     316                 :             : {
     317                 :        2317 :   auto it = associated_traits_to_impls.find (trait_id);
     318                 :        2317 :   if (it == associated_traits_to_impls.end ())
     319                 :             :     {
     320                 :         821 :       associated_traits_to_impls[trait_id] = {};
     321                 :             :     }
     322                 :             : 
     323                 :        2317 :   associated_traits_to_impls[trait_id].push_back ({impl_type, impl_id});
     324                 :        2317 : }
     325                 :             : 
     326                 :             : bool
     327                 :           0 : TypeCheckContext::lookup_associated_impl_mapping_for_self (
     328                 :             :   HirId trait_id, const TyTy::BaseType *self, HirId *mapping)
     329                 :             : {
     330                 :           0 :   auto it = associated_traits_to_impls.find (trait_id);
     331                 :           0 :   if (it == associated_traits_to_impls.end ())
     332                 :             :     return false;
     333                 :             : 
     334                 :           0 :   for (auto &item : it->second)
     335                 :             :     {
     336                 :           0 :       if (item.first->can_eq (self, false))
     337                 :             :         {
     338                 :           0 :           *mapping = item.second;
     339                 :           0 :           return true;
     340                 :             :         }
     341                 :             :     }
     342                 :             :   return false;
     343                 :             : }
     344                 :             : 
     345                 :             : void
     346                 :       22776 : TypeCheckContext::insert_autoderef_mappings (
     347                 :             :   HirId id, std::vector<Adjustment> &&adjustments)
     348                 :             : {
     349                 :       22776 :   autoderef_mappings.emplace (id, std::move (adjustments));
     350                 :       22776 : }
     351                 :             : 
     352                 :             : bool
     353                 :       25016 : TypeCheckContext::lookup_autoderef_mappings (
     354                 :             :   HirId id, std::vector<Adjustment> **adjustments)
     355                 :             : {
     356                 :       25016 :   auto it = autoderef_mappings.find (id);
     357                 :       25016 :   if (it == autoderef_mappings.end ())
     358                 :             :     return false;
     359                 :             : 
     360                 :       17798 :   *adjustments = &it->second;
     361                 :       17798 :   return true;
     362                 :             : }
     363                 :             : 
     364                 :             : void
     365                 :        3297 : TypeCheckContext::insert_cast_autoderef_mappings (
     366                 :             :   HirId id, std::vector<Adjustment> &&adjustments)
     367                 :             : {
     368                 :        3297 :   cast_autoderef_mappings.emplace (id, std::move (adjustments));
     369                 :        3297 : }
     370                 :             : 
     371                 :             : bool
     372                 :        3001 : TypeCheckContext::lookup_cast_autoderef_mappings (
     373                 :             :   HirId id, std::vector<Adjustment> **adjustments)
     374                 :             : {
     375                 :        3001 :   auto it = cast_autoderef_mappings.find (id);
     376                 :        3001 :   if (it == cast_autoderef_mappings.end ())
     377                 :             :     return false;
     378                 :             : 
     379                 :        3001 :   *adjustments = &it->second;
     380                 :        3001 :   return true;
     381                 :             : }
     382                 :             : 
     383                 :             : void
     384                 :         655 : TypeCheckContext::insert_variant_definition (HirId id, HirId variant)
     385                 :             : {
     386                 :         655 :   auto it = variants.find (id);
     387                 :         655 :   rust_assert (it == variants.end ());
     388                 :             : 
     389                 :         655 :   variants[id] = variant;
     390                 :         655 : }
     391                 :             : 
     392                 :             : bool
     393                 :        1010 : TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant)
     394                 :             : {
     395                 :        1010 :   auto it = variants.find (id);
     396                 :        1010 :   if (it == variants.end ())
     397                 :             :     return false;
     398                 :             : 
     399                 :        1010 :   *variant = it->second;
     400                 :        1010 :   return true;
     401                 :             : }
     402                 :             : 
     403                 :             : void
     404                 :         271 : TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site)
     405                 :             : {
     406                 :         271 :   auto it = operator_overloads.find (id);
     407                 :         271 :   rust_assert (it == operator_overloads.end ());
     408                 :             : 
     409                 :         271 :   operator_overloads[id] = call_site;
     410                 :         271 : }
     411                 :             : 
     412                 :             : bool
     413                 :       11124 : TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call)
     414                 :             : {
     415                 :       11124 :   auto it = operator_overloads.find (id);
     416                 :       11124 :   if (it == operator_overloads.end ())
     417                 :             :     return false;
     418                 :             : 
     419                 :         481 :   *call = it->second;
     420                 :         481 :   return true;
     421                 :             : }
     422                 :             : 
     423                 :             : void
     424                 :        3051 : TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status)
     425                 :             : {
     426                 :        3051 :   unconstrained[id] = status;
     427                 :        3051 : }
     428                 :             : 
     429                 :             : bool
     430                 :       11974 : TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result)
     431                 :             : {
     432                 :       11974 :   auto it = unconstrained.find (id);
     433                 :       11974 :   bool found = it != unconstrained.end ();
     434                 :       11974 :   if (!found)
     435                 :             :     return false;
     436                 :             : 
     437                 :        8923 :   *result = it->second;
     438                 :        8923 :   return true;
     439                 :             : }
     440                 :             : 
     441                 :             : void
     442                 :        3114 : TypeCheckContext::insert_resolved_predicate (HirId id,
     443                 :             :                                              TyTy::TypeBoundPredicate predicate)
     444                 :             : {
     445                 :             :   // auto it = predicates.find (id);
     446                 :             :   // rust_assert (it == predicates.end ());
     447                 :             : 
     448                 :        3114 :   predicates.insert ({id, predicate});
     449                 :        3114 : }
     450                 :             : 
     451                 :             : bool
     452                 :       12033 : TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result)
     453                 :             : {
     454                 :       12033 :   auto it = predicates.find (id);
     455                 :       12033 :   bool found = it != predicates.end ();
     456                 :       12033 :   if (!found)
     457                 :             :     return false;
     458                 :             : 
     459                 :        8911 :   *result = it->second;
     460                 :        8911 :   return true;
     461                 :             : }
     462                 :             : 
     463                 :             : void
     464                 :        3005 : TypeCheckContext::insert_query (HirId id)
     465                 :             : {
     466                 :        3005 :   querys_in_progress.insert (id);
     467                 :        3005 : }
     468                 :             : 
     469                 :             : void
     470                 :        3005 : TypeCheckContext::query_completed (HirId id)
     471                 :             : {
     472                 :        3005 :   querys_in_progress.erase (id);
     473                 :        3005 : }
     474                 :             : 
     475                 :             : bool
     476                 :      242456 : TypeCheckContext::query_in_progress (HirId id) const
     477                 :             : {
     478                 :      242456 :   return querys_in_progress.find (id) != querys_in_progress.end ();
     479                 :             : }
     480                 :             : 
     481                 :             : void
     482                 :        2081 : TypeCheckContext::insert_trait_query (DefId id)
     483                 :             : {
     484                 :        2081 :   trait_queries_in_progress.insert (id);
     485                 :        2081 : }
     486                 :             : 
     487                 :             : void
     488                 :        2080 : TypeCheckContext::trait_query_completed (DefId id)
     489                 :             : {
     490                 :        2080 :   trait_queries_in_progress.erase (id);
     491                 :        2080 : }
     492                 :             : 
     493                 :             : bool
     494                 :        2085 : TypeCheckContext::trait_query_in_progress (DefId id) const
     495                 :             : {
     496                 :        2085 :   return trait_queries_in_progress.find (id)
     497                 :        2085 :          != trait_queries_in_progress.end ();
     498                 :             : }
     499                 :             : 
     500                 :             : Lifetime
     501                 :         829 : TypeCheckContext::intern_lifetime (const HIR::Lifetime &lifetime)
     502                 :             : {
     503                 :         829 :   if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
     504                 :             :     {
     505                 :         829 :       auto maybe_interned = lookup_lifetime (lifetime);
     506                 :         829 :       if (maybe_interned)
     507                 :         775 :         return *maybe_interned;
     508                 :             : 
     509                 :          54 :       auto interned = next_lifetime_index.next ();
     510                 :          54 :       lifetime_name_interner[lifetime.get_name ()] = interned;
     511                 :          54 :       return interned;
     512                 :             :     }
     513                 :           0 :   if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
     514                 :             :     {
     515                 :           0 :       return next_lifetime_index.next ();
     516                 :             :     }
     517                 :           0 :   if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
     518                 :             :     {
     519                 :           0 :       return Lifetime::static_lifetime ();
     520                 :             :     }
     521                 :           0 :   rust_unreachable ();
     522                 :             : }
     523                 :             : 
     524                 :             : tl::optional<Lifetime>
     525                 :        7045 : TypeCheckContext::lookup_lifetime (const HIR::Lifetime &lifetime) const
     526                 :             : {
     527                 :        7045 :   if (lifetime.get_lifetime_type () == AST::Lifetime::NAMED)
     528                 :             :     {
     529                 :        1065 :       rust_assert (lifetime.get_name () != "static");
     530                 :        1065 :       const auto name = lifetime.get_name ();
     531                 :        1065 :       auto it = lifetime_name_interner.find (name);
     532                 :        1065 :       if (it == lifetime_name_interner.end ())
     533                 :          80 :         return tl::nullopt;
     534                 :         985 :       return it->second;
     535                 :        1065 :     }
     536                 :        5980 :   if (lifetime.get_lifetime_type () == AST::Lifetime::WILDCARD)
     537                 :             :     {
     538                 :        5969 :       return Lifetime::anonymous_lifetime ();
     539                 :             :     }
     540                 :          11 :   if (lifetime.get_lifetime_type () == AST::Lifetime::STATIC)
     541                 :             :     {
     542                 :          11 :       return Lifetime::static_lifetime ();
     543                 :             :     }
     544                 :           0 :   rust_unreachable ();
     545                 :             : }
     546                 :             : 
     547                 :             : WARN_UNUSED_RESULT tl::optional<TyTy::Region>
     548                 :        6216 : TypeCheckContext::lookup_and_resolve_lifetime (
     549                 :             :   const HIR::Lifetime &lifetime) const
     550                 :             : {
     551                 :        6216 :   auto maybe_interned = lookup_lifetime (lifetime);
     552                 :        6216 :   if (!maybe_interned)
     553                 :          26 :     return tl::nullopt;
     554                 :             : 
     555                 :        6190 :   return get_lifetime_resolver ().resolve (maybe_interned.value ());
     556                 :             : }
     557                 :             : void
     558                 :          14 : TypeCheckContext::intern_and_insert_lifetime (const HIR::Lifetime &lifetime)
     559                 :             : {
     560                 :          14 :   get_lifetime_resolver ().insert_mapping (intern_lifetime (lifetime));
     561                 :          14 : }
     562                 :             : 
     563                 :             : WARN_UNUSED_RESULT std::vector<TyTy::Region>
     564                 :        4550 : TypeCheckContext::regions_from_generic_args (const HIR::GenericArgs &args) const
     565                 :             : {
     566                 :        4550 :   std::vector<TyTy::Region> regions;
     567                 :        4560 :   for (const auto &lifetime : args.get_lifetime_args ())
     568                 :             :     {
     569                 :          10 :       auto resolved = lookup_and_resolve_lifetime (lifetime);
     570                 :          10 :       if (!resolved)
     571                 :             :         {
     572                 :           0 :           rust_error_at (lifetime.get_locus (), "unresolved lifetime");
     573                 :           0 :           return {};
     574                 :             :         }
     575                 :          10 :       regions.push_back (*resolved);
     576                 :             :     }
     577                 :        4550 :   return regions;
     578                 :        4550 : }
     579                 :             : 
     580                 :             : void
     581                 :        3361 : TypeCheckContext::compute_inference_variables (bool error)
     582                 :             : {
     583                 :        3361 :   auto mappings = Analysis::Mappings::get ();
     584                 :             : 
     585                 :             :   // default inference variables if possible
     586                 :        3361 :   iterate ([&] (HirId id, TyTy::BaseType *ty) mutable -> bool {
     587                 :             :     // nothing to do
     588                 :      293473 :     if (ty->get_kind () != TyTy::TypeKind::INFER)
     589                 :             :       return true;
     590                 :             : 
     591                 :       32831 :     TyTy::InferType *infer_var = static_cast<TyTy::InferType *> (ty);
     592                 :       32831 :     TyTy::BaseType *default_type;
     593                 :             : 
     594                 :       32831 :     rust_debug_loc (mappings->lookup_location (id),
     595                 :             :                     "trying to default infer-var: %s",
     596                 :       32831 :                     infer_var->as_string ().c_str ());
     597                 :       32831 :     bool ok = infer_var->default_type (&default_type);
     598                 :       32831 :     if (!ok)
     599                 :             :       {
     600                 :          12 :         if (error)
     601                 :          12 :           rust_error_at (mappings->lookup_location (id), ErrorCode::E0282,
     602                 :             :                          "type annotations needed");
     603                 :          12 :         return true;
     604                 :             :       }
     605                 :             : 
     606                 :       32819 :     auto result
     607                 :       32819 :       = unify_site (id, TyTy::TyWithLocation (ty),
     608                 :             :                     TyTy::TyWithLocation (default_type), UNDEF_LOCATION);
     609                 :       32819 :     rust_assert (result);
     610                 :       32819 :     rust_assert (result->get_kind () != TyTy::TypeKind::ERROR);
     611                 :       32819 :     result->set_ref (id);
     612                 :       32819 :     insert_type (Analysis::NodeMapping (mappings->get_current_crate (), 0, id,
     613                 :             :                                         UNKNOWN_LOCAL_DEFID),
     614                 :             :                  result);
     615                 :             : 
     616                 :       32819 :     return true;
     617                 :             :   });
     618                 :        3361 : }
     619                 :             : 
     620                 :             : // TypeCheckContextItem
     621                 :             : 
     622                 :        5654 : TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}
     623                 :             : 
     624                 :        3348 : TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block,
     625                 :             :                                   HIR::Function *item)
     626                 :        3348 :   : impl_item ({impl_block, item})
     627                 :        3348 : {}
     628                 :             : 
     629                 :         165 : TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item)
     630                 :         165 :   : trait_item (trait_item)
     631                 :         165 : {}
     632                 :             : 
     633                 :        4992 : TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item)
     634                 :        4992 :   : type (ItemType::ITEM), item (item)
     635                 :        4992 : {}
     636                 :             : 
     637                 :        3348 : TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock *impl_block,
     638                 :        3348 :                                             HIR::Function *item)
     639                 :        3348 :   : type (ItemType::IMPL_ITEM), item (impl_block, item)
     640                 :        3348 : {}
     641                 :             : 
     642                 :         165 : TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item)
     643                 :         165 :   : type (ItemType::TRAIT_ITEM), item (trait_item)
     644                 :         165 : {}
     645                 :             : 
     646                 :       18263 : TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other)
     647                 :       18263 :   : type (other.type), item (other.item)
     648                 :             : {
     649                 :       18263 :   switch (other.type)
     650                 :             :     {
     651                 :       10476 :     case ITEM:
     652                 :       10476 :       item.item = other.item.item;
     653                 :       10476 :       break;
     654                 :             : 
     655                 :        7395 :     case IMPL_ITEM:
     656                 :        7395 :       item.impl_item = other.item.impl_item;
     657                 :        7395 :       break;
     658                 :             : 
     659                 :         392 :     case TRAIT_ITEM:
     660                 :         392 :       item.trait_item = other.item.trait_item;
     661                 :         392 :       break;
     662                 :             : 
     663                 :           0 :     case ERROR:
     664                 :           0 :       item.item = nullptr;
     665                 :           0 :       break;
     666                 :             :     }
     667                 :       18263 : }
     668                 :             : 
     669                 :         662 : TypeCheckContextItem::TypeCheckContextItem ()
     670                 :         662 :   : type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr))
     671                 :         662 : {}
     672                 :             : 
     673                 :             : TypeCheckContextItem &
     674                 :         662 : TypeCheckContextItem::operator= (const TypeCheckContextItem &other)
     675                 :             : {
     676                 :         662 :   type = other.type;
     677                 :         662 :   switch (other.type)
     678                 :             :     {
     679                 :         303 :     case ITEM:
     680                 :         303 :       item.item = other.item.item;
     681                 :         303 :       break;
     682                 :             : 
     683                 :         359 :     case IMPL_ITEM:
     684                 :         359 :       item.impl_item = other.item.impl_item;
     685                 :         359 :       break;
     686                 :             : 
     687                 :           0 :     case TRAIT_ITEM:
     688                 :           0 :       item.trait_item = other.item.trait_item;
     689                 :           0 :       break;
     690                 :             : 
     691                 :           0 :     case ERROR:
     692                 :           0 :       item.item = nullptr;
     693                 :           0 :       break;
     694                 :             :     }
     695                 :             : 
     696                 :         662 :   return *this;
     697                 :             : }
     698                 :             : 
     699                 :             : TypeCheckContextItem
     700                 :         662 : TypeCheckContextItem::get_error ()
     701                 :             : {
     702                 :         662 :   return TypeCheckContextItem ();
     703                 :             : }
     704                 :             : 
     705                 :             : bool
     706                 :           0 : TypeCheckContextItem::is_error () const
     707                 :             : {
     708                 :           0 :   return type == ERROR;
     709                 :             : }
     710                 :             : 
     711                 :             : HIR::Function *
     712                 :          53 : TypeCheckContextItem::get_item ()
     713                 :             : {
     714                 :          53 :   rust_assert (get_type () == ItemType::ITEM);
     715                 :          53 :   return item.item;
     716                 :             : }
     717                 :             : 
     718                 :             : std::pair<HIR::ImplBlock *, HIR::Function *> &
     719                 :         273 : TypeCheckContextItem::get_impl_item ()
     720                 :             : {
     721                 :         273 :   rust_assert (get_type () == ItemType::IMPL_ITEM);
     722                 :         273 :   return item.impl_item;
     723                 :             : }
     724                 :             : 
     725                 :             : HIR::TraitItemFunc *
     726                 :           0 : TypeCheckContextItem::get_trait_item ()
     727                 :             : {
     728                 :           0 :   rust_assert (get_type () == ItemType::TRAIT_ITEM);
     729                 :           0 :   return item.trait_item;
     730                 :             : }
     731                 :             : 
     732                 :             : TypeCheckContextItem::ItemType
     733                 :        1607 : TypeCheckContextItem::get_type () const
     734                 :             : {
     735                 :        1607 :   return type;
     736                 :             : }
     737                 :             : 
     738                 :             : TyTy::FnType *
     739                 :          53 : TypeCheckContextItem::get_context_type ()
     740                 :             : {
     741                 :          53 :   auto &context = *TypeCheckContext::get ();
     742                 :             : 
     743                 :          53 :   HirId reference = UNKNOWN_HIRID;
     744                 :          53 :   switch (get_type ())
     745                 :             :     {
     746                 :          53 :     case ITEM:
     747                 :          53 :       reference = get_item ()->get_mappings ().get_hirid ();
     748                 :          53 :       break;
     749                 :             : 
     750                 :           0 :     case IMPL_ITEM:
     751                 :           0 :       reference = get_impl_item ().second->get_mappings ().get_hirid ();
     752                 :           0 :       break;
     753                 :             : 
     754                 :           0 :     case TRAIT_ITEM:
     755                 :           0 :       reference = get_trait_item ()->get_mappings ().get_hirid ();
     756                 :           0 :       break;
     757                 :             : 
     758                 :           0 :     case ERROR:
     759                 :           0 :       rust_unreachable ();
     760                 :             :       return nullptr;
     761                 :             :     }
     762                 :             : 
     763                 :          53 :   rust_assert (reference != UNKNOWN_HIRID);
     764                 :             : 
     765                 :          53 :   TyTy::BaseType *lookup = nullptr;
     766                 :          53 :   bool ok = context.lookup_type (reference, &lookup);
     767                 :          53 :   rust_assert (ok);
     768                 :          53 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
     769                 :          53 :   return static_cast<TyTy::FnType *> (lookup);
     770                 :             : }
     771                 :             : 
     772                 :             : DefId
     773                 :         674 : TypeCheckContextItem::get_defid () const
     774                 :             : {
     775                 :         674 :   switch (get_type ())
     776                 :             :     {
     777                 :         327 :     case ITEM:
     778                 :         327 :       return item.item->get_mappings ().get_defid ();
     779                 :             : 
     780                 :         347 :     case IMPL_ITEM:
     781                 :         347 :       return item.impl_item.second->get_mappings ().get_defid ();
     782                 :             : 
     783                 :           0 :     case TRAIT_ITEM:
     784                 :           0 :       return item.trait_item->get_mappings ().get_defid ();
     785                 :             : 
     786                 :           0 :     case ERROR:
     787                 :           0 :       return UNKNOWN_DEFID;
     788                 :             :     }
     789                 :             : 
     790                 :           0 :   return UNKNOWN_DEFID;
     791                 :             : }
     792                 :             : 
     793                 :             : } // namespace Resolver
     794                 :             : } // namespace Rust
        

Generated by: LCOV version 2.0-1

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.