LCOV - code coverage report
Current view: top level - gcc/rust/resolve - rust-ast-resolve-type.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.4 % 398 356
Test Date: 2025-04-26 15:52:03 Functions: 94.7 % 38 36
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #include "rust-ast-resolve-type.h"
      20                 :             : #include "rust-ast-resolve-expr.h"
      21                 :             : #include "rust-canonical-path.h"
      22                 :             : #include "rust-type.h"
      23                 :             : #include "rust-hir-map.h"
      24                 :             : 
      25                 :             : namespace Rust {
      26                 :             : namespace Resolver {
      27                 :             : 
      28                 :             : // rust-ast-resolve-type.h
      29                 :             : 
      30                 :             : NodeId
      31                 :       56420 : ResolveType::go (AST::Type &type)
      32                 :             : {
      33                 :       56420 :   ResolveType resolver;
      34                 :       56420 :   type.accept_vis (resolver);
      35                 :       56420 :   return resolver.resolved_node;
      36                 :       56420 : }
      37                 :             : 
      38                 :             : void
      39                 :          46 : ResolveType::visit (AST::BareFunctionType &fntype)
      40                 :             : {
      41                 :          90 :   for (auto &param : fntype.get_function_params ())
      42                 :          44 :     ResolveType::go (param.get_type ());
      43                 :             : 
      44                 :          46 :   if (fntype.has_return_type ())
      45                 :          35 :     ResolveType::go (fntype.get_return_type ());
      46                 :          46 : }
      47                 :             : 
      48                 :             : void
      49                 :         354 : ResolveType::visit (AST::TupleType &tuple)
      50                 :             : {
      51                 :         354 :   if (tuple.is_unit_type ())
      52                 :             :     {
      53                 :          65 :       resolved_node = resolver->get_unit_type_node_id ();
      54                 :          65 :       return;
      55                 :             :     }
      56                 :             : 
      57                 :         880 :   for (auto &elem : tuple.get_elems ())
      58                 :         591 :     ResolveType::go (*elem);
      59                 :             : }
      60                 :             : 
      61                 :             : void
      62                 :       46753 : ResolveType::visit (AST::TypePath &path)
      63                 :             : {
      64                 :       46753 :   ResolveRelativeTypePath::go (path, resolved_node);
      65                 :       46753 : }
      66                 :             : 
      67                 :             : void
      68                 :         171 : ResolveType::visit (AST::QualifiedPathInType &path)
      69                 :             : {
      70                 :         171 :   ResolveRelativeQualTypePath::go (path);
      71                 :         171 : }
      72                 :             : 
      73                 :             : void
      74                 :         574 : ResolveType::visit (AST::ArrayType &type)
      75                 :             : {
      76                 :         574 :   type.get_elem_type ().accept_vis (*this);
      77                 :         574 :   ResolveExpr::go (type.get_size_expr (), CanonicalPath::create_empty (),
      78                 :         574 :                    CanonicalPath::create_empty ());
      79                 :         574 : }
      80                 :             : 
      81                 :             : void
      82                 :         156 : ResolveType::visit (AST::TraitObjectTypeOneBound &type)
      83                 :             : {
      84                 :         156 :   ResolveTypeBound::go (type.get_trait_bound ());
      85                 :         156 : }
      86                 :             : 
      87                 :             : void
      88                 :          12 : ResolveType::visit (AST::TraitObjectType &type)
      89                 :             : {
      90                 :          41 :   for (auto &bound : type.get_type_param_bounds ())
      91                 :             :     {
      92                 :             :       /* NodeId bound_resolved_id = */
      93                 :          29 :       ResolveTypeBound::go (*bound);
      94                 :             :     }
      95                 :          12 : }
      96                 :             : 
      97                 :             : void
      98                 :           2 : ResolveType::visit (AST::ParenthesisedType &type)
      99                 :             : {
     100                 :           2 :   resolved_node = ResolveType::go (*type.get_type_in_parens ());
     101                 :           2 : }
     102                 :             : 
     103                 :             : void
     104                 :        2300 : ResolveType::visit (AST::ReferenceType &type)
     105                 :             : {
     106                 :        2300 :   resolved_node = ResolveType::go (type.get_type_referenced ());
     107                 :        2300 : }
     108                 :             : 
     109                 :             : void
     110                 :        5589 : ResolveType::visit (AST::RawPointerType &type)
     111                 :             : {
     112                 :        5589 :   resolved_node = ResolveType::go (type.get_type_pointed_to ());
     113                 :        5589 : }
     114                 :             : 
     115                 :             : void
     116                 :         163 : ResolveType::visit (AST::InferredType &)
     117                 :             : {
     118                 :             :   // nothing to do
     119                 :         163 : }
     120                 :             : 
     121                 :             : void
     122                 :          46 : ResolveType::visit (AST::NeverType &)
     123                 :             : {
     124                 :          46 :   resolved_node = resolver->get_never_type_node_id ();
     125                 :          46 : }
     126                 :             : 
     127                 :             : void
     128                 :         824 : ResolveType::visit (AST::SliceType &type)
     129                 :             : {
     130                 :         824 :   resolved_node = ResolveType::go (type.get_elem_type ());
     131                 :         824 : }
     132                 :             : 
     133                 :             : void
     134                 :           0 : ResolveType::visit (AST::ImplTraitType &type)
     135                 :             : {
     136                 :           0 :   for (auto &bound : type.get_type_param_bounds ())
     137                 :           0 :     ResolveTypeBound::go (*bound);
     138                 :           0 : }
     139                 :             : 
     140                 :             : void
     141                 :           4 : ResolveType::visit (AST::ImplTraitTypeOneBound &type)
     142                 :             : {
     143                 :           4 :   ResolveTypeBound::go (type.get_trait_bound ());
     144                 :           4 : }
     145                 :             : 
     146                 :             : // resolve relative type-paths
     147                 :             : 
     148                 :             : bool
     149                 :       46753 : ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
     150                 :             : {
     151                 :       46753 :   auto resolver = Resolver::get ();
     152                 :       46753 :   auto &mappings = Analysis::Mappings::get ();
     153                 :             : 
     154                 :       46753 :   NodeId module_scope_id = resolver->peek_current_module_scope ();
     155                 :       46753 :   NodeId previous_resolved_node_id = module_scope_id;
     156                 :       94681 :   for (size_t i = 0; i < path.get_segments ().size (); i++)
     157                 :             :     {
     158                 :       47950 :       auto &segment = path.get_segments ().at (i);
     159                 :       47950 :       bool is_first_segment = i == 0;
     160                 :       47950 :       NodeId crate_scope_id = resolver->peek_crate_module_scope ();
     161                 :       47950 :       auto ident_string = segment->is_lang_item ()
     162                 :       47950 :                             ? LangItem::PrettyString (segment->get_lang_item ())
     163                 :       47950 :                             : segment->get_ident_segment ().as_string ();
     164                 :             : 
     165                 :       47950 :       resolved_node_id = UNKNOWN_NODEID;
     166                 :             : 
     167                 :       47950 :       if (segment->is_lang_item ())
     168                 :             :         {
     169                 :         112 :           resolved_node_id = Analysis::Mappings::get ().get_lang_item_node (
     170                 :             :             segment->get_lang_item ());
     171                 :         112 :           previous_resolved_node_id = resolved_node_id;
     172                 :             :         }
     173                 :             :       else
     174                 :             :         {
     175                 :       47838 :           bool in_middle_of_path = i > 0;
     176                 :       47838 :           if (in_middle_of_path && segment->is_lower_self_seg ())
     177                 :             :             {
     178                 :           1 :               rust_error_at (segment->get_locus (), ErrorCode::E0433,
     179                 :             :                              "leading path segment %qs can only be used at the "
     180                 :             :                              "beginning of a path",
     181                 :           1 :                              segment->as_string ().c_str ());
     182                 :           1 :               return false;
     183                 :             :             }
     184                 :             : 
     185                 :       47837 :           if (segment->is_crate_path_seg ())
     186                 :             :             {
     187                 :             :               // what is the current crate scope node id?
     188                 :           5 :               module_scope_id = crate_scope_id;
     189                 :           5 :               previous_resolved_node_id = module_scope_id;
     190                 :           5 :               resolver->insert_resolved_name (segment->get_node_id (),
     191                 :             :                                               module_scope_id);
     192                 :             : 
     193                 :           5 :               continue;
     194                 :             :             }
     195                 :       47832 :           else if (segment->is_super_path_seg ())
     196                 :             :             {
     197                 :           0 :               if (module_scope_id == crate_scope_id)
     198                 :             :                 {
     199                 :           0 :                   rust_error_at (segment->get_locus (),
     200                 :             :                                  "cannot use super at the crate scope");
     201                 :           0 :                   return false;
     202                 :             :                 }
     203                 :             : 
     204                 :           0 :               module_scope_id = resolver->peek_parent_module_scope ();
     205                 :           0 :               previous_resolved_node_id = module_scope_id;
     206                 :           0 :               resolver->insert_resolved_name (segment->get_node_id (),
     207                 :             :                                               module_scope_id);
     208                 :           0 :               continue;
     209                 :             :             }
     210                 :             :         }
     211                 :             : 
     212                 :       47944 :       switch (segment->get_type ())
     213                 :             :         {
     214                 :        1859 :           case AST::TypePathSegment::SegmentType::GENERIC: {
     215                 :        1859 :             AST::TypePathSegmentGeneric *s
     216                 :        1859 :               = static_cast<AST::TypePathSegmentGeneric *> (segment.get ());
     217                 :        1859 :             if (s->has_generic_args ())
     218                 :        1859 :               ResolveGenericArgs::go (s->get_generic_args ());
     219                 :             :           }
     220                 :             :           break;
     221                 :             : 
     222                 :             :         case AST::TypePathSegment::SegmentType::REG:
     223                 :             :           // nothing to do
     224                 :             :           break;
     225                 :             : 
     226                 :          22 :         case AST::TypePathSegment::SegmentType::FUNCTION:
     227                 :          22 :           AST::TypePathSegmentFunction *fnseg
     228                 :          22 :             = static_cast<AST::TypePathSegmentFunction *> (segment.get ());
     229                 :             : 
     230                 :          22 :           AST::TypePathFunction &fn = fnseg->get_type_path_function ();
     231                 :          46 :           for (auto &param : fn.get_params ())
     232                 :             :             {
     233                 :          24 :               ResolveType::go (*param);
     234                 :             :             }
     235                 :             : 
     236                 :          22 :           if (fn.has_return_type ())
     237                 :             :             {
     238                 :          20 :               ResolveType::go (fn.get_return_type ());
     239                 :             :             }
     240                 :             : 
     241                 :             :           break;
     242                 :             :         }
     243                 :             : 
     244                 :       47944 :       if (is_first_segment)
     245                 :             :         {
     246                 :             :           // name scope first
     247                 :       46748 :           NodeId resolved_node = UNKNOWN_NODEID;
     248                 :       46748 :           const CanonicalPath path
     249                 :       46748 :             = CanonicalPath::new_seg (segment->get_node_id (), ident_string);
     250                 :       46748 :           if (resolver->get_type_scope ().lookup (path, &resolved_node))
     251                 :             :             {
     252                 :       46178 :               NodeId existing = UNKNOWN_NODEID;
     253                 :       46178 :               bool ok = resolver->lookup_resolved_type (segment->get_node_id (),
     254                 :             :                                                         &existing);
     255                 :             : 
     256                 :       46178 :               if (ok)
     257                 :         786 :                 rust_assert (existing == resolved_node);
     258                 :             :               else
     259                 :       45392 :                 resolver->insert_resolved_type (segment->get_node_id (),
     260                 :             :                                                 resolved_node);
     261                 :       46178 :               resolved_node_id = resolved_node;
     262                 :             :             }
     263                 :         570 :           else if (resolver->get_name_scope ().lookup (path, &resolved_node))
     264                 :             :             {
     265                 :         114 :               NodeId existing = UNKNOWN_NODEID;
     266                 :         114 :               bool ok = resolver->lookup_resolved_name (segment->get_node_id (),
     267                 :             :                                                         &existing);
     268                 :             : 
     269                 :         114 :               if (ok)
     270                 :           0 :                 rust_assert (existing == resolved_node);
     271                 :             :               else
     272                 :         114 :                 resolver->insert_resolved_name (segment->get_node_id (),
     273                 :             :                                                 resolved_node);
     274                 :         114 :               resolved_node_id = resolved_node;
     275                 :             :             }
     276                 :         456 :           else if (!segment->is_lang_item () && segment->is_lower_self_seg ())
     277                 :             :             {
     278                 :             :               // what is the current crate scope node id?
     279                 :           3 :               module_scope_id = crate_scope_id;
     280                 :           3 :               previous_resolved_node_id = module_scope_id;
     281                 :             : 
     282                 :           3 :               NodeId existing = UNKNOWN_NODEID;
     283                 :           3 :               bool ok = resolver->lookup_resolved_name (segment->get_node_id (),
     284                 :             :                                                         &existing);
     285                 :             : 
     286                 :           3 :               if (ok)
     287                 :           0 :                 rust_assert (existing == module_scope_id);
     288                 :             :               else
     289                 :           3 :                 resolver->insert_resolved_name (segment->get_node_id (),
     290                 :             :                                                 module_scope_id);
     291                 :             : 
     292                 :           3 :               continue;
     293                 :           3 :             }
     294                 :       46748 :         }
     295                 :             : 
     296                 :       47941 :       if (resolved_node_id == UNKNOWN_NODEID
     297                 :        1537 :           && previous_resolved_node_id == module_scope_id)
     298                 :             :         {
     299                 :         563 :           tl::optional<CanonicalPath &> resolved_child
     300                 :         563 :             = mappings.lookup_module_child (module_scope_id, ident_string);
     301                 :         563 :           if (resolved_child.has_value ())
     302                 :             :             {
     303                 :         541 :               NodeId resolved_node = resolved_child->get_node_id ();
     304                 :         541 :               if (resolver->get_name_scope ().decl_was_declared_here (
     305                 :             :                     resolved_node))
     306                 :             :                 {
     307                 :         114 :                   resolved_node_id = resolved_node;
     308                 :             : 
     309                 :         114 :                   NodeId existing = UNKNOWN_NODEID;
     310                 :         114 :                   bool ok
     311                 :         114 :                     = resolver->lookup_resolved_name (segment->get_node_id (),
     312                 :             :                                                       &existing);
     313                 :             : 
     314                 :         114 :                   if (ok)
     315                 :           0 :                     rust_assert (existing == resolved_node);
     316                 :             :                   else
     317                 :         114 :                     resolver->insert_resolved_name (segment->get_node_id (),
     318                 :             :                                                     resolved_node);
     319                 :             :                 }
     320                 :         427 :               else if (resolver->get_type_scope ().decl_was_declared_here (
     321                 :             :                          resolved_node))
     322                 :             :                 {
     323                 :         427 :                   resolved_node_id = resolved_node;
     324                 :             : 
     325                 :         427 :                   NodeId existing = UNKNOWN_NODEID;
     326                 :         427 :                   bool ok
     327                 :         427 :                     = resolver->lookup_resolved_type (segment->get_node_id (),
     328                 :             :                                                       &existing);
     329                 :             : 
     330                 :         427 :                   if (ok)
     331                 :           0 :                     rust_assert (existing == resolved_node);
     332                 :             :                   else
     333                 :         427 :                     resolver->insert_resolved_type (segment->get_node_id (),
     334                 :             :                                                     resolved_node);
     335                 :             :                 }
     336                 :             :               else
     337                 :             :                 {
     338                 :           0 :                   rust_error_at (segment->get_locus (),
     339                 :             :                                  "Cannot find path %qs in this scope",
     340                 :           0 :                                  segment->as_string ().c_str ());
     341                 :           0 :                   return false;
     342                 :             :                 }
     343                 :             :             }
     344                 :             :         }
     345                 :             : 
     346                 :       47941 :       bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID;
     347                 :       47941 :       if (did_resolve_segment)
     348                 :             :         {
     349                 :       46945 :           if (mappings.node_is_module (resolved_node_id)
     350                 :       46945 :               || mappings.node_is_crate (resolved_node_id))
     351                 :             :             {
     352                 :         866 :               module_scope_id = resolved_node_id;
     353                 :             :             }
     354                 :       46945 :           previous_resolved_node_id = resolved_node_id;
     355                 :             :         }
     356                 :         996 :       else if (is_first_segment)
     357                 :             :         {
     358                 :          42 :           rust_error_at (segment->get_locus (), ErrorCode::E0412,
     359                 :             :                          "could not resolve type path %qs",
     360                 :          21 :                          segment->get_ident_segment ().as_string ().c_str ());
     361                 :          21 :           return false;
     362                 :             :         }
     363                 :       47950 :     }
     364                 :             : 
     365                 :       46731 :   if (resolved_node_id != UNKNOWN_NODEID)
     366                 :             :     {
     367                 :             :       // name scope first
     368                 :       45756 :       if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
     369                 :             :         {
     370                 :         356 :           NodeId existing = UNKNOWN_NODEID;
     371                 :         356 :           bool ok
     372                 :         356 :             = resolver->lookup_resolved_name (path.get_node_id (), &existing);
     373                 :             : 
     374                 :         356 :           if (ok)
     375                 :           1 :             rust_assert (existing == resolved_node_id);
     376                 :             :           else
     377                 :         355 :             resolver->insert_resolved_name (path.get_node_id (),
     378                 :             :                                             resolved_node_id);
     379                 :             :         }
     380                 :             :       // check the type scope
     381                 :       45400 :       else if (resolver->get_type_scope ().decl_was_declared_here (
     382                 :             :                  resolved_node_id))
     383                 :             :         {
     384                 :       45400 :           NodeId existing = UNKNOWN_NODEID;
     385                 :       45400 :           bool ok
     386                 :       45400 :             = resolver->lookup_resolved_type (path.get_node_id (), &existing);
     387                 :             : 
     388                 :       45400 :           if (ok)
     389                 :         785 :             rust_assert (existing == resolved_node_id);
     390                 :             :           else
     391                 :       44615 :             resolver->insert_resolved_type (path.get_node_id (),
     392                 :             :                                             resolved_node_id);
     393                 :             :         }
     394                 :             :       else
     395                 :             :         {
     396                 :           0 :           rust_unreachable ();
     397                 :             :         }
     398                 :             :     }
     399                 :             : 
     400                 :             :   return true;
     401                 :             : }
     402                 :             : 
     403                 :             : // qualified type paths
     404                 :             : 
     405                 :         171 : ResolveRelativeQualTypePath::ResolveRelativeQualTypePath ()
     406                 :         171 :   : failure_flag (false)
     407                 :         171 : {}
     408                 :             : 
     409                 :             : bool
     410                 :         171 : ResolveRelativeQualTypePath::go (AST::QualifiedPathInType &path)
     411                 :             : {
     412                 :         171 :   ResolveRelativeQualTypePath o;
     413                 :             : 
     414                 :             :   // resolve the type and trait path
     415                 :         171 :   auto &qualified_path = path.get_qualified_path_type ();
     416                 :         171 :   if (!o.resolve_qual_seg (qualified_path))
     417                 :             :     return false;
     418                 :             : 
     419                 :             :   // qualified types are similar to other paths in that we cannot guarantee
     420                 :             :   // that we can resolve the path at name resolution. We must look up
     421                 :             :   // associated types and type information to figure this out properly
     422                 :             : 
     423                 :         171 :   std::unique_ptr<AST::TypePathSegment> &associated
     424                 :         171 :     = path.get_associated_segment ();
     425                 :             : 
     426                 :         171 :   associated->accept_vis (o);
     427                 :         171 :   if (o.failure_flag)
     428                 :             :     return false;
     429                 :             : 
     430                 :         171 :   for (auto &seg : path.get_segments ())
     431                 :             :     {
     432                 :           0 :       seg->accept_vis (o);
     433                 :           0 :       if (o.failure_flag)
     434                 :           0 :         return false;
     435                 :             :     }
     436                 :             : 
     437                 :             :   return true;
     438                 :         171 : }
     439                 :             : 
     440                 :             : bool
     441                 :         171 : ResolveRelativeQualTypePath::resolve_qual_seg (AST::QualifiedPathType &seg)
     442                 :             : {
     443                 :         171 :   if (seg.is_error ())
     444                 :             :     {
     445                 :           0 :       rust_error_at (seg.get_locus (), "segment has error: %s",
     446                 :           0 :                      seg.as_string ().c_str ());
     447                 :           0 :       return false;
     448                 :             :     }
     449                 :             : 
     450                 :         171 :   auto &type = seg.get_type ();
     451                 :         171 :   ResolveType::go (type);
     452                 :             : 
     453                 :         171 :   if (seg.has_as_clause ())
     454                 :         170 :     ResolveType::go (seg.get_as_type_path ());
     455                 :             : 
     456                 :             :   return true;
     457                 :             : }
     458                 :             : 
     459                 :             : void
     460                 :           0 : ResolveRelativeQualTypePath::visit (AST::TypePathSegmentGeneric &seg)
     461                 :             : {
     462                 :           0 :   if (seg.is_error ())
     463                 :             :     {
     464                 :           0 :       failure_flag = true;
     465                 :           0 :       rust_error_at (seg.get_locus (), "segment has error: %s",
     466                 :           0 :                      seg.as_string ().c_str ());
     467                 :           0 :       return;
     468                 :             :     }
     469                 :             : 
     470                 :           0 :   ResolveGenericArgs::go (seg.get_generic_args ());
     471                 :             : }
     472                 :             : 
     473                 :             : void
     474                 :         171 : ResolveRelativeQualTypePath::visit (AST::TypePathSegment &seg)
     475                 :             : {
     476                 :         171 :   if (seg.is_error ())
     477                 :             :     {
     478                 :           0 :       failure_flag = true;
     479                 :           0 :       rust_error_at (seg.get_locus (), "segment has error: %s",
     480                 :           0 :                      seg.as_string ().c_str ());
     481                 :           0 :       return;
     482                 :             :     }
     483                 :             : }
     484                 :             : 
     485                 :             : // resolve to canonical path
     486                 :             : 
     487                 :             : bool
     488                 :        9065 : ResolveTypeToCanonicalPath::go (AST::Type &type, CanonicalPath &result)
     489                 :             : {
     490                 :        9065 :   ResolveTypeToCanonicalPath resolver;
     491                 :        9065 :   type.accept_vis (resolver);
     492                 :        9065 :   result = resolver.result;
     493                 :        9065 :   return !resolver.result.is_empty ();
     494                 :        9065 : }
     495                 :             : 
     496                 :             : void
     497                 :        8464 : ResolveTypeToCanonicalPath::visit (AST::TypePath &path)
     498                 :             : {
     499                 :        8464 :   NodeId resolved_node = UNKNOWN_NODEID;
     500                 :        8464 :   if (!resolver->lookup_resolved_name (path.get_node_id (), &resolved_node))
     501                 :             :     {
     502                 :        8241 :       resolver->lookup_resolved_type (path.get_node_id (), &resolved_node);
     503                 :             :     }
     504                 :             : 
     505                 :        8464 :   if (resolved_node == UNKNOWN_NODEID)
     506                 :           4 :     return;
     507                 :             : 
     508                 :        8460 :   if (auto type_path = mappings.lookup_canonical_path (resolved_node))
     509                 :             :     {
     510                 :        8460 :       auto &final_seg = path.get_segments ().back ();
     511                 :        8460 :       switch (final_seg->get_type ())
     512                 :             :         {
     513                 :         688 :           case AST::TypePathSegment::SegmentType::GENERIC: {
     514                 :         688 :             AST::TypePathSegmentGeneric *s
     515                 :         688 :               = static_cast<AST::TypePathSegmentGeneric *> (final_seg.get ());
     516                 :             : 
     517                 :         688 :             std::vector<CanonicalPath> args;
     518                 :         688 :             if (s->has_generic_args ())
     519                 :             :               {
     520                 :         688 :                 ResolveGenericArgs::go (s->get_generic_args ());
     521                 :        1455 :                 for (auto &generic : s->get_generic_args ().get_generic_args ())
     522                 :             :                   {
     523                 :             :                     // FIXME: What do we want to do here in case there is a
     524                 :             :                     // constant or an ambiguous const generic?
     525                 :             :                     // TODO: At that point, will all generics have been
     526                 :             :                     // disambiguated? Can we thus canonical resolve types and
     527                 :             :                     // const and `rust_unreachable` on ambiguous types?
     528                 :             :                     // This is probably fine as we just want to canonicalize
     529                 :             :                     // types, right?
     530                 :         767 :                     if (generic.get_kind () == AST::GenericArg::Kind::Type)
     531                 :             :                       {
     532                 :         767 :                         CanonicalPath arg = CanonicalPath::create_empty ();
     533                 :         767 :                         bool ok
     534                 :         767 :                           = ResolveTypeToCanonicalPath::go (generic.get_type (),
     535                 :             :                                                             arg);
     536                 :         767 :                         if (ok)
     537                 :         766 :                           args.push_back (std::move (arg));
     538                 :         767 :                       }
     539                 :             :                   }
     540                 :             :               }
     541                 :             : 
     542                 :         688 :             result = *type_path;
     543                 :         688 :             if (!args.empty ())
     544                 :             :               {
     545                 :             :                 // append this onto the path
     546                 :         675 :                 std::string buf;
     547                 :        1441 :                 for (size_t i = 0; i < args.size (); i++)
     548                 :             :                   {
     549                 :         766 :                     bool has_next = (i + 1) < args.size ();
     550                 :         766 :                     const auto &arg = args.at (i);
     551                 :             : 
     552                 :         766 :                     buf += arg.get ();
     553                 :         766 :                     if (has_next)
     554                 :          91 :                       buf += ", ";
     555                 :             :                   }
     556                 :             : 
     557                 :        1350 :                 std::string arg_seg = "<" + buf + ">";
     558                 :         675 :                 CanonicalPath argument_seg
     559                 :         675 :                   = CanonicalPath::new_seg (s->get_node_id (), arg_seg);
     560                 :         675 :                 result = result.append (argument_seg);
     561                 :         675 :               }
     562                 :         688 :           }
     563                 :         688 :           break;
     564                 :             : 
     565                 :        7772 :         default:
     566                 :        7772 :           result = *type_path;
     567                 :        7772 :           break;
     568                 :             :         }
     569                 :             :     }
     570                 :             : }
     571                 :             : 
     572                 :             : void
     573                 :         257 : ResolveTypeToCanonicalPath::visit (AST::ReferenceType &type)
     574                 :             : {
     575                 :         257 :   CanonicalPath path = CanonicalPath::create_empty ();
     576                 :         257 :   bool ok = ResolveTypeToCanonicalPath::go (type.get_type_referenced (), path);
     577                 :         257 :   if (ok)
     578                 :             :     {
     579                 :         445 :       std::string ref_type_str = type.is_mut () ? "mut" : "";
     580                 :         514 :       std::string ref_path = "&" + ref_type_str + " " + path.get ();
     581                 :         257 :       result = CanonicalPath::new_seg (type.get_node_id (), ref_path);
     582                 :         257 :     }
     583                 :         257 : }
     584                 :             : 
     585                 :             : void
     586                 :         157 : ResolveTypeToCanonicalPath::visit (AST::RawPointerType &type)
     587                 :             : {
     588                 :         157 :   CanonicalPath path = CanonicalPath::create_empty ();
     589                 :         157 :   bool ok = ResolveTypeToCanonicalPath::go (type.get_type_pointed_to (), path);
     590                 :         157 :   if (ok)
     591                 :             :     {
     592                 :         157 :       std::string ptr_type_str
     593                 :         157 :         = type.get_pointer_type () == AST::RawPointerType::CONST ? "const"
     594                 :         185 :                                                                  : "mut";
     595                 :         314 :       std::string ptr_path = "*" + ptr_type_str + " " + path.get ();
     596                 :         157 :       result = CanonicalPath::new_seg (type.get_node_id (), ptr_path);
     597                 :         157 :     }
     598                 :         157 : }
     599                 :             : 
     600                 :             : void
     601                 :         173 : ResolveTypeToCanonicalPath::visit (AST::SliceType &type)
     602                 :             : {
     603                 :         173 :   CanonicalPath path = CanonicalPath::create_empty ();
     604                 :         173 :   bool ok = ResolveTypeToCanonicalPath::go (type.get_elem_type (), path);
     605                 :         173 :   if (ok)
     606                 :             :     {
     607                 :         346 :       std::string slice_path = "[" + path.get () + "]";
     608                 :         173 :       result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
     609                 :         173 :     }
     610                 :         173 : }
     611                 :             : 
     612                 :             : void
     613                 :           7 : ResolveTypeToCanonicalPath::visit (AST::TraitObjectTypeOneBound &type)
     614                 :             : {
     615                 :           7 :   CanonicalPath path = CanonicalPath::create_empty ();
     616                 :           7 :   bool ok
     617                 :           7 :     = ResolveTypeToCanonicalPath::go (type.get_trait_bound ().get_type_path (),
     618                 :             :                                       path);
     619                 :           7 :   if (ok)
     620                 :             :     {
     621                 :          14 :       std::string slice_path = "<dyn " + path.get () + ">";
     622                 :           7 :       result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
     623                 :           7 :     }
     624                 :           7 : }
     625                 :             : 
     626                 :             : void
     627                 :           3 : ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &type)
     628                 :             : {
     629                 :           3 :   rust_assert (!type.get_type_param_bounds ().empty ());
     630                 :             : 
     631                 :           3 :   auto &first_bound = type.get_type_param_bounds ().front ();
     632                 :             : 
     633                 :             :   // Is it allowed or even possible to have a lifetime bound as a first bound?
     634                 :           3 :   if (first_bound->get_bound_type () == AST::TraitBound::LIFETIME)
     635                 :           0 :     rust_unreachable ();
     636                 :             : 
     637                 :           3 :   auto &trait = static_cast<AST::TraitBound &> (*first_bound);
     638                 :             : 
     639                 :           3 :   CanonicalPath path = CanonicalPath::create_empty ();
     640                 :           3 :   bool ok = ResolveTypeToCanonicalPath::go (trait.get_type_path (), path);
     641                 :             : 
     642                 :             :   // right?
     643                 :           3 :   rust_assert (ok);
     644                 :             : 
     645                 :           3 :   auto slice_path = "<dyn " + path.get ();
     646                 :             : 
     647                 :           9 :   for (size_t idx = 1; idx < type.get_type_param_bounds ().size (); idx++)
     648                 :             :     {
     649                 :           6 :       auto &additional_bound = type.get_type_param_bounds ()[idx];
     650                 :             : 
     651                 :           6 :       std::string str;
     652                 :             : 
     653                 :           6 :       switch (additional_bound->get_bound_type ())
     654                 :             :         {
     655                 :           6 :           case AST::TypeParamBound::TRAIT: {
     656                 :           6 :             auto bound_path = CanonicalPath::create_empty ();
     657                 :             : 
     658                 :           6 :             auto &bound_type_path
     659                 :           6 :               = static_cast<AST::TraitBound &> (*additional_bound)
     660                 :           6 :                   .get_type_path ();
     661                 :           6 :             bool ok
     662                 :           6 :               = ResolveTypeToCanonicalPath::go (bound_type_path, bound_path);
     663                 :             : 
     664                 :           6 :             if (!ok)
     665                 :           1 :               continue;
     666                 :             : 
     667                 :           5 :             str = bound_path.get ();
     668                 :           5 :             break;
     669                 :           6 :           }
     670                 :           0 :         case AST::TypeParamBound::LIFETIME:
     671                 :           0 :           rust_unreachable ();
     672                 :           5 :           break;
     673                 :             :         }
     674                 :           5 :       slice_path += " + " + str;
     675                 :           6 :     }
     676                 :             : 
     677                 :           3 :   slice_path += ">";
     678                 :             : 
     679                 :           3 :   result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
     680                 :           3 : }
     681                 :             : 
     682                 :             : void
     683                 :           2 : ResolveTypeToCanonicalPath::visit (AST::NeverType &type)
     684                 :             : {
     685                 :           2 :   result = CanonicalPath::new_seg (type.get_node_id (), "!");
     686                 :           2 : }
     687                 :             : 
     688                 :             : void
     689                 :           2 : ResolveTypeToCanonicalPath::visit (AST::TupleType &type)
     690                 :             : {
     691                 :           2 :   if (!type.is_unit_type ())
     692                 :           0 :     rust_unreachable ();
     693                 :             : 
     694                 :           2 :   result = CanonicalPath::new_seg (type.get_node_id (), "()");
     695                 :           2 : }
     696                 :             : 
     697                 :        9065 : ResolveTypeToCanonicalPath::ResolveTypeToCanonicalPath ()
     698                 :        9065 :   : ResolverBase (), result (CanonicalPath::create_empty ())
     699                 :        9065 : {}
     700                 :             : 
     701                 :             : bool
     702                 :        1756 : ResolveGenericArgs::is_const_value_name (const CanonicalPath &path)
     703                 :             : {
     704                 :        1756 :   NodeId resolved;
     705                 :        1756 :   auto found = resolver->get_name_scope ().lookup (path, &resolved);
     706                 :             : 
     707                 :        1756 :   return found;
     708                 :             : }
     709                 :             : 
     710                 :             : bool
     711                 :        1756 : ResolveGenericArgs::is_type_name (const CanonicalPath &path)
     712                 :             : {
     713                 :        1756 :   NodeId resolved;
     714                 :        1756 :   auto found = resolver->get_type_scope ().lookup (path, &resolved);
     715                 :             : 
     716                 :        1756 :   return found;
     717                 :             : }
     718                 :             : 
     719                 :             : void
     720                 :        1756 : ResolveGenericArgs::disambiguate (AST::GenericArg &arg)
     721                 :             : {
     722                 :        1756 :   auto path = canonical_prefix.append (
     723                 :        1756 :     CanonicalPath::new_seg (UNKNOWN_NODEID, arg.get_path ()));
     724                 :             : 
     725                 :        1756 :   auto is_type = is_type_name (path);
     726                 :        1756 :   auto is_value = is_const_value_name (path);
     727                 :             : 
     728                 :             :   // In case we cannot find anything, we resolve the ambiguity to a type.
     729                 :             :   // This causes the typechecker to error out properly and when necessary.
     730                 :             :   // But types also take priority over const values in the case of
     731                 :             :   // ambiguities, hence the weird control flow
     732                 :        1756 :   if (is_type || (!is_type && !is_value))
     733                 :        1753 :     arg = arg.disambiguate_to_type ();
     734                 :           3 :   else if (is_value)
     735                 :           3 :     arg = arg.disambiguate_to_const ();
     736                 :        1756 : }
     737                 :             : 
     738                 :             : void
     739                 :        3580 : ResolveGenericArgs::resolve_disambiguated_generic (AST::GenericArg &arg)
     740                 :             : {
     741                 :        3580 :   switch (arg.get_kind ())
     742                 :             :     {
     743                 :           8 :     case AST::GenericArg::Kind::Const:
     744                 :           8 :       ResolveExpr::go (arg.get_expression (), prefix, canonical_prefix);
     745                 :           8 :       break;
     746                 :        3572 :     case AST::GenericArg::Kind::Type:
     747                 :        3572 :       ResolveType::go (arg.get_type ());
     748                 :        3572 :       break;
     749                 :           0 :     default:
     750                 :           0 :       rust_unreachable ();
     751                 :             :     }
     752                 :        3580 : }
     753                 :             : void
     754                 :        3322 : ResolveGenericArgs::go (AST::GenericArgs &generic_args)
     755                 :             : {
     756                 :        3322 :   auto empty = CanonicalPath::create_empty ();
     757                 :             : 
     758                 :        3322 :   go (generic_args, empty, empty);
     759                 :        3322 : }
     760                 :             : 
     761                 :             : void
     762                 :        3344 : ResolveGenericArgs::go (AST::GenericArgs &generic_args,
     763                 :             :                         const CanonicalPath &prefix,
     764                 :             :                         const CanonicalPath &canonical_prefix)
     765                 :             : {
     766                 :        3344 :   auto resolver = ResolveGenericArgs (prefix, canonical_prefix);
     767                 :             : 
     768                 :        6924 :   for (auto &arg : generic_args.get_generic_args ())
     769                 :             :     {
     770                 :        3580 :       if (arg.get_kind () == AST::GenericArg::Kind::Either)
     771                 :        1756 :         resolver.disambiguate (arg);
     772                 :             : 
     773                 :        3580 :       resolver.resolve_disambiguated_generic (arg);
     774                 :             :     }
     775                 :             : 
     776                 :        3400 :   for (auto &binding : generic_args.get_binding_args ())
     777                 :             :     {
     778                 :          56 :       ResolveType::go (binding.get_type ());
     779                 :             :     }
     780                 :        3344 : }
     781                 :             : 
     782                 :             : } // namespace Resolver
     783                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

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