Skip to content

Add support for repetition to proc_macro::quote #141608

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

moatom
Copy link

@moatom moatom commented May 26, 2025

Fixed #140238

ToDo
  • refactoring
  • > any new tests in tests/ui/proc-macro/quote for things we should reject
  • squash commits

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 26, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@moatom moatom changed the title WIP: Add support for repetition to proc_macro::quote Add support for repetition to proc_macro::quote May 31, 2025
@@ -1613,3 +1614,202 @@ pub mod tracked_path {
crate::bridge::client::FreeFunctions::track_path(path);
}
}

#[doc(hidden)]
#[unstable(feature = "proc_macro_quote", issue = "54722")]
Copy link
Author

@moatom moatom May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether these annotations are appropriate. (I added them just based on my speculation.)

In addition,

It's probably easiest to just copy quote's logic here, which uses an extension trait to facilitate this.

do we need to take care of its license?

#[unstable(feature = "proc_macro_quote", issue = "54722")]
pub mod ext {
use core::slice;
use std::collections::btree_set::{self, BTreeSet};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an alternative for alloc::collections

}

self.tokens[self.pos] = self.iter.next();
let token_opt = self.tokens[self.pos].clone();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper struct may require significant refactoring. (Especially, the use of .clone() might be avoidable.)
Do you have any comments or suggestions?

Copy link

@ora-0 ora-0 May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth using just a Peekable. I think it would be better to use it at the expense of some extra parsing code, than using a custom lookahead iterator

@@ -71,10 +160,97 @@ pub fn quote(stream: TokenStream) -> TokenStream {
let mut after_dollar = false;

let mut tokens = crate::TokenStream::new();
for tree in stream {
let mut iter = LookaheadIter::new(stream);
while let Some(tree) = iter.next() {
if after_dollar {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after_dollar can be combined with LookaheadIter, if you prefer.

Comment on lines +107 to +109
assert_eq!("X, X, X, X,", quote!($($primes,)*).to_string());

assert_eq!("X, X, X, X", quote!($($primes),*).to_string());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted the expected spacing around SEP in the original test code. Is it appropriate?
Cf. https://github.com/dtolnay/quote/blob/62fd385a800f7398ab416c00100664479261a86e/tests/test.rs#L84

Comment on lines -1 to -4
// FIXME(quote): `proc_macro::quote!` doesn't support repetition at the moment, so the stderr is
// expected to be incorrect.
//@ known-bug: #54722

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it sufficient to simply remove these, or is some additional workaround needed?

E.g.:

//@ check-pass
//@ force-host
//@ no-prefer-dynamic
//@ compile-flags: -Z unpretty=expanded
//@ needs-unwind compiling proc macros with panic=abort causes a warning
//@ edition: 2015

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plus any new tests in tests/ui/proc-macro/quote for things we should reject

Do you have any examples besides these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for repetition to proc_macro::quote
4 participants