-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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: Use ToTokens
trait in quote
macro
#134693
base: master
Are you sure you want to change the base?
proc_macro: Use ToTokens
trait in quote
macro
#134693
Conversation
This comment has been minimized.
This comment has been minimized.
519aec5
to
0b32dec
Compare
@@ -73,7 +75,7 @@ pub fn quote(stream: TokenStream) -> TokenStream { | |||
after_dollar = false; | |||
match tree { | |||
TokenTree::Ident(_) => { | |||
return Some(quote!(Into::<crate::TokenStream>::into( | |||
return Some(quote!(ToTokens::into_token_stream( | |||
Clone::clone(&(@ tree))),)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the way this function works should be changed; instead of having a big tree that gets .collect()
ed here, it should create a TokenStream
at the beginning and then use ToTokens::to_tokens
to append to it, rather than always cloning.
That should also let the loop be flattened a bit:
pub fn quote(stream: TokenStream) -> TokenStream {
let mut output = TokenStream::new();
if stream.is_empty() {
return output;
}
let proc_macro_crate = quote!(crate);
let mut after_dollar = false;
for tree in stream {
if after_dollar {
after_dollar = false;
match tree { /* ... */ }
continue;
}
if let TokenTree::punct(ref tt) = tree {
if tt.as_char() == '$' {
after_dollar = true;
continue;
}
}
match tree {
// similar to what exists except the arms will just use `to_tokens`
// rather than cloning and returning something.
}
}
}
It may also be good to rename the macro_rules! quote
in this file to quote_impl
since reading all the quote!
invocations in this function is somewhat misleading.
Thanks for working on this! Could you update the new test file with applicable tests from https://github.com/dtolnay/quote/blob/aafba72e10919ad47c05169271cb78e614fb2b9d/tests/test.rs? Because of token stream limitations we can't actually mark them We can probably reuse the UI tests from https://github.com/dtolnay/quote/tree/aafba72e10919ad47c05169271cb78e614fb2b9d/tests/ui too. I think you can just make a new |
Tracking issues: #130977, #54722
This PR changed
quote
macro to useToTokens
trait instead ofTokenStream::from
, and added a test case for directly referencing an individual token.r? @dtolnay
CC @tgross35