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-11-14 16:39:50 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        45440 : TypeCheckContext::LifetimeResolver::resolve (const Lifetime &placeholder) const
      37              : {
      38        45440 :   if (placeholder.is_static ())
      39          158 :     return TyTy::Region::make_static ();
      40              : 
      41        45282 :   if (placeholder == Lifetime::anonymous_lifetime ())
      42        36849 :     return TyTy::Region::make_anonymous ();
      43              : 
      44         8611 :   for (auto it = lifetime_lookup.rbegin (); it != lifetime_lookup.rend (); ++it)
      45              :     {
      46         8611 :       if (it->first == placeholder)
      47              :         {
      48         8433 :           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         8403 :               return (is_body)
      53         8403 :                        ? TyTy::Region::make_named (it->second.index + 1)
      54         8403 :                        : 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         4973 : TypeResolution::Resolve (HIR::Crate &crate)
      70              : {
      71        26109 :   for (auto &it : crate.get_items ())
      72        21144 :     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        12054 : 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        12054 :   : identifier (identifier), optional_flag (optional), type (type),
      92        12054 :     hir_trait_item (hir_trait_item),
      93        12054 :     inherited_substitutions (std::move (substitutions)), locus (locus),
      94        12054 :     self (self), context (TypeCheckContext::get ())
      95        12054 : {}
      96              : 
      97        28375 : TraitItemReference::TraitItemReference (TraitItemReference const &other)
      98        28375 :   : identifier (other.identifier), optional_flag (other.optional_flag),
      99        28375 :     type (other.type), hir_trait_item (other.hir_trait_item),
     100        28375 :     locus (other.locus), self (other.self), context (TypeCheckContext::get ())
     101              : {
     102        28375 :   inherited_substitutions.clear ();
     103        28375 :   inherited_substitutions.reserve (other.inherited_substitutions.size ());
     104        95961 :   for (size_t i = 0; i < other.inherited_substitutions.size (); i++)
     105        78422 :     inherited_substitutions.push_back (
     106        39211 :       other.inherited_substitutions.at (i).clone ());
     107        28375 : }
     108              : 
     109              : TraitItemReference &
     110         6020 : TraitItemReference::operator= (TraitItemReference const &other)
     111              : {
     112         6020 :   identifier = other.identifier;
     113         6020 :   optional_flag = other.optional_flag;
     114         6020 :   type = other.type;
     115         6020 :   hir_trait_item = other.hir_trait_item;
     116         6020 :   self = other.self;
     117         6020 :   locus = other.locus;
     118         6020 :   context = other.context;
     119              : 
     120         6020 :   inherited_substitutions.clear ();
     121         6020 :   inherited_substitutions.reserve (other.inherited_substitutions.size ());
     122        20384 :   for (size_t i = 0; i < other.inherited_substitutions.size (); i++)
     123        16688 :     inherited_substitutions.push_back (
     124         8344 :       other.inherited_substitutions.at (i).clone ());
     125              : 
     126         6020 :   return *this;
     127              : }
     128              : 
     129              : TyTy::BaseType *
     130        39090 : TraitItemReference::get_type_from_typealias (/*const*/
     131              :                                              HIR::TraitItemType &type) const
     132              : {
     133        39090 :   TyTy::TyVar var (get_mappings ().get_hirid ());
     134        39090 :   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         4771 : TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const
     155              : {
     156         4771 :   auto binder_pin = context->push_clean_lifetime_resolver ();
     157              : 
     158         4771 :   auto &mappings = Analysis::Mappings::get ();
     159         4771 :   auto *trait
     160         4771 :     = mappings.lookup_trait_item_mapping (get_mappings ().get_hirid ());
     161         4771 :   rust_assert (trait != nullptr);
     162        11470 :   for (auto &param : trait->get_generic_params ())
     163         6699 :     if (param->get_kind () == HIR::GenericParam::GenericKind::LIFETIME)
     164              :       {
     165          101 :         auto &lifetime_param = static_cast<HIR::LifetimeParam &> (*param);
     166          101 :         context->intern_and_insert_lifetime (lifetime_param.get_lifetime ());
     167              :       }
     168              : 
     169         4771 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     170         4771 :     = inherited_substitutions;
     171              : 
     172         4771 :   HIR::TraitFunctionDecl &function = fn.get_decl ();
     173         4771 :   if (function.has_generics ())
     174              :     {
     175          491 :       TypeCheckBase::ResolveGenericParams (HIR::Item::ItemKind::Function,
     176              :                                            fn.get_locus (),
     177          491 :                                            function.get_generic_params (),
     178              :                                            substitutions, false /*is_foreign*/,
     179              :                                            ABI::RUST);
     180              :     }
     181              : 
     182         4771 :   TyTy::RegionConstraints region_constraints;
     183         4771 :   ResolveWhereClauseItem::Resolve (function.get_where_clause (),
     184              :                                    region_constraints);
     185              : 
     186         4771 :   TyTy::BaseType *ret_type = nullptr;
     187         4771 :   if (!function.has_return_type ())
     188          762 :     ret_type = TyTy::TupleType::get_unit_type ();
     189              :   else
     190              :     {
     191         4009 :       auto resolved = TypeCheckType::Resolve (function.get_return_type ());
     192         4009 :       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         4007 :       ret_type = resolved->clone ();
     199         4007 :       ret_type->set_ref (
     200         4007 :         function.get_return_type ().get_mappings ().get_hirid ());
     201              :     }
     202              : 
     203         4769 :   std::vector<TyTy::FnParam> params;
     204              : 
     205         4769 :   if (function.is_method ())
     206              :     {
     207              :       // these are implicit mappings and not used
     208         4200 :       auto &mappings = Analysis::Mappings::get ();
     209         4200 :       auto crate_num = mappings.crate.get_current_crate ();
     210         4200 :       Analysis::NodeMapping mapping (crate_num, mappings.get_next_node_id (),
     211              :                                      mappings.get_next_hir_id (crate_num),
     212         4200 :                                      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         4200 :       HIR::SelfParam &self_param = function.get_self_unchecked ();
     218         4200 :       std::unique_ptr<HIR::Pattern> self_pattern
     219         4200 :         = std::make_unique<HIR::IdentifierPattern> (HIR::IdentifierPattern (
     220         4200 :           mapping, {"self"}, self_param.get_locus (), self_param.is_ref (),
     221         4200 :           self_param.is_mut () ? Mutability::Mut : Mutability::Imm,
     222        20089 :           std::unique_ptr<HIR::Pattern> (nullptr)));
     223              :       // might have a specified type
     224         4200 :       TyTy::BaseType *self_type = nullptr;
     225         4200 :       if (self_param.has_type ())
     226              :         {
     227           15 :           HIR::Type &specified_type = self_param.get_type ();
     228           15 :           self_type = TypeCheckType::Resolve (specified_type);
     229              :         }
     230              :       else
     231              :         {
     232         4185 :           switch (self_param.get_self_kind ())
     233              :             {
     234         1333 :             case HIR::SelfParam::IMM:
     235         1333 :             case HIR::SelfParam::MUT:
     236         1333 :               self_type = self->clone ();
     237         1333 :               break;
     238              : 
     239         2852 :             case HIR::SelfParam::IMM_REF:
     240         2852 :             case HIR::SelfParam::MUT_REF:
     241         2852 :               {
     242         2852 :                 auto mutability
     243         2852 :                   = self_param.get_self_kind () == HIR::SelfParam::IMM_REF
     244         2852 :                       ? Mutability::Imm
     245         2852 :                       : Mutability::Mut;
     246         2852 :                 rust_assert (self_param.has_lifetime ());
     247              : 
     248         2852 :                 auto region = TyTy::Region::make_anonymous ();
     249         2852 :                 auto maybe_region = context->lookup_and_resolve_lifetime (
     250         2852 :                   self_param.get_lifetime ());
     251         2852 :                 if (maybe_region.has_value ())
     252         2851 :                   region = maybe_region.value ();
     253              :                 else
     254              :                   {
     255            1 :                     rust_error_at (self_param.get_locus (),
     256              :                                    "failed to resolve lifetime");
     257              :                   }
     258              : 
     259         5704 :                 self_type = new TyTy::ReferenceType (
     260         2852 :                   self_param.get_mappings ().get_hirid (),
     261         2852 :                   TyTy::TyVar (self->get_ref ()), mutability, region);
     262              :               }
     263         2852 :               break;
     264              : 
     265            0 :             default:
     266            0 :               rust_unreachable ();
     267              :               return nullptr;
     268              :             }
     269              :         }
     270              : 
     271         4200 :       context->insert_type (self_param.get_mappings (), self_type);
     272         4200 :       params.emplace_back (std::move (self_pattern), self_type);
     273         4200 :     }
     274              : 
     275         8096 :   for (auto &param : function.get_function_params ())
     276              :     {
     277              :       // get the name as well required for later on
     278         3327 :       auto param_tyty = TypeCheckType::Resolve (param.get_type ());
     279         3327 :       context->insert_type (param.get_mappings (), param_tyty);
     280         3327 :       TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
     281              :       // FIXME: Should we take the name ? Use a shared pointer instead ?
     282         3327 :       params.emplace_back (param.get_param_name ().clone_pattern (),
     283              :                            param_tyty);
     284              :     }
     285              : 
     286         4769 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     287              : 
     288         4769 :   CanonicalPath canonical_path
     289         4769 :     = nr_ctx.to_canonical_path (fn.get_mappings ().get_nodeid (),
     290         4769 :                                 Resolver2_0::Namespace::Types);
     291              : 
     292         9538 :   RustIdent ident{canonical_path, fn.get_locus ()};
     293         4769 :   auto resolved = new TyTy::FnType (
     294         9538 :     fn.get_mappings ().get_hirid (), fn.get_mappings ().get_defid (),
     295         4769 :     function.get_function_name ().as_string (), ident,
     296         4769 :     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         9538 :     TyTy::SubstitutionArgumentMappings::empty (
     300         4769 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     301        23845 :     region_constraints);
     302         4769 :   context->insert_type (fn.get_mappings (), resolved);
     303         4769 :   return resolved;
     304         9540 : }
     305              : 
     306              : } // namespace Resolver
     307              : } // 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.