LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-derive-coerce-pointee.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.6 % 70 69
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 11 11
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 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-attribute-values.h"
      20              : #include "rust-derive-coerce-pointee.h"
      21              : #include "rust-feature-store.h"
      22              : #include "rust-feature.h"
      23              : #include "rust-session-manager.h"
      24              : 
      25              : namespace Rust {
      26              : namespace AST {
      27              : 
      28           11 : DeriveCoercePointee::DeriveCoercePointee (location_t loc,
      29              :                                           Builder::Source item_source)
      30           11 :   : DeriveVisitor (loc, item_source)
      31           11 : {}
      32              : 
      33              : std::unique_ptr<AST::Item>
      34           11 : DeriveCoercePointee::go (Item &item)
      35              : {
      36           11 :   item.accept_vis (*this);
      37           22 :   Features::EarlyFeatureGateStore::get ().add (
      38              :     Feature::Name::DERIVE_COERCE_POINTEE,
      39           11 :     Error (loc, "use of unstable library feature %<derive_coerce_pointee%>"));
      40              : 
      41              :   // NOTE: This is technically a library feature, so I don't think this is
      42              :   // how it should be gated. But as far as I can tell at the moment we have
      43              :   // no mechanisms for handling these, and furthermore this will go through
      44              :   // our compatibility layer.
      45              :   //
      46              :   // What this means is that the `CoercePointee` macro will *not* be defined
      47              :   // in the `core` that we will be using to compile the kernel at first.
      48              :   //
      49              :   // The basic example for `CoercePointee` is something like this:
      50              :   //
      51              :   // ```rust
      52              :   // use std::marker::CoercePointee;
      53              :   //
      54              :   // #[derive(CoercePointee)]
      55              :   // struct Flip<Flop>(*const Flop);
      56              :   // ```
      57              :   //
      58              :   // but the marker will not be present in the `core` we will be using at
      59              :   // first, so we will need to fake its existence with
      60              :   // -frust-compat-version, and then the compiler can assume that it is a
      61              :   // built-in derive. At least that's my expectation.
      62              : 
      63           11 :   if (!Session::get_instance ().should_support_coerce_pointee ())
      64              :     {
      65            1 :       rust_error_at (loc, "derive(CoercePointee) requires a compatibility mode "
      66              :                           "greater or equal to 1.84");
      67            1 :       return {};
      68              :     }
      69              : 
      70           10 :   rust_warning_at (
      71              :     loc, 0,
      72              :     "derive(CoercePointee) is currently unimplemented and has no effect");
      73              : 
      74           10 :   return {};
      75              : }
      76              : 
      77              : // CoercePointee requires the Struct or Tuple to have transparent representation
      78              : void
      79            8 : DeriveCoercePointee::validate_repr_transparent (const Item &item)
      80              : {
      81            8 :   const auto &attrs = item.get_outer_attrs ();
      82            8 :   for (const auto &attr : attrs)
      83              :     {
      84            7 :       if (attr.get_path ().as_string () != Values::Attributes::REPR)
      85            0 :         continue;
      86            7 :       rust_assert (!attr.empty_input ());
      87              : 
      88            7 :       const auto &attr_input = attr.get_attr_input ();
      89            7 :       rust_assert (attr_input.get_attr_input_type ()
      90              :                    == AST::AttrInput::AttrInputType::TOKEN_TREE);
      91              : 
      92            7 :       auto meta_item = static_cast<const AST::DelimTokenTree &> (attr_input)
      93            7 :                          .parse_to_meta_item ();
      94            7 :       rust_assert (meta_item);
      95              : 
      96            7 :       for (const auto &item : meta_item->get_items ())
      97            7 :         if (item->as_string () == "transparent")
      98            8 :           return;
      99              :     }
     100            1 :   rust_error_at (item.get_locus (), ErrorCode::E0802,
     101              :                  "%<CoercePointee%> is only applicable to struct/tuple with "
     102              :                  "repr(transparent) layout");
     103              : }
     104              : 
     105              : // Structs or Tuples with CoercePointee must have minimum of one field else
     106              : // compile time error
     107              : void
     108            8 : DeriveCoercePointee::validate_number_of_fields (const Item &item)
     109              : {
     110            8 :   rust_assert (item.get_item_kind () == Item::Kind::Struct);
     111              : 
     112            8 :   auto &base_struct = static_cast<const Struct &> (item);
     113            8 :   size_t field_count = base_struct.get_field_count ();
     114              : 
     115            8 :   if (field_count == 0)
     116            1 :     rust_error_at (item.get_locus (), ErrorCode::E0802,
     117              :                    "%<CoercePointee%> can only be derived on a struct "
     118              :                    "with at least one field");
     119            8 : }
     120              : 
     121              : // #[pointee] needs to be on a generic type parameter, and if there are 2 or
     122              : // more generic parameters (not traits), one of them must have the attribute
     123              : void
     124            8 : DeriveCoercePointee::validate_non_generic_pointee (const Rust::AST::Item &item)
     125              : {
     126            8 :   rust_assert (item.get_item_kind () == Item::Kind::Struct);
     127            8 :   const StructStruct &struct_item = static_cast<const StructStruct &> (item);
     128            8 :   const std::vector<std::unique_ptr<GenericParam>> &generic_params
     129            8 :     = struct_item.get_generic_params ();
     130              : 
     131              :   // Implicitly it becomes the pointee
     132            8 :   if (generic_params.size () == 1)
     133              :     return;
     134              : 
     135              :   size_t pointee_count = 0;
     136              : 
     137            9 :   for (size_t i = 0; i < generic_params.size (); ++i)
     138              :     {
     139            6 :       if (generic_params[i].get ()->get_kind () != GenericParam::Kind::Const
     140            6 :           && (generic_params)[i].get ()->get_kind ()
     141              :                != GenericParam::Kind::Lifetime)
     142              :         {
     143            6 :           auto &type_param
     144            6 :             = static_cast<TypeParam &> (*(generic_params)[i].get ());
     145              : 
     146            9 :           for (const auto &attr : type_param.get_outer_attrs ())
     147              :             {
     148            3 :               if (attr.get_path ().as_string () == Values::Attributes::POINTEE)
     149            3 :                 pointee_count += 1;
     150              :             }
     151              :         }
     152              :     }
     153              : 
     154            3 :   if (pointee_count == 0 || pointee_count > 1)
     155            2 :     rust_error_at (item.get_locus (), ErrorCode::E0802,
     156              :                    "exactly one generic type parameter must be marked "
     157              :                    "as %<#[pointee]%> to derive %<CoercePointee%> traits");
     158              : }
     159              : 
     160              : // validates all the requirements for a type to be derived as CoercePointee
     161              : void
     162            8 : DeriveCoercePointee::validate (const Item &item)
     163              : {
     164            8 :   validate_repr_transparent (item);
     165            8 :   validate_number_of_fields (item);
     166            8 :   validate_non_generic_pointee (item);
     167            8 : }
     168              : 
     169              : void
     170            6 : DeriveCoercePointee::visit_struct (StructStruct &item)
     171              : {
     172            6 :   validate (item);
     173            6 : }
     174              : 
     175              : void
     176            2 : DeriveCoercePointee::visit_tuple (TupleStruct &item)
     177              : {
     178            2 :   validate (item);
     179            2 : }
     180              : 
     181              : void
     182            2 : DeriveCoercePointee::visit_enum (Enum &item)
     183              : {
     184            2 :   error_for_non_supported_objects (item);
     185            2 : }
     186              : 
     187              : void
     188            1 : DeriveCoercePointee::visit_union (Union &item)
     189              : {
     190            1 :   error_for_non_supported_objects (item);
     191            1 : }
     192              : 
     193              : void
     194            3 : DeriveCoercePointee::error_for_non_supported_objects (const Item &item)
     195              : {
     196            3 :   rust_error_at (item.get_locus (), ErrorCode::E0802,
     197              :                  "%<CoercePointee%> can only be derived on structs with "
     198              :                  "%<#[repr(transparent)]%>");
     199            3 : }
     200              : } // namespace AST
     201              : } // 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.