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

Open
wants to merge 5 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

Copy link
Author

Choose a reason for hiding this comment

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

@ora-0
I think Peekable only supports single-element lookahead.

However, placing a simplified and extended version of Peekable here might be a better approach than defining the completely original lookahead iterator from scratch. 🤔

Copy link

@ora-0 ora-0 Jun 1, 2025

Choose a reason for hiding this comment

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

I'm not sure if a lookahead is necessary. Would this work? I can't be completely sure since I haven't tested this code.

let mut iter = stream.into_iter();
// ...
let mut sep_opt: Option<TokenTree> = None;
if let next @ Some(TokenTree::Punct(token_1)) = iter.next() {
    if token_1.as_char() != '*' {
        sep_opt = next;
        if !matches!(iter.next(), Some(TokenTree::Punct(token_2)) if token_2 == "*") {
            panic!("`$(...)` must be followed by `*` in `quote!`");
        }
    }
}

Since we are panicking at the wildcard we could just use .next()s. It is a bit less declarative but it avoids the complexity of another data structure.

Copy link

@ora-0 ora-0 Jun 1, 2025

Choose a reason for hiding this comment

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

Nvm, turns out it is necessary, because the separator itself may end up being a star. But I think we only need one lookahead. The code ends up being pretty similar to the current:

let mut iter = stream.into_iter().peekable();
// ...
let sep_opt: Option<TokenTree> = match (iter.next(), iter.peek()) {
    (Some(TokenTree::Punct(sep)), Some(&TokenTree::Punct(star))) 
        if sep.spacing() == Spacing::Joint && star.as_char() == '*' => 
    {
        iter.next();
        Some(TokenTree::Punct(sep))
    }
    (Some(TokenTree::Punct(star)), _) if star.as_char() == '*' => None,
    _ => panic!("`$(...)` must be followed by `*` in `quote!`"),
};

Copy link
Author

Choose a reason for hiding this comment

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

But I think we only need one lookahead.

Thank you for the great point! We must consume at least one * here.

@@ -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?

@moatom moatom marked this pull request as ready for review June 1, 2025 00:48
@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2025

⚠️ Warning ⚠️

@moatom
Copy link
Author

moatom commented Jun 1, 2025

@tgross35 @dtolnay @ora-0
Hi! Although it still needs some refactoring, it passes all the updated tests.
I'd be grateful for your review and feedback.

@moatom
Copy link
Author

moatom commented Jun 1, 2025

r? @tgross35

@rustbot rustbot assigned tgross35 and unassigned petrochenkov Jun 1, 2025
@tgross35
Copy link
Contributor

tgross35 commented Jun 1, 2025

I’ll leave a review but David knows this area much better, so

r? dtolnay

@rustbot rustbot assigned dtolnay and unassigned tgross35 Jun 1, 2025
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/pass-dep/tokio/file-io.rs ... FAILED
tests/pass-dep/libc/mmap.rs ... ok

FAILED TEST: tests/pass-dep/tokio/file-io.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-LrAeks" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/miri" "--error-format=json" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/pass-dep/tokio" "tests/pass-dep/tokio/file-io.rs" "-Zmiri-disable-isolation" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libcfg_if-55a32e410325a882.rlib" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libcfg_if-55a32e410325a882.rmeta" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-77190f9b44a3c009.rlib" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-77190f9b44a3c009.rmeta" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-56888fca95a09bf8.rlib" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-56888fca95a09bf8.rmeta" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-a8fc577bb44b6d80.rlib" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libgetrandom-a8fc577bb44b6d80.rmeta" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/liblibc-dd98cac8117d550a.rlib" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/liblibc-dd98cac8117d550a.rmeta" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libnum_cpus-7ab8becb6da559b7.rlib" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libnum_cpus-7ab8becb6da559b7.rmeta" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libpage_size-c34469ad90c5eb76.rlib" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libpage_size-c34469ad90c5eb76.rmeta" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libtempfile-d40c54a83e8abae9.rlib" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libtempfile-d40c54a83e8abae9.rmeta" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libtokio-539f2d149eae570a.rlib" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps/libtokio-539f2d149eae570a.rmeta" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/debug/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/x86_64-unknown-linux-gnu/debug/deps" "--edition" "2021"

error: test got exit status: 101, but expected 0
 = note: the compiler panicked

error: no output was expected
---
   4: test_create_and_write::{closure#0}
             at tests/pass-dep/tokio/file-io.rs:24:5
   5: main::{closure#0}
             at tests/pass-dep/tokio/file-io.rs:14:29
   6: tokio::runtime::park::CachedParkThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/park.rs:284:60
   7: tokio::task::coop::with_budget::<std::task::Poll<()>, {closure@tokio::runtime::park::CachedParkThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/task/coop/mod.rs:167:5
   8: tokio::task::coop::budget::<std::task::Poll<()>, {closure@tokio::runtime::park::CachedParkThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/task/coop/mod.rs:133:5
   9: tokio::runtime::park::CachedParkThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/park.rs:284:31
  10: tokio::runtime::context::blocking::BlockingRegionGuard::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/context/blocking.rs:66:9
  11: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/scheduler/multi_thread/mod.rs:87:13
  12: tokio::runtime::context::runtime::enter_runtime::<{closure@tokio::runtime::scheduler::multi_thread::MultiThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}, ()>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/context/runtime.rs:65:16
  13: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/scheduler/multi_thread/mod.rs:86:9
  14: tokio::runtime::Runtime::block_on_inner::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/runtime.rs:370:45
  15: tokio::runtime::Runtime::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/runtime.rs:342:13
  16: main
             at tests/pass-dep/tokio/file-io.rs:15:5
  17: <fn() as std::ops::FnOnce<()>>::call_once - shim(fn())
             at /checkout/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---
   4: test_create_and_write::{closure#0}
             at tests/pass-dep/tokio/file-io.rs:24:5
   5: main::{closure#0}
             at tests/pass-dep/tokio/file-io.rs:14:29
   6: tokio::runtime::park::CachedParkThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/park.rs:284:60
   7: tokio::task::coop::with_budget::<std::task::Poll<()>, {closure@tokio::runtime::park::CachedParkThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/task/coop/mod.rs:167:5
   8: tokio::task::coop::budget::<std::task::Poll<()>, {closure@tokio::runtime::park::CachedParkThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/task/coop/mod.rs:133:5
   9: tokio::runtime::park::CachedParkThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/park.rs:284:31
  10: tokio::runtime::context::blocking::BlockingRegionGuard::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/context/blocking.rs:66:9
  11: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/scheduler/multi_thread/mod.rs:87:13
  12: tokio::runtime::context::runtime::enter_runtime::<{closure@tokio::runtime::scheduler::multi_thread::MultiThread::block_on<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>::{closure#0}}, ()>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/context/runtime.rs:65:16
  13: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/scheduler/multi_thread/mod.rs:86:9
  14: tokio::runtime::Runtime::block_on_inner::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/runtime.rs:370:45
  15: tokio::runtime::Runtime::block_on::<{async block@tests/pass-dep/tokio/file-io.rs:12:1: 12:15}>
             at /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.44.2/src/runtime/runtime.rs:342:13
  16: main
             at tests/pass-dep/tokio/file-io.rs:15:5
  17: <fn() as std::ops::FnOnce<()>>::call_once - shim(fn())
             at /checkout/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---

Location:
   /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ui_test-0.29.2/src/lib.rs:369

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
error: test failed, to rerun pass `--test ui`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/release/deps/ui-f58affc77002becb` (exit status: 1)
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:05:03
  local time: Sun Jun  1 15:06:29 UTC 2025
  network time: Sun, 01 Jun 2025 15:06:29 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for repetition to proc_macro::quote
7 participants