Skip to content

Commit a8e862c

Browse files
committed
Avoid producing NoDelim values in Frame.
The code currently ignores the actual delimiter on the RHS and fakes up a `NoDelim`/`DelimSpan::dummy()` one. This commit changes it to use the actual delimiter. The commit also reorders the fields for the `Delimited` variant to match the `Sequence` variant.
1 parent 6b367a0 commit a8e862c

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,15 @@ fn generic_extension<'cx, 'tt>(
260260
// Merge the gated spans from parsing the matcher with the pre-existing ones.
261261
sess.gated_spans.merge(gated_spans_snapshot);
262262

263-
// Ignore the delimiters on the RHS.
264-
let rhs = match &rhses[i] {
265-
mbe::TokenTree::Delimited(_, delimited) => &delimited.tts,
263+
let (rhs, rhs_span): (&mbe::Delimited, DelimSpan) = match &rhses[i] {
264+
mbe::TokenTree::Delimited(span, delimited) => (&delimited, *span),
266265
_ => cx.span_bug(sp, "malformed macro rhs"),
267266
};
268267
let arm_span = rhses[i].span();
269268

270-
let rhs_spans = rhs.iter().map(|t| t.span()).collect::<Vec<_>>();
269+
let rhs_spans = rhs.tts.iter().map(|t| t.span()).collect::<Vec<_>>();
271270
// rhs has holes ( `$id` and `$(...)` that need filled)
272-
let mut tts = match transcribe(cx, &named_matches, &rhs, transparency) {
271+
let mut tts = match transcribe(cx, &named_matches, &rhs, rhs_span, transparency) {
273272
Ok(tts) => tts,
274273
Err(mut err) => {
275274
err.emit();

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ impl MutVisitor for Marker {
2929
enum Frame<'a> {
3030
Delimited {
3131
tts: &'a [mbe::TokenTree],
32-
delim_token: token::DelimToken,
3332
idx: usize,
33+
delim_token: token::DelimToken,
3434
span: DelimSpan,
3535
},
3636
Sequence {
@@ -42,8 +42,8 @@ enum Frame<'a> {
4242

4343
impl<'a> Frame<'a> {
4444
/// Construct a new frame around the delimited set of tokens.
45-
fn new(tts: &'a [mbe::TokenTree]) -> Frame<'a> {
46-
Frame::Delimited { tts, delim_token: token::NoDelim, idx: 0, span: DelimSpan::dummy() }
45+
fn new(src: &'a mbe::Delimited, span: DelimSpan) -> Frame<'a> {
46+
Frame::Delimited { tts: &src.tts, idx: 0, delim_token: src.delim, span }
4747
}
4848
}
4949

@@ -85,17 +85,18 @@ impl<'a> Iterator for Frame<'a> {
8585
pub(super) fn transcribe<'a>(
8686
cx: &ExtCtxt<'a>,
8787
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
88-
src: &[mbe::TokenTree],
88+
src: &mbe::Delimited,
89+
src_span: DelimSpan,
8990
transparency: Transparency,
9091
) -> PResult<'a, TokenStream> {
9192
// Nothing for us to transcribe...
92-
if src.is_empty() {
93+
if src.tts.is_empty() {
9394
return Ok(TokenStream::default());
9495
}
9596

9697
// We descend into the RHS (`src`), expanding things as we go. This stack contains the things
9798
// we have yet to expand/are still expanding. We start the stack off with the whole RHS.
98-
let mut stack: SmallVec<[Frame<'_>; 1]> = smallvec![Frame::new(&src)];
99+
let mut stack: SmallVec<[Frame<'_>; 1]> = smallvec![Frame::new(&src, src_span)];
99100

100101
// As we descend in the RHS, we will need to be able to match nested sequences of matchers.
101102
// `repeats` keeps track of where we are in matching at each level, with the last element being

0 commit comments

Comments
 (0)