Skip to content

Commit c8844df

Browse files
committed
Clarify the meaning of the span within mbe::TokenTree::MetaVar.
1 parent 39e02f1 commit c8844df

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

compiler/rustc_expand/src/mbe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ enum TokenTree {
7373
Delimited(DelimSpan, DelimSpacing, Delimited),
7474
/// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
7575
Sequence(DelimSpan, SequenceRepetition),
76-
/// e.g., `$var`.
76+
/// e.g., `$var`. The span covers the leading dollar and the ident. (The span within the ident
77+
/// only covers the ident, e.g. `var`.)
7778
MetaVar(Span, Ident),
7879
/// e.g., `$var:expr`. Only appears on the LHS.
7980
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),

compiler/rustc_expand/src/mbe/quoted.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn parse_tree<'a>(
176176
// Depending on what `tree` is, we could be parsing different parts of a macro
177177
match tree {
178178
// `tree` is a `$` token. Look at the next token in `trees`
179-
&tokenstream::TokenTree::Token(Token { kind: token::Dollar, span }, _) => {
179+
&tokenstream::TokenTree::Token(Token { kind: token::Dollar, span: dollar_span }, _) => {
180180
// FIXME: Handle `Invisible`-delimited groups in a more systematic way
181181
// during parsing.
182182
let mut next = outer_trees.next();
@@ -209,7 +209,7 @@ fn parse_tree<'a>(
209209
err.emit();
210210
// Returns early the same read `$` to avoid spanning
211211
// unrelated diagnostics that could be performed afterwards
212-
return TokenTree::token(token::Dollar, span);
212+
return TokenTree::token(token::Dollar, dollar_span);
213213
}
214214
Ok(elem) => {
215215
maybe_emit_macro_metavar_expr_feature(
@@ -251,7 +251,7 @@ fn parse_tree<'a>(
251251
// special metavariable that names the crate of the invocation.
252252
Some(tokenstream::TokenTree::Token(token, _)) if token.is_ident() => {
253253
let (ident, is_raw) = token.ident().unwrap();
254-
let span = ident.span.with_lo(span.lo());
254+
let span = ident.span.with_lo(dollar_span.lo());
255255
if ident.name == kw::Crate && matches!(is_raw, IdentIsRaw::No) {
256256
TokenTree::token(token::Ident(kw::DollarCrate, is_raw), span)
257257
} else {
@@ -260,16 +260,19 @@ fn parse_tree<'a>(
260260
}
261261

262262
// `tree` is followed by another `$`. This is an escaped `$`.
263-
Some(&tokenstream::TokenTree::Token(Token { kind: token::Dollar, span }, _)) => {
263+
Some(&tokenstream::TokenTree::Token(
264+
Token { kind: token::Dollar, span: dollar_span2 },
265+
_,
266+
)) => {
264267
if parsing_patterns {
265268
span_dollar_dollar_or_metavar_in_the_lhs_err(
266269
sess,
267-
&Token { kind: token::Dollar, span },
270+
&Token { kind: token::Dollar, span: dollar_span2 },
268271
);
269272
} else {
270-
maybe_emit_macro_metavar_expr_feature(features, sess, span);
273+
maybe_emit_macro_metavar_expr_feature(features, sess, dollar_span2);
271274
}
272-
TokenTree::token(token::Dollar, span)
275+
TokenTree::token(token::Dollar, dollar_span2)
273276
}
274277

275278
// `tree` is followed by some other token. This is an error.
@@ -281,7 +284,7 @@ fn parse_tree<'a>(
281284
}
282285

283286
// There are no more tokens. Just return the `$` we already have.
284-
None => TokenTree::token(token::Dollar, span),
287+
None => TokenTree::token(token::Dollar, dollar_span),
285288
}
286289
}
287290

0 commit comments

Comments
 (0)