You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is not technically necessary because proc macro attributes can remove these attributes from their input unlike derives, but should be supportable pretty easily as a convenience feature once #64694 lands.
Good point; I do think it would be useful for the language to do this automatically as an ergonomics thing from the POV of a macro author myself. Nominating based on @petrochenkov's comment to see what the preliminary interest is in the language team. We may or may not ask for an RFC based on the discussion.
Can we clarify what the expected behavior is here?
If you include attributes(helper), does it just strip out that attribute from wherever it appears in the final output?
I'm generally positive on the idea but would like to see a more complete write-up, at minimum. I mildly prefer a small RFC but I think a PR that links to a good write-up would probably suffice.
(Presumably we would then create a tracking issue.)
This is not technically necessary because proc macro attributes can remove these attributes from their input unlike derives, but should be supportable pretty easily as a convenience feature once #64694 lands.
Is this always true? I just tried this with a proc macro attribute on a function, and the compiler attempted to expand the "helper" attribute and failed before the macro got a chance to strip it. In my scenario the helper attribute was placed on a statement in the function, if that makes any difference.
I just tried a minimal example and it worked as you describe, I imagine it's likely I wasn't properly stripping out the helper in my larger example. So is this safe behavior to rely on right now? Similar questions were asked in rust-lang/reference#578 and rust-lang/reference#692 but it doesn't look like there's a clear answer. Perhaps helper attribute syntax would be nice but I think documenting the behavior might go a long way.
Having gone through and stumbled over this myself just recently (I actually thought adding a remark to the docs would make sense: rust-lang/reference#716) I must say I really think this needs no compiler support. Here are just a few simple lines (using the syn crate) that completely obliterate all attributes from the AST given to it: extrawurst/alpaca-rust@46dbba6#diff-8f097454b072a95a96c470f9c3f84c5fR136-R151 but that could easily be extended to just the attributes that you want to remove.
And none of the derives has to worry that serde attribute will be stolen by some other macro.
In attribute macros this is probably trickier - you could for example check other attributes that are applied to struct, and skip cleaning helper attributes if you know that this macro will use them. The downside is that this can break easily if user renames attribute or imports it from unrelated liblary.
Since 1.52 and #79078 I think this is no longer an issue: the primary attribute can strip out all helper attributes, with the sole restriction that helper attributes cannot appear before the primary attribute (this restriction also applies to #[derive] now).
If you include attributes(helper), does it just strip out that attribute from wherever it appears in the final output?
This potential feature would now be the sole point of "helper attributes", but in my opinion it's just not necessary (to be useful, custom parsing code must match them anyway, so it might as well remove them from the output too).
You can't just start stripping the field attributes, because the second call to macro_x might require them.
A solution would be to parse all calls to macro_x the first time macro_x is called, this is problematic because we might be unable to determine macro_xs path:
Now both problems are fixed, helper attributes don't need to be stripped and #[macro_x(...)] isn't a path anymore but has to be written a certain way to be valid.
This is just an example that can work with a derive, others might not, in any case, it would be appreciated if you could re-open the issue @dhardy.
@daxpedda actually, what you mean is the ability to have multiple uses of a single attribute which can observe other uses of that same attribute, and without the first instance doing the work of implementing all instances (which is also possible).
Note that there was also an afore-mentioned point about paths and renaming of the macro import, which is still a potential issue here.
... the first instance doing the work of implementing all instances (which is also possible).
The reason this isn't possible, is because of
... paths and renaming of the macro import ...
The solution to that problem is to replace multiple uses of a proc_macro_attribute with a single use of a proc_macro_attribute. The only need to have multiple uses of a proc_macro_attribute is because it would contain different options. So to implement this solution, all options have to be moved to a helper attribute:
This avoids any pathing issues, because attribute helpers don't use paths. This still requires cleaning all helper attributes and is, from the point of view of syntactic effort, equivalent to:
With the additional benefit of not requiring to clean up helper attributes.
Of course this whole argument only makes sense when talking about using proc_macro_attribute on structs, enums or unions, derive isn't usable anywhere else, but proc_macro_attribute is.
Not trying to argue against myself here. I would still like to see support for attribute helpers in proc_macro_attribute, as it would not require this "workaround". Just want to make clear what the issues are and point out all workarounds so the Rust team can make the best decisions ❤️.
Another case this would be really helpful, which I don't think anyone's mentioned yet, is for when there's a syntax error in the item the attribute is on, e.g.:
#[foo]fnbar(#[baz]y:i32,#[baz]z:i32,){let x = y.z z();// ^ cursor here, user was in the middle of typing, and hit ctrl-S or flycheck kicked in}
Now, not only are rustc and syn yelling at the user that there's a syntax error, but rustc is also complaining that the attribute baz wasn't found, which can make it really difficult to figure out where the syntax error actually is, especially when the attribute is on a bigger function or module. Obviously, syntax errors aren't necessarily the case to optimize for, but this is something that I feel would really improve the DX of using proc macros in IDEs.
Another silly trick you can use if you're lazy and attribute macro is supposed to be applied to the type: make derive macro with helper attribute and derive it.
Activity
Centril commentedon Oct 26, 2019
cc @petrochenkov
petrochenkov commentedon Oct 26, 2019
This is not technically necessary because proc macro attributes can remove these attributes from their input unlike derives, but should be supportable pretty easily as a convenience feature once #64694 lands.
Centril commentedon Oct 26, 2019
Good point; I do think it would be useful for the language to do this automatically as an ergonomics thing from the POV of a macro author myself. Nominating based on @petrochenkov's comment to see what the preliminary interest is in the language team. We may or may not ask for an RFC based on the discussion.
nikomatsakis commentedon Oct 31, 2019
Can we clarify what the expected behavior is here?
If you include
attributes(helper), does it just strip out that attribute from wherever it appears in the final output?I'm generally positive on the idea but would like to see a more complete write-up, at minimum. I mildly prefer a small RFC but I think a PR that links to a good write-up would probably suffice.
(Presumably we would then create a tracking issue.)
calebzulawski commentedon Nov 3, 2019
Is this always true? I just tried this with a proc macro attribute on a function, and the compiler attempted to expand the "helper" attribute and failed before the macro got a chance to strip it. In my scenario the helper attribute was placed on a statement in the function, if that makes any difference.
petrochenkov commentedon Nov 3, 2019
@calebzulawski
Could you give a minimized reproduction? This shouldn't happen in theory.
calebzulawski commentedon Nov 3, 2019
I just tried a minimal example and it worked as you describe, I imagine it's likely I wasn't properly stripping out the helper in my larger example. So is this safe behavior to rely on right now? Similar questions were asked in rust-lang/reference#578 and rust-lang/reference#692 but it doesn't look like there's a clear answer. Perhaps helper attribute syntax would be nice but I think documenting the behavior might go a long way.
extrawurst commentedon Nov 13, 2019
Having gone through and stumbled over this myself just recently (I actually thought adding a remark to the docs would make sense: rust-lang/reference#716) I must say I really think this needs no compiler support. Here are just a few simple lines (using the syn crate) that completely obliterate all attributes from the AST given to it: extrawurst/alpaca-rust@46dbba6#diff-8f097454b072a95a96c470f9c3f84c5fR136-R151 but that could easily be extended to just the attributes that you want to remove.
macpp commentedon Nov 17, 2019
This will make things easier when multiple attribute macros use the same helper attributes. With
proc_macro_deriveyou can writeAnd none of the derives has to worry that
serdeattribute will be stolen by some other macro.In attribute macros this is probably trickier - you could for example check other attributes that are applied to struct, and skip cleaning helper attributes if you know that this macro will use them. The downside is that this can break easily if user renames attribute or imports it from unrelated liblary.
4 remaining items
dhardy commentedon Nov 15, 2021
Since 1.52 and #79078 I think this is no longer an issue: the primary attribute can strip out all helper attributes, with the sole restriction that helper attributes cannot appear before the primary attribute (this restriction also applies to
#[derive]now).This goes back to @nikomatsakis's comment:
This potential feature would now be the sole point of "helper attributes", but in my opinion it's just not necessary (to be useful, custom parsing code must match them anyway, so it might as well remove them from the output too).
daxpedda commentedon Nov 16, 2021
The issue arises when you need support for multiple calls to a
proc_macro_attribute.Imagine a
proc_macro_attributecalledmacro_xwith an attributeattr_y:You can't just start stripping the field attributes, because the second call to
macro_xmight require them.A solution would be to parse all calls to
macro_xthe first timemacro_xis called, this is problematic because we might be unable to determinemacro_xs path:This ofc can be solved by just switching to
proc_macro_derivebecause it does support helper attributes:Now both problems are fixed, helper attributes don't need to be stripped and
#[macro_x(...)]isn't a path anymore but has to be written a certain way to be valid.This is just an example that can work with a
derive, others might not, in any case, it would be appreciated if you could re-open the issue @dhardy.dhardy commentedon Nov 17, 2021
Fair argument, though personally I don't agree.
Your example uses
derive(macro_x)to do the parsing andmacro_xas a parameter.But you can do the same with any type of proc macro preceeding the repeated attribute, e.g.
The only difference from
proc_macro_deriveis that your macro is responsible for removing the attribute from output.ModProg commentedon Nov 18, 2021
The idea was to have multiple calls to a proc macro being able to read the same attributes without having to delete them.
petrochenkov commentedon Nov 18, 2021
That's a pretty good motivation, IMO.
daxpedda commentedon Nov 18, 2021
@dhardy you are right that this is exactly the same as the
deriveexample I gave above. The issue here is that it's necessary.Basically what I would like is to have the ability to do this:
Without a necessary line on top.
I hope my explanation helps.
dhardy commentedon Nov 18, 2021
@daxpedda actually, what you mean is the ability to have multiple uses of a single attribute which can observe other uses of that same attribute, and without the first instance doing the work of implementing all instances (which is also possible).
Note that there was also an afore-mentioned point about paths and renaming of the macro import, which is still a potential issue here.
daxpedda commentedon Nov 19, 2021
The goal is
The way to implement this, is to have
The reason this isn't possible, is because of
The solution to that problem is to replace multiple uses of a
proc_macro_attributewith a single use of aproc_macro_attribute. The only need to have multiple uses of aproc_macro_attributeis because it would contain different options. So to implement this solution, all options have to be moved to a helper attribute:This avoids any pathing issues, because attribute helpers don't use paths. This still requires cleaning all helper attributes and is, from the point of view of syntactic effort, equivalent to:
With the additional benefit of not requiring to clean up helper attributes.
Of course this whole argument only makes sense when talking about using
proc_macro_attributeonstructs,enums orunions,deriveisn't usable anywhere else, butproc_macro_attributeis.Not trying to argue against myself here. I would still like to see support for attribute helpers in
proc_macro_attribute, as it would not require this "workaround". Just want to make clear what the issues are and point out all workarounds so the Rust team can make the best decisions ❤️.Mulling commentedon May 27, 2024
What's stopping this from going forward? I think @daxpedda outlined it very well, it's useful in several situations and not that hard to implement.
Tangentially, we shouldn't offload the problem upwards, i.e., expect syn to solve everything. The ergonomics of macro development are important.
coolreader18 commentedon Feb 26, 2025
Another case this would be really helpful, which I don't think anyone's mentioned yet, is for when there's a syntax error in the item the attribute is on, e.g.:
Now, not only are rustc and syn yelling at the user that there's a syntax error, but rustc is also complaining that the attribute
bazwasn't found, which can make it really difficult to figure out where the syntax error actually is, especially when the attribute is on a bigger function or module. Obviously, syntax errors aren't necessarily the case to optimize for, but this is something that I feel would really improve the DX of using proc macros in IDEs.nerodono commentedon Apr 15, 2025
Another silly trick you can use if you're lazy and attribute macro is supposed to be applied to the type: make derive macro with helper attribute and derive it.
Give the field attributes one list and pin the helper derive to it
Carry out the developer decisions of the code quality report and bump…