LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-hir-type-check-item.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.2 % 744 708
Test Date: 2026-11-14 16:39:50 Functions: 97.1 % 34 33
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-item.h"
      20              : #include "optional.h"
      21              : #include "rust-canonical-path.h"
      22              : #include "rust-diagnostics.h"
      23              : #include "rust-hir-item.h"
      24              : #include "rust-hir-type-check-enumitem.h"
      25              : #include "rust-hir-type-check-implitem.h"
      26              : #include "rust-hir-type-check-type.h"
      27              : #include "rust-hir-type-check-expr.h"
      28              : #include "rust-hir-type-check-pattern.h"
      29              : #include "rust-hir-trait-resolve.h"
      30              : #include "rust-hir-type-check.h"
      31              : #include "rust-identifier.h"
      32              : #include "rust-rib.h"
      33              : #include "rust-session-manager.h"
      34              : #include "rust-finalized-name-resolution-context.h"
      35              : #include "rust-substitution-mapper.h"
      36              : #include "rust-type-util.h"
      37              : #include "rust-tyty-variance-analysis.h"
      38              : #include "rust-tyty.h"
      39              : #include "options.h"
      40              : #include "rust-compile-base.h"
      41              : #include "rust-compile-context.h"
      42              : 
      43              : namespace Rust {
      44              : namespace Resolver {
      45              : 
      46              : static bool
      47        30252 : type_contains_adt_without_indirection (
      48              :   const TyTy::BaseType *type, DefId target,
      49              :   std::set<const TyTy::BaseType *> &visited)
      50              : {
      51        30252 :   bool found = visited.find (type) != visited.end ();
      52        30252 :   if (found)
      53              :     return false;
      54        30252 :   visited.insert (type);
      55              : 
      56        30252 :   switch (type->get_kind ())
      57              :     {
      58        18643 :     case TyTy::TypeKind::ADT:
      59        18643 :       {
      60        18643 :         auto *adt = static_cast<const TyTy::ADTType *> (type);
      61        18643 :         if (adt->get_id () == target)
      62            5 :           return true;
      63              : 
      64        38177 :         for (auto *variant : adt->get_variants ())
      65              :           {
      66        39464 :             for (auto *field : variant->get_fields ())
      67              :               {
      68        19925 :                 if (type_contains_adt_without_indirection (
      69        19925 :                       field->get_field_type (), target, visited))
      70        30252 :                   return true;
      71              :               }
      72              :           }
      73              : 
      74              :         return false;
      75              :       }
      76              : 
      77           88 :     case TyTy::TypeKind::TUPLE:
      78           88 :       {
      79           88 :         auto *tuple = static_cast<const TyTy::TupleType *> (type);
      80           99 :         for (const auto &field : tuple->get_fields ())
      81              :           {
      82           12 :             if (type_contains_adt_without_indirection (field.get_tyty (),
      83              :                                                        target, visited))
      84        30252 :               return true;
      85              :           }
      86              : 
      87              :         return false;
      88              :       }
      89              : 
      90          107 :     case TyTy::TypeKind::ARRAY:
      91          107 :       return type_contains_adt_without_indirection (
      92          107 :         static_cast<const TyTy::ArrayType *> (type)->get_element_type (),
      93          107 :         target, visited);
      94              : 
      95           15 :     case TyTy::TypeKind::SLICE:
      96           15 :       return type_contains_adt_without_indirection (
      97           15 :         static_cast<const TyTy::SliceType *> (type)->get_element_type (),
      98           15 :         target, visited);
      99              : 
     100              :     default:
     101              :       return false;
     102              :     }
     103              : }
     104              : 
     105              : static bool
     106         5566 : validate_adt_size (TyTy::ADTType *type)
     107              : {
     108        12279 :   for (auto *variant : type->get_variants ())
     109              :     {
     110        16906 :       for (auto *field : variant->get_fields ())
     111              :         {
     112        10193 :           std::set<const TyTy::BaseType *> visited;
     113        10193 :           if (!type_contains_adt_without_indirection (field->get_field_type (),
     114              :                                                       type->get_id (), visited))
     115        10188 :             continue;
     116              : 
     117           10 :           rust_error_at (field->get_locus (), ErrorCode::E0072,
     118              :                          "recursive type %qs has infinite size",
     119            5 :                          type->get_identifier ().c_str ());
     120            5 :           return false;
     121        10193 :         }
     122              :     }
     123              : 
     124              :   return true;
     125              : }
     126              : 
     127              : // Const-evaluate the discriminants of a repr(C) enum and warn when a value does
     128              : // not fit into a C int/unsigned int. Done here, during type resolution, using
     129              : // the compile context (a singleton shared with the backend).
     130              : static void
     131            3 : check_repr_c_enum_discriminants (Compile::Context *ctx, TyTy::BaseType *type)
     132              : {
     133            3 :   if (type->get_kind () != TyTy::TypeKind::ADT)
     134              :     return;
     135              : 
     136            3 :   auto &adt = static_cast<TyTy::ADTType &> (*type);
     137            3 :   if (!adt.is_enum ()
     138            3 :       || adt.get_repr_options ().repr_kind != TyTy::ADTType::ReprKind::C)
     139              :     return;
     140              : 
     141            4 :   for (auto &variant : adt.get_variants ())
     142              :     {
     143            2 :       if (!variant->has_discriminant ())
     144            0 :         continue;
     145              : 
     146            2 :       HIR::Expr &discriminant = variant->get_discriminant ();
     147            2 :       TyTy::BaseType *discrim_ty = nullptr;
     148            2 :       if (!ctx->get_tyctx ()->lookup_type (
     149            2 :             discriminant.get_mappings ().get_hirid (), &discrim_ty))
     150            0 :         continue;
     151              : 
     152            2 :       tree folded
     153            2 :         = Compile::HIRCompileBase::query_compile_const_expr (ctx, discrim_ty,
     154              :                                                              discriminant);
     155            2 :       if (folded == error_mark_node || TREE_CODE (folded) != INTEGER_CST)
     156            0 :         continue;
     157              : 
     158            2 :       widest_int value = wi::to_widest (folded);
     159            2 :       if (wi::lts_p (value, INT32_MIN) || wi::gts_p (value, UINT32_MAX))
     160            1 :         rust_warning_at (discriminant.get_locus (), OPT_Woverflow,
     161              :                          "%<repr(C)%> enum discriminant does not fit into C "
     162              :                          "%<int%> nor into C %<unsigned int%>");
     163            2 :     }
     164              : }
     165              : 
     166       774446 : TypeCheckItem::TypeCheckItem () : TypeCheckBase (), infered (nullptr) {}
     167              : 
     168              : TyTy::BaseType *
     169        35950 : TypeCheckItem::Resolve (HIR::Item &item)
     170              : {
     171              :   // is it already resolved?
     172        35950 :   auto context = TypeCheckContext::get ();
     173        35950 :   TyTy::BaseType *resolved = nullptr;
     174        35950 :   bool already_resolved
     175        35950 :     = context->lookup_type (item.get_mappings ().get_hirid (), &resolved);
     176        35950 :   if (already_resolved)
     177              :     {
     178         5409 :       bool function_body_pending = false;
     179         5409 :       if (auto fn = resolved->try_as<TyTy::FnType> ())
     180          194 :         function_body_pending = context->function_body_pending (fn->get_id ());
     181              : 
     182          194 :       if (!function_body_pending)
     183         5220 :         return resolved;
     184              :     }
     185              : 
     186        30730 :   rust_assert (item.get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM);
     187        30730 :   HIR::VisItem &vis_item = static_cast<HIR::VisItem &> (item);
     188              : 
     189        30730 :   TypeCheckItem resolver;
     190        30730 :   vis_item.accept_vis (resolver);
     191        30652 :   return resolver.infered;
     192        30652 : }
     193              : 
     194              : TyTy::FnType *
     195          189 : TypeCheckItem::ResolveFunctionSignature (HIR::Function &function)
     196              : {
     197          189 :   TypeCheckItem resolver;
     198          189 :   auto lifetime_pin = resolver.context->push_clean_lifetime_resolver ();
     199          189 :   TyTy::FnType *result = resolver.resolve_function_signature (function);
     200          189 :   if (result != nullptr)
     201          189 :     resolver.context->mark_function_body_pending (result->get_id ());
     202          378 :   return result;
     203          189 : }
     204              : 
     205              : TyTy::BaseType *
     206          412 : TypeCheckItem::ResolveTraitSignature (HIR::Trait &trait)
     207              : {
     208          412 :   TypeCheckItem resolver;
     209          412 :   return resolver.resolve_trait (trait, false);
     210          412 : }
     211              : 
     212              : TyTy::BaseType *
     213            1 : TypeCheckItem::ResolveImplItem (HIR::ImplBlock &impl_block, HIR::ImplItem &item)
     214              : {
     215            1 :   TypeCheckItem resolver;
     216            1 :   return resolver.resolve_impl_item (impl_block, item);
     217            1 : }
     218              : 
     219              : TyTy::BaseType *
     220       724011 : TypeCheckItem::ResolveImplBlockSelf (HIR::ImplBlock &impl_block)
     221              : {
     222       724011 :   TypeCheckItem resolver;
     223              : 
     224              :   // method probing asks for this on every impl it looks at, and redoing
     225              :   // the generics and bounds each time is what runs core out of memory
     226       724011 :   TyTy::BaseType *self = nullptr;
     227       724011 :   if (resolver.context->lookup_type (
     228       724011 :         impl_block.get_type ().get_mappings ().get_hirid (), &self)
     229       724011 :       && self->get_kind () != TyTy::TypeKind::ERROR)
     230       677595 :     return self;
     231              : 
     232        46416 :   auto lifetime_pin = resolver.context->push_clean_lifetime_resolver (true);
     233              : 
     234        46416 :   bool failed_flag = false;
     235        46416 :   auto result
     236        46416 :     = resolver.resolve_impl_block_substitutions (impl_block, failed_flag);
     237        46395 :   if (failed_flag)
     238              :     {
     239            0 :       return new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
     240              :     }
     241        46395 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     242        46395 :     = std::move (result.first);
     243        46395 :   TyTy::RegionConstraints region_constraints = std::move (result.second);
     244              : 
     245        46395 :   return resolver.resolve_impl_block_self (impl_block);
     246        46395 : }
     247              : 
     248              : TyTy::BaseType *
     249         3484 : TypeCheckItem::ResolveImplBlockSelfWithInference (
     250              :   HIR::ImplBlock &impl, location_t locus,
     251              :   TyTy::SubstitutionArgumentMappings *infer_arguments)
     252              : {
     253         3484 :   TypeCheckItem resolver;
     254         3484 :   auto lifetime_pin = resolver.context->push_clean_lifetime_resolver (true);
     255              : 
     256         3484 :   bool failed_flag = false;
     257         3484 :   auto result = resolver.resolve_impl_block_substitutions (impl, failed_flag);
     258         3484 :   if (failed_flag)
     259              :     {
     260            1 :       return new TyTy::ErrorType (impl.get_mappings ().get_hirid ());
     261              :     }
     262         3483 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     263         3483 :     = std::move (result.first);
     264         3483 :   TyTy::RegionConstraints region_constraints = std::move (result.second);
     265              : 
     266              :   // now that we have the param mappings we need to query the self type
     267         3483 :   TyTy::BaseType *self = resolver.resolve_impl_block_self (impl);
     268              : 
     269              :   // nothing to do
     270         3483 :   if (substitutions.empty () || self->is_concrete ())
     271              :     return self;
     272              : 
     273              :   // generate inference variables for the subst-param-mappings
     274          794 :   std::vector<TyTy::SubstitutionArg> args;
     275         1622 :   for (auto &p : substitutions)
     276              :     {
     277          828 :       auto param = p.get_param_ty ();
     278          828 :       if (!p.needs_substitution ())
     279              :         {
     280            0 :           auto resolved = param->destructure ();
     281            0 :           args.emplace_back (&p, resolved);
     282              : 
     283            0 :           continue;
     284            0 :         }
     285              : 
     286          828 :       TyTy::BaseType *argument = nullptr;
     287          828 :       if (param->get_kind () == TyTy::TypeKind::CONST)
     288              :         {
     289           28 :           auto i = TyTy::TyVar::get_implicit_const_infer_var (locus);
     290           28 :           argument = i.get_tyty ();
     291              :         }
     292              :       else
     293              :         {
     294          800 :           auto i = TyTy::TyVar::get_implicit_infer_var (locus);
     295          800 :           argument = i.get_tyty ();
     296              :         }
     297          828 :       args.emplace_back (&p, argument);
     298              :     }
     299              : 
     300              :   // create argument mappings
     301          794 :   *infer_arguments = TyTy::SubstitutionArgumentMappings (
     302              :     std::move (args), {},
     303         1588 :     TyTy::SubstitutionArgumentMappings::regions_from_nullable_args (
     304              :       infer_arguments),
     305         1588 :     locus);
     306              : 
     307          794 :   TyTy::BaseType *infer = SubstMapperInternal::Resolve (self, *infer_arguments);
     308              : 
     309              :   // we only need to apply to the bounds manually on types which dont bind
     310              :   // generics
     311          794 :   if (!infer->has_substitutions_defined ())
     312              :     {
     313          707 :       for (auto &bound : infer->get_specified_bounds ())
     314          185 :         bound.handle_substitions (*infer_arguments);
     315              :     }
     316              : 
     317          794 :   return infer;
     318         4278 : }
     319              : 
     320              : std::vector<TyTy::SubstitutionParamMapping>
     321        15619 : TypeCheckItem::ResolveImplBlockSubstitutions (HIR::ImplBlock &impl_block,
     322              :                                               bool &failure_flag)
     323              : {
     324        15619 :   TypeCheckItem resolver;
     325        15619 :   auto result
     326        15619 :     = resolver.resolve_impl_block_substitutions (impl_block, failure_flag);
     327        15619 :   return std::move (result.first);
     328        15619 : }
     329              : 
     330              : void
     331         8167 : TypeCheckItem::validate_trait_impl_block (
     332              :   const TyTy::TypeBoundPredicate &specified_bound,
     333              :   std::vector<const TraitItemReference *> trait_item_refs,
     334              :   TraitReference *trait_reference, HIR::ImplBlock &impl_block,
     335              :   TyTy::BaseType *self,
     336              :   std::vector<TyTy::SubstitutionParamMapping> &substitutions)
     337              : {
     338         8167 :   bool impl_block_missing_trait_items
     339         8167 :     = !specified_bound.is_error ()
     340         8167 :       && trait_reference->size () != trait_item_refs.size ();
     341         2520 :   if (impl_block_missing_trait_items
     342         2520 :       && impl_block.get_polarity () == BoundPolarity::RegularBound)
     343              :     {
     344              :       // filter the missing impl_items
     345         2518 :       std::vector<std::reference_wrapper<const TraitItemReference>>
     346         2518 :         missing_trait_items;
     347         7829 :       for (const auto &trait_item_ref : trait_reference->get_trait_items ())
     348              :         {
     349         5311 :           bool found = false;
     350         8100 :           for (auto implemented_trait_item : trait_item_refs)
     351              :             {
     352         5135 :               std::string trait_item_name = trait_item_ref.get_identifier ();
     353         5135 :               std::string impl_item_name
     354         5135 :                 = implemented_trait_item->get_identifier ();
     355         5135 :               found = trait_item_name == impl_item_name;
     356         5135 :               if (found)
     357              :                 break;
     358         5135 :             }
     359              : 
     360         5311 :           bool is_required_trait_item = !trait_item_ref.is_optional ();
     361         5311 :           if (!found && is_required_trait_item)
     362            7 :             missing_trait_items.emplace_back (trait_item_ref);
     363              :         }
     364              : 
     365         2518 :       if (!missing_trait_items.empty ())
     366              :         {
     367            5 :           std::string missing_items_buf;
     368            5 :           rich_location r (line_table, impl_block.get_locus ());
     369           17 :           for (size_t i = 0; i < missing_trait_items.size (); i++)
     370              :             {
     371            7 :               bool has_more = (i + 1) < missing_trait_items.size ();
     372            7 :               const TraitItemReference &missing_trait_item
     373            7 :                 = missing_trait_items.at (i);
     374            7 :               missing_items_buf += missing_trait_item.get_identifier ()
     375           21 :                                    + (has_more ? ", " : "");
     376            7 :               r.add_range (missing_trait_item.get_locus ());
     377              :             }
     378              : 
     379            5 :           rust_error_at (r, ErrorCode::E0046,
     380              :                          "missing %s in implementation of trait %qs",
     381              :                          missing_items_buf.c_str (),
     382            5 :                          trait_reference->get_name ().c_str ());
     383            5 :         }
     384         2518 :     }
     385         8167 : }
     386              : 
     387              : void
     388           96 : TypeCheckItem::visit (HIR::TypeAlias &alias)
     389              : {
     390           96 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
     391              : 
     392           96 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     393           96 :   if (alias.has_generics ())
     394           10 :     resolve_generic_params (HIR::Item::ItemKind::TypeAlias, alias.get_locus (),
     395           10 :                             alias.get_generic_params (), substitutions);
     396              : 
     397           96 :   TyTy::BaseType *actual_type
     398           96 :     = TypeCheckType::Resolve (alias.get_type_aliased ());
     399              : 
     400           96 :   context->insert_type (alias.get_mappings (), actual_type);
     401              : 
     402           96 :   TyTy::RegionConstraints region_constraints;
     403           96 :   ResolveWhereClauseItem::Resolve (alias.get_where_clause (),
     404              :                                    region_constraints);
     405           96 :   infered = actual_type;
     406           96 : }
     407              : 
     408              : void
     409         1749 : TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
     410              : {
     411         1749 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
     412              : 
     413         1749 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     414         1749 :   if (struct_decl.has_generics ())
     415          496 :     resolve_generic_params (HIR::Item::ItemKind::Struct,
     416              :                             struct_decl.get_locus (),
     417          496 :                             struct_decl.get_generic_params (), substitutions);
     418              : 
     419         1749 :   TyTy::RegionConstraints region_constraints;
     420         1749 :   ResolveWhereClauseItem::Resolve (struct_decl.get_where_clause (),
     421              :                                    region_constraints);
     422              : 
     423              :   // Process #[repr(X)] attribute, if any
     424         1749 :   const AST::AttrVec &attrs = struct_decl.get_outer_attrs ();
     425         1749 :   TyTy::ADTType::ReprOptions repr
     426         1749 :     = parse_repr_options (attrs, struct_decl.get_locus ());
     427              : 
     428         1749 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     429         1749 :   CanonicalPath path
     430         1749 :     = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid (),
     431         1749 :                                 Resolver2_0::Namespace::Types);
     432         1749 :   RustIdent ident{path, struct_decl.get_locus ()};
     433              : 
     434         1749 :   std::vector<TyTy::VariantDef *> variants;
     435         1749 :   auto *type = new TyTy::ADTType (
     436         1749 :     struct_decl.get_mappings ().get_defid (),
     437         1749 :     struct_decl.get_mappings ().get_hirid (),
     438         1749 :     struct_decl.get_mappings ().get_hirid (),
     439         1749 :     struct_decl.get_identifier ().as_string (), ident,
     440              :     TyTy::ADTType::ADTKind::TUPLE_STRUCT, std::move (variants),
     441              :     std::move (substitutions), repr,
     442         3498 :     TyTy::SubstitutionArgumentMappings::empty (
     443         1749 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     444         6996 :     region_constraints);
     445         1749 :   context->insert_type (struct_decl.get_mappings (), type);
     446              : 
     447         1749 :   std::vector<TyTy::StructFieldType *> fields;
     448         1749 :   size_t idx = 0;
     449         6723 :   for (auto &field : struct_decl.get_fields ())
     450              :     {
     451         4981 :       TyTy::BaseType *field_type
     452         4981 :         = TypeCheckType::Resolve (field.get_field_type ());
     453         4974 :       auto *ty_field
     454         9948 :         = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
     455         4974 :                                      std::to_string (idx), field_type,
     456         4974 :                                      field.get_locus ());
     457         4974 :       fields.push_back (ty_field);
     458         4974 :       context->insert_type (field.get_mappings (), ty_field->get_field_type ());
     459         4974 :       idx++;
     460              :     }
     461              : 
     462         1742 :   if (repr.repr_kind == TyTy::ADTType::ReprKind::SIMD)
     463              :     {
     464          385 :       bool is_valid = validate_repr_simd (fields, struct_decl.get_locus ());
     465          385 :       if (!is_valid)
     466              :         return;
     467              :     }
     468              : 
     469              :   // its a single variant ADT
     470         1739 :   auto variant
     471         1739 :     = new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
     472         1739 :                             struct_decl.get_mappings ().get_defid (),
     473         1739 :                             struct_decl.get_identifier ().as_string (), ident,
     474         3478 :                             TyTy::VariantDef::VariantType::TUPLE, tl::nullopt,
     475         3478 :                             std::move (fields));
     476         1739 :   type->get_variants ().push_back (variant);
     477         1739 :   if (!validate_adt_size (type))
     478              :     {
     479            2 :       infered = new TyTy::ErrorType (struct_decl.get_mappings ().get_hirid ());
     480            2 :       context->insert_type (struct_decl.get_mappings (), infered);
     481            2 :       return;
     482              :     }
     483         1737 :   infered = type;
     484              : 
     485         1737 :   context->get_variance_analysis_ctx ().add_type_constraints (*type);
     486         3470 : }
     487              : 
     488              : void
     489         2923 : TypeCheckItem::visit (HIR::StructStruct &struct_decl)
     490              : {
     491         2923 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
     492         2923 :   auto &mappings = Analysis::Mappings::get ();
     493              : 
     494         2923 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     495         2923 :   CanonicalPath path
     496         2923 :     = nr_ctx.to_canonical_path (struct_decl.get_mappings ().get_nodeid (),
     497         2923 :                                 Resolver2_0::Namespace::Types);
     498         2923 :   RustIdent ident{path, struct_decl.get_locus ()};
     499              : 
     500         2923 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     501         2923 :   if (struct_decl.has_generics ())
     502         1329 :     resolve_generic_params (HIR::Item::ItemKind::Struct,
     503              :                             struct_decl.get_locus (),
     504         1329 :                             struct_decl.get_generic_params (), substitutions);
     505              : 
     506         2923 :   TyTy::RegionConstraints region_constraints;
     507         2923 :   ResolveWhereClauseItem::Resolve (struct_decl.get_where_clause (),
     508              :                                    region_constraints);
     509              : 
     510              :   // Process #[repr(X)] attribute, if any
     511         2923 :   const AST::AttrVec &attrs = struct_decl.get_outer_attrs ();
     512         2923 :   TyTy::ADTType::ReprOptions repr
     513         2923 :     = parse_repr_options (attrs, struct_decl.get_locus ());
     514              : 
     515         2923 :   std::vector<TyTy::VariantDef *> variants;
     516         2923 :   auto *type = new TyTy::ADTType (
     517         2923 :     struct_decl.get_mappings ().get_defid (),
     518         2923 :     struct_decl.get_mappings ().get_hirid (),
     519         2923 :     struct_decl.get_mappings ().get_hirid (),
     520         2923 :     struct_decl.get_identifier ().as_string (), ident,
     521              :     TyTy::ADTType::ADTKind::STRUCT_STRUCT, std::move (variants),
     522              :     std::move (substitutions), repr,
     523         5846 :     TyTy::SubstitutionArgumentMappings::empty (
     524         2923 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     525        11692 :     region_constraints);
     526         2923 :   context->insert_type (struct_decl.get_mappings (), type);
     527              : 
     528         2923 :   std::vector<TyTy::StructFieldType *> fields;
     529         6937 :   for (auto &field : struct_decl.get_fields ())
     530              :     {
     531         4019 :       TyTy::BaseType *field_type
     532         4019 :         = TypeCheckType::Resolve (field.get_field_type ());
     533         4019 :       auto infer_type = field_type->contains_infer ();
     534         4019 :       if (infer_type)
     535              :         {
     536            5 :           rust_error_at (mappings.lookup_location (infer_type->get_ref ()),
     537              :                          "the placeholder %<_%> is not allowed within types on "
     538              :                          "item signatures for structs");
     539            5 :           infered
     540            5 :             = new TyTy::ErrorType (struct_decl.get_mappings ().get_hirid ());
     541            5 :           context->insert_type (struct_decl.get_mappings (), infered);
     542            5 :           return;
     543              :         }
     544         4014 :       auto *ty_field
     545         4014 :         = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
     546         4014 :                                      field.get_field_name ().as_string (),
     547         8028 :                                      field_type, field.get_locus ());
     548         4014 :       fields.push_back (ty_field);
     549         4014 :       context->insert_type (field.get_mappings (), ty_field->get_field_type ());
     550              :     }
     551              : 
     552         2918 :   if (repr.repr_kind == TyTy::ADTType::ReprKind::SIMD)
     553              :     {
     554            0 :       bool is_valid = validate_repr_simd (fields, struct_decl.get_locus ());
     555            0 :       if (!is_valid)
     556              :         return;
     557              :     }
     558         2918 :   if (repr.repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT)
     559              :     {
     560           82 :       size_t num_non_zst = 0;
     561          181 :       for (auto &field : fields)
     562              :         {
     563           99 :           if (!field->get_field_type ()->is_zero_sized ())
     564           82 :             num_non_zst++;
     565              :         }
     566           82 :       if (num_non_zst > 1)
     567              :         {
     568            1 :           rust_error_at (struct_decl.get_locus (), ErrorCode::E0690,
     569              :                          "transparent struct needs at most one field with "
     570              :                          "non-trivial size or alignment, but has %lu",
     571              :                          (unsigned long) num_non_zst);
     572            1 :           return;
     573              :         }
     574              :     }
     575              : 
     576              :   // its a single variant ADT
     577         2917 :   auto variant_type = struct_decl.is_unit_struct ()
     578         2917 :                         ? TyTy::VariantDef::VariantType::UNIT
     579         2125 :                         : TyTy::VariantDef::VariantType::STRUCT;
     580         2917 :   auto variant
     581         2917 :     = new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (),
     582         2917 :                             struct_decl.get_mappings ().get_defid (),
     583         2917 :                             struct_decl.get_identifier ().as_string (), ident,
     584         5834 :                             variant_type, tl::nullopt, std::move (fields));
     585         2917 :   type->get_variants ().push_back (variant);
     586         2917 :   if (!validate_adt_size (type))
     587              :     {
     588            3 :       infered = new TyTy::ErrorType (struct_decl.get_mappings ().get_hirid ());
     589            3 :       context->insert_type (struct_decl.get_mappings (), infered);
     590            3 :       return;
     591              :     }
     592         2914 :   infered = type;
     593              : 
     594         2914 :   context->get_variance_analysis_ctx ().add_type_constraints (*type);
     595         5846 : }
     596              : 
     597              : void
     598          807 : TypeCheckItem::visit (HIR::Enum &enum_decl)
     599              : {
     600          807 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
     601          807 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     602          807 :   if (enum_decl.has_generics ())
     603          303 :     resolve_generic_params (HIR::Item::ItemKind::Enum, enum_decl.get_locus (),
     604          303 :                             enum_decl.get_generic_params (), substitutions);
     605              : 
     606          807 :   TyTy::RegionConstraints region_constraints;
     607          807 :   ResolveWhereClauseItem::Resolve (enum_decl.get_where_clause (),
     608              :                                    region_constraints);
     609              : 
     610          807 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     611          807 :   CanonicalPath canonical_path
     612          807 :     = nr_ctx.to_canonical_path (enum_decl.get_mappings ().get_nodeid (),
     613          807 :                                 Resolver2_0::Namespace::Types);
     614              : 
     615          807 :   RustIdent ident{canonical_path, enum_decl.get_locus ()};
     616              : 
     617              :   // Process #[repr(X)] attribute, if any
     618          807 :   const AST::AttrVec &attrs = enum_decl.get_outer_attrs ();
     619          807 :   TyTy::ADTType::ReprOptions repr
     620          807 :     = parse_repr_options (attrs, enum_decl.get_locus ());
     621              : 
     622          807 :   std::vector<TyTy::VariantDef *> variants;
     623          807 :   auto *type = new TyTy::ADTType (
     624          807 :     enum_decl.get_mappings ().get_defid (),
     625          807 :     enum_decl.get_mappings ().get_hirid (),
     626          807 :     enum_decl.get_mappings ().get_hirid (),
     627          807 :     enum_decl.get_identifier ().as_string (), ident,
     628              :     TyTy::ADTType::ADTKind::ENUM, std::move (variants),
     629              :     std::move (substitutions), repr,
     630         1614 :     TyTy::SubstitutionArgumentMappings::empty (
     631          807 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     632         3228 :     region_constraints);
     633          807 :   context->insert_type (enum_decl.get_mappings (), type);
     634              : 
     635          807 :   int64_t discriminant_value = 0;
     636         2759 :   for (auto &variant : enum_decl.get_variants ())
     637              :     {
     638         1959 :       TyTy::VariantDef *field_type
     639         1959 :         = TypeCheckEnumItem::Resolve (*variant, discriminant_value);
     640         1952 :       if (field_type)
     641              :         {
     642         1950 :           discriminant_value++;
     643         1950 :           type->get_variants ().push_back (field_type);
     644              :         }
     645              :     }
     646              : 
     647              :   // Check for zero-variant enum compatibility
     648          800 :   if (enum_decl.is_zero_variant ())
     649              :     {
     650           40 :       if (repr.repr_kind == TyTy::ADTType::ReprKind::INT
     651           40 :           || repr.repr_kind == TyTy::ADTType::ReprKind::C)
     652              :         {
     653            2 :           rust_error_at (enum_decl.get_locus (),
     654              :                          "unsupported representation for zero-variant enum");
     655            2 :           return;
     656              :         }
     657              :     }
     658              : 
     659          798 :   if (!validate_adt_size (type))
     660              :     {
     661            0 :       infered = new TyTy::ErrorType (enum_decl.get_mappings ().get_hirid ());
     662            0 :       context->insert_type (enum_decl.get_mappings (), infered);
     663            0 :       return;
     664              :     }
     665          798 :   infered = type;
     666              : 
     667          798 :   context->get_variance_analysis_ctx ().add_type_constraints (*type);
     668              : 
     669          798 :   if (flag_unused_check_2_0)
     670            3 :     check_repr_c_enum_discriminants (Compile::Context::get (), type);
     671         1600 : }
     672              : 
     673              : void
     674          112 : TypeCheckItem::visit (HIR::Union &union_decl)
     675              : {
     676          112 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
     677          112 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     678          112 :   if (union_decl.has_generics ())
     679           82 :     resolve_generic_params (HIR::Item::ItemKind::Union, union_decl.get_locus (),
     680           82 :                             union_decl.get_generic_params (), substitutions);
     681              : 
     682          112 :   TyTy::RegionConstraints region_constraints;
     683          112 :   ResolveWhereClauseItem::Resolve (union_decl.get_where_clause (),
     684              :                                    region_constraints);
     685              : 
     686          112 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     687          112 :   CanonicalPath canonical_path
     688          112 :     = nr_ctx.to_canonical_path (union_decl.get_mappings ().get_nodeid (),
     689          112 :                                 Resolver2_0::Namespace::Types);
     690              : 
     691          112 :   RustIdent ident{canonical_path, union_decl.get_locus ()};
     692              : 
     693              :   // Process #[repr(X)] attribute, if any
     694          112 :   const AST::AttrVec &attrs = union_decl.get_outer_attrs ();
     695          112 :   TyTy::ADTType::ReprOptions repr
     696          112 :     = parse_repr_options (attrs, union_decl.get_locus ());
     697              : 
     698          112 :   std::vector<TyTy::VariantDef *> variants;
     699          112 :   auto *type = new TyTy::ADTType (
     700          112 :     union_decl.get_mappings ().get_defid (),
     701          112 :     union_decl.get_mappings ().get_hirid (),
     702          112 :     union_decl.get_mappings ().get_hirid (),
     703          112 :     union_decl.get_identifier ().as_string (), ident,
     704              :     TyTy::ADTType::ADTKind::UNION, std::move (variants),
     705              :     std::move (substitutions), repr,
     706          224 :     TyTy::SubstitutionArgumentMappings::empty (
     707          112 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     708          448 :     region_constraints);
     709          112 :   context->insert_type (union_decl.get_mappings (), type);
     710              : 
     711          112 :   std::vector<TyTy::StructFieldType *> fields;
     712          430 :   for (auto &variant : union_decl.get_variants ())
     713              :     {
     714          318 :       TyTy::BaseType *variant_type
     715          318 :         = TypeCheckType::Resolve (variant.get_field_type ());
     716          318 :       auto *ty_variant
     717          318 :         = new TyTy::StructFieldType (variant.get_mappings ().get_hirid (),
     718          318 :                                      variant.get_field_name ().as_string (),
     719          636 :                                      variant_type, variant.get_locus ());
     720          318 :       fields.push_back (ty_variant);
     721          318 :       context->insert_type (variant.get_mappings (),
     722              :                             ty_variant->get_field_type ());
     723              :     }
     724              : 
     725          112 :   auto variant
     726          112 :     = new TyTy::VariantDef (union_decl.get_mappings ().get_hirid (),
     727          112 :                             union_decl.get_mappings ().get_defid (),
     728          112 :                             union_decl.get_identifier ().as_string (), ident,
     729          224 :                             TyTy::VariantDef::VariantType::STRUCT, tl::nullopt,
     730          224 :                             std::move (fields));
     731          112 :   type->get_variants ().push_back (variant);
     732          112 :   if (!validate_adt_size (type))
     733              :     {
     734            0 :       infered = new TyTy::ErrorType (union_decl.get_mappings ().get_hirid ());
     735            0 :       context->insert_type (union_decl.get_mappings (), infered);
     736            0 :       return;
     737              :     }
     738              : 
     739          112 :   infered = type;
     740              : 
     741          112 :   context->get_variance_analysis_ctx ().add_type_constraints (*type);
     742          224 : }
     743              : 
     744              : void
     745           76 : TypeCheckItem::visit (HIR::StaticItem &var)
     746              : {
     747           76 :   TyTy::BaseType *type = TypeCheckType::Resolve (var.get_type ());
     748           76 :   TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (var.get_expr ());
     749              : 
     750           76 :   TyTy::BaseType *unified = type;
     751           76 :   if (!expr_type->is<TyTy::ErrorType> ())
     752          152 :     unified = coercion_site (
     753           76 :       var.get_mappings ().get_hirid (),
     754           76 :       TyTy::TyWithLocation (type, var.get_type ().get_locus ()),
     755           76 :       TyTy::TyWithLocation (expr_type, var.get_expr ().get_locus ()),
     756              :       var.get_locus ());
     757           76 :   context->insert_type (var.get_mappings (), unified);
     758           76 :   infered = unified;
     759           76 : }
     760              : 
     761              : void
     762          546 : TypeCheckItem::visit (HIR::ConstantItem &constant)
     763              : {
     764          546 :   TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
     765          546 :   context->push_const_context ();
     766          546 :   TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
     767          539 :   context->pop_const_context ();
     768              : 
     769              :   // the declared type stands if the initializer failed
     770          539 :   TyTy::BaseType *unified = type;
     771          539 :   if (!expr_type->is<TyTy::ErrorType> ())
     772         1042 :     unified = coercion_site (
     773          521 :       constant.get_mappings ().get_hirid (),
     774          521 :       TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
     775          521 :       TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
     776              :       constant.get_locus ());
     777          539 :   context->insert_type (constant.get_mappings (), unified);
     778          539 :   infered = unified;
     779          539 : }
     780              : 
     781              : void
     782         9272 : TypeCheckItem::visit (HIR::ImplBlock &impl_block)
     783              : {
     784         9272 :   if (impl_block.has_trait_ref ())
     785         8214 :     resolve_trait_impl_block (impl_block);
     786              :   else
     787         1058 :     resolve_impl_block (impl_block);
     788         9229 : }
     789              : 
     790              : void
     791         1058 : TypeCheckItem::resolve_impl_block (HIR::ImplBlock &impl_block)
     792              : {
     793         1058 :   auto binder_pin = context->push_clean_lifetime_resolver (true);
     794              : 
     795         1058 :   bool failed_flag = false;
     796         1058 :   auto result = resolve_impl_block_substitutions (impl_block, failed_flag);
     797         1058 :   if (failed_flag)
     798              :     {
     799            3 :       infered = new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
     800            3 :       return;
     801              :     }
     802         1055 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     803         1055 :     = std::move (result.first);
     804         1055 :   TyTy::RegionConstraints region_constraints = std::move (result.second);
     805              : 
     806         1055 :   TyTy::BaseType *self = resolve_impl_block_self (impl_block);
     807              : 
     808         4277 :   for (auto &impl_item : impl_block.get_impl_items ())
     809         3222 :     TypeCheckImplItem::Resolve (impl_block, *impl_item, self, substitutions);
     810         1058 : }
     811              : 
     812              : void
     813         8214 : TypeCheckItem::resolve_trait_impl_block (HIR::ImplBlock &impl_block)
     814              : {
     815         8214 :   auto binder_pin = context->push_clean_lifetime_resolver (true);
     816              : 
     817         8214 :   bool failed_flag = false;
     818         8214 :   auto result = resolve_impl_block_substitutions (impl_block, failed_flag);
     819         8213 :   if (failed_flag)
     820              :     {
     821            1 :       infered = new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
     822            1 :       return;
     823              :     }
     824         8212 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     825         8212 :     = std::move (result.first);
     826         8212 :   TyTy::RegionConstraints region_constraints = std::move (result.second);
     827              : 
     828         8212 :   auto specified_bound = TyTy::TypeBoundPredicate::error ();
     829         8212 :   TraitReference *trait_reference = &TraitReference::error_node ();
     830         8212 :   TyTy::BaseType *self = resolve_impl_block_self (impl_block);
     831              : 
     832         8212 :   HIR::TypePath &ref = impl_block.get_trait_ref ();
     833         8212 :   trait_reference = TraitResolver::Resolve (ref);
     834         8212 :   if (trait_reference->is_error ())
     835            3 :     return;
     836              : 
     837              :   // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs
     838              :   // for example
     839        16418 :   specified_bound = get_predicate_from_bound (ref, impl_block.get_type (),
     840         8209 :                                               impl_block.get_polarity ());
     841              : 
     842              :   // need to check that if this specified bound has super traits does this
     843              :   // Self implement them?
     844         8209 :   specified_bound.validate_type_implements_super_traits (
     845         8209 :     *self, impl_block.get_type (), impl_block.get_trait_ref ());
     846              : 
     847         8209 :   std::map<DefId, AssocTypeEntry> assoc_types_by_trait_item;
     848         8209 :   std::vector<const TraitItemReference *> trait_item_refs;
     849         8209 :   ResolveImplTraitAssociatedTypes (context, impl_block, specified_bound, self,
     850              :                                    substitutions, assoc_types_by_trait_item,
     851              :                                    trait_item_refs);
     852              : 
     853         8209 :   ImplTraitContextFrame frame{trait_reference, self,
     854         8209 :                               std::move (assoc_types_by_trait_item)};
     855         8209 :   ImplTraitFrameGuard guard (frame);
     856        17638 :   for (auto &impl_item : impl_block.get_impl_items ())
     857              :     {
     858         9471 :       bool is_type_alias = impl_item->get_impl_item_type ()
     859         9471 :                            == HIR::ImplItem::ImplItemType::TYPE_ALIAS;
     860         9471 :       if (is_type_alias)
     861         2666 :         continue;
     862              : 
     863         6805 :       auto trait_item_ref
     864         6805 :         = TypeCheckImplItemWithTrait::Resolve (impl_block, *impl_item, self,
     865              :                                                specified_bound, substitutions);
     866         6763 :       if (!trait_item_ref.is_error ())
     867         6758 :         trait_item_refs.push_back (trait_item_ref.get_raw_item ());
     868              :     }
     869              : 
     870         8167 :   validate_trait_impl_block (specified_bound, trait_item_refs, trait_reference,
     871              :                              impl_block, self, substitutions);
     872              : 
     873         8167 :   AssociatedImplTrait associated (trait_reference, specified_bound, &impl_block,
     874         8167 :                                   self, frame);
     875         8167 :   context->insert_associated_trait_impl (
     876         8167 :     impl_block.get_mappings ().get_hirid (), std::move (associated));
     877         8167 :   context->insert_associated_impl_mapping (
     878         8167 :     trait_reference->get_mappings ().get_hirid (), self,
     879         8167 :     impl_block.get_mappings ().get_hirid ());
     880        16341 : }
     881              : 
     882              : void
     883        22833 : TypeCheckItem::ResolveImplTraitAssociatedTypes (
     884              :   TypeCheckContext *context, HIR::ImplBlock &impl_block,
     885              :   TyTy::TypeBoundPredicate &specified_bound, TyTy::BaseType *self,
     886              :   std::vector<TyTy::SubstitutionParamMapping> &substitutions,
     887              :   std::map<DefId, AssocTypeEntry> &assoc_types_by_trait_item,
     888              :   std::vector<const TraitItemReference *> &trait_item_refs)
     889              : {
     890        61021 :   for (auto &impl_item : impl_block.get_impl_items ())
     891              :     {
     892        38223 :       bool is_type_alias = impl_item->get_impl_item_type ()
     893        38223 :                            == HIR::ImplItem::ImplItemType::TYPE_ALIAS;
     894        38223 :       if (!is_type_alias)
     895        23279 :         continue;
     896              : 
     897        14944 :       rust_debug_loc (impl_item->get_locus (), "RESOLVE ITEM 1");
     898        14944 :       self->debug ();
     899        14944 :       auto trait_item_ref
     900        14944 :         = TypeCheckImplItemWithTrait::Resolve (impl_block, *impl_item, self,
     901              :                                                specified_bound, substitutions);
     902        14909 :       rust_debug_loc (impl_item->get_locus (), "RESOLVE ITEM 2");
     903        14909 :       if (!trait_item_ref.is_error ())
     904              :         {
     905        14909 :           const TraitItemReference *tiref = trait_item_ref.get_raw_item ();
     906        14909 :           trait_item_refs.push_back (tiref);
     907              : 
     908        14909 :           DefId impl_item_defid = impl_item->get_impl_mappings ().get_defid ();
     909        14909 :           DefId trait_item_defid = tiref->get_mappings ().get_defid ();
     910        14909 :           TyTy::BaseType *impl_item_ty;
     911              : 
     912        14909 :           bool ok = context->lookup_type (
     913        14909 :             impl_item->get_impl_mappings ().get_hirid (), &impl_item_ty);
     914        14909 :           rust_assert (ok);
     915              : 
     916        14909 :           AssocTypeEntry entry
     917        14909 :             = {trait_item_defid, impl_item_defid, impl_item_ty};
     918        14909 :           assoc_types_by_trait_item[trait_item_defid] = std::move (entry);
     919              :         }
     920              :     }
     921        22798 : }
     922              : 
     923              : TyTy::BaseType *
     924            1 : TypeCheckItem::resolve_impl_item (HIR::ImplBlock &impl_block,
     925              :                                   HIR::ImplItem &item)
     926              : {
     927            1 :   bool failed_flag = false;
     928            1 :   auto result = resolve_impl_block_substitutions (impl_block, failed_flag);
     929            1 :   if (failed_flag)
     930              :     {
     931            1 :       return new TyTy::ErrorType (impl_block.get_mappings ().get_hirid ());
     932              :     }
     933              : 
     934            0 :   std::vector<TyTy::SubstitutionParamMapping> substitutions
     935            0 :     = std::move (result.first);
     936            0 :   TyTy::RegionConstraints region_constraints = std::move (result.second);
     937              : 
     938            0 :   TyTy::BaseType *self = resolve_impl_block_self (impl_block);
     939              : 
     940            0 :   return TypeCheckImplItem::Resolve (impl_block, item, self, substitutions);
     941            1 : }
     942              : 
     943              : TyTy::FnType *
     944         7382 : TypeCheckItem::resolve_function_signature (HIR::Function &function)
     945              : {
     946         7382 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
     947         7382 :   if (function.has_generics ())
     948          700 :     resolve_generic_params (HIR::Item::ItemKind::Function,
     949              :                             function.get_locus (),
     950          700 :                             function.get_generic_params (), substitutions);
     951              : 
     952         7382 :   TyTy::RegionConstraints region_constraints;
     953         7382 :   ResolveWhereClauseItem::Resolve (function.get_where_clause (),
     954              :                                    region_constraints);
     955              : 
     956         7382 :   TyTy::BaseType *ret_type = nullptr;
     957         7382 :   if (!function.has_function_return_type ())
     958         3591 :     ret_type = TyTy::TupleType::get_unit_type ();
     959              :   else
     960              :     {
     961         3791 :       auto resolved = TypeCheckType::Resolve (function.get_return_type ());
     962         3791 :       if (resolved->get_kind () == TyTy::TypeKind::ERROR)
     963              :         return nullptr;
     964              : 
     965         3787 :       ret_type = resolved->clone ();
     966         3787 :       ret_type->set_ref (
     967         3787 :         function.get_return_type ().get_mappings ().get_hirid ());
     968              :     }
     969              : 
     970         7378 :   std::vector<TyTy::FnParam> params;
     971         9741 :   for (auto &param : function.get_function_params ())
     972              :     {
     973              :       // get the name as well required for later on
     974         2363 :       auto param_tyty = TypeCheckType::Resolve (param.get_type ());
     975         2363 :       context->insert_type (param.get_mappings (), param_tyty);
     976         2363 :       TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
     977         2363 :       params.emplace_back (param.get_param_name ().clone_pattern (),
     978              :                            param_tyty);
     979              :     }
     980              : 
     981         7378 :   auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
     982              : 
     983         7378 :   CanonicalPath path
     984         7378 :     = nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid (),
     985         7378 :                                 Resolver2_0::Namespace::Values);
     986              : 
     987         7378 :   RustIdent ident{path, function.get_locus ()};
     988              : 
     989         7378 :   auto fn_type = new TyTy::FnType (
     990         7378 :     function.get_mappings ().get_hirid (),
     991         7378 :     function.get_mappings ().get_defid (),
     992         7378 :     function.get_function_name ().as_string (), ident,
     993              :     TyTy::FnType::FNTYPE_DEFAULT_FLAGS, ABI::RUST, std::move (params), ret_type,
     994              :     std::move (substitutions),
     995        14756 :     TyTy::SubstitutionArgumentMappings::empty (
     996         7378 :       context->get_lifetime_resolver ().get_num_bound_regions ()),
     997        29512 :     region_constraints);
     998              : 
     999         7378 :   context->insert_type (function.get_mappings (), fn_type);
    1000              : 
    1001         7378 :   return fn_type;
    1002        14760 : }
    1003              : 
    1004              : void
    1005         7382 : TypeCheckItem::visit (HIR::Function &function)
    1006              : {
    1007         7382 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
    1008              : 
    1009         7382 :   TyTy::BaseType *resolved = nullptr;
    1010         7382 :   TyTy::FnType *resolved_fn_type = nullptr;
    1011         7382 :   if (context->lookup_type (function.get_mappings ().get_hirid (), &resolved))
    1012              :     {
    1013          189 :       if (resolved->get_kind () != TyTy::TypeKind::FNDEF)
    1014              :         return;
    1015          189 :       resolved_fn_type = static_cast<TyTy::FnType *> (resolved);
    1016              :     }
    1017              :   else
    1018         7193 :     resolved_fn_type = resolve_function_signature (function);
    1019              : 
    1020         7382 :   if (resolved_fn_type == nullptr)
    1021              :     return;
    1022              : 
    1023              :   // Mark the body as claimed before resolving it.  Recursive queries from the
    1024              :   // body must reuse the cached signature rather than re-entering this body.
    1025         7378 :   context->clear_function_body_pending (resolved_fn_type->get_id ());
    1026              : 
    1027              :   // need to get the return type from this
    1028         7378 :   auto expected_ret_tyty = resolved_fn_type->get_return_type ();
    1029         7378 :   context->push_return_type (TypeCheckContextItem (&function),
    1030              :                              expected_ret_tyty);
    1031              : 
    1032         7378 :   context->switch_to_fn_body ();
    1033         7378 :   auto block_expr_ty = TypeCheckExpr::Resolve (function.get_definition ());
    1034              : 
    1035              :   // emit check for
    1036              :   // error[E0121]: the type placeholder `_` is not allowed within types on item
    1037         7378 :   const auto placeholder = expected_ret_tyty->contains_infer ();
    1038         7378 :   if (placeholder != nullptr && function.has_return_type ())
    1039              :     {
    1040              :       // FIXME
    1041              :       // this will be a great place for the Default Hir Visitor we want to
    1042              :       // grab the locations of the placeholders (HIR::InferredType) their
    1043              :       // location, for now maybe we can use their hirid to lookup the location
    1044            3 :       location_t placeholder_locus
    1045            3 :         = mappings.lookup_location (placeholder->get_ref ());
    1046            3 :       location_t type_locus = function.get_return_type ().get_locus ();
    1047            3 :       rich_location r (line_table, placeholder_locus);
    1048              : 
    1049            3 :       bool have_expected_type
    1050            6 :         = block_expr_ty != nullptr && !block_expr_ty->is<TyTy::ErrorType> ();
    1051            3 :       if (!have_expected_type)
    1052              :         {
    1053            0 :           r.add_range (type_locus);
    1054              :         }
    1055              :       else
    1056              :         {
    1057            3 :           std::string fixit
    1058            3 :             = "replace with the correct type " + block_expr_ty->get_name ();
    1059            3 :           r.add_fixit_replace (type_locus, fixit.c_str ());
    1060            3 :         }
    1061              : 
    1062            3 :       rust_error_at (r, ErrorCode::E0121,
    1063              :                      "the type placeholder %<_%> is not allowed within types "
    1064              :                      "on item signatures");
    1065            3 :     }
    1066              : 
    1067         7378 :   location_t fn_return_locus = function.has_function_return_type ()
    1068         7378 :                                  ? function.get_return_type ().get_locus ()
    1069         3591 :                                  : function.get_locus ();
    1070        14756 :   coercion_site (function.get_definition ().get_mappings ().get_hirid (),
    1071         7378 :                  TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus),
    1072         7378 :                  TyTy::TyWithLocation (block_expr_ty),
    1073         7378 :                  function.get_definition ().get_locus ());
    1074              : 
    1075         7378 :   context->pop_return_type ();
    1076              : 
    1077         7378 :   infered = resolved_fn_type;
    1078         7382 : }
    1079              : 
    1080              : void
    1081         1421 : TypeCheckItem::visit (HIR::Module &module)
    1082              : {
    1083         7231 :   for (auto &item : module.get_items ())
    1084         5817 :     TypeCheckItem::Resolve (*item);
    1085         1414 : }
    1086              : 
    1087              : void
    1088         4506 : TypeCheckItem::visit (HIR::Trait &trait)
    1089              : {
    1090         4506 :   infered = resolve_trait (trait, true);
    1091         4506 : }
    1092              : 
    1093              : TyTy::BaseType *
    1094         4918 : TypeCheckItem::resolve_trait (HIR::Trait &trait, bool resolve_bodies)
    1095              : {
    1096         4918 :   auto lifetime_pin = context->push_clean_lifetime_resolver ();
    1097              : 
    1098         4918 :   if (trait.has_type_param_bounds ())
    1099              :     {
    1100         1971 :       for (auto &tp_bound : trait.get_type_param_bounds ())
    1101              :         {
    1102         1061 :           if (tp_bound.get ()->get_bound_type ()
    1103              :               == HIR::TypeParamBound::BoundType::TRAITBOUND)
    1104              :             {
    1105         1061 :               HIR::TraitBound &tb
    1106         1061 :                 = static_cast<HIR::TraitBound &> (*tp_bound.get ());
    1107         1061 :               if (tb.get_polarity () == BoundPolarity::AntiBound)
    1108              :                 {
    1109            1 :                   rust_error_at (tb.get_locus (),
    1110              :                                  "%<?Trait%> is not permitted in supertraits");
    1111              :                 }
    1112              :             }
    1113              :         }
    1114              :     }
    1115              : 
    1116         4918 :   TraitReference *trait_ref = TraitResolver::Resolve (trait);
    1117         4918 :   if (trait_ref->is_error ())
    1118              :     {
    1119            7 :       infered = new TyTy::ErrorType (trait.get_mappings ().get_hirid ());
    1120            7 :       return infered;
    1121              :     }
    1122              : 
    1123         4911 :   if (resolve_bodies)
    1124         4500 :     trait_ref->resolve_default_function_bodies ();
    1125              : 
    1126         4911 :   RustIdent ident{CanonicalPath::create_empty (), trait.get_locus ()};
    1127         4911 :   return new TyTy::DynamicObjectType (
    1128         4911 :     trait.get_mappings ().get_hirid (), ident,
    1129              :     {TyTy::TypeBoundPredicate (*trait_ref, BoundPolarity::RegularBound,
    1130         9822 :                                trait.get_locus ())});
    1131         4918 : }
    1132              : 
    1133              : void
    1134         1840 : TypeCheckItem::visit (HIR::ExternBlock &extern_block)
    1135              : {
    1136         5077 :   for (auto &item : extern_block.get_extern_items ())
    1137              :     {
    1138         3237 :       TypeCheckTopLevelExternItem::Resolve (*item, extern_block);
    1139              :     }
    1140         1840 : }
    1141              : 
    1142              : void
    1143            0 : TypeCheckItem::visit (HIR::ExternCrate &extern_crate)
    1144              : {
    1145            0 :   if (extern_crate.references_self ())
    1146              :     return;
    1147              : 
    1148            0 :   auto &crate_mappings = Analysis::Mappings::get ().crate;
    1149            0 :   CrateNum num
    1150            0 :     = crate_mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
    1151            0 :         .value ();
    1152            0 :   HIR::Crate &crate = mappings.get_hir_crate (num);
    1153              : 
    1154            0 :   CrateNum saved_crate_num = crate_mappings.get_current_crate ();
    1155            0 :   crate_mappings.set_current_crate (num);
    1156            0 :   for (auto &item : crate.get_items ())
    1157            0 :     TypeCheckItem::Resolve (*item);
    1158            0 :   crate_mappings.set_current_crate (saved_crate_num);
    1159              : }
    1160              : 
    1161              : std::pair<std::vector<TyTy::SubstitutionParamMapping>, TyTy::RegionConstraints>
    1162        74792 : TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block,
    1163              :                                                  bool &failure_flag)
    1164              : {
    1165        74792 :   std::vector<TyTy::SubstitutionParamMapping> substitutions;
    1166        74792 :   if (impl_block.has_generics ())
    1167        21540 :     resolve_generic_params (HIR::Item::ItemKind::Impl, impl_block.get_locus (),
    1168        21540 :                             impl_block.get_generic_params (), substitutions);
    1169              : 
    1170        74792 :   TyTy::RegionConstraints region_constraints;
    1171        74792 :   ResolveWhereClauseItem::Resolve (impl_block.get_where_clause (),
    1172              :                                    region_constraints);
    1173              : 
    1174        74792 :   auto specified_bound = TyTy::TypeBoundPredicate::error ();
    1175        74792 :   TraitReference *trait_reference = &TraitReference::error_node ();
    1176        74792 :   if (impl_block.has_trait_ref ())
    1177              :     {
    1178        69527 :       auto &ref = impl_block.get_trait_ref ();
    1179        69527 :       trait_reference = TraitResolver::Resolve (ref);
    1180        69527 :       if (!trait_reference->is_error ())
    1181              :         {
    1182              :           // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs
    1183              :           // for example
    1184        69524 :           specified_bound
    1185       139026 :             = get_predicate_from_bound (ref, impl_block.get_type (),
    1186        69502 :                                         impl_block.get_polarity ());
    1187              :         }
    1188              :     }
    1189              : 
    1190        74770 :   TyTy::BaseType *self = TypeCheckType::Resolve (impl_block.get_type ());
    1191        74770 :   if (self->is<TyTy::ErrorType> ())
    1192              :     {
    1193              :       // we cannot check for unconstrained type arguments when the Self type is
    1194              :       // not resolved it will just add extra errors that dont help as well as
    1195              :       // the case where this could just be a recursive type query that should
    1196              :       // fail and will work later on anyway
    1197            1 :       return {substitutions, region_constraints};
    1198              :     }
    1199              : 
    1200              :   // inherit the bounds
    1201        74769 :   if (!specified_bound.is_error ())
    1202        69499 :     self->inherit_bound (specified_bound);
    1203              : 
    1204              :   // check for any unconstrained type-params
    1205        74769 :   const TyTy::SubstitutionArgumentMappings trait_constraints
    1206        74769 :     = specified_bound.get_substitution_arguments ();
    1207        74769 :   const TyTy::SubstitutionArgumentMappings impl_constraints
    1208        74769 :     = GetUsedSubstArgs::From (self);
    1209              : 
    1210        74769 :   failure_flag = check_for_unconstrained (substitutions, trait_constraints,
    1211              :                                           impl_constraints, self);
    1212              : 
    1213        74769 :   return {substitutions, region_constraints};
    1214       149540 : }
    1215              : 
    1216              : TyTy::BaseType *
    1217        59145 : TypeCheckItem::resolve_impl_block_self (HIR::ImplBlock &impl_block)
    1218              : {
    1219        59145 :   return TypeCheckType::Resolve (impl_block.get_type ());
    1220              : }
    1221              : 
    1222              : bool
    1223          385 : TypeCheckItem::validate_repr_simd (
    1224              :   const std::vector<TyTy::StructFieldType *> &fields, location_t locus)
    1225              : {
    1226          385 :   if (fields.empty ())
    1227              :     {
    1228            0 :       rust_error_at (locus, ErrorCode::E0075, "SIMD vector cannot be empty");
    1229            0 :       return false;
    1230              :     }
    1231              : 
    1232              :   // in 1.49, repr simd assumes all fields are same type with its size
    1233              :   // being power-of-two.
    1234              :   //
    1235              :   // TODO update this typecheck to make repr simd take in a single field
    1236              :   // of an array instead when we move past 1.49. Relevant Rust github
    1237              :   // issues/PRs:
    1238              :   // - https://github.com/rust-lang/compiler-team/issues/621
    1239              :   // - https://github.com/rust-lang/rust/pull/78863 (implemented
    1240              :   //   for 1.50.0)
    1241              : 
    1242          385 :   TyTy::BaseType *first_field_ty = fields.at (0)->get_field_type ();
    1243          385 :   TyTy::TypeKind ty_kind = first_field_ty->get_kind ();
    1244          385 :   bool fields_are_same_type = true;
    1245              : 
    1246          385 :   switch (ty_kind)
    1247              :     {
    1248          172 :     case TyTy::TypeKind::INT:
    1249          172 :       {
    1250          172 :         auto int_ty = static_cast<TyTy::IntType *> (first_field_ty);
    1251          172 :         auto int_kind = int_ty->get_int_kind ();
    1252         1457 :         for (const auto field : fields)
    1253              :           {
    1254         1285 :             if (field->get_field_type ()->get_kind () != ty_kind)
    1255              :               {
    1256              :                 fields_are_same_type = false;
    1257              :                 break;
    1258              :               }
    1259         1285 :             auto field_int_ty
    1260         1285 :               = static_cast<TyTy::IntType *> (field->get_field_type ());
    1261         1285 :             if (field_int_ty->get_int_kind () != int_kind)
    1262              :               {
    1263              :                 fields_are_same_type = false;
    1264              :                 break;
    1265              :               }
    1266              :           }
    1267              :         break;
    1268              :       }
    1269          120 :     case TyTy::TypeKind::UINT:
    1270          120 :       {
    1271          120 :         auto uint_ty = static_cast<TyTy::UintType *> (first_field_ty);
    1272          120 :         auto uint_kind = uint_ty->get_uint_kind ();
    1273         1083 :         for (const auto field : fields)
    1274              :           {
    1275          963 :             if (field->get_field_type ()->get_kind () != ty_kind)
    1276              :               {
    1277              :                 fields_are_same_type = false;
    1278              :                 break;
    1279              :               }
    1280          963 :             auto field_uint_ty
    1281          963 :               = static_cast<TyTy::UintType *> (field->get_field_type ());
    1282          963 :             if (field_uint_ty->get_uint_kind () != uint_kind)
    1283              :               {
    1284              :                 fields_are_same_type = false;
    1285              :                 break;
    1286              :               }
    1287              :           }
    1288              :         break;
    1289              :       }
    1290           92 :     case TyTy::TypeKind::FLOAT:
    1291           92 :       {
    1292           92 :         auto float_ty = static_cast<TyTy::FloatType *> (first_field_ty);
    1293           92 :         auto float_kind = float_ty->get_float_kind ();
    1294          712 :         for (const auto field : fields)
    1295              :           {
    1296          621 :             if (field->get_field_type ()->get_kind () != ty_kind)
    1297              :               {
    1298              :                 fields_are_same_type = false;
    1299              :                 break;
    1300              :               }
    1301          620 :             auto field_float_ty
    1302          620 :               = static_cast<TyTy::FloatType *> (field->get_field_type ());
    1303          620 :             if (field_float_ty->get_float_kind () != float_kind)
    1304              :               {
    1305              :                 fields_are_same_type = false;
    1306              :                 break;
    1307              :               }
    1308              :           }
    1309              :         break;
    1310              :       }
    1311            1 :     default:
    1312            1 :       rust_error_at (locus, ErrorCode::E0077,
    1313              :                      "SIMD vector element type should be a primitive scalar");
    1314            1 :       return false;
    1315              :     }
    1316              : 
    1317          384 :   if (!fields_are_same_type)
    1318              :     {
    1319            1 :       rust_error_at (locus, "SIMD struct fields should be of the same type");
    1320            1 :       return false;
    1321              :     }
    1322              : 
    1323              :   // check whether field count is power of 2
    1324          383 :   size_t field_count = fields.size ();
    1325          383 :   if ((field_count & (field_count - 1)) != 0)
    1326              :     {
    1327            1 :       rust_error_at (locus, "Size of SIMD struct must be a power of 2");
    1328            1 :       return false;
    1329              :     }
    1330              :   return true;
    1331              : }
    1332              : 
    1333              : } // namespace Resolver
    1334              : } // 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.