Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SpriteOvO
Copy link
Contributor

@SpriteOvO SpriteOvO commented Dec 23, 2024

Tracking issues: #130977, #54722

This PR changed quote macro to use ToTokens trait instead of TokenStream::from, and added a test case for directly referencing an individual token.

r? @dtolnay
CC @tgross35

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2024
@rust-log-analyzer

This comment has been minimized.

@SpriteOvO SpriteOvO force-pushed the proc-macro-use-to-tokens-in-quote branch from 519aec5 to 0b32dec Compare December 23, 2024 18:02
@@ -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))),));
Copy link
Contributor

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.

@tgross35
Copy link
Contributor

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 #[test] or add the assert! (eventually we will, this should be a FIXME), but keeping the quote! invocations at least allows us to verify that the expected things do quote.

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 tests/ui/proc-macro/quote directory.

@tgross35 tgross35 added A-proc-macros Area: Procedural macros WG-macros Working group: Macros T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-macros Working group: Macros
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants