Skip to content

Conversation

@lucasly-ba
Copy link

@lucasly-ba lucasly-ba commented Oct 14, 2025

Derive may only be applied to structs, enums and unions.

Fixes #3971

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc(ExpandVisitor::expand_inner_items): Added check and error.

gcc/testsuite/ChangeLog:

* rust/compile/issue-3971.rs: New test.

Signed-off-by: Lucas Ly Ba [email protected]

Comment on lines 184 to 192
if (item.get_item_kind () != AST::Item::Kind::Enum
&& item.get_item_kind () != AST::Item::Kind::Struct
&& item.get_item_kind () != AST::Item::Kind::Union)
{
rust_error_at (current.get_locus (),
"derive may only be applied to structs, "
"enums and unions");
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put that check in DeriveVisitor::derive instead:

DeriveVisitor::derive (Item &item, const Attribute &attr,

The ExpandVisitor is already a massive visitor, so I would rather we move specific checks to more specific visitors.

The code itself is great - maybe just create a temporary variable so it's not as long:

auto item_kind = item.get_item_kind();

if (item_kind != AST::Item::Kind::Struct && item_kind != ...)
    rust_error_at(...);

@lucasly-ba lucasly-ba force-pushed the 3972 branch 4 times, most recently from bdc4c1b to c246504 Compare October 16, 2025 16:43
Derive may only be applied to structs, enums and unions.

gcc/rust/ChangeLog:

	* expand/rust-derive.cc (DeriveVisitor::derive):
	Add check and error.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3971.rs: New test.

gcc/rust/ChangeLog:

	* expand/rust-derive.cc (DeriveVisitor::derive):
	* expand/rust-expand-visitor.cc:

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3971.rs: New test.

Signed-off-by: Lucas Ly Ba <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

segfault in expansion

2 participants