Skip to content

Add support for repetition to proc_macro::quote #140238

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

Open
tgross35 opened this issue Apr 24, 2025 · 3 comments
Open

Add support for repetition to proc_macro::quote #140238

tgross35 opened this issue Apr 24, 2025 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros C-feature-request Category: A feature request, i.e: not implemented / a PR. E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-macros Working group: Macros

Comments

@tgross35
Copy link
Contributor

I'm opening a standalone issue to have something with "help wanted" labels in case anyone is interested in picking this up.

Our proc_macro::quote does not support repetition, unlike quote from the quote crate. As mentioned many times on the tracking issue, this is something we should support or at least account for before proc_macro::quote can be stabilized.

This should use the syntax:

proc_macro::quote! {
    $( CONTENTS )SEP*
}

Where CONTENTS is the thing to be repeated and SEP is an optional single-character separator. Expansion should work for anything that implements IntoIterator. This matches the quote crate's logic (except quote::quote uses # rather than $).

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 24, 2025
@tgross35 tgross35 added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs Relevant to the library team, which will review and decide on the PR/issue. A-proc-macros Area: Procedural macros A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) WG-macros Working group: Macros and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 24, 2025
@tgross35
Copy link
Contributor Author

Cc @dtolnay in case you have any notes here

@tgross35 tgross35 added E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. labels Apr 24, 2025
@tgross35
Copy link
Contributor Author

Also fyi @SpriteOvO if you might be interested in this, since you have done a lot of other work with quote!

@dtolnay
Copy link
Member

dtolnay commented Apr 24, 2025

I recommend copying from the quote crate. It is tricky to get something that has all the behaviors that people expect are "obvious". If you see $($thing)*,—

  • You cannot turn it into for x in $thing {...} because this is incompatible with $($thing)* $($thing)* where thing is something non-copyable like a Vec, a ubiquitous use case.

  • You cannot turn it into for x in &$thing {...} because this is incompatible with thing being an Iterator, a ubiquitous use case.

  • You cannot turn it into for x in &mut $thing {...} because this requires let mut thing in the caller's code which is highly unexpected.

  • On top of all the above, in $($thing1 $thing2)* the correct behavior depends on which one of these is an iterator/collection and which one is a single token (Ident) or TokenStream. For a TokenStream, even though TokenStream has an IntoIterator impl, using that here produces an extremely unexpected result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros C-feature-request Category: A feature request, i.e: not implemented / a PR. E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. 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

No branches or pull requests

3 participants