From: Arthur Cohen <[email protected]>
Checking whether an item is an Item::Kind::Struct is not enough to
differentiate between
StructStruct and TupleStruct, so add a virtual `get_field_count` method to the
base
`AST::Struct` class to avoid dispatching.
gcc/rust/ChangeLog:
* ast/rust-item.h: New `get_field_count` virtual method for AST::Struct
and implementation
in AST::{StructStruct, TupleStruct}.
* expand/rust-derive-coerce-pointee.cc
(DeriveCoercePointee::validate_number_of_fields): Use
said new method.
Tested on x86_64-pc-linux-gnu.
---
gcc/rust/ast/rust-item.h | 6 ++++++
gcc/rust/expand/rust-derive-coerce-pointee.cc | 5 +++--
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 2129ac423cf..6fbe290a882 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1801,6 +1801,8 @@ public:
Identifier get_struct_name () const { return struct_name; }
+ virtual size_t get_field_count () const = 0;
+
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
@@ -2008,6 +2010,8 @@ public:
std::vector<StructField> &get_fields () { return fields; }
const std::vector<StructField> &get_fields () const { return fields; }
+ size_t get_field_count () const override { return fields.size (); }
+
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
@@ -2143,6 +2147,8 @@ public:
std::vector<TupleField> &get_fields () { return fields; }
const std::vector<TupleField> &get_fields () const { return fields; }
+ size_t get_field_count () const override { return fields.size (); }
+
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
diff --git a/gcc/rust/expand/rust-derive-coerce-pointee.cc
b/gcc/rust/expand/rust-derive-coerce-pointee.cc
index e23362b7563..de385878889 100644
--- a/gcc/rust/expand/rust-derive-coerce-pointee.cc
+++ b/gcc/rust/expand/rust-derive-coerce-pointee.cc
@@ -108,8 +108,9 @@ void
DeriveCoercePointee::validate_number_of_fields (const Item &item)
{
rust_assert (item.get_item_kind () == Item::Kind::Struct);
- auto &struct_item = static_cast<const StructStruct &> (item);
- size_t field_count = struct_item.get_fields ().size ();
+
+ auto &base_struct = static_cast<const Struct &> (item);
+ size_t field_count = base_struct.get_field_count ();
if (field_count == 0)
rust_error_at (item.get_locus (), ErrorCode::E0802,
--
2.55.0