From f6294883f81b1adf865b8d64b8342106983d22a3 Mon Sep 17 00:00:00 2001 From: cijiugechu Date: Mon, 20 Apr 2026 13:36:27 +0800 Subject: [PATCH] Make reparsed guard metavars collect tokens --- compiler/rustc_parse/src/parser/expr.rs | 4 +++- tests/ui/macros/macro-guard-matcher-recursion.rs | 10 ++++++++++ .../ui/macros/macro-guard-matcher-recursion.stderr | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/ui/macros/macro-guard-matcher-recursion.rs create mode 100644 tests/ui/macros/macro-guard-matcher-recursion.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 437102d549e7..a0601cc71d03 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -3464,7 +3464,9 @@ pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> { } pub(crate) fn eat_metavar_guard(&mut self) -> Option> { - self.eat_metavar_seq(MetaVarKind::Guard, |this| this.parse_match_arm_guard()).flatten() + self.eat_metavar_seq(MetaVarKind::Guard, |this| { + this.expect_match_arm_guard(ForceCollect::Yes) + }) } fn parse_match_arm_guard(&mut self) -> PResult<'a, Option>> { diff --git a/tests/ui/macros/macro-guard-matcher-recursion.rs b/tests/ui/macros/macro-guard-matcher-recursion.rs new file mode 100644 index 000000000000..c916d9500377 --- /dev/null +++ b/tests/ui/macros/macro-guard-matcher-recursion.rs @@ -0,0 +1,10 @@ +//! Regression test for +#![feature(macro_guard_matcher)] +fn main() { + macro_rules! m { + ($g : guard) => { + m!($g) //~ ERROR recursion limit reached while expanding `m!` + }; + } + m!(if x) +} diff --git a/tests/ui/macros/macro-guard-matcher-recursion.stderr b/tests/ui/macros/macro-guard-matcher-recursion.stderr new file mode 100644 index 000000000000..e12b2de76510 --- /dev/null +++ b/tests/ui/macros/macro-guard-matcher-recursion.stderr @@ -0,0 +1,14 @@ +error: recursion limit reached while expanding `m!` + --> $DIR/macro-guard-matcher-recursion.rs:6:13 + | +LL | m!($g) + | ^^^^^^ +... +LL | m!(if x) + | -------- in this macro invocation + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`macro_guard_matcher_recursion`) + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error +