Skip to content

Conversation

@Pasta-coder
Copy link
Contributor

@Pasta-coder Pasta-coder commented Jan 6, 2026

The DeriveVisitor previously dispatched calls to specific handlers (like DeriveClone) without validating the input item type. This caused internal compiler errors (ICEs) when users attempted to derive traits on invalid items like functions or traits, as the handlers assumed the input was a struct, enum, or union.

This patch adds a validation predicate to the DeriveVisitor to reject invalid items gracefully with a diagnostic error before dispatching.

Fixes #3875

gcc/rust/ChangeLog:

* expand/rust-derive.h: Add validate_item prototype.
* expand/rust-derive.cc (DeriveVisitor::derive): Use visitor validation.
(DeriveVisitor::validate_item): New function to check item kind.

gcc/testsuite/ChangeLog:

* rust/compile/derive-on-invalid-item.rs: New test.

@dkm
Copy link
Member

dkm commented Jan 6, 2026

Hello, thanks for your contribution!

Small nitpick on the Changelog part...

* expand/rust-derive.h: Add validate_item prototype.

Does not target a specific "item". You could be more precise using:

* expand/rust-derive.h (DeriveVisitor::validate_item): New.

@Pasta-coder Pasta-coder force-pushed the issue-3875 branch 3 times, most recently from ff0806a to 9e84d88 Compare January 6, 2026 16:06
@Pasta-coder
Copy link
Contributor Author

Pasta-coder commented Jan 6, 2026

@dkm hello,

commit message regarding changelog changed .

thanks

@Pasta-coder Pasta-coder force-pushed the issue-3875 branch 4 times, most recently from c78aad2 to 11bd185 Compare January 6, 2026 21:37
The DeriveVisitor previously dispatched calls to specific handlers (like
DeriveClone) without validating the input item type. This caused internal
compiler errors (ICEs) when users attempted to derive traits on invalid items
like functions or traits, as the handlers assumed the input was a struct,
enum, or union.

This patch adds a validation predicate `validate_item` to the DeriveVisitor
to reject invalid items gracefully with a diagnostic error before dispatching.

Fixes Rust-GCC#3875

gcc/rust/ChangeLog:

	* expand/rust-derive.h (DeriveVisitor::validate_item): Add prototype.
	* expand/rust-derive.cc (DeriveVisitor::derive): Use visitor validation.
	(DeriveVisitor::validate_item): New function to check item kind.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
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.

ICE in go, at rust/expand/rust-derive-clone.cc:119

2 participants