serde
serde copied to clipboard
Derive: Helper attributes on generic parameters are preserved
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.
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.
Maybe this (removing all attributes except built-ins) is something syn's Generics::split_for_impl should do?
This was proposed in https://github.com/dtolnay/syn/issues/422, but the issue was closed.