LCOV - code coverage report
Current view: top level - gcc/rust/typecheck - rust-hir-type-check-enumitem.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.5 % 141 136
Test Date: 2026-04-20 14:57:17 Functions: 100.0 % 6 6
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #include "rust-hir-expr.h"
      20              : #include "rust-hir-type-check-type.h"
      21              : #include "rust-hir-type-check-expr.h"
      22              : #include "rust-hir-type-check-enumitem.h"
      23              : #include "rust-type-util.h"
      24              : #include "rust-immutable-name-resolution-context.h"
      25              : 
      26              : namespace Rust {
      27              : namespace Resolver {
      28              : 
      29              : TyTy::VariantDef *
      30         1215 : TypeCheckEnumItem::Resolve (HIR::EnumItem &item, int64_t last_discriminant)
      31              : {
      32         1215 :   TypeCheckEnumItem resolver (last_discriminant);
      33         1215 :   switch (item.get_enum_item_kind ())
      34              :     {
      35          441 :     case HIR::EnumItem::EnumItemKind::Named:
      36          441 :       resolver.visit (static_cast<HIR::EnumItem &> (item));
      37          441 :       break;
      38              : 
      39          414 :     case HIR::EnumItem::EnumItemKind::Tuple:
      40          414 :       resolver.visit (static_cast<HIR::EnumItemTuple &> (item));
      41          414 :       break;
      42              : 
      43           86 :     case HIR::EnumItem::EnumItemKind::Struct:
      44           86 :       resolver.visit (static_cast<HIR::EnumItemStruct &> (item));
      45           86 :       break;
      46              : 
      47          274 :     case HIR::EnumItem::EnumItemKind::Discriminant:
      48          274 :       resolver.visit (static_cast<HIR::EnumItemDiscriminant &> (item));
      49          274 :       break;
      50              :     }
      51         1215 :   return resolver.variant;
      52         1215 : }
      53              : 
      54         1215 : TypeCheckEnumItem::TypeCheckEnumItem (int64_t last_discriminant)
      55         1215 :   : TypeCheckBase (), variant (nullptr), last_discriminant (last_discriminant)
      56         1215 : {}
      57              : 
      58              : void
      59          441 : TypeCheckEnumItem::visit (HIR::EnumItem &item)
      60              : {
      61          441 :   if (last_discriminant == INT64_MAX)
      62            0 :     rust_error_at (item.get_locus (), "discriminant too big");
      63              : 
      64          882 :   Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
      65          441 :                                  item.get_mappings ().get_nodeid (),
      66          441 :                                  mappings.get_next_hir_id (
      67          441 :                                    item.get_mappings ().get_crate_num ()),
      68          441 :                                  item.get_mappings ().get_local_defid ());
      69          441 :   auto discim_expr = std::make_unique<HIR::LiteralExpr> (
      70          882 :     HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
      71              :                       HIR::Literal::LitType::INT,
      72         1323 :                       PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
      73              : 
      74          441 :   TyTy::BaseType *isize = nullptr;
      75          441 :   bool ok = context->lookup_builtin ("isize", &isize);
      76          441 :   rust_assert (ok);
      77          441 :   context->insert_type (mapping, isize);
      78              : 
      79          441 :   auto &nr_ctx
      80          441 :     = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
      81              : 
      82          441 :   CanonicalPath canonical_path
      83          441 :     = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ());
      84              : 
      85          441 :   RustIdent ident{canonical_path, item.get_locus ()};
      86          441 :   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
      87          441 :                                   item.get_mappings ().get_defid (),
      88          441 :                                   item.get_identifier ().as_string (), ident,
      89         1764 :                                   std::move (discim_expr));
      90          441 : }
      91              : 
      92              : void
      93          274 : TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item)
      94              : {
      95          274 :   if (last_discriminant == INT64_MAX)
      96            0 :     rust_error_at (item.get_locus (), "discriminant too big");
      97              : 
      98          274 :   auto &discriminant = item.get_discriminant_expression ();
      99          274 :   auto capacity_type = TypeCheckExpr::Resolve (discriminant);
     100          274 :   if (capacity_type->get_kind () == TyTy::TypeKind::ERROR)
     101            0 :     return;
     102              : 
     103          274 :   TyTy::ISizeType *expected_ty
     104          274 :     = new TyTy::ISizeType (discriminant.get_mappings ().get_hirid ());
     105          274 :   context->insert_type (discriminant.get_mappings (), expected_ty);
     106              : 
     107          274 :   unify_site (item.get_mappings ().get_hirid (),
     108          274 :               TyTy::TyWithLocation (expected_ty),
     109          274 :               TyTy::TyWithLocation (capacity_type), item.get_locus ());
     110              : 
     111          274 :   auto &nr_ctx
     112          274 :     = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     113              : 
     114          274 :   CanonicalPath canonical_path
     115          274 :     = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ());
     116              : 
     117          274 :   RustIdent ident{canonical_path, item.get_locus ()};
     118          274 :   variant
     119          274 :     = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
     120          274 :                             item.get_mappings ().get_defid (),
     121          274 :                             item.get_identifier ().as_string (), ident,
     122         1096 :                             item.get_discriminant_expression ().clone_expr ());
     123          274 : }
     124              : 
     125              : void
     126          414 : TypeCheckEnumItem::visit (HIR::EnumItemTuple &item)
     127              : {
     128          414 :   if (last_discriminant == INT64_MAX)
     129            0 :     rust_error_at (item.get_locus (), "discriminant too big");
     130              : 
     131          414 :   std::vector<TyTy::StructFieldType *> fields;
     132          414 :   size_t idx = 0;
     133          842 :   for (auto &field : item.get_tuple_fields ())
     134              :     {
     135          428 :       TyTy::BaseType *field_type
     136          428 :         = TypeCheckType::Resolve (field.get_field_type ());
     137          428 :       TyTy::StructFieldType *ty_field
     138          856 :         = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
     139          428 :                                      std::to_string (idx), field_type,
     140          428 :                                      field.get_locus ());
     141          428 :       fields.push_back (ty_field);
     142          428 :       context->insert_type (field.get_mappings (), ty_field->get_field_type ());
     143          428 :       idx++;
     144              :     }
     145              : 
     146          828 :   Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
     147          414 :                                  item.get_mappings ().get_nodeid (),
     148          414 :                                  mappings.get_next_hir_id (
     149          414 :                                    item.get_mappings ().get_crate_num ()),
     150          414 :                                  item.get_mappings ().get_local_defid ());
     151          414 :   auto discim_expr = std::make_unique<HIR::LiteralExpr> (
     152          828 :     HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
     153              :                       HIR::Literal::LitType::INT,
     154         1242 :                       PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
     155              : 
     156          414 :   TyTy::BaseType *isize = nullptr;
     157          414 :   bool ok = context->lookup_builtin ("isize", &isize);
     158          414 :   rust_assert (ok);
     159          414 :   context->insert_type (mapping, isize);
     160              : 
     161          414 :   auto &nr_ctx
     162          414 :     = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     163              : 
     164          414 :   CanonicalPath canonical_path
     165          414 :     = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ());
     166              : 
     167          414 :   RustIdent ident{canonical_path, item.get_locus ()};
     168          414 :   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
     169          414 :                                   item.get_mappings ().get_defid (),
     170          414 :                                   item.get_identifier ().as_string (), ident,
     171              :                                   TyTy::VariantDef::VariantType::TUPLE,
     172         1656 :                                   std::move (discim_expr), fields);
     173          414 : }
     174              : 
     175              : void
     176           86 : TypeCheckEnumItem::visit (HIR::EnumItemStruct &item)
     177              : {
     178           86 :   if (last_discriminant == INT64_MAX)
     179            0 :     rust_error_at (item.get_locus (), "discriminant too big");
     180              : 
     181           86 :   std::vector<TyTy::StructFieldType *> fields;
     182          228 :   for (auto &field : item.get_struct_fields ())
     183              :     {
     184          142 :       TyTy::BaseType *field_type
     185          142 :         = TypeCheckType::Resolve (field.get_field_type ());
     186          142 :       TyTy::StructFieldType *ty_field
     187          142 :         = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
     188          142 :                                      field.get_field_name ().as_string (),
     189          284 :                                      field_type, field.get_locus ());
     190          142 :       fields.push_back (ty_field);
     191          142 :       context->insert_type (field.get_mappings (), ty_field->get_field_type ());
     192              :     }
     193              : 
     194          172 :   Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
     195           86 :                                  item.get_mappings ().get_nodeid (),
     196           86 :                                  mappings.get_next_hir_id (
     197           86 :                                    item.get_mappings ().get_crate_num ()),
     198           86 :                                  item.get_mappings ().get_local_defid ());
     199           86 :   auto discrim_expr = std::make_unique<HIR::LiteralExpr> (
     200          172 :     HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
     201              :                       HIR::Literal::LitType::INT,
     202          258 :                       PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
     203              : 
     204           86 :   TyTy::BaseType *isize = nullptr;
     205           86 :   bool ok = context->lookup_builtin ("isize", &isize);
     206           86 :   rust_assert (ok);
     207           86 :   context->insert_type (mapping, isize);
     208              : 
     209           86 :   auto &nr_ctx
     210           86 :     = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     211              : 
     212           86 :   CanonicalPath canonical_path
     213           86 :     = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid ());
     214              : 
     215           86 :   RustIdent ident{canonical_path, item.get_locus ()};
     216           86 :   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
     217           86 :                                   item.get_mappings ().get_defid (),
     218           86 :                                   item.get_identifier ().as_string (), ident,
     219              :                                   TyTy::VariantDef::VariantType::STRUCT,
     220          344 :                                   std::move (discrim_expr), fields);
     221           86 : }
     222              : 
     223              : } // namespace Resolver
     224              : } // namespace Rust
        

Generated by: LCOV version 2.4-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.