-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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::TokenStream: provide AST node kind hint #50053
Comments
Alternately this could be encoded directly into pub struct AttributeInput {
// assume we'd use getters instead
pub tokens: TokenStream,
pub node_kind: SyntaxNodeKind,
}
#[proc_macro_attribute]
pub fn my_attr(attr: TokenStream, input: AttributeInput) -> TokenStream {} Or as an optional third argument: #[proc_macro_attribute]
pub fn my_attr(attr: TokenStream, input: TokenStream, kind: SyntaxNodeKind) -> TokenStream {} |
I think this is a neat idea but I would prefer for it to not make procedural macros more wordy in all situations. We always have the support of adding multiple valid signatures for In that sense if we wanted to change the defaults I'd prefer something like the @abonander do you feel like this greatly empowers macro authors to do more though over what we provide today? |
It's more of a quality of life issue and an error reporting one. Currently attributes can reasonably assume that they'll only be applied to items but that's not going to be the case forever. This would allow them to detect cases that they don't support, or decide which parsing methods to use instead of guessing and checking. |
Right now the approach I like the most is simply adding a method to |
@abonander but you can do this today, right? In the sense that you can use a library like |
@alexcrichton This is so you can choose what node to parse with For attributes that only accept one kind, like right now most are written to implicitly accept items only, they can immediately produce a clear and concise error ("this attribute only works on items") instead of a generic parsing error ("expected |
Yeah I don't doubt that an error can be generated more efficiently, but I don't think it's necessarily enabling anything fundamental that's not already being done, so I'm tempted to not add it yet and see how pressing it becomes over time. |
This would be exclusively for
#[proc_macro_attribute]
s which parse their input as AST nodes:cc @alexcrichton @dtolnay @petrochenkov
The text was updated successfully, but these errors were encountered: