Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
petrochenkov marked this conversation as resolved.
}

fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<Box<Guard>>> {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/macros/macro-guard-matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

fn main() {
macro_rules! m {
($x:guard) => {};
($g:guard) => {};
}

// Accepts
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions tests/ui/macros/macro-guard-matcher.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading