Skip to content

Commit fe549f1

Browse files
committed
Fix macro matching for NoDelim delimited sequences.
PR rust-lang#95159 unintentionally changed the behaviour of declarative macro matching for `NoDelim` delimited sequences. - Before rust-lang#95159, delimiters were implicit in `mbe::Delimited`. When doing macro matching, normal delimiters were generated out of thin air as necessary, but `NoDelim` delimiters were not. This was done within `TokenTree::{get_tt,len}`. - After rust-lang#95159, the delimiters were explicit. There was an unintentional change whereby `NoDelim` delimiters were represented explicitly just like normal delimeters. - rust-lang#95555 introduced a new matcher representation (`MatcherLoc`) and the `NoDelim` delimiters were made explicit within it, just like `mbe::Delimited`. - rust-lang#95797 then changed `mbe::Delimited` back to having implicit delimiters, but because matching is now being done on `MatcherLoc`, the behavioural change persisted. The fix is simple: remove the explicit `NoDelim` delimiters in the `MatcherLoc` representation. This gives back the original behaviour. As for why this took so long to be found: it seems that `NoDelim` sequences are unusual. It took a macro that generates another macro (from the `yarte_lexer` crate, found via a crater run) to uncover this. Fixes rust-lang#96305.
1 parent b04c532 commit fe549f1

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
144144
let close_token = Token::new(token::CloseDelim(delimited.delim), span.close);
145145

146146
locs.push(MatcherLoc::Delimited);
147-
locs.push(MatcherLoc::Token { token: open_token });
147+
if delimited.delim != token::DelimToken::NoDelim {
148+
locs.push(MatcherLoc::Token { token: open_token });
149+
}
148150
inner(&delimited.tts, locs, next_metavar, seq_depth);
149-
locs.push(MatcherLoc::Token { token: close_token });
151+
if delimited.delim != token::DelimToken::NoDelim {
152+
locs.push(MatcherLoc::Token { token: close_token });
153+
}
150154
}
151155
TokenTree::Sequence(_, seq) => {
152156
// We can't determine `idx_first_after` and construct the final

0 commit comments

Comments
 (0)