Why are proc-macro derives prohibited for Unions?


#1

I wrote a procedural macro for union types but noticed that custom deriving is allowed only for struct/enum.

Unions support some built-in derives, so it is reasonable to allow custom derives.
Is there any reason for this limitation?

error: proc-macro derives may only be applied to struct/enum items
  --> src/value.rs:48:10
   |
48 | #[derive(Still)]
   |          ^^^^^

#2

This looks 100% like an omission. I didn’t find anything digging around the issue tracker, so I suggest raising an issue.


#3

Thank you! I’ll open an issue.

opened: https://github.com/rust-lang/rust/issues/50223


#4

Guess its down unsafe.

From pr (Quick scan seems to have been removed/replaced.)
+Since accessing union fields reliably requires extra knowledge, traits trying to
+do it (e.g. PartialEq) cannot be derived automatically.


#5

Yes, some traits cannot be implemented safely.

I want to create a derive to guarantee that all union fields implement a specific trait.
Such derives are safe for any unions because their fields are not accessed at run-time.