Skip to content
Open
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
21 changes: 15 additions & 6 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,8 +1627,9 @@ fn check_matcher_core<'tt>(
err.span_label(sp, format!("not allowed after `{kind}` fragments"));

if kind == NonterminalKind::Pat(PatWithOr)
&& sess.psess.edition.at_least_rust_2021()
&& next_token.is_token(&token::Or)
&& ((sess.psess.edition.at_least_rust_2021()
&& next_token.is_token(&token::Or))
|| next_token.is_token(&token::Colon))
{
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
span,
Expand Down Expand Up @@ -1745,18 +1746,26 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
_ => IsInFollow::No(TOKENS),
}
}
NonterminalKind::Pat(PatParam { .. }) => {
const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"];
NonterminalKind::Pat(PatParam { inferred }) => {
let explicit = !inferred;
// only allow `:` after explicit `pat_param` matchers,
// not after edition2015 `pat`
let tokens: &[&str] = if explicit {
&["`=>`", "`,`", "`=`", "`|`", "`:`", "`if`", "`if let`", "`in`"]
} else {
&["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"]
};
match tok {
TokenTree::Token(token) => match token.kind {
FatArrow | Comma | Eq | Or => IsInFollow::Yes,
Colon if explicit => IsInFollow::Yes,
Ident(name, IdentKind::Normal) if name == kw::If || name == kw::In => {
IsInFollow::Yes
}
_ => IsInFollow::No(TOKENS),
_ => IsInFollow::No(tokens),
},
TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
_ => IsInFollow::No(TOKENS),
_ => IsInFollow::No(tokens),
}
}
NonterminalKind::Pat(PatWithOr) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/macros/macro-follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ macro_rules! follow_pat {
($p:pat $i:item) => {}; //~ERROR `$p:pat` is followed by `$i:item`
($p:pat $m:meta) => {}; //~ERROR `$p:pat` is followed by `$m:meta`
}
// FOLLOW(pat_param) = {FOLLOW(pat), Colon}
macro_rules! follow_pat {
($p:pat_param ()) => {}; //~ERROR `$p:pat_param` is followed by `(`
($p:pat_param []) => {}; //~ERROR `$p:pat_param` is followed by `[`
($p:pat_param {}) => {}; //~ERROR `$p:pat_param` is followed by `{`
($p:pat_param :) => {}; // ok
($p:pat_param >) => {}; //~ERROR `$p:pat_param` is followed by `>`
($p:pat_param +) => {}; //~ERROR `$p:pat_param` is followed by `+`
($p:pat_param ident) => {}; //~ERROR `$p:pat_param` is followed by `ident`
($p:pat_param $q:pat) => {}; //~ERROR `$p:pat_param` is followed by `$q:pat`
($p:pat_param $e:expr) => {}; //~ERROR `$p:pat_param` is followed by `$e:expr`
($p:pat_param $t:ty) => {}; //~ERROR `$p:pat_param` is followed by `$t:ty`
($p:pat_param $s:stmt) => {}; //~ERROR `$p:pat_param` is followed by `$s:stmt`
($p:pat_param $q:path) => {}; //~ERROR `$p:pat_param` is followed by `$q:path`
($p:pat_param $b:block) => {}; //~ERROR `$p:pat_param` is followed by `$b:block`
($p:pat_param $i:ident) => {}; //~ERROR `$p:pat_param` is followed by `$i:ident`
($p:pat_param $t:tt) => {}; //~ERROR `$p:pat_param` is followed by `$t:tt`
($p:pat_param $i:item) => {}; //~ERROR `$p:pat_param` is followed by `$i:item`
($p:pat_param $m:meta) => {}; //~ERROR `$p:pat_param` is followed by `$m:meta`
}
// FOLLOW(expr) = {FatArrow, Comma, Semicolon}
macro_rules! follow_expr {
($e:expr ()) => {}; //~ERROR `$e:expr` is followed by `(`
Expand Down
Loading
Loading