LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-hir-type-check-implitem.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 88.8 % 339 301
Test Date: 2025-08-30 13:27:53 Functions: 94.1 % 17 16
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #include "rust-hir-type-check-implitem.h"
      20                 :             : #include "rust-diagnostics.h"
      21                 :             : #include "rust-hir-full-decls.h"
      22                 :             : #include "rust-hir-pattern.h"
      23                 :             : #include "rust-hir-type-check-base.h"
      24                 :             : #include "rust-hir-type-check-type.h"
      25                 :             : #include "rust-hir-type-check-expr.h"
      26                 :             : #include "rust-hir-type-check-pattern.h"
      27                 :             : #include "rust-type-util.h"
      28                 :             : #include "rust-tyty.h"
      29                 :             : #include "rust-immutable-name-resolution-context.h"
      30                 :             : 
      31                 :             : namespace Rust {
      32                 :             : namespace Resolver {
      33                 :             : 
      34                 :        2169 : TypeCheckTopLevelExternItem::TypeCheckTopLevelExternItem (
      35                 :        2169 :   const HIR::ExternBlock &parent)
      36                 :        2169 :   : TypeCheckBase (), parent (parent)
      37                 :        2169 : {}
      38                 :             : 
      39                 :             : TyTy::BaseType *
      40                 :        2219 : TypeCheckTopLevelExternItem::Resolve (HIR::ExternalItem &item,
      41                 :             :                                       const HIR::ExternBlock &parent)
      42                 :             : {
      43                 :             :   // is it already resolved?
      44                 :        2219 :   auto context = TypeCheckContext::get ();
      45                 :        2219 :   TyTy::BaseType *resolved = nullptr;
      46                 :        2219 :   bool already_resolved
      47                 :        2219 :     = context->lookup_type (item.get_mappings ().get_hirid (), &resolved);
      48                 :        2219 :   if (already_resolved)
      49                 :          50 :     return resolved;
      50                 :             : 
      51                 :        2169 :   TypeCheckTopLevelExternItem resolver (parent);
      52                 :        2169 :   item.accept_vis (resolver);
      53                 :        2168 :   return resolver.resolved;
      54                 :        2168 : }
      55                 :             : 
      56                 :             : void
      57                 :           1 : TypeCheckTopLevelExternItem::visit (HIR::ExternalStaticItem &item)
      58                 :             : {
      59                 :           1 :   TyTy::BaseType *actual_type = TypeCheckType::Resolve (item.get_item_type ());
      60                 :             : 
      61                 :           1 :   context->insert_type (item.get_mappings (), actual_type);
      62                 :           1 :   resolved = actual_type;
      63                 :           1 : }
      64                 :             : 
      65                 :             : void
      66                 :        2168 : TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
      67                 :             : {
      68                 :        2168 :   auto binder_pin = context->push_clean_lifetime_resolver ();
      69                 :             : 
      70                 :        2168 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
      71                 :        2168 :   if (function.has_generics ())
      72                 :             :     {
      73                 :         808 :       resolve_generic_params (HIR::Item::ItemKind::Function,
      74                 :             :                               function.get_locus (),
      75                 :         808 :                               function.get_generic_params (), substitutions,
      76                 :         808 :                               true /*is_foreign*/, parent.get_abi ());
      77                 :             :     }
      78                 :             : 
      79                 :        2167 :   TyTy::RegionConstraints region_constraints;
      80                 :        2167 :   if (function.has_where_clause ())
      81                 :             :     {
      82                 :           0 :       for (auto &where_clause_item : function.get_where_clause ().get_items ())
      83                 :             :         {
      84                 :           0 :           ResolveWhereClauseItem::Resolve (*where_clause_item,
      85                 :             :                                            region_constraints);
      86                 :             :         }
      87                 :             :     }
      88                 :             : 
      89                 :        2167 :   TyTy::BaseType *ret_type = nullptr;
      90                 :        2167 :   if (!function.has_return_type ())
      91                 :        1149 :     ret_type = TyTy::TupleType::get_unit_type ();
      92                 :             :   else
      93                 :             :     {
      94                 :        1018 :       auto resolved = TypeCheckType::Resolve (function.get_return_type ());
      95                 :        1018 :       if (resolved == nullptr)
      96                 :             :         {
      97                 :           0 :           rust_error_at (function.get_locus (),
      98                 :             :                          "failed to resolve return type");
      99                 :           0 :           return;
     100                 :             :         }
     101                 :             : 
     102                 :        1018 :       ret_type = resolved->clone ();
     103                 :        1018 :       ret_type->set_ref (
     104                 :        1018 :         function.get_return_type ().get_mappings ().get_hirid ());
     105                 :             :     }
     106                 :             : 
     107                 :        2167 :   std::vector<TyTy::FnParam> params;
     108                 :        4816 :   for (auto &param : function.get_function_params ())
     109                 :             :     {
     110                 :             :       // get the name as well required for later on
     111                 :        2649 :       auto param_tyty = TypeCheckType::Resolve (param.get_type ());
     112                 :             : 
     113                 :             :       // these are implicit mappings and not used
     114                 :        2649 :       auto crate_num = mappings.get_current_crate ();
     115                 :        5298 :       Analysis::NodeMapping mapping (crate_num, mappings.get_next_node_id (),
     116                 :        2649 :                                      mappings.get_next_hir_id (crate_num),
     117                 :        2649 :                                      UNKNOWN_LOCAL_DEFID);
     118                 :             : 
     119                 :        2649 :       auto param_pattern = std::make_unique<HIR::IdentifierPattern> (
     120                 :        2649 :         HIR::IdentifierPattern (mapping, param.get_param_name (),
     121                 :             :                                 UNDEF_LOCATION, false, Mutability::Imm,
     122                 :        5298 :                                 std::unique_ptr<HIR::Pattern> (nullptr)));
     123                 :             : 
     124                 :        2649 :       params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty));
     125                 :             : 
     126                 :        2649 :       context->insert_type (param.get_mappings (), param_tyty);
     127                 :             : 
     128                 :             :       // FIXME do we need error checking for patterns here?
     129                 :             :       // see https://github.com/Rust-GCC/gccrs/issues/995
     130                 :        2649 :     }
     131                 :             : 
     132                 :        2167 :   uint8_t flags = TyTy::FnType::FNTYPE_IS_EXTERN_FLAG;
     133                 :        2167 :   if (function.is_variadic ())
     134                 :             :     {
     135                 :         819 :       flags |= TyTy::FnType::FNTYPE_IS_VARADIC_FLAG;
     136                 :         819 :       if (parent.get_abi () != Rust::ABI::C)
     137                 :             :         {
     138                 :           1 :           rust_error_at (
     139                 :             :             function.get_locus (), ErrorCode::E0045,
     140                 :             :             "C-variadic function must have C or cdecl calling convention");
     141                 :             :         }
     142                 :             :     }
     143                 :             : 
     144                 :        2167 :   RustIdent ident{
     145                 :        4334 :     CanonicalPath::new_seg (function.get_mappings ().get_nodeid (),
     146                 :        2167 :                             function.get_item_name ().as_string ()),
     147                 :        2167 :     function.get_locus ()};
     148                 :             : 
     149                 :        2167 :   auto fnType = new TyTy::FnType (
     150                 :        2167 :     function.get_mappings ().get_hirid (),
     151                 :        4334 :     function.get_mappings ().get_defid (),
     152                 :        2167 :     function.get_item_name ().as_string (), ident, flags, parent.get_abi (),
     153                 :             :     std::move (params), ret_type, std::move (substitutions),
     154                 :        4334 :     TyTy::SubstitutionArgumentMappings::empty (
     155                 :        2167 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     156                 :        8668 :     region_constraints);
     157                 :             : 
     158                 :        2167 :   context->insert_type (function.get_mappings (), fnType);
     159                 :        2167 :   resolved = fnType;
     160                 :        4334 : }
     161                 :             : 
     162                 :             : void
     163                 :           0 : TypeCheckTopLevelExternItem::visit (HIR::ExternalTypeItem &type)
     164                 :             : {
     165                 :           0 :   rust_sorry_at (type.get_locus (), "extern types are not supported yet");
     166                 :             :   // TODO
     167                 :           0 : }
     168                 :             : 
     169                 :        8020 : TypeCheckImplItem::TypeCheckImplItem (
     170                 :             :   HIR::ImplBlock &parent, TyTy::BaseType *self,
     171                 :        8020 :   std::vector<TyTy::SubstitutionParamMapping> substitutions)
     172                 :        8020 :   : TypeCheckBase (), parent (parent), self (self),
     173                 :        8020 :     substitutions (substitutions)
     174                 :        8020 : {}
     175                 :             : 
     176                 :             : TyTy::BaseType *
     177                 :       16380 : TypeCheckImplItem::Resolve (
     178                 :             :   HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self,
     179                 :             :   std::vector<TyTy::SubstitutionParamMapping> substitutions)
     180                 :             : {
     181                 :             :   // is it already resolved?
     182                 :       16380 :   auto context = TypeCheckContext::get ();
     183                 :       16380 :   TyTy::BaseType *resolved = nullptr;
     184                 :       16380 :   bool already_resolved
     185                 :       16380 :     = context->lookup_type (item.get_impl_mappings ().get_hirid (), &resolved);
     186                 :       16380 :   if (already_resolved)
     187                 :        8360 :     return resolved;
     188                 :             : 
     189                 :             :   // resolve
     190                 :        8020 :   TypeCheckImplItem resolver (parent, self, substitutions);
     191                 :        8020 :   resolver.context->block_context ().enter (
     192                 :        8020 :     TypeCheckBlockContextItem (&parent));
     193                 :        8020 :   item.accept_vis (resolver);
     194                 :        8020 :   resolver.context->block_context ().exit ();
     195                 :             : 
     196                 :        8020 :   return resolver.result;
     197                 :        8020 : }
     198                 :             : 
     199                 :             : void
     200                 :        6814 : TypeCheckImplItem::visit (HIR::Function &function)
     201                 :             : {
     202                 :        6814 :   auto binder_pin = context->push_lifetime_binder ();
     203                 :             : 
     204                 :        6814 :   if (function.has_generics ())
     205                 :         102 :     resolve_generic_params (HIR::Item::ItemKind::Function,
     206                 :             :                             function.get_locus (),
     207                 :         102 :                             function.get_generic_params (), substitutions);
     208                 :             : 
     209                 :        6814 :   TyTy::RegionConstraints region_constraints;
     210                 :        6815 :   for (auto &where_clause_item : function.get_where_clause ().get_items ())
     211                 :             :     {
     212                 :           1 :       ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
     213                 :             :                                        region_constraints);
     214                 :             :     }
     215                 :             : 
     216                 :        6814 :   TyTy::BaseType *ret_type = nullptr;
     217                 :        6814 :   if (!function.has_function_return_type ())
     218                 :         407 :     ret_type = TyTy::TupleType::get_unit_type ();
     219                 :             :   else
     220                 :             :     {
     221                 :        6407 :       auto resolved = TypeCheckType::Resolve (function.get_return_type ());
     222                 :        6407 :       if (resolved == nullptr)
     223                 :             :         {
     224                 :           0 :           rust_error_at (function.get_locus (),
     225                 :             :                          "failed to resolve return type");
     226                 :           0 :           return;
     227                 :             :         }
     228                 :             : 
     229                 :        6407 :       ret_type = resolved->clone ();
     230                 :        6407 :       ret_type->set_ref (
     231                 :        6407 :         function.get_return_type ().get_mappings ().get_hirid ());
     232                 :             :     }
     233                 :             : 
     234                 :        6814 :   std::vector<TyTy::FnParam> params;
     235                 :        6814 :   if (function.is_method ())
     236                 :             :     {
     237                 :             :       // these are implicit mappings and not used
     238                 :        5702 :       auto crate_num = mappings.get_current_crate ();
     239                 :       11404 :       Analysis::NodeMapping mapping (crate_num, mappings.get_next_node_id (),
     240                 :        5702 :                                      mappings.get_next_hir_id (crate_num),
     241                 :        5702 :                                      UNKNOWN_LOCAL_DEFID);
     242                 :             : 
     243                 :             :       // add the synthetic self param at the front, this is a placeholder for
     244                 :             :       // compilation to know parameter names. The types are ignored but we
     245                 :             :       // reuse the HIR identifier pattern which requires it
     246                 :        5702 :       HIR::SelfParam &self_param = function.get_self_param_unchecked ();
     247                 :             :       // FIXME: which location should be used for Rust::Identifier for `self`?
     248                 :        5702 :       std::unique_ptr<HIR::Pattern> self_pattern
     249                 :        5702 :         = std::make_unique<HIR::IdentifierPattern> (
     250                 :       11404 :           HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (),
     251                 :        5702 :                                   self_param.is_ref (), self_param.get_mut (),
     252                 :       28510 :                                   std::unique_ptr<HIR::Pattern> (nullptr)));
     253                 :             : 
     254                 :             :       // might have a specified type
     255                 :        5702 :       TyTy::BaseType *self_type = nullptr;
     256                 :        5702 :       if (self_param.has_type ())
     257                 :             :         {
     258                 :           1 :           auto &specified_type = self_param.get_type ();
     259                 :           1 :           self_type = TypeCheckType::Resolve (specified_type);
     260                 :             :         }
     261                 :             :       else
     262                 :             :         {
     263                 :        5701 :           switch (self_param.get_self_kind ())
     264                 :             :             {
     265                 :        2512 :             case HIR::SelfParam::IMM:
     266                 :        2512 :             case HIR::SelfParam::MUT:
     267                 :        2512 :               self_type = self->clone ();
     268                 :        2512 :               break;
     269                 :             : 
     270                 :        3036 :             case HIR::SelfParam::IMM_REF:
     271                 :        3036 :               {
     272                 :        3036 :                 tl::optional<TyTy::Region> region;
     273                 :        3036 :                 if (self_param.has_lifetime ())
     274                 :             :                   {
     275                 :        5560 :                     region = context->lookup_and_resolve_lifetime (
     276                 :        2780 :                       self_param.get_lifetime ());
     277                 :        2780 :                     if (!region.has_value ())
     278                 :             :                       {
     279                 :           0 :                         rust_inform (self_param.get_locus (),
     280                 :             :                                      "failed to resolve lifetime");
     281                 :           0 :                         return;
     282                 :             :                       }
     283                 :             :                   }
     284                 :             :                 else
     285                 :             :                   {
     286                 :         256 :                     region = TyTy::Region::make_anonymous ();
     287                 :             :                   }
     288                 :        6072 :                 self_type = new TyTy::ReferenceType (
     289                 :        6072 :                   self_param.get_mappings ().get_hirid (),
     290                 :        3036 :                   TyTy::TyVar (self->get_ref ()), Mutability::Imm,
     291                 :        6072 :                   region.value ());
     292                 :             :               }
     293                 :        3036 :               break;
     294                 :             : 
     295                 :         153 :             case HIR::SelfParam::MUT_REF:
     296                 :         153 :               {
     297                 :         153 :                 tl::optional<TyTy::Region> region;
     298                 :         153 :                 if (self_param.has_lifetime ())
     299                 :             :                   {
     300                 :         306 :                     region = context->lookup_and_resolve_lifetime (
     301                 :         153 :                       self_param.get_lifetime ());
     302                 :         153 :                     if (!region.has_value ())
     303                 :             :                       {
     304                 :           0 :                         rust_error_at (self_param.get_locus (),
     305                 :             :                                        "failed to resolve lifetime");
     306                 :           0 :                         return;
     307                 :             :                       }
     308                 :             :                   }
     309                 :             :                 else
     310                 :             :                   {
     311                 :           0 :                     region = TyTy::Region::make_anonymous ();
     312                 :             :                   }
     313                 :         306 :                 self_type = new TyTy::ReferenceType (
     314                 :         306 :                   self_param.get_mappings ().get_hirid (),
     315                 :         153 :                   TyTy::TyVar (self->get_ref ()), Mutability::Mut,
     316                 :         306 :                   region.value ());
     317                 :             :               }
     318                 :         153 :               break;
     319                 :             : 
     320                 :           0 :             default:
     321                 :           0 :               rust_unreachable ();
     322                 :             :               return;
     323                 :             :             }
     324                 :             :         }
     325                 :             : 
     326                 :        5702 :       context->insert_type (self_param.get_mappings (), self_type);
     327                 :        5702 :       params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
     328                 :        5702 :     }
     329                 :             : 
     330                 :       11177 :   for (auto &param : function.get_function_params ())
     331                 :             :     {
     332                 :             :       // get the name as well required for later on
     333                 :        4363 :       auto param_tyty = TypeCheckType::Resolve (param.get_type ());
     334                 :             : 
     335                 :        4363 :       context->insert_type (param.get_mappings (), param_tyty);
     336                 :        4363 :       TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
     337                 :             : 
     338                 :        8726 :       params.push_back (
     339                 :        8726 :         TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
     340                 :             :     }
     341                 :             : 
     342                 :        6814 :   auto &nr_ctx
     343                 :        6814 :     = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     344                 :             : 
     345                 :        6814 :   CanonicalPath canonical_path
     346                 :        6814 :     = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid ());
     347                 :             : 
     348                 :       13628 :   RustIdent ident{canonical_path, function.get_locus ()};
     349                 :        6814 :   auto fnType = new TyTy::FnType (
     350                 :        6814 :     function.get_mappings ().get_hirid (),
     351                 :        6814 :     function.get_mappings ().get_defid (),
     352                 :        6814 :     function.get_function_name ().as_string (), ident,
     353                 :        6814 :     function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG
     354                 :             :                           : TyTy::FnType::FNTYPE_DEFAULT_FLAGS,
     355                 :        6814 :     ABI::RUST, std::move (params), ret_type, std::move (substitutions),
     356                 :       13628 :     TyTy::SubstitutionArgumentMappings::empty (
     357                 :        6814 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     358                 :       34070 :     region_constraints);
     359                 :             : 
     360                 :        6814 :   context->insert_type (function.get_mappings (), fnType);
     361                 :        6814 :   result = fnType;
     362                 :             : 
     363                 :             :   // need to get the return type from this
     364                 :        6814 :   TyTy::FnType *resolve_fn_type = fnType;
     365                 :        6814 :   auto expected_ret_tyty = resolve_fn_type->get_return_type ();
     366                 :        6814 :   context->push_return_type (TypeCheckContextItem (parent, &function),
     367                 :             :                              expected_ret_tyty);
     368                 :             : 
     369                 :        6814 :   auto block_expr_ty = TypeCheckExpr::Resolve (function.get_definition ());
     370                 :             : 
     371                 :        6814 :   location_t fn_return_locus = function.has_function_return_type ()
     372                 :        6814 :                                  ? function.get_return_type ().get_locus ()
     373                 :         407 :                                  : function.get_locus ();
     374                 :             : 
     375                 :       13628 :   coercion_site (function.get_definition ().get_mappings ().get_hirid (),
     376                 :        6814 :                  TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
     377                 :        6814 :                  TyTy::TyWithLocation (block_expr_ty),
     378                 :        6814 :                  function.get_definition ().get_locus ());
     379                 :             : 
     380                 :        6814 :   context->pop_return_type ();
     381                 :       13628 : }
     382                 :             : 
     383                 :             : void
     384                 :          40 : TypeCheckImplItem::visit (HIR::ConstantItem &constant)
     385                 :             : {
     386                 :          40 :   TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
     387                 :          40 :   TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
     388                 :             : 
     389                 :          80 :   TyTy::BaseType *unified = unify_site (
     390                 :          40 :     constant.get_mappings ().get_hirid (),
     391                 :          40 :     TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
     392                 :          40 :     TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
     393                 :             :     constant.get_locus ());
     394                 :          40 :   context->insert_type (constant.get_mappings (), unified);
     395                 :          40 :   result = unified;
     396                 :          40 : }
     397                 :             : 
     398                 :             : void
     399                 :        1166 : TypeCheckImplItem::visit (HIR::TypeAlias &alias)
     400                 :             : {
     401                 :        1166 :   auto binder_pin = context->push_lifetime_binder ();
     402                 :             : 
     403                 :        1166 :   if (alias.has_generics ())
     404                 :           0 :     resolve_generic_params (HIR::Item::ItemKind::TypeAlias, alias.get_locus (),
     405                 :           0 :                             alias.get_generic_params (), substitutions);
     406                 :             : 
     407                 :        1166 :   TyTy::BaseType *actual_type
     408                 :        1166 :     = TypeCheckType::Resolve (alias.get_type_aliased ());
     409                 :             : 
     410                 :        1166 :   context->insert_type (alias.get_mappings (), actual_type);
     411                 :        1166 :   result = actual_type;
     412                 :        1166 :   TyTy::RegionConstraints region_constraints;
     413                 :        1166 :   for (auto &where_clause_item : alias.get_where_clause ().get_items ())
     414                 :             :     {
     415                 :           0 :       ResolveWhereClauseItem::Resolve (*where_clause_item.get (),
     416                 :             :                                        region_constraints);
     417                 :             :     }
     418                 :        1166 : }
     419                 :             : 
     420                 :        5321 : TypeCheckImplItemWithTrait::TypeCheckImplItemWithTrait (
     421                 :             :   HIR::ImplBlock &parent, TyTy::BaseType *self,
     422                 :             :   TyTy::TypeBoundPredicate &trait_reference,
     423                 :        5321 :   std::vector<TyTy::SubstitutionParamMapping> substitutions)
     424                 :        5321 :   : TypeCheckBase (), trait_reference (trait_reference),
     425                 :        5321 :     resolved_trait_item (TyTy::TypeBoundPredicateItem::error ()),
     426                 :        5321 :     parent (parent), self (self), substitutions (substitutions)
     427                 :             : {
     428                 :        5321 :   rust_assert (is_trait_impl_block ());
     429                 :        5321 : }
     430                 :             : 
     431                 :             : TyTy::TypeBoundPredicateItem
     432                 :        5321 : TypeCheckImplItemWithTrait::Resolve (
     433                 :             :   HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self,
     434                 :             :   TyTy::TypeBoundPredicate &trait_reference,
     435                 :             :   std::vector<TyTy::SubstitutionParamMapping> substitutions)
     436                 :             : {
     437                 :        5321 :   TypeCheckImplItemWithTrait resolver (parent, self, trait_reference,
     438                 :        5321 :                                        substitutions);
     439                 :        5321 :   item.accept_vis (resolver);
     440                 :        5321 :   return resolver.resolved_trait_item;
     441                 :        5321 : }
     442                 :             : 
     443                 :             : void
     444                 :          31 : TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant)
     445                 :             : {
     446                 :             :   // normal resolution of the item
     447                 :          31 :   TyTy::BaseType *lookup
     448                 :          31 :     = TypeCheckImplItem::Resolve (parent, constant, self, substitutions);
     449                 :             : 
     450                 :             :   // map the impl item to the associated trait item
     451                 :          31 :   const auto tref = trait_reference.get ();
     452                 :          31 :   const TraitItemReference *raw_trait_item = nullptr;
     453                 :          31 :   bool found
     454                 :          31 :     = tref->lookup_trait_item_by_type (constant.get_identifier ().as_string (),
     455                 :             :                                        TraitItemReference::TraitItemType::CONST,
     456                 :             :                                        &raw_trait_item);
     457                 :             : 
     458                 :             :   // unknown trait item - https://doc.rust-lang.org/error_codes/E0323.html
     459                 :          31 :   if (!found || raw_trait_item->is_error ())
     460                 :             :     {
     461                 :           1 :       rich_location r (line_table, constant.get_locus ());
     462                 :           1 :       r.add_range (trait_reference.get_locus ());
     463                 :           1 :       rust_error_at (r, ErrorCode::E0323,
     464                 :             :                      "item %qs is an associated const, which does not match "
     465                 :             :                      "its trait %qs",
     466                 :           2 :                      constant.get_identifier ().as_string ().c_str (),
     467                 :           1 :                      trait_reference.get_name ().c_str ());
     468                 :           1 :       return;
     469                 :           1 :     }
     470                 :             : 
     471                 :             :   // get the item from the predicate
     472                 :          30 :   resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
     473                 :          30 :   rust_assert (!resolved_trait_item.is_error ());
     474                 :             : 
     475                 :             :   // merge the attributes
     476                 :          30 :   const HIR::TraitItem *hir_trait_item
     477                 :          30 :     = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
     478                 :          30 :   merge_attributes (constant.get_outer_attrs (), *hir_trait_item);
     479                 :             : 
     480                 :             :   // check the types are compatible
     481                 :          30 :   auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
     482                 :          30 :   if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
     483                 :          30 :                          TyTy::TyWithLocation (lookup), constant.get_locus (),
     484                 :             :                          true /*emit_errors*/))
     485                 :             :     {
     486                 :           0 :       rich_location r (line_table, constant.get_locus ());
     487                 :           0 :       r.add_range (resolved_trait_item.get_locus ());
     488                 :             : 
     489                 :           0 :       rust_error_at (r, "constant %qs has an incompatible type for trait %qs",
     490                 :           0 :                      constant.get_identifier ().as_string ().c_str (),
     491                 :           0 :                      trait_reference.get_name ().c_str ());
     492                 :           0 :     }
     493                 :             : }
     494                 :             : 
     495                 :             : void
     496                 :        1166 : TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
     497                 :             : {
     498                 :             :   // normal resolution of the item
     499                 :        1166 :   TyTy::BaseType *lookup
     500                 :        1166 :     = TypeCheckImplItem::Resolve (parent, type, self, substitutions);
     501                 :             : 
     502                 :             :   // map the impl item to the associated trait item
     503                 :        1166 :   const auto tref = trait_reference.get ();
     504                 :        1166 :   const TraitItemReference *raw_trait_item = nullptr;
     505                 :        1166 :   bool found
     506                 :        1166 :     = tref->lookup_trait_item_by_type (type.get_new_type_name ().as_string (),
     507                 :             :                                        TraitItemReference::TraitItemType::TYPE,
     508                 :             :                                        &raw_trait_item);
     509                 :             : 
     510                 :             :   // unknown trait item
     511                 :        1166 :   if (!found || raw_trait_item->is_error ())
     512                 :             :     {
     513                 :           0 :       rich_location r (line_table, type.get_locus ());
     514                 :           0 :       r.add_range (trait_reference.get_locus ());
     515                 :           0 :       rust_error_at (r, "type alias %qs is not a member of trait %qs",
     516                 :           0 :                      type.get_new_type_name ().as_string ().c_str (),
     517                 :           0 :                      trait_reference.get_name ().c_str ());
     518                 :           0 :       return;
     519                 :           0 :     }
     520                 :             : 
     521                 :             :   // get the item from the predicate
     522                 :        1166 :   resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
     523                 :        1166 :   rust_assert (!resolved_trait_item.is_error ());
     524                 :             : 
     525                 :             :   // merge the attributes
     526                 :        1166 :   const HIR::TraitItem *hir_trait_item
     527                 :        1166 :     = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
     528                 :        1166 :   merge_attributes (type.get_outer_attrs (), *hir_trait_item);
     529                 :             : 
     530                 :             :   // check the types are compatible
     531                 :        1166 :   auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
     532                 :        1166 :   if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
     533                 :        1166 :                          TyTy::TyWithLocation (lookup), type.get_locus (),
     534                 :             :                          true /*emit_errors*/))
     535                 :             :     {
     536                 :           0 :       rich_location r (line_table, type.get_locus ());
     537                 :           0 :       r.add_range (resolved_trait_item.get_locus ());
     538                 :             : 
     539                 :           0 :       rust_error_at (r, "type alias %qs has an incompatible type for trait %qs",
     540                 :           0 :                      type.get_new_type_name ().as_string ().c_str (),
     541                 :           0 :                      trait_reference.get_name ().c_str ());
     542                 :           0 :     }
     543                 :             : 
     544                 :             :   // its actually a projection, since we need a way to actually bind the
     545                 :             :   // generic substitutions to the type itself
     546                 :        1166 :   TyTy::ProjectionType *projection
     547                 :        1166 :     = new TyTy::ProjectionType (type.get_mappings ().get_hirid (), lookup, tref,
     548                 :        1166 :                                 raw_trait_item->get_mappings ().get_defid (),
     549                 :        3498 :                                 substitutions);
     550                 :             : 
     551                 :        1166 :   context->insert_type (type.get_mappings (), projection);
     552                 :        1166 :   raw_trait_item->associated_type_set (projection);
     553                 :             : }
     554                 :             : 
     555                 :             : void
     556                 :        4124 : TypeCheckImplItemWithTrait::visit (HIR::Function &function)
     557                 :             : {
     558                 :             :   // normal resolution of the item
     559                 :        4124 :   TyTy::BaseType *lookup
     560                 :        4124 :     = TypeCheckImplItem::Resolve (parent, function, self, substitutions);
     561                 :             : 
     562                 :             :   // map the impl item to the associated trait item
     563                 :        4124 :   const auto tref = trait_reference.get ();
     564                 :        4124 :   const TraitItemReference *raw_trait_item = nullptr;
     565                 :        4124 :   bool found = tref->lookup_trait_item_by_type (
     566                 :        4124 :     function.get_function_name ().as_string (),
     567                 :             :     TraitItemReference::TraitItemType::FN, &raw_trait_item);
     568                 :             : 
     569                 :             :   // unknown trait item
     570                 :        4124 :   if (!found || raw_trait_item->is_error ())
     571                 :             :     {
     572                 :           2 :       rich_location r (line_table, function.get_locus ());
     573                 :           2 :       r.add_range (trait_reference.get_locus ());
     574                 :           2 :       rust_error_at (r, "method %qs is not a member of trait %qs",
     575                 :           4 :                      function.get_function_name ().as_string ().c_str (),
     576                 :           2 :                      trait_reference.get_name ().c_str ());
     577                 :           2 :       return;
     578                 :           2 :     }
     579                 :             : 
     580                 :             :   // get the item from the predicate
     581                 :        4122 :   resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
     582                 :        4122 :   rust_assert (!resolved_trait_item.is_error ());
     583                 :             : 
     584                 :             :   // merge the attributes
     585                 :        4122 :   const HIR::TraitItem *hir_trait_item
     586                 :        4122 :     = resolved_trait_item.get_raw_item ()->get_hir_trait_item ();
     587                 :        4122 :   merge_attributes (function.get_outer_attrs (), *hir_trait_item);
     588                 :             : 
     589                 :             :   // check the types are compatible
     590                 :        4122 :   auto trait_item_type = resolved_trait_item.get_tyty_for_receiver (self);
     591                 :        4122 :   if (!types_compatable (TyTy::TyWithLocation (trait_item_type),
     592                 :        4122 :                          TyTy::TyWithLocation (lookup), function.get_locus (),
     593                 :             :                          true /*emit_errors*/))
     594                 :             :     {
     595                 :           3 :       rich_location r (line_table, function.get_locus ());
     596                 :           3 :       r.add_range (resolved_trait_item.get_locus ());
     597                 :             : 
     598                 :           3 :       rust_error_at (r, ErrorCode::E0053,
     599                 :             :                      "method %qs has an incompatible type for trait %qs",
     600                 :           6 :                      function.get_function_name ().as_string ().c_str (),
     601                 :           3 :                      trait_reference.get_name ().c_str ());
     602                 :           3 :     }
     603                 :             : }
     604                 :             : 
     605                 :             : void
     606                 :        5318 : TypeCheckImplItemWithTrait::merge_attributes (AST::AttrVec &impl_item_attrs,
     607                 :             :                                               const HIR::TraitItem &trait_item)
     608                 :             : {
     609                 :       19542 :   for (const auto &attr : trait_item.get_outer_attrs ())
     610                 :             :     {
     611                 :       14224 :       impl_item_attrs.push_back (attr);
     612                 :             :     }
     613                 :        5318 : }
     614                 :             : 
     615                 :             : bool
     616                 :        5321 : TypeCheckImplItemWithTrait::is_trait_impl_block () const
     617                 :             : {
     618                 :        5321 :   return !trait_reference.is_error ();
     619                 :             : }
     620                 :             : 
     621                 :             : } // namespace Resolver
     622                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

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