Make the expansion of guard metavars begin guard non-terminals

This commit is contained in:
León Orell Valerian Liehr
2026-04-12 03:15:26 +02:00
parent 5f36a7f807
commit fc2c72cb67
4 changed files with 16 additions and 9 deletions
+1 -5
View File
@@ -3464,11 +3464,7 @@ impl<'a> Parser<'a> {
}
pub(crate) fn eat_metavar_guard(&mut self) -> Option<Box<Guard>> {
self.eat_metavar_seq_with_matcher(
|mv_kind| matches!(mv_kind, MetaVarKind::Guard),
|this| this.parse_match_arm_guard(),
)
.flatten()
self.eat_metavar_seq(MetaVarKind::Guard, |this| this.parse_match_arm_guard()).flatten()
}
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<Box<Guard>>> {
@@ -105,7 +105,10 @@ impl<'a> Parser<'a> {
token::Lifetime(..) | token::NtLifetime(..) => true,
_ => false,
},
NonterminalKind::Guard => token.is_keyword(kw::If),
NonterminalKind::Guard => match token.kind {
token::OpenInvisible(InvisibleOrigin::MetaVar(MetaVarKind::Guard)) => true,
_ => token.is_keyword(kw::If),
},
NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
token.kind.close_delim().is_none()
}
+9 -1
View File
@@ -2,7 +2,7 @@
fn main() {
macro_rules! m {
($x:guard) => {};
($g:guard) => {};
}
// Accepts
@@ -14,4 +14,12 @@ fn main() {
// Rejects
m!(let Some(x) = Some(1)); //~ERROR no rules expected keyword `let`
macro_rules! m_m {
($g:guard) => { m!($g); };
}
// Accepted since `m` recognizes that the sequence produced by the expansion of
// metavar `$g` "begins" (i.e., is) a guard since it's of kind `guard`.
m_m!(if true);
}
+2 -2
View File
@@ -7,10 +7,10 @@ LL | macro_rules! m {
LL | m!(let Some(x) = Some(1));
| ^^^ no rules expected this token in macro call
|
note: while trying to match meta-variable `$x:guard`
note: while trying to match meta-variable `$g:guard`
--> $DIR/macro-guard-matcher.rs:5:10
|
LL | ($x:guard) => {};
LL | ($g:guard) => {};
| ^^^^^^^^
error: aborting due to 1 previous error