-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Adding #[meta] to an item in a TokenStream #153
Comments
Pretty-printed output proc_macro2::TokenStream, spans manually removedTokenStream [
Punct {
ch: '#',
spacing: Alone,
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "doc",
},
Punct {
ch: '=',
spacing: Alone,
},
Literal { lit: Lit { kind: Str, symbol: "Generate custom docs...", suffix: None }, .. },
],
},
Ident {
ident: "pub",
},
Ident {
ident: "fn",
},
Ident {
ident: "capacity",
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Punct {
ch: '&',
spacing: Alone,
},
Ident {
ident: "self",
},
],
},
Punct {
ch: '-',
spacing: Joint,
},
Punct {
ch: '>',
spacing: Alone,
},
Ident {
ident: "i32",
},
Group {
delimiter: Brace,
stream: TokenStream [
Literal { lit: Lit { kind: Integer, symbol: "0", suffix: None }, .. },
],
},
] After converting back Additional test cases: Pure proxy-passWithout adding doc attribute, pure proxy-passing input to quote!(
#input
).into() Proxy-pass with other meta attributesSurprisingly, works. Proc macro code as above; function code here: impl MyWrapper {
#[allow(unused)]
pub fn capacity(&self) -> i32 { 0 }
} TokenStream up to "pub fn"TokenStream [
Punct {
ch: '#',
spacing: Alone,
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "allow",
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "attributesunused",
},
],
},
],
},
Ident {
ident: "pub",
},
Ident {
ident: "fn",
},
..
] Looks pretty-much the same as the first example, but magically works. Adding other meta attributesTried to add other attribute than doc. Failed on Still can't see the difference between generated (from quote) and supplied (from input stream) attributes in the output TokenStream. |
This is a compiler bug that was fixed in 1.43.0. |
@dtolnay |
Yes, |
Perfecto! Thanks a million, that was extremely helpful of you. |
There is an example in the
quote!
macro documentation which states that documentation can be added only with#[doc = #msg]
syntax. While this is true, I can't seem to be able to add the documentation to an arbitrary item represented as an inputTokenStream
in some cases.My use case is building a custom proc macro (
proc_macro_attribute
) which only adds documentation to an item in question. Like this:However it works for only for top-level module items: structs, functions, types — they all work fine. But on nested impl-methods I get this weird error message:
As you could guess, original method under macro looked like this:
Also, note that I do not want to parse
input
stream into anything meaningful, I only want to proxy-pass it further with all original tokens' spans etc.The text was updated successfully, but these errors were encountered: