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.
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/aa74e9a32411367390f8a6e3448d596ade1b6c03
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5064
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 2129ac423..6fbe290a8 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 e23362b75..de3858788 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,
base-commit: 0f5ca4ae84d36f9c188aaf7fdb715dfcfea83796
--
2.56.0