LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-tyty-bounds.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 503 474
Test Date: 2026-11-14 16:39:50 Functions: 90.9 % 55 50
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2021-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-full-decls.h"
      20              : #include "rust-hir-type-bounds.h"
      21              : #include "rust-hir-trait-resolve.h"
      22              : #include "rust-substitution-mapper.h"
      23              : #include "rust-hir-trait-resolve.h"
      24              : #include "rust-type-util.h"
      25              : #include "rust-tyty.h"
      26              : 
      27              : namespace Rust {
      28              : namespace Resolver {
      29              : 
      30       239357 : TypeBoundsProbe::TypeBoundsProbe (TyTy::BaseType *receiver,
      31              :                                   const HIR::Trait *specified_trait)
      32       239357 :   : TypeCheckBase (), receiver (receiver), specified_trait (specified_trait)
      33       239357 : {}
      34              : 
      35              : std::vector<std::pair<TraitReference *, HIR::ImplBlock *>>
      36       239357 : TypeBoundsProbe::Probe (TyTy::BaseType *receiver,
      37              :                         const HIR::Trait *specified_trait)
      38              : {
      39       239357 :   TypeBoundsProbe probe (receiver, specified_trait);
      40       239357 :   probe.scan ();
      41       239336 :   return probe.trait_references;
      42       239336 : }
      43              : 
      44              : bool
      45           82 : TypeBoundsProbe::is_bound_satisfied_for_type (TyTy::BaseType *receiver,
      46              :                                               TraitReference *ref)
      47              : {
      48           82 :   for (auto &bound : receiver->get_specified_bounds ())
      49              :     {
      50            0 :       const TraitReference *b = bound.get ();
      51            0 :       if (b->is_equal (*ref))
      52           82 :         return true;
      53              :     }
      54              : 
      55           82 :   std::vector<std::pair<TraitReference *, HIR::ImplBlock *>> bounds
      56           82 :     = Probe (receiver, ref->get_hir_trait_ref ());
      57           82 :   for (auto &bound : bounds)
      58              :     {
      59           80 :       const TraitReference *b = bound.first;
      60           80 :       if (b->is_equal (*ref))
      61           82 :         return true;
      62              :     }
      63              : 
      64              :   return false;
      65           82 : }
      66              : 
      67              : bool
      68      4854516 : TypeBoundsProbe::process_impl_block (
      69              :   HirId id, HIR::ImplBlock *impl,
      70              :   std::vector<std::pair<HIR::TypePath *, HIR::ImplBlock *>>
      71              :     &possible_trait_paths)
      72              : {
      73              :   // we are filtering for trait-impl-blocks
      74      4854516 :   if (!impl->has_trait_ref ())
      75              :     return true;
      76              : 
      77              :   // can be recursive trait resolution
      78      4854516 :   HIR::Trait *t = TraitResolver::ResolveHirItem (impl->get_trait_ref ());
      79      4854516 :   if (t == nullptr)
      80              :     return true;
      81              : 
      82      4854516 :   HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid ();
      83      4854516 :   TyTy::BaseType *impl_type = nullptr;
      84      4854516 :   if (!query_type (impl_ty_id, &impl_type))
      85              :     return true;
      86              : 
      87      4853851 :   if (!types_compatable (TyTy::TyWithLocation (receiver),
      88      4853851 :                          TyTy::TyWithLocation (impl_type), impl->get_locus (),
      89              :                          false /*emit_errors*/, false /*check-bounds*/))
      90              :     return true;
      91              : 
      92        86297 :   possible_trait_paths.emplace_back (&impl->get_trait_ref (), impl);
      93        86297 :   return true;
      94              : }
      95              : 
      96              : void
      97       239357 : TypeBoundsProbe::scan ()
      98              : {
      99       239357 :   std::vector<std::pair<HIR::TypePath *, HIR::ImplBlock *>>
     100       239357 :     possible_trait_paths;
     101      5093873 :   auto process_impl = [&] (HirId id, HIR::ImplBlock *impl) mutable -> bool {
     102      4854516 :     return process_impl_block (id, impl, possible_trait_paths);
     103       239357 :   };
     104              : 
     105       239357 :   if (specified_trait == nullptr)
     106          487 :     mappings.iterate_trait_impl_blocks (process_impl);
     107              :   else
     108       238870 :     mappings.iterate_trait_impl_blocks (
     109       477726 :       specified_trait->get_mappings ().get_defid (), process_impl);
     110              : 
     111       324310 :   for (auto &path : possible_trait_paths)
     112              :     {
     113        84974 :       HIR::TypePath *trait_path = path.first;
     114        84974 :       TraitReference *trait_ref = TraitResolver::Resolve (*trait_path);
     115              : 
     116        84974 :       if (!trait_ref->is_error ())
     117        84974 :         trait_references.emplace_back (trait_ref, path.second);
     118              :     }
     119              : 
     120              :   // marker traits...
     121       239336 :   assemble_marker_builtins ();
     122              : 
     123              :   // add auto trait bounds, only the requested one when probing for a trait
     124       775406 :   for (auto *auto_trait : mappings.get_auto_traits ())
     125       536070 :     if (specified_trait == nullptr || specified_trait == auto_trait)
     126          172 :       add_trait_bound (auto_trait);
     127       239336 : }
     128              : 
     129              : void
     130       239336 : TypeBoundsProbe::assemble_marker_builtins ()
     131              : {
     132       239336 :   const TyTy::BaseType *raw = receiver->destructure ();
     133              : 
     134              :   // https://runrust.miraheze.org/wiki/Dynamically_Sized_Type
     135              :   // everything is sized except for:
     136              :   //
     137              :   //   1. dyn traits
     138              :   //   2. slices
     139              :   //   3. str
     140              :   //   4. ADT's which contain any of the above
     141              :   //   t. tuples which contain any of the above
     142       239336 :   switch (raw->get_kind ())
     143              :     {
     144       144612 :     case TyTy::ARRAY:
     145       144612 :     case TyTy::REF:
     146       144612 :     case TyTy::POINTER:
     147       144612 :     case TyTy::PARAM:
     148       144612 :     case TyTy::FNDEF:
     149       144612 :     case TyTy::BOOL:
     150       144612 :     case TyTy::CHAR:
     151       144612 :     case TyTy::INT:
     152       144612 :     case TyTy::UINT:
     153       144612 :     case TyTy::FLOAT:
     154       144612 :     case TyTy::USIZE:
     155       144612 :     case TyTy::ISIZE:
     156       144612 :     case TyTy::INFER:
     157       144612 :     case TyTy::NEVER:
     158       144612 :     case TyTy::PLACEHOLDER:
     159       144612 :     case TyTy::PROJECTION:
     160       144612 :     case TyTy::OPAQUE:
     161       144612 :       assemble_builtin_candidate (LangItem::Kind::SIZED);
     162       144612 :       break;
     163              : 
     164          336 :     case TyTy::FNPTR:
     165          336 :     case TyTy::CLOSURE:
     166          336 :       assemble_builtin_candidate (LangItem::Kind::SIZED);
     167          336 :       assemble_builtin_candidate (LangItem::Kind::FN_ONCE);
     168          336 :       assemble_builtin_candidate (LangItem::Kind::FN);
     169          336 :       assemble_builtin_candidate (LangItem::Kind::FN_MUT);
     170          336 :       break;
     171              : 
     172              :       // FIXME str and slice need to be moved and test cases updated
     173         5739 :     case TyTy::SLICE:
     174         5739 :     case TyTy::STR:
     175         5739 :     case TyTy::TUPLE:
     176              :       // FIXME add extra checks
     177         5739 :       assemble_builtin_candidate (LangItem::Kind::SIZED);
     178         5739 :       break;
     179              : 
     180        88381 :     case TyTy::ADT:
     181        88381 :       {
     182        88381 :         const auto &adt = *static_cast<const TyTy::ADTType *> (raw);
     183        88381 :         if (adt.get_adt_kind () != TyTy::ADTType::ADTKind::EXTERN)
     184        88380 :           assemble_builtin_candidate (LangItem::Kind::SIZED);
     185              :       }
     186              :       break;
     187              : 
     188              :     case TyTy::CONST:
     189              :     case TyTy::DYNAMIC:
     190              :     case TyTy::ERROR:
     191              :       break;
     192              :     }
     193       239336 : }
     194              : 
     195              : void
     196       239309 : TypeBoundsProbe::add_trait_bound (HIR::Trait *trait)
     197              : {
     198       239309 :   auto trait_ref = TraitResolver::Resolve (*trait);
     199              : 
     200       324522 :   for (const auto &existing : trait_references)
     201        85217 :     if (existing.first->is_equal (*trait_ref))
     202           14 :       return;
     203              : 
     204       239305 :   if (receiver->get_kind () == TyTy::TypeKind::ADT)
     205              :     {
     206        88363 :       TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (receiver);
     207       181982 :       for (auto &variant : adt->get_variants ())
     208              :         {
     209       186671 :           for (auto &field : variant->get_fields ())
     210              :             {
     211        93052 :               TyTy::BaseType *field_ty = field->get_field_type ();
     212              : 
     213              :               // TODO: A loop guard is needed here to prevent infinite
     214              :               // recursion, but self-referential types currently crash due to
     215              :               // issue Rust-GCC/gccrs#4709. Therefore, I avoided adding an
     216              :               // untested guard for now.
     217              : 
     218        93052 :               if (!field_ty->satisfies_bound (
     219        93052 :                     TyTy::TypeBoundPredicate (*trait_ref,
     220              :                                               BoundPolarity::RegularBound,
     221        93052 :                                               UNDEF_LOCATION),
     222              :                     false))
     223           14 :                 return;
     224              :             }
     225              :         }
     226              :     }
     227              : 
     228       239295 :   trait_references.emplace_back (trait_ref, mappings.lookup_builtin_marker ());
     229              : }
     230              : 
     231              : void
     232       240075 : TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item)
     233              : {
     234       240075 :   auto lang_item_defined = mappings.lookup_lang_item (lang_item);
     235       240075 :   if (!lang_item_defined)
     236          938 :     return;
     237       239137 :   DefId &id = lang_item_defined.value ();
     238              : 
     239       239137 :   auto defid = mappings.lookup_defid (id);
     240       239137 :   if (!defid)
     241              :     return;
     242       239137 :   auto item = defid.value ();
     243              : 
     244       239137 :   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
     245       239137 :   HIR::Trait *trait = static_cast<HIR::Trait *> (item);
     246       239137 :   const TyTy::BaseType *raw = receiver->destructure ();
     247              : 
     248       239137 :   add_trait_bound (trait);
     249              : 
     250       239137 :   rust_debug ("Added builtin lang_item: %s for %s",
     251              :               LangItem::ToString (lang_item).c_str (),
     252              :               raw->get_name ().c_str ());
     253              : }
     254              : 
     255              : TraitReference *
     256        88409 : TypeCheckBase::resolve_trait_path (HIR::TypePath &path)
     257              : {
     258        88409 :   return TraitResolver::Resolve (path);
     259              : }
     260              : 
     261              : TyTy::TypeBoundPredicate
     262       130590 : TypeCheckBase::get_predicate_from_bound (
     263              :   HIR::TypePath &type_path,
     264              :   tl::optional<std::reference_wrapper<HIR::Type>> associated_self,
     265              :   BoundPolarity polarity, bool is_qualified_type_path, bool is_super_trait,
     266              :   bool defer_bindings)
     267              : {
     268       130590 :   TyTy::TypeBoundPredicate lookup = TyTy::TypeBoundPredicate::error ();
     269       130590 :   bool already_resolved
     270       130590 :     = context->lookup_predicate (type_path.get_mappings ().get_hirid (),
     271              :                                  &lookup);
     272       130590 :   if (already_resolved)
     273        42181 :     return lookup;
     274              : 
     275        88409 :   TraitReference *trait = resolve_trait_path (type_path);
     276        88409 :   if (trait->is_error ())
     277            9 :     return TyTy::TypeBoundPredicate::error ();
     278              : 
     279        88400 :   TyTy::TypeBoundPredicate predicate (*trait, polarity, type_path.get_locus ());
     280        88400 :   HIR::GenericArgs args
     281        88400 :     = HIR::GenericArgs::create_empty (type_path.get_locus ());
     282              : 
     283        88400 :   auto &final_seg = type_path.get_final_segment ();
     284        88400 :   switch (final_seg.get_type ())
     285              :     {
     286        43310 :     case HIR::TypePathSegment::SegmentType::GENERIC:
     287        43310 :       {
     288        43310 :         auto &final_generic_seg
     289              :           = static_cast<HIR::TypePathSegmentGeneric &> (final_seg);
     290        43310 :         if (final_generic_seg.has_generic_args ())
     291              :           {
     292        43310 :             args = final_generic_seg.get_generic_args ();
     293        43310 :             if (args.get_binding_args ().size () > 0
     294        43310 :                 && associated_self.has_value () && is_qualified_type_path)
     295              :               {
     296            1 :                 auto &binding_args = args.get_binding_args ();
     297              : 
     298            1 :                 rich_location r (line_table, args.get_locus ());
     299            2 :                 for (auto it = binding_args.begin (); it != binding_args.end ();
     300            1 :                      it++)
     301              :                   {
     302            1 :                     auto &arg = *it;
     303            1 :                     r.add_fixit_remove (arg.get_locus ());
     304              :                   }
     305            1 :                 rust_error_at (r, ErrorCode::E0229,
     306              :                                "associated type bindings are not allowed here");
     307            1 :               }
     308              :           }
     309              :       }
     310              :       break;
     311              : 
     312         2080 :     case HIR::TypePathSegment::SegmentType::FUNCTION:
     313         2080 :       {
     314         2080 :         auto &final_function_seg
     315              :           = static_cast<HIR::TypePathSegmentFunction &> (final_seg);
     316         2080 :         auto &fn = final_function_seg.get_function_path ();
     317              : 
     318              :         // we need to make implicit generic args which must be an implicit
     319              :         // Tuple
     320         2080 :         auto crate_num = mappings.crate.get_current_crate ();
     321         2080 :         HirId implicit_args_id = mappings.get_next_hir_id ();
     322         2080 :         Analysis::NodeMapping mapping (crate_num,
     323         2080 :                                        final_seg.get_mappings ().get_nodeid (),
     324         2080 :                                        implicit_args_id, UNKNOWN_LOCAL_DEFID);
     325              : 
     326         2080 :         std::vector<std::unique_ptr<HIR::Type>> params_copy;
     327         4350 :         for (auto &p : fn.get_params ())
     328              :           {
     329         2270 :             params_copy.push_back (p->clone_type ());
     330              :           }
     331              : 
     332         2080 :         std::vector<std::unique_ptr<HIR::Type>> inputs;
     333         2080 :         inputs.push_back (
     334         2080 :           std::make_unique<HIR::TupleType> (mapping, std::move (params_copy),
     335         2080 :                                             final_seg.get_locus ()));
     336              : 
     337         2080 :         HIR::TraitItem *trait_item
     338         2080 :           = mappings
     339         2080 :               .lookup_trait_item_lang_item (LangItem::Kind::FN_ONCE_OUTPUT,
     340              :                                             final_seg.get_locus ())
     341         2080 :               .value ();
     342              : 
     343         2080 :         std::vector<HIR::GenericArgsBinding> bindings;
     344              : 
     345         2080 :         if (fn.has_return_type () && !defer_bindings)
     346              :           {
     347         1030 :             TypeCheckType::Resolve (fn.get_return_type ());
     348              : 
     349         1030 :             location_t output_locus = fn.get_return_type ().get_locus ();
     350         3090 :             bindings.emplace_back (Identifier (trait_item->trait_identifier ()),
     351         2060 :                                    fn.get_return_type ().clone_type (),
     352              :                                    output_locus);
     353              :           }
     354              : 
     355         4160 :         args = HIR::GenericArgs ({} /* lifetimes */,
     356              :                                  std::move (inputs) /* type_args*/,
     357              :                                  std::move (bindings) /* binding_args*/,
     358         4160 :                                  {} /* const_args */, final_seg.get_locus ());
     359         2080 :       }
     360         2080 :       break;
     361              : 
     362              :     default:
     363              :       /* nothing to do */
     364              :       break;
     365              :     }
     366              : 
     367        88400 :   if (associated_self.has_value ())
     368              :     {
     369        86220 :       std::vector<std::unique_ptr<HIR::Type>> type_args;
     370        86220 :       type_args.push_back (std::unique_ptr<HIR::Type> (
     371        86220 :         associated_self.value ().get ().clone_type ()));
     372       129165 :       for (auto &arg : args.get_type_args ())
     373              :         {
     374        42945 :           type_args.push_back (std::unique_ptr<HIR::Type> (arg->clone_type ()));
     375              :         }
     376              : 
     377       258660 :       args = HIR::GenericArgs (args.get_lifetime_args (), std::move (type_args),
     378        86220 :                                args.get_binding_args (), args.get_const_args (),
     379       172440 :                                args.get_locus ());
     380        86220 :     }
     381              : 
     382        88400 :   if (defer_bindings)
     383         4405 :     args.get_binding_args ().clear ();
     384              : 
     385              :   // we try to apply generic arguments when they are non empty and or when the
     386              :   // predicate requires them so that we get the relevant Foo expects x number
     387              :   // arguments but got zero see test case rust/compile/traits12.rs
     388        88400 :   if (!args.is_empty () || predicate.requires_generic_args ())
     389              :     {
     390              :       // this is applying generic arguments to a trait reference
     391        86913 :       predicate.apply_generic_arguments (&args, associated_self.has_value (),
     392              :                                          is_super_trait);
     393              :     }
     394              : 
     395        88378 :   if (!defer_bindings)
     396        83973 :     context->insert_resolved_predicate (type_path.get_mappings ().get_hirid (),
     397              :                                         predicate);
     398              : 
     399        88378 :   return predicate;
     400       130568 : }
     401              : 
     402              : } // namespace Resolver
     403              : 
     404              : namespace TyTy {
     405              : 
     406       238172 : TypeBoundPredicate::TypeBoundPredicate (
     407              :   const Resolver::TraitReference &trait_reference, BoundPolarity polarity,
     408              :   location_t locus)
     409       238172 :   : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
     410       238172 :     reference (trait_reference.get_mappings ().get_defid ()), locus (locus),
     411       238172 :     error_flag (false), polarity (polarity),
     412       714516 :     super_traits (trait_reference.get_super_traits ())
     413              : {
     414       238172 :   rust_assert (!trait_reference.get_trait_substs ().empty ());
     415              : 
     416       238172 :   substitutions.clear ();
     417       528881 :   for (const auto &p : trait_reference.get_trait_substs ())
     418       290709 :     substitutions.push_back (p.clone ());
     419              : 
     420              :   // we setup a dummy implict self argument
     421       238172 :   SubstitutionArg placeholder_self (&get_substs ().front (), nullptr);
     422       238172 :   used_arguments.get_mappings ().push_back (placeholder_self);
     423       238172 : }
     424              : 
     425         5374 : TypeBoundPredicate::TypeBoundPredicate (
     426              :   DefId reference, std::vector<SubstitutionParamMapping> subst,
     427              :   BoundPolarity polarity, location_t locus)
     428         5374 :   : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
     429         5374 :     reference (reference), locus (locus), error_flag (false),
     430        10748 :     polarity (polarity)
     431              : {
     432         5374 :   rust_assert (!subst.empty ());
     433              : 
     434         5374 :   substitutions.clear ();
     435        11863 :   for (const auto &p : subst)
     436         6489 :     substitutions.push_back (p.clone ());
     437              : 
     438              :   // we setup a dummy implict self argument
     439         5374 :   SubstitutionArg placeholder_self (&get_substs ().front (), nullptr);
     440         5374 :   used_arguments.get_mappings ().push_back (placeholder_self);
     441         5374 : }
     442              : 
     443       239505 : TypeBoundPredicate::TypeBoundPredicate (mark_is_error)
     444       239505 :   : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
     445       239505 :     reference (UNKNOWN_DEFID), locus (UNDEF_LOCATION), error_flag (true),
     446       239505 :     polarity (BoundPolarity::RegularBound)
     447       239505 : {}
     448              : 
     449      7319789 : TypeBoundPredicate::TypeBoundPredicate (const TypeBoundPredicate &other)
     450      7319789 :   : SubstitutionRef ({}, SubstitutionArgumentMappings::empty (), {}),
     451      7319789 :     reference (other.reference), locus (other.locus),
     452      7319789 :     error_flag (other.error_flag), polarity (other.polarity),
     453     14639578 :     super_traits (other.super_traits)
     454              : {
     455      7319789 :   substitutions.clear ();
     456     16015611 :   for (const auto &p : other.get_substs ())
     457      8695822 :     substitutions.push_back (p.clone ());
     458              : 
     459              :   // we need to remap the argument mappings based on this copied constructor
     460      7319789 :   std::vector<SubstitutionArg> copied_arg_mappings;
     461      7319789 :   size_t i = 0;
     462     15265227 :   for (const auto &m : other.used_arguments.get_mappings ())
     463              :     {
     464      7945438 :       TyTy::BaseType *argument
     465      7945438 :         = m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone ();
     466      7945438 :       SubstitutionArg c (&substitutions.at (i++), argument);
     467      7945438 :       copied_arg_mappings.push_back (std::move (c));
     468              :     }
     469              : 
     470      7319789 :   used_arguments = SubstitutionArgumentMappings (
     471              :     copied_arg_mappings, other.used_arguments.get_binding_args (),
     472              :     other.used_arguments.get_regions (), other.used_arguments.get_locus (),
     473     14639578 :     false, false, other.used_arguments.get_constraint_args ());
     474      7319789 : }
     475              : 
     476              : TypeBoundPredicate &
     477       139172 : TypeBoundPredicate::operator= (const TypeBoundPredicate &other)
     478              : {
     479       139172 :   reference = other.reference;
     480       139172 :   locus = other.locus;
     481       139172 :   error_flag = other.error_flag;
     482       139172 :   polarity = other.polarity;
     483       139172 :   used_arguments = SubstitutionArgumentMappings::empty ();
     484              : 
     485       139172 :   substitutions.clear ();
     486       351342 :   for (const auto &p : other.get_substs ())
     487       212170 :     substitutions.push_back (p.clone ());
     488              : 
     489       139172 :   if (other.is_error ())
     490              :     return *this;
     491              : 
     492              :   // we need to remap the argument mappings based on this copied constructor
     493       126243 :   std::vector<SubstitutionArg> copied_arg_mappings;
     494       126243 :   size_t i = 0;
     495       338396 :   for (const auto &m : other.used_arguments.get_mappings ())
     496              :     {
     497       212153 :       TyTy::BaseType *argument
     498       212153 :         = m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone ();
     499              : 
     500       212153 :       copied_arg_mappings.emplace_back (&substitutions.at (i++), argument);
     501              :     }
     502              : 
     503       126243 :   used_arguments = SubstitutionArgumentMappings (
     504              :     copied_arg_mappings, other.used_arguments.get_binding_args (),
     505              :     other.used_arguments.get_regions (), other.used_arguments.get_locus (),
     506       252486 :     false, false, other.used_arguments.get_constraint_args ());
     507       126243 :   super_traits = other.super_traits;
     508              : 
     509       126243 :   return *this;
     510       126243 : }
     511              : 
     512              : TypeBoundPredicate
     513       239505 : TypeBoundPredicate::error ()
     514              : {
     515       239505 :   return TypeBoundPredicate (mark_is_error ());
     516              : }
     517              : 
     518              : std::string
     519          227 : TypeBoundPredicate::as_string () const
     520              : {
     521          227 :   return get ()->as_string () + subst_as_string ();
     522              : }
     523              : 
     524              : std::string
     525          913 : TypeBoundPredicate::as_name () const
     526              : {
     527          913 :   return get ()->get_name () + subst_as_string ();
     528              : }
     529              : 
     530              : const Resolver::TraitReference *
     531       678522 : TypeBoundPredicate::get () const
     532              : {
     533       678522 :   auto context = Resolver::TypeCheckContext::get ();
     534              : 
     535       678522 :   Resolver::TraitReference *ref = nullptr;
     536       678522 :   bool ok = context->lookup_trait_reference (reference, &ref);
     537       678522 :   rust_assert (ok);
     538              : 
     539       678522 :   return ref;
     540              : }
     541              : 
     542              : std::string
     543         7427 : TypeBoundPredicate::get_name () const
     544              : {
     545         7427 :   return get ()->get_name ();
     546              : }
     547              : 
     548              : bool
     549          793 : TypeBoundPredicate::is_object_safe (bool emit_error, location_t locus) const
     550              : {
     551          793 :   const Resolver::TraitReference *trait = get ();
     552          793 :   rust_assert (trait != nullptr);
     553          793 :   return trait->is_object_safe (emit_error, locus);
     554              : }
     555              : 
     556              : void
     557        86979 : TypeBoundPredicate::apply_generic_arguments (HIR::GenericArgs *generic_args,
     558              :                                              bool has_associated_self,
     559              :                                              bool is_super_trait)
     560              : {
     561        86979 :   rust_assert (!substitutions.empty ());
     562        86979 :   if (has_associated_self)
     563              :     {
     564        86220 :       used_arguments = SubstitutionArgumentMappings::empty ();
     565              :     }
     566              :   else
     567              :     {
     568              :       // we need to get the substitutions argument mappings but also remember
     569              :       // that we have an implicit Self argument which we must be careful to
     570              :       // respect
     571          759 :       rust_assert (!used_arguments.is_empty ());
     572              :     }
     573              : 
     574              :   // now actually perform a substitution
     575        86979 :   auto args = get_mappings_from_generic_args (
     576              :     *generic_args,
     577        86979 :     Resolver::TypeCheckContext::get ()->regions_from_generic_args (
     578       173958 :       *generic_args));
     579              : 
     580        86957 :   apply_argument_mappings (args, is_super_trait);
     581        86957 : }
     582              : 
     583              : void
     584       123084 : TypeBoundPredicate::apply_argument_mappings (
     585              :   SubstitutionArgumentMappings &arguments, bool is_super_trait)
     586              : {
     587       123084 :   used_arguments = arguments;
     588       123084 :   error_flag |= used_arguments.is_error ();
     589       123084 :   auto &subst_mappings = used_arguments;
     590              : 
     591       123084 :   bool substs_need_bounds_check = !is_super_trait;
     592       308442 :   for (auto &sub : get_substs ())
     593              :     {
     594       185358 :       SubstitutionArg arg = SubstitutionArg::error ();
     595       185358 :       bool ok
     596       185358 :         = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg);
     597       185358 :       if (ok && arg.get_tyty () != nullptr)
     598       183620 :         sub.fill_param_ty (subst_mappings, subst_mappings.get_locus (),
     599              :                            substs_need_bounds_check);
     600              :     }
     601              : 
     602              :   // Associated type binding args (Iterator<Item = i32>) are consumed
     603              :   // by BaseType::satisfies_bound at check time
     604       158394 :   for (auto &super_trait : super_traits)
     605              :     {
     606        35310 :       auto adjusted
     607              :         = super_trait.adjust_mappings_for_this (used_arguments,
     608        35310 :                                                 true /*trait mode*/);
     609        35310 :       super_trait.apply_argument_mappings (adjusted, is_super_trait);
     610        35310 :     }
     611       123084 : }
     612              : 
     613              : bool
     614            0 : TypeBoundPredicate::contains_item (const std::string &search) const
     615              : {
     616            0 :   auto trait_ref = get ();
     617            0 :   const Resolver::TraitItemReference *trait_item_ref = nullptr;
     618            0 :   return trait_ref->lookup_trait_item (search, &trait_item_ref);
     619              : }
     620              : 
     621              : tl::optional<TypeBoundPredicateItem>
     622        58747 : TypeBoundPredicate::lookup_associated_item (const std::string &search) const
     623              : {
     624        58747 :   auto trait_ref = get ();
     625        58747 :   const Resolver::TraitItemReference *trait_item_ref = nullptr;
     626        58747 :   if (trait_ref->lookup_trait_item (search, &trait_item_ref,
     627              :                                     false /*lookup supers*/))
     628        52565 :     return TypeBoundPredicateItem (this, trait_item_ref);
     629              : 
     630         6361 :   for (auto &super_trait : super_traits)
     631              :     {
     632         2177 :       auto lookup = super_trait.lookup_associated_item (search);
     633         2177 :       if (lookup.has_value ())
     634         1998 :         return lookup;
     635              :     }
     636              : 
     637         4184 :   return tl::nullopt;
     638              : }
     639              : 
     640        74448 : TypeBoundPredicateItem::TypeBoundPredicateItem (
     641              :   const TypeBoundPredicate *parent,
     642              :   const Resolver::TraitItemReference *trait_item_ref)
     643        74448 :   : parent (parent), trait_item_ref (trait_item_ref)
     644        74448 : {}
     645              : 
     646              : TypeBoundPredicateItem
     647        21810 : TypeBoundPredicateItem::error ()
     648              : {
     649        21810 :   return TypeBoundPredicateItem (nullptr, nullptr);
     650              : }
     651              : 
     652              : bool
     653        23797 : TypeBoundPredicateItem::is_error () const
     654              : {
     655        23797 :   return parent == nullptr || parent->is_error () || trait_item_ref == nullptr;
     656              : }
     657              : 
     658              : const TypeBoundPredicate *
     659         5722 : TypeBoundPredicateItem::get_parent () const
     660              : {
     661         5722 :   return parent;
     662              : }
     663              : 
     664              : tl::optional<TypeBoundPredicateItem>
     665        21674 : TypeBoundPredicate::lookup_associated_item (
     666              :   const Resolver::TraitItemReference *ref) const
     667              : {
     668        21674 :   return lookup_associated_item (ref->get_identifier ());
     669              : }
     670              : 
     671              : BaseType *
     672        41625 : TypeBoundPredicateItem::get_tyty_for_receiver (const TyTy::BaseType *receiver)
     673              : {
     674        41625 :   auto ctx = Resolver::TypeCheckContext::get ();
     675              : 
     676        41625 :   TyTy::BaseType *trait_item_tyty = get_raw_item ()->get_tyty ();
     677        41625 :   if (parent->get_substitution_arguments ().is_empty ())
     678              :     return trait_item_tyty;
     679              : 
     680              :   // set up the self mapping
     681        41625 :   SubstitutionArgumentMappings gargs = parent->get_substitution_arguments ();
     682        41625 :   rust_assert (!gargs.is_empty ());
     683              : 
     684              :   // The associated-type projection we are rebasing is stored in trait
     685              :   // coordinates
     686              :   //
     687              :   //   Self/X for trait SliceIndex<X>
     688              :   //
     689              :   // The predicate's own  SubstitutionParamMappings may have already been
     690              :   // mutated by SubstitutionParamMapping::fill_param_ty when the bound is a
     691              :   // where-clause like I: SliceIndex<[T]>: substituting Self with the
     692              :   // ParamType I rebinds the predicate's first param from Self to I.
     693              :   // Building adjusted_mappings from those renamed mappings would make
     694              :   // name-based lookup (get_argument_for_symbol) miss the trait's Self/X
     695              :   // symbols inside the projection.
     696        41625 :   const auto &trait_substs = parent->get ()->get_trait_substs ();
     697        41625 :   rust_assert (gargs.get_mappings ().size () <= trait_substs.size ());
     698              : 
     699        41625 :   std::vector<SubstitutionArg> adjusted_mappings;
     700       116755 :   for (size_t i = 0; i < gargs.get_mappings ().size (); i++)
     701              :     {
     702        75130 :       auto &mapping = gargs.get_mappings ().at (i);
     703              : 
     704        75130 :       bool is_implicit_self = i == 0;
     705        75130 :       TyTy::BaseType *argument
     706        75130 :         = is_implicit_self ? receiver->clone () : mapping.get_tyty ();
     707              : 
     708        75130 :       adjusted_mappings.emplace_back (&trait_substs.at (i), argument);
     709              :     }
     710              : 
     711        41625 :   SubstitutionArgumentMappings adjusted (adjusted_mappings, {},
     712              :                                          gargs.get_regions (),
     713              :                                          gargs.get_locus (),
     714        41625 :                                          true /* trait-mode-flag */);
     715        41625 :   TyTy::BaseType *res
     716        41625 :     = Resolver::SubstMapperInternal::Resolve (trait_item_tyty, adjusted);
     717              : 
     718        41625 :   if (res != trait_item_tyty)
     719              :     {
     720        41625 :       auto &mappings = Analysis::Mappings::get ();
     721        41625 :       HirId fresh = mappings.get_next_hir_id ();
     722        41625 :       res->set_ref (fresh);
     723        41625 :       res->set_ty_ref (fresh);
     724        41625 :       ctx->insert_implicit_type (fresh, res);
     725              :     }
     726              : 
     727        41625 :   return res;
     728        41625 : }
     729              : bool
     730       307476 : TypeBoundPredicate::is_error () const
     731              : {
     732       307476 :   auto context = Resolver::TypeCheckContext::get ();
     733              : 
     734       307476 :   Resolver::TraitReference *ref = nullptr;
     735       307476 :   bool ok = context->lookup_trait_reference (reference, &ref);
     736              : 
     737       307476 :   return !ok || error_flag;
     738              : }
     739              : 
     740              : BaseType *
     741       258241 : TypeBoundPredicate::handle_substitions (
     742              :   SubstitutionArgumentMappings &subst_mappings)
     743              : {
     744       616109 :   for (auto &sub : get_substs ())
     745              :     {
     746       357868 :       if (sub.get_param_ty () == nullptr)
     747            0 :         continue;
     748              : 
     749       357868 :       auto p = sub.get_param_ty ();
     750       357868 :       BaseType *r = p->resolve ();
     751       357868 :       BaseType *s = Resolver::SubstMapperInternal::Resolve (r, subst_mappings);
     752              : 
     753       357868 :       p->set_ty_ref (s->get_ty_ref ());
     754              :     }
     755              : 
     756              :   // FIXME more error handling at some point
     757              :   // used_arguments = subst_mappings;
     758              :   // error_flag |= used_arguments.is_error ();
     759              : 
     760       258241 :   return nullptr;
     761              : }
     762              : 
     763              : bool
     764         1527 : TypeBoundPredicate::requires_generic_args () const
     765              : {
     766         1527 :   if (is_error ())
     767              :     return false;
     768              : 
     769         1527 :   return substitutions.size () > 1;
     770              : }
     771              : 
     772              : bool
     773            0 : TypeBoundPredicate::contains_associated_types () const
     774              : {
     775            0 :   return get_num_associated_bindings () > 0;
     776              : }
     777              : 
     778              : size_t
     779         4026 : TypeBoundPredicate::get_num_associated_bindings () const
     780              : {
     781         4026 :   size_t count = 0;
     782              : 
     783         4026 :   get_trait_hierachy ([&count] (const Resolver::TraitReference &ref) {
     784        75284 :     for (const auto &trait_item : ref.get_trait_items ())
     785              :       {
     786        65530 :         bool is_associated_type
     787        65530 :           = trait_item.get_trait_item_type ()
     788        65530 :             == Resolver::TraitItemReference::TraitItemType::TYPE;
     789        65530 :         if (is_associated_type)
     790         6470 :           count++;
     791              :       }
     792         9754 :   });
     793              : 
     794         4026 :   return count;
     795              : }
     796              : 
     797              : void
     798         6890 : TypeBoundPredicate::get_trait_hierachy (
     799              :   std::function<void (const Resolver::TraitReference &)> callback) const
     800              : {
     801         6890 :   auto trait_ref = get ();
     802         6890 :   callback (*trait_ref);
     803              : 
     804         9754 :   for (auto &super : super_traits)
     805              :     {
     806         2864 :       const auto &super_trait_ref = *super.get ();
     807         2864 :       callback (super_trait_ref);
     808         2864 :       super.get_trait_hierachy (callback);
     809              :     }
     810         6890 : }
     811              : 
     812              : TypeBoundPredicateItem
     813         2064 : TypeBoundPredicate::lookup_associated_type (const std::string &search)
     814              : {
     815         2064 :   tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search);
     816              : 
     817              :   // only need to check that it is infact an associated type because other
     818              :   // wise if it was not found it will just be an error node anyway
     819         2064 :   if (item.has_value ())
     820              :     {
     821         2064 :       const auto raw = item->get_raw_item ();
     822         2064 :       if (raw->get_trait_item_type ()
     823              :           != Resolver::TraitItemReference::TraitItemType::TYPE)
     824            0 :         return TypeBoundPredicateItem::error ();
     825              :     }
     826         2064 :   return item.value ();
     827              : }
     828              : 
     829              : std::vector<TypeBoundPredicateItem>
     830            0 : TypeBoundPredicate::get_associated_type_items ()
     831              : {
     832            0 :   std::vector<TypeBoundPredicateItem> items;
     833            0 :   auto trait_ref = get ();
     834            0 :   for (const auto &trait_item : trait_ref->get_trait_items ())
     835              :     {
     836            0 :       bool is_associated_type
     837            0 :         = trait_item.get_trait_item_type ()
     838            0 :           == Resolver::TraitItemReference::TraitItemType::TYPE;
     839            0 :       if (is_associated_type)
     840            0 :         items.emplace_back (this, &trait_item);
     841              :     }
     842            0 :   return items;
     843              : }
     844              : 
     845              : bool
     846         1600 : TypeBoundPredicate::is_equal (const TypeBoundPredicate &other) const
     847              : {
     848              :   // check they match the same trait reference
     849         1600 :   if (reference != other.reference)
     850              :     return false;
     851              : 
     852              :   // check that the generics match
     853         1582 :   if (get_num_substitutions () != other.get_num_substitutions ())
     854              :     return false;
     855              : 
     856              :   // then match the generics applied
     857         3318 :   for (size_t i = 0; i < get_num_substitutions (); i++)
     858              :     {
     859         1736 :       SubstitutionParamMapping a = substitutions.at (i);
     860         1736 :       SubstitutionParamMapping b = other.substitutions.at (i);
     861              : 
     862         1736 :       auto ap = a.get_param_ty ();
     863         1736 :       auto bp = b.get_param_ty ();
     864              : 
     865         1736 :       BaseType *apd = ap->destructure ();
     866         1736 :       BaseType *bpd = bp->destructure ();
     867              : 
     868         1736 :       if (!Resolver::types_compatable (TyTy::TyWithLocation (apd),
     869         1736 :                                        TyTy::TyWithLocation (bpd),
     870              :                                        UNKNOWN_LOCATION, false))
     871            0 :         return false;
     872              :     }
     873              : 
     874              :   return true;
     875              : }
     876              : 
     877              : bool
     878        14638 : TypeBoundPredicate::validate_type_implements_super_traits (
     879              :   TyTy::BaseType &self, HIR::Type &impl_type, HIR::Type &trait) const
     880              : {
     881        14638 :   if (get_polarity () != BoundPolarity::RegularBound)
     882              :     return true;
     883              : 
     884        14630 :   auto &ptref = *get ();
     885        21059 :   for (auto &super : super_traits)
     886              :     {
     887         6430 :       if (super.get_polarity () != BoundPolarity::RegularBound)
     888            0 :         continue;
     889              : 
     890         6430 :       if (!super.validate_type_implements_this (self, impl_type, trait))
     891              :         {
     892            1 :           auto &sptref = *super.get ();
     893              : 
     894              :           // emit error
     895            1 :           std::string fixit1
     896            1 :             = "required by this bound in: " + ptref.get_name ();
     897            2 :           std::string fixit2 = "the trait " + sptref.get_name ()
     898            2 :                                + " is not implemented for "
     899            2 :                                + impl_type.to_string ();
     900              : 
     901            1 :           rich_location r (line_table, trait.get_locus ());
     902            1 :           r.add_fixit_insert_after (super.get_locus (), fixit1.c_str ());
     903            1 :           r.add_fixit_insert_after (trait.get_locus (), fixit2.c_str ());
     904            1 :           rust_error_at (r, ErrorCode::E0277,
     905              :                          "the trait bound %<%s: %s%> is not satisfied",
     906            2 :                          impl_type.to_string ().c_str (),
     907            1 :                          sptref.get_name ().c_str ());
     908              : 
     909            1 :           return false;
     910            1 :         }
     911              : 
     912         6429 :       if (!super.validate_type_implements_super_traits (self, impl_type, trait))
     913              :         return false;
     914              :     }
     915              : 
     916              :   return true;
     917              : }
     918              : 
     919              : bool
     920         6430 : TypeBoundPredicate::validate_type_implements_this (TyTy::BaseType &self,
     921              :                                                    HIR::Type &impl_type,
     922              :                                                    HIR::Type &trait) const
     923              : {
     924         6430 :   const auto &ptref = *get ();
     925         6430 :   auto probed_bounds
     926         6430 :     = Resolver::TypeBoundsProbe::Probe (&self, ptref.get_hir_trait_ref ());
     927         6430 :   for (auto &elem : probed_bounds)
     928              :     {
     929         6429 :       auto &tref = *(elem.first);
     930         6429 :       if (ptref.is_equal (tref))
     931         6430 :         return true;
     932              :     }
     933              : 
     934              :   return false;
     935         6430 : }
     936              : 
     937              : // trait item reference
     938              : 
     939              : const Resolver::TraitItemReference *
     940        99780 : TypeBoundPredicateItem::get_raw_item () const
     941              : {
     942        99780 :   return trait_item_ref;
     943              : }
     944              : 
     945              : bool
     946            0 : TypeBoundPredicateItem::needs_implementation () const
     947              : {
     948            0 :   return !get_raw_item ()->is_optional ();
     949              : }
     950              : 
     951              : location_t
     952            5 : TypeBoundPredicateItem::get_locus () const
     953              : {
     954            5 :   return get_raw_item ()->get_locus ();
     955              : }
     956              : 
     957              : // TypeBoundsMappings
     958              : 
     959     15423839 : TypeBoundsMappings::TypeBoundsMappings (
     960              :   std::vector<TypeBoundPredicate> specified_bounds)
     961     15423839 :   : specified_bounds (std::move (specified_bounds))
     962     15423839 : {}
     963              : 
     964              : std::vector<TypeBoundPredicate> &
     965       688179 : TypeBoundsMappings::get_specified_bounds ()
     966              : {
     967       688179 :   return specified_bounds;
     968              : }
     969              : 
     970              : const std::vector<TypeBoundPredicate> &
     971      7317871 : TypeBoundsMappings::get_specified_bounds () const
     972              : {
     973      7317871 :   return specified_bounds;
     974              : }
     975              : 
     976              : TypeBoundPredicate
     977            0 : TypeBoundsMappings::lookup_predicate (DefId id)
     978              : {
     979            0 :   for (auto &b : specified_bounds)
     980              :     {
     981            0 :       if (b.get_id () == id)
     982            0 :         return b;
     983              :     }
     984            0 :   return TypeBoundPredicate::error ();
     985              : }
     986              : 
     987              : size_t
     988      2268990 : TypeBoundsMappings::num_specified_bounds () const
     989              : {
     990      2268990 :   return specified_bounds.size ();
     991              : }
     992              : 
     993              : std::string
     994        38808 : TypeBoundsMappings::raw_bounds_as_string () const
     995              : {
     996        38808 :   std::string buf;
     997        39035 :   for (size_t i = 0; i < specified_bounds.size (); i++)
     998              :     {
     999          227 :       const TypeBoundPredicate &b = specified_bounds.at (i);
    1000          227 :       bool has_next = (i + 1) < specified_bounds.size ();
    1001          904 :       buf += b.as_string () + (has_next ? " + " : "");
    1002              :     }
    1003        38808 :   return buf;
    1004              : }
    1005              : 
    1006              : std::string
    1007        38645 : TypeBoundsMappings::bounds_as_string () const
    1008              : {
    1009        77290 :   return "bounds:[" + raw_bounds_as_string () + "]";
    1010              : }
    1011              : 
    1012              : std::string
    1013          902 : TypeBoundsMappings::raw_bounds_as_name () const
    1014              : {
    1015          902 :   std::string buf;
    1016         1815 :   for (size_t i = 0; i < specified_bounds.size (); i++)
    1017              :     {
    1018          913 :       const TypeBoundPredicate &b = specified_bounds.at (i);
    1019          913 :       bool has_next = (i + 1) < specified_bounds.size ();
    1020         3641 :       buf += b.as_name () + (has_next ? " + " : "");
    1021              :     }
    1022              : 
    1023          902 :   return buf;
    1024              : }
    1025              : 
    1026              : void
    1027       220363 : TypeBoundsMappings::add_bound (const TypeBoundPredicate &predicate)
    1028              : {
    1029       247719 :   for (auto &bound : specified_bounds)
    1030              :     {
    1031        49433 :       bool same_trait_ref_p = bound.get_id () == predicate.get_id ();
    1032        27356 :       if (same_trait_ref_p)
    1033       220363 :         return;
    1034              :     }
    1035              : 
    1036       198286 :   specified_bounds.push_back (predicate);
    1037              : }
    1038              : 
    1039              : } // namespace TyTy
    1040              : } // 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.