-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Proc-macro helper attributes on lifetime and const parameters fail to resolve if a built-in derive macro is present #132561
Comments
Minimal:
extern crate proc_macro;
#[proc_macro_derive(M, attributes(x))]
pub fn m(_: proc_macro::TokenStream) -> proc_macro::TokenStream { Default::default() }
#[derive(a::M, Clone)] struct S<#[x] 'a>(&'a ());
|
It seems that the issue is that the built-in macros don't remove the attributes from lifetimes and const generics for some reason, when expanding their implementations, while they do for normal type generics. Sounds like this should be pretty easy to fix, and I would love to contribute. The issue is probably in the |
The relevant code is here: rust/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs Lines 631 to 681 in c682aa1
As you've correctly guessed, for type parameters the attributes are effectively removed (well, a new type parameter gets created with Footnotes |
However, simply removing all attributes also feels very weird to me. It means we strip #![deny(non_camel_case_types)]
// #[derive(PartialEq)] // <-- uncomment to make this fail
struct S<#[allow(non_camel_case_types)] t>(t); |
Yes I agree, maybe there should be a list of built-in attributes that should not be removed? If I understand correctly, normal proc macros don't even "see" the helper attributes of other macros. Built-in macros should work the same way, that means removing specifically helper attributes defined by other proc macros on that item |
I don't think that is true (but I wish that was case)? How did you test this? |
Well, this all seems a bit tricky. I think you should go ahead and open a PR that removes these attributes unconditionally. I'll think about this some more and probably get into contact with other people to discuss this, too. |
Oops yeah, you're right.
Ok sounds good! |
Remove attributes from generics in built-in derive macros Related issue rust-lang#132561 Removes all attributes from generics in the expanded implementations of built-in derive macros.
Rollup merge of rust-lang#132651 - PonasKovas:master, r=fmease Remove attributes from generics in built-in derive macros Related issue rust-lang#132561 Removes all attributes from generics in the expanded implementations of built-in derive macros.
I was making a derive proc macro and stumbled upon this weird error, and was told on the rust discord that this might be a bug. Basically, I have a proc macro that uses a helper attribute like this:
The
#[test_helper]
attribute is used on generics - including lifetime generics.When I use it normally it works fine, but if I try to derive it together with another trait, such as
Clone
- it fails to compile, saying that#[test_helper]
was not found in the scope.There is no error if I put the attribute on a type generic or on a field.
cargo expand
-ed code:I have also made a minimal reproducible example.
Meta
rustc --version --verbose
:If this is really a bug, I can try to investigate if anyone can help me and point me in the right direction.
The text was updated successfully, but these errors were encountered: