LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-hir-type-check.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.1 % 155 152
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 8 8
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-hir-full.h"
      21              : #include "rust-hir-inherent-impl-overlap.h"
      22              : #include "rust-hir-pattern.h"
      23              : #include "rust-hir-type-check-expr.h"
      24              : #include "rust-hir-type-check-item.h"
      25              : #include "rust-hir-type-check-pattern.h"
      26              : #include "rust-hir-type-check-struct-field.h"
      27              : #include "rust-finalized-name-resolution-context.h"
      28              : #include "rust-rib.h"
      29              : 
      30              : extern bool saw_errors (void);
      31              : 
      32              : namespace Rust {
      33              : namespace Resolver {
      34              : 
      35              : tl::optional<TyTy::Region>
      36        65156 : TypeCheckContext::LifetimeResolver::resolve (const Lifetime &placeholder) const
      37              : {
      38        65156 :   if (placeholder.is_static ())
      39          174 :     return TyTy::Region::make_static ();
      40              : 
      41        64982 :   if (placeholder == Lifetime::anonymous_lifetime ())
      42        51909 :     return TyTy::Region::make_anonymous ();
      43              : 
      44        13327 :   for (auto it = lifetime_lookup.rbegin (); it != lifetime_lookup.rend (); ++it)
      45              :     {
      46        13327 :       if (it->first == placeholder)
      47              :         {
      48        13073 :           if (it->second.scope <= ITEM_SCOPE)
      49              :             {
      50              :               // It is useful to have the static lifetime and named
      51              :               // lifetimed disjoint so we add the +1 here.
      52        13043 :               return (is_body)
      53        13043 :                        ? TyTy::Region::make_named (it->second.index + 1)
      54        13043 :                        : TyTy::Region::make_early_bound (it->second.index);
      55              :             }
      56              :           else
      57              :             {
      58           30 :               return TyTy::Region::make_late_bound (get_current_scope ()
      59           30 :                                                       - it->second.scope,
      60           30 :                                                     it->second.index);
      61              :             }
      62              :         }
      63              :     }
      64              : 
      65            0 :   return tl::nullopt;
      66              : }
      67              : 
      68              : void
      69         4977 : TypeResolution::Resolve (HIR::Crate &crate)
      70              : {
      71        26125 :   for (auto &it : crate.get_items ())
      72        21160 :     TypeCheckItem::Resolve (*it);
      73              : 
      74         4965 :   if (saw_errors ())
      75              :     return;
      76              : 
      77         4750 :   OverlappingImplItemPass::go ();
      78         4750 :   if (saw_errors ())
      79              :     return;
      80              : 
      81         4749 :   auto context = TypeCheckContext::get ();
      82         4749 :   context->compute_inference_variables (true);
      83              : }
      84              : 
      85              : // rust-hir-trait-ref.h
      86              : 
      87        14606 : TraitItemReference::TraitItemReference (
      88              :   std::string identifier, bool optional, TraitItemType type,
      89              :   HIR::TraitItem *hir_trait_item, TyTy::BaseType *self,
      90              :   std::vector<TyTy::SubstitutionParamMapping> substitutions, location_t locus)
      91        14606 :   : identifier (identifier), optional_flag (optional), type (type),
      92        14606 :     hir_trait_item (hir_trait_item),
      93        14606 :     inherited_substitutions (std::move (substitutions)), locus (locus),
      94        14606 :     self (self), context (TypeCheckContext::get ())
      95        14606 : {}
      96              : 
      97        34715 : TraitItemReference::TraitItemReference (TraitItemReference const &other)
      98        34715 :   : identifier (other.identifier), optional_flag (other.optional_flag),
      99        34715 :     type (other.type), hir_trait_item (other.hir_trait_item),
     100        34715 :     locus (other.locus), self (other.self), context (TypeCheckContext::get ())
     101              : {
     102        34715 :   inherited_substitutions.clear ();
     103        34715 :   inherited_substitutions.reserve (other.inherited_substitutions.size ());
     104       117033 :   for (size_t i = 0; i < other.inherited_substitutions.size (); i++)
     105        95206 :     inherited_substitutions.push_back (
     106        47603 :       other.inherited_substitutions.at (i).clone ());
     107        34715 : }
     108              : 
     109              : TraitItemReference &
     110         7296 : TraitItemReference::operator= (TraitItemReference const &other)
     111              : {
     112         7296 :   identifier = other.identifier;
     113         7296 :   optional_flag = other.optional_flag;
     114         7296 :   type = other.type;
     115         7296 :   hir_trait_item = other.hir_trait_item;
     116         7296 :   self = other.self;
     117         7296 :   locus = other.locus;
     118         7296 :   context = other.context;
     119              : 
     120         7296 :   inherited_substitutions.clear ();
     121         7296 :   inherited_substitutions.reserve (other.inherited_substitutions.size ());
     122        24656 :   for (size_t i = 0; i < other.inherited_substitutions.size (); i++)
     123        20128 :     inherited_substitutions.push_back (
     124        10064 :       other.inherited_substitutions.at (i).clone ());
     125              : 
     126         7296 :   return *this;
     127              : }
     128              : 
     129              : TyTy::BaseType *
     130        56782 : TraitItemReference::get_type_from_typealias (/*const*/
     131              :                                              HIR::TraitItemType &type) const
     132              : {
     133        56782 :   TyTy::TyVar var (get_mappings ().get_hirid ());
     134        56782 :   return var.get_tyty ();
     135              : }
     136              : 
     137              : TyTy::BaseType *
     138           32 : TraitItemReference::get_type_from_constant (
     139              :   /*const*/ HIR::TraitItemConst &constant) const
     140              : {
     141           32 :   TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
     142           32 :   if (constant.has_expr ())
     143              :     {
     144            7 :       TyTy::BaseType *expr = TypeCheckExpr::Resolve (constant.get_expr ());
     145              : 
     146           14 :       return unify_site (constant.get_mappings ().get_hirid (),
     147            7 :                          TyTy::TyWithLocation (type),
     148            7 :                          TyTy::TyWithLocation (expr), constant.get_locus ());
     149              :     }
     150              :   return type;
     151              : }
     152              : 
     153              : TyTy::BaseType *
     154         5839 : TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
     155              : {
     156         5839 :   auto binder_pin = context->push_clean_lifetime_resolver ();
     157              : 
     158         5839 :   auto &mappings = Analysis::Mappings::get ();
     159         5839 :   auto *trait
     160         5839 :     = mappings.lookup_trait_item_mapping (get_mappings ().get_hirid ());
     161         5839 :   rust_assert (trait != nullptr);
     162        14022 :   for (auto &param : trait->get_generic_params ())
     163         8183 :     if (param->get_kind () == HIR::GenericParam::GenericKind::LIFETIME)
     164              :       {
     165          153 :         auto &lifetime_param = static_cast<HIR::LifetimeParam &> (*param);
     166          153 :         context->intern_and_insert_lifetime (lifetime_param.get_lifetime ());
     167              :       }
     168              : 
     169         5839 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     170         5839 :     = inherited_substitutions;
     171              : 
     172         5839 :   HIR::TraitFunctionDecl &function = fn.get_decl ();
     173         5839 :   if (function.has_generics ())
     174              :     {
     175          755 :       TypeCheckBase::ResolveGenericParams (HIR::Item::ItemKind::Function,
     176              :                                            fn.get_locus (),
     177          755 :                                            function.get_generic_params (),
     178              :                                            substitutions, false /*is_foreign*/,
     179              :                                            ABI::RUST);
     180              :     }
     181              : 
     182         5839 :   TyTy::RegionConstraints region_constraints;
     183         5839 :   ResolveWhereClauseItem::Resolve (function.get_where_clause (),
     184              :                                    region_constraints);
     185              : 
     186         5839 :   TyTy::BaseType *ret_type = nullptr;
     187         5839 :   if (!function.has_return_type ())
     188          898 :     ret_type = TyTy::TupleType::get_unit_type ();
     189              :   else
     190              :     {
     191         4941 :       auto resolved = TypeCheckType::Resolve (function.get_return_type ());
     192         4941 :       if (resolved->get_kind () == TyTy::TypeKind::ERROR)
     193              :         {
     194            2 :           rust_error_at (fn.get_locus (), "failed to resolve return type");
     195            2 :           return get_error ();
     196              :         }
     197              : 
     198         4939 :       ret_type = resolved->clone ();
     199         4939 :       ret_type->set_ref (
     200         4939 :         function.get_return_type ().get_mappings ().get_hirid ());
     201              :     }
     202              : 
     203         5837 :   std::vector<TyTy::FnParam> params;
     204              : 
     205         5837 :   if (function.is_method ())
     206              :     {
     207              :       // these are implicit mappings and not used
     208         5136 :       auto &mappings = Analysis::Mappings::get ();
     209         5136 :       auto crate_num = mappings.crate.get_current_crate ();
     210         5136 :       Analysis::NodeMapping mapping (crate_num, mappings.get_next_node_id (),
     211              :                                      mappings.get_next_hir_id (crate_num),
     212         5136 :                                      UNKNOWN_LOCAL_DEFID);
     213              : 
     214              :       // add the synthetic self param at the front, this is a placeholder
     215              :       // for compilation to know parameter names. The types are ignored
     216              :       // but we reuse the HIR identifier pattern which requires it
     217         5136 :       HIR::SelfParam &self_param = function.get_self_unchecked ();
     218         5136 :       std::unique_ptr<HIR::Pattern> self_pattern
     219         5136 :         = std::make_unique<HIR::IdentifierPattern> (HIR::IdentifierPattern (
     220         5136 :           mapping, {"self"}, self_param.get_locus (), self_param.is_ref (),
     221         5136 :           self_param.is_mut () ? Mutability::Mut : Mutability::Imm,
     222        24421 :           std::unique_ptr<HIR::Pattern> (nullptr)));
     223              :       // might have a specified type
     224         5136 :       TyTy::BaseType *self_type = nullptr;
     225         5136 :       if (self_param.has_type ())
     226              :         {
     227           23 :           HIR::Type &specified_type = self_param.get_type ();
     228           23 :           self_type = TypeCheckType::Resolve (specified_type);
     229              :         }
     230              :       else
     231              :         {
     232         5113 :           switch (self_param.get_self_kind ())
     233              :             {
     234         1737 :             case HIR::SelfParam::IMM:
     235         1737 :             case HIR::SelfParam::MUT:
     236         1737 :               self_type = self->clone ();
     237         1737 :               break;
     238              : 
     239         3376 :             case HIR::SelfParam::IMM_REF:
     240         3376 :             case HIR::SelfParam::MUT_REF:
     241         3376 :               {
     242         3376 :                 auto mutability
     243         3376 :                   = self_param.get_self_kind () == HIR::SelfParam::IMM_REF
     244         3376 :                       ? Mutability::Imm
     245         3376 :                       : Mutability::Mut;
     246         3376 :                 rust_assert (self_param.has_lifetime ());
     247              : 
     248         3376 :                 auto region = TyTy::Region::make_anonymous ();
     249         3376 :                 auto maybe_region = context->lookup_and_resolve_lifetime (
     250         3376 :                   self_param.get_lifetime ());
     251         3376 :                 if (maybe_region.has_value ())
     252         3375 :                   region = maybe_region.value ();
     253              :                 else
     254              :                   {
     255            1 :                     rust_error_at (self_param.get_locus (),
     256              :                                    "failed to resolve lifetime");
     257              :                   }
     258              : 
     259         6752 :                 self_type = new TyTy::ReferenceType (
     260         3376 :                   self_param.get_mappings ().get_hirid (),
     261         3376 :                   TyTy::TyVar (self->get_ref ()), mutability, region);
     262              :               }
     263         3376 :               break;
     264              : 
     265            0 :             default:
     266            0 :               rust_unreachable ();
     267              :               return nullptr;
     268              :             }
     269              :         }
     270              : 
     271         5136 :       context->insert_type (self_param.get_mappings (), self_type);
     272         5136 :       params.emplace_back (std::move (self_pattern), self_type);
     273         5136 :     }
     274              : 
     275        10028 :   for (auto &param : function.get_function_params ())
     276              :     {
     277              :       // get the name as well required for later on
     278         4191 :       auto param_tyty = TypeCheckType::Resolve (param.get_type ());
     279         4191 :       context->insert_type (param.get_mappings (), param_tyty);
     280         4191 :       TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
     281              :       // FIXME: Should we take the name ? Use a shared pointer instead ?
     282         4191 :       params.emplace_back (param.get_param_name ().clone_pattern (),
     283              :                            param_tyty);
     284              :     }
     285              : 
     286         5837 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     287              : 
     288         5837 :   CanonicalPath canonical_path
     289         5837 :     = nr_ctx.to_canonical_path (fn.get_mappings ().get_nodeid (),
     290         5837 :                                 Resolver2_0::Namespace::Types);
     291              : 
     292        11674 :   RustIdent ident{canonical_path, fn.get_locus ()};
     293         5837 :   auto resolved = new TyTy::FnType (
     294        11674 :     fn.get_mappings ().get_hirid (), fn.get_mappings ().get_defid (),
     295         5837 :     function.get_function_name ().as_string (), ident,
     296         5837 :     function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG
     297              :                           : TyTy::FnType::FNTYPE_DEFAULT_FLAGS,
     298              :     ABI::RUST, std::move (params), ret_type, substitutions,
     299        11674 :     TyTy::SubstitutionArgumentMappings::empty (
     300         5837 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     301        29185 :     region_constraints);
     302         5837 :   context->insert_type (fn.get_mappings (), resolved);
     303         5837 :   return resolved;
     304        11676 : }
     305              : 
     306              : } // namespace Resolver
     307              : } // namespace Rust
        

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