serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.82k stars 748 forks source link

Derive: Helper attributes on generic parameters are preserved #2710

Open NyxCode opened 4 months ago

NyxCode commented 4 months ago

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 commented 4 months ago

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

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

NyxCode commented 4 months ago

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

WilsonGramer commented 4 months ago

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