Reorder match arms in parse_tt_inner.

To match the order the variants are declared in.
This commit is contained in:
Nicholas Nethercote 2022-04-04 15:47:53 +10:00
parent 88f8fbcce0
commit 0bd47e8a39

View file

@ -443,6 +443,29 @@ impl<'tt> TtParser<'tt> {
while let Some(mut mp) = self.cur_mps.pop() {
match &self.locs[mp.idx] {
MatcherLoc::Token { token: t } => {
// If it's a doc comment, we just ignore it and move on to the next tt in the
// matcher. This is a bug, but #95267 showed that existing programs rely on
// this behaviour, and changing it would require some care and a transition
// period.
//
// If the token matches, we can just advance the parser.
//
// Otherwise, this match has failed, there is nothing to do, and hopefully
// another mp in `cur_mps` will match.
if matches!(t, Token { kind: DocComment(..), .. }) {
mp.idx += 1;
self.cur_mps.push(mp);
} else if token_name_eq(&t, token) {
mp.idx += 1;
self.next_mps.push(mp);
}
}
MatcherLoc::Delimited => {
// Entering the delimeter is trivial.
mp.idx += 1;
self.cur_mps.push(mp);
}
&MatcherLoc::Sequence {
op,
num_metavar_decls,
@ -471,37 +494,6 @@ impl<'tt> TtParser<'tt> {
mp.idx += 1;
self.cur_mps.push(mp);
}
MatcherLoc::MetaVarDecl { kind, .. } => {
// Built-in nonterminals never start with these tokens, so we can eliminate
// them from consideration. We use the span of the metavariable declaration
// to determine any edition-specific matching behavior for non-terminals.
if Parser::nonterminal_may_begin_with(*kind, token) {
self.bb_mps.push(mp);
}
}
MatcherLoc::Delimited => {
// Entering the delimeter is trivial.
mp.idx += 1;
self.cur_mps.push(mp);
}
MatcherLoc::Token { token: t } => {
// If it's a doc comment, we just ignore it and move on to the next tt in the
// matcher. This is a bug, but #95267 showed that existing programs rely on
// this behaviour, and changing it would require some care and a transition
// period.
//
// If the token matches, we can just advance the parser.
//
// Otherwise, this match has failed, there is nothing to do, and hopefully
// another mp in `cur_mps` will match.
if matches!(t, Token { kind: DocComment(..), .. }) {
mp.idx += 1;
self.cur_mps.push(mp);
} else if token_name_eq(&t, token) {
mp.idx += 1;
self.next_mps.push(mp);
}
}
&MatcherLoc::SequenceKleeneOpNoSep { op, idx_first } => {
// We are past the end of a sequence with no separator. Try ending the
// sequence. If that's not possible, `ending_mp` will fail quietly when it is
@ -540,6 +532,14 @@ impl<'tt> TtParser<'tt> {
mp.idx = idx_first;
self.cur_mps.push(mp);
}
MatcherLoc::MetaVarDecl { kind, .. } => {
// Built-in nonterminals never start with these tokens, so we can eliminate
// them from consideration. We use the span of the metavariable declaration
// to determine any edition-specific matching behavior for non-terminals.
if Parser::nonterminal_may_begin_with(*kind, token) {
self.bb_mps.push(mp);
}
}
MatcherLoc::Eof => {
// We are past the matcher's end, and not in a sequence. Try to end things.
debug_assert_eq!(mp.idx, self.locs.len() - 1);