serde icon indicating copy to clipboard operation
serde copied to clipboard

Derive: Helper attributes on generic parameters are preserved

Open NyxCode opened this issue 1 year ago • 3 comments
trafficstars

When the derive macros generate the impl Serialize and impl Deserialize, all attributes on generic parameters are preserved.

Example:

#[derive(Serialize, OtherMacro)]
struct X<#[some_helper_attribute] Y>(Y);

results in

impl<#[some_helper_attribute] Y> Serialize for X<Y> where Y: Serialize { ... }

causing this error: cannot find attribute 'some_helper_attribute' in this scope


This becomes a problem when #[derive(Serialize)] is used together with other derive macros which use helper attributes on generic parameters.

In the example above, #[some_helper_attribute] is a helper attribute of the derive macro OtherMacro.

These helper attributes are preserved by serde, and copied into the impl block. There, they are outside of the context of the derive macro OtherMacro, and cause a "cannot find attribute .. in this scope" error.

NyxCode avatar Mar 10 '24 03:03 NyxCode

As far as I can tell, the only attributes valid in this position are

  • built-ins like #[cfg(..)] and #[cfg_attr(..)]
  • derive helper attributes

Specifically, attribute-style proc macros are not allowed there.

NyxCode avatar Mar 10 '24 09:03 NyxCode

Maybe this (removing all attributes except built-ins) is something syn's Generics::split_for_impl should do?

NyxCode avatar Mar 10 '24 13:03 NyxCode

This was proposed in https://github.com/dtolnay/syn/issues/422, but the issue was closed.

WilsonGramer avatar Mar 10 '24 15:03 WilsonGramer