LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-derive-eq.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.8 % 111 103
Test Date: 2026-11-14 16:39:50 Functions: 91.7 % 12 11
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2025-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-derive-eq.h"
      20              : #include "rust-ast.h"
      21              : #include "rust-expr.h"
      22              : #include "rust-item.h"
      23              : #include "rust-path.h"
      24              : #include "rust-pattern.h"
      25              : #include "rust-system.h"
      26              : 
      27              : namespace Rust {
      28              : namespace AST {
      29              : 
      30              : static TypePath
      31         1213 : get_eq_trait_path (Builder &builder)
      32              : {
      33         6065 :   return builder.type_path ({builder.get_path_start (), "cmp", "Eq"},
      34         2426 :                             builder.has_opening_scope ());
      35              : }
      36              : 
      37          539 : DeriveEq::DeriveEq (location_t loc, Builder::Source item_source)
      38          539 :   : DeriveVisitor (loc, item_source)
      39          539 : {}
      40              : 
      41              : std::vector<std::unique_ptr<AST::Item>>
      42          539 : DeriveEq::go (Item &item)
      43              : {
      44          539 :   item.accept_vis (*this);
      45              : 
      46          539 :   return std::move (expanded);
      47              : }
      48              : 
      49              : std::unique_ptr<AssociatedItem>
      50          539 : DeriveEq::assert_receiver_is_total_eq_fn (
      51              :   std::vector<std::unique_ptr<Type>> &&types)
      52              : {
      53          539 :   auto stmts = std::vector<std::unique_ptr<Stmt>> ();
      54              : 
      55          539 :   stmts.emplace_back (assert_param_is_eq ());
      56              : 
      57         1230 :   for (auto &&type : types)
      58          691 :     stmts.emplace_back (assert_type_is_eq (std::move (type)));
      59              : 
      60          539 :   auto block = std::unique_ptr<BlockExpr> (
      61          539 :     new BlockExpr (std::move (stmts), nullptr, {}, {}, tl::nullopt, loc, loc));
      62              : 
      63          539 :   auto self = builder.self_ref_param ();
      64              : 
      65         2156 :   return builder.function ("assert_receiver_is_total_eq",
      66         1617 :                            vec (std::move (self)), {}, std::move (block));
      67          539 : }
      68              : 
      69              : std::unique_ptr<Stmt>
      70          539 : DeriveEq::assert_param_is_eq ()
      71              : {
      72          539 :   auto eq_bound = std::unique_ptr<TypeParamBound> (
      73          539 :     new TraitBound (get_eq_trait_path (builder), loc));
      74              : 
      75          539 :   auto sized_bound = std::unique_ptr<TypeParamBound> (
      76         1078 :     new TraitBound (builder.type_path (LangItem::Kind::SIZED), loc, false,
      77          539 :                     true /* opening_question_mark */));
      78              : 
      79          539 :   auto bounds = vec (std::move (eq_bound), std::move (sized_bound));
      80              : 
      81          539 :   auto assert_param_is_eq = "AssertParamIsEq";
      82              : 
      83          539 :   auto t = std::unique_ptr<GenericParam> (
      84         1078 :     new TypeParam (Identifier ("T"), loc, std::move (bounds)));
      85              : 
      86         1617 :   return builder.struct_struct (
      87         1078 :     assert_param_is_eq, vec (std::move (t)),
      88              :     {StructField (
      89         1617 :       Identifier ("_t"),
      90         1078 :       builder.single_generic_type_path (
      91              :         LangItem::Kind::PHANTOM_DATA,
      92          539 :         GenericArgs (
      93         1617 :           {}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
      94         1617 :       Visibility::create_private (), loc)});
      95          539 : }
      96              : 
      97              : std::unique_ptr<Stmt>
      98          691 : DeriveEq::assert_type_is_eq (std::unique_ptr<Type> &&type)
      99              : {
     100          691 :   auto assert_param_is_eq = "AssertParamIsEq";
     101              : 
     102              :   // AssertParamIsCopy::<Self>
     103          691 :   auto assert_param_is_eq_ty
     104              :     = std::unique_ptr<TypePathSegment> (new TypePathSegmentGeneric (
     105         1382 :       PathIdentSegment (assert_param_is_eq, loc), false,
     106         2073 :       GenericArgs ({}, {GenericArg::create_type (std::move (type))}, {}, loc),
     107         1382 :       loc));
     108              : 
     109              :   // TODO: Improve this, it's really ugly
     110          691 :   auto type_paths = std::vector<std::unique_ptr<TypePathSegment>> ();
     111          691 :   type_paths.emplace_back (std::move (assert_param_is_eq_ty));
     112              : 
     113          691 :   auto full_path
     114          691 :     = std::unique_ptr<Type> (new TypePath ({std::move (type_paths)}, loc));
     115              : 
     116          691 :   return builder.let (builder.wildcard (), std::move (full_path));
     117          691 : }
     118              : 
     119              : std::vector<std::unique_ptr<Item>>
     120          539 : DeriveEq::eq_impls (
     121              :   std::unique_ptr<AssociatedItem> &&fn, std::string name,
     122              :   const std::vector<std::unique_ptr<GenericParam>> &type_generics)
     123              : {
     124          539 :   auto eq = [this] () { return get_eq_trait_path (builder); };
     125          135 :   auto eq_bound = [&, this] () { return builder.trait_bound (eq ()); };
     126              : 
     127          539 :   auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
     128              : 
     129          539 :   auto trait_items = vec (std::move (fn));
     130              : 
     131          539 :   auto eq_generics = setup_impl_generics (name, type_generics, eq_bound);
     132          539 :   auto steq_generics = setup_impl_generics (name, type_generics);
     133              : 
     134         1078 :   auto eq_impl = builder.trait_impl (eq (), std::move (eq_generics.self_type),
     135              :                                      std::move (trait_items),
     136          539 :                                      std::move (eq_generics.impl));
     137              : 
     138              :   // StructuralEq is a marker trait
     139          539 :   decltype (trait_items) steq_trait_items = {};
     140              : 
     141          539 :   auto steq_impl
     142         1078 :     = builder.trait_impl (steq, std::move (steq_generics.self_type),
     143              :                           std::move (steq_trait_items),
     144          539 :                           std::move (steq_generics.impl));
     145              : 
     146          539 :   return vec (std::move (eq_impl), std::move (steq_impl));
     147         1617 : }
     148              : 
     149              : void
     150          130 : DeriveEq::visit_tuple (TupleStruct &item)
     151              : {
     152          130 :   auto types = std::vector<std::unique_ptr<Type>> ();
     153              : 
     154          260 :   for (auto &field : item.get_fields ())
     155          130 :     types.emplace_back (field.get_field_type ().reconstruct ());
     156              : 
     157          260 :   expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
     158          130 :                        item.get_identifier ().as_string (),
     159          260 :                        item.get_generic_params ());
     160          130 : }
     161              : 
     162              : void
     163          265 : DeriveEq::visit_struct (StructStruct &item)
     164              : {
     165          265 :   auto types = std::vector<std::unique_ptr<Type>> ();
     166              : 
     167          669 :   for (auto &field : item.get_fields ())
     168          404 :     types.emplace_back (field.get_field_type ().reconstruct ());
     169              : 
     170          530 :   expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
     171          265 :                        item.get_identifier ().as_string (),
     172          530 :                        item.get_generic_params ());
     173          265 : }
     174              : 
     175              : void
     176          144 : DeriveEq::visit_enum (Enum &item)
     177              : {
     178          144 :   auto types = std::vector<std::unique_ptr<Type>> ();
     179              : 
     180          599 :   for (auto &variant : item.get_variants ())
     181              :     {
     182          455 :       switch (variant->get_enum_item_kind ())
     183              :         {
     184          317 :         case EnumItem::Kind::Identifier:
     185          317 :         case EnumItem::Kind::Discriminant:
     186              :           // nothing to do as they contain no inner types
     187          317 :           continue;
     188          137 :         case EnumItem::Kind::Tuple:
     189          137 :           {
     190          137 :             auto &tuple = static_cast<EnumItemTuple &> (*variant);
     191              : 
     192          292 :             for (auto &field : tuple.get_tuple_fields ())
     193          155 :               types.emplace_back (field.get_field_type ().reconstruct ());
     194              :             break;
     195              :           }
     196            1 :         case EnumItem::Kind::Struct:
     197            1 :           {
     198            1 :             auto &tuple = static_cast<EnumItemStruct &> (*variant);
     199              : 
     200            3 :             for (auto &field : tuple.get_struct_fields ())
     201            2 :               types.emplace_back (field.get_field_type ().reconstruct ());
     202              : 
     203              :             break;
     204              :           }
     205          317 :         }
     206              :     }
     207              : 
     208          432 :   expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
     209          144 :                        item.get_identifier ().as_string (),
     210          288 :                        item.get_generic_params ());
     211          144 : }
     212              : 
     213              : void
     214            0 : DeriveEq::visit_union (Union &item)
     215              : {
     216            0 :   auto types = std::vector<std::unique_ptr<Type>> ();
     217              : 
     218            0 :   for (auto &field : item.get_variants ())
     219            0 :     types.emplace_back (field.get_field_type ().reconstruct ());
     220              : 
     221            0 :   expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
     222            0 :                        item.get_identifier ().as_string (),
     223            0 :                        item.get_generic_params ());
     224            0 : }
     225              : 
     226              : } // namespace AST
     227              : } // 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.