Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive: Helper attributes on generic parameters are preserved #2710

Open
NyxCode opened this issue Mar 10, 2024 · 3 comments
Open

Derive: Helper attributes on generic parameters are preserved #2710

NyxCode opened this issue Mar 10, 2024 · 3 comments

Comments

@NyxCode
Copy link

NyxCode commented Mar 10, 2024

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
Copy link
Author

NyxCode commented Mar 10, 2024

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
Copy link
Author

NyxCode commented Mar 10, 2024

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

@WilsonGramer
Copy link

This was proposed in dtolnay/syn#422, but the issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants