From 1ff7ed583f9b6f552644b27f19c1265976f9eb77 Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sat, 7 Mar 2026 21:02:10 +0000 Subject: [PATCH] add test for closure precedence in `TokenStream`s --- .../proc-macro/identity-closure-preserving.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/ui/proc-macro/identity-closure-preserving.rs diff --git a/tests/ui/proc-macro/identity-closure-preserving.rs b/tests/ui/proc-macro/identity-closure-preserving.rs new file mode 100644 index 0000000000000..8ced183c2f551 --- /dev/null +++ b/tests/ui/proc-macro/identity-closure-preserving.rs @@ -0,0 +1,22 @@ +//! Make sure that the closure still gets the correct precedence when round-tripping +//! through a proc macro. +//! The correct precendence is `(|| ()) as fn()`, even though these parentheses are not +//! directly part of the code. +//! If it would get lost, the code would be `|| () as fn()`, get parsed as +//! `|| (() as fn())` and fail to compile. +//! Notably, this will also fail to compile if we use `recollect` instead of `identity`. +//! Regression test for https://github.com/rust-lang/rust/pull/151830#issuecomment-4010899019. +//@ proc-macro: test-macros.rs +//@ check-pass + +macro_rules! operator_impl { + ($target_expr:expr) => { + test_macros::identity! { + $target_expr as fn() + }; + }; +} + +fn main() { + operator_impl!(|| ()); +}