Skip to content

Commit 9a63a42

Browse files
committed
Remove a Span from TokenKind::Interpolated.
This span records the declaration of the metavariable in the LHS of the macro. It's used in a couple of error messages. Unfortunately, it gets in the way of the long-term goal of removing `TokenKind::Interpolated`. So this commit removes it, which degrades a couple of (obscure) error messages but makes things simpler and enables the next commit.
1 parent 852a78e commit 9a63a42

19 files changed

+62
-97
lines changed

compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl MetaItem {
345345
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
346346
Path { span, segments, tokens: None }
347347
}
348-
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &nt.0 {
348+
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
349349
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
350350
token::Nonterminal::NtPath(path) => (**path).clone(),
351351
_ => return None,

compiler/rustc_ast/src/mut_visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,6 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
783783
}
784784
token::Interpolated(nt) => {
785785
let nt = Lrc::make_mut(nt);
786-
let (nt, sp) = (&mut nt.0, &mut nt.1);
787-
vis.visit_span(sp);
788786
visit_nonterminal(nt, vis);
789787
}
790788
_ => {}

compiler/rustc_ast/src/token.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Lit {
111111
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
112112
Literal(token_lit) => Some(token_lit),
113113
Interpolated(ref nt)
114-
if let NtExpr(expr) | NtLiteral(expr) = &nt.0
114+
if let NtExpr(expr) | NtLiteral(expr) = &**nt
115115
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
116116
{
117117
Some(token_lit)
@@ -333,7 +333,11 @@ pub enum TokenKind {
333333
/// - It prevents `Token` from implementing `Copy`.
334334
/// It adds complexity and likely slows things down. Please don't add new
335335
/// occurrences of this token kind!
336-
Interpolated(Lrc<(Nonterminal, Span)>),
336+
///
337+
/// The span in the surrounding `Token` is that of the metavariable in the
338+
/// macro's RHS. The span within the Nonterminal is that of the fragment
339+
/// passed to the macro at the call site.
340+
Interpolated(Lrc<Nonterminal>),
337341

338342
/// A doc comment token.
339343
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
@@ -441,7 +445,7 @@ impl Token {
441445
/// if they keep spans or perform edition checks.
442446
pub fn uninterpolated_span(&self) -> Span {
443447
match &self.kind {
444-
Interpolated(nt) => nt.0.use_span(),
448+
Interpolated(nt) => nt.use_span(),
445449
_ => self.span,
446450
}
447451
}
@@ -486,7 +490,7 @@ impl Token {
486490
PathSep | // global path
487491
Lifetime(..) | // labeled loop
488492
Pound => true, // expression attributes
489-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
493+
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
490494
NtExpr(..) |
491495
NtBlock(..) |
492496
NtPath(..)),
@@ -510,7 +514,7 @@ impl Token {
510514
| DotDot | DotDotDot | DotDotEq // ranges
511515
| Lt | BinOp(Shl) // associated path
512516
| PathSep => true, // global path
513-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
517+
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
514518
NtPat(..) |
515519
NtBlock(..) |
516520
NtPath(..)),
@@ -533,7 +537,7 @@ impl Token {
533537
Lifetime(..) | // lifetime bound in trait object
534538
Lt | BinOp(Shl) | // associated path
535539
PathSep => true, // global path
536-
Interpolated(ref nt) => matches!(&nt.0, NtTy(..) | NtPath(..)),
540+
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
537541
// For anonymous structs or unions, which only appear in specific positions
538542
// (type of struct fields or union fields), we don't consider them as regular types
539543
_ => false,
@@ -544,7 +548,7 @@ impl Token {
544548
pub fn can_begin_const_arg(&self) -> bool {
545549
match self.kind {
546550
OpenDelim(Delimiter::Brace) => true,
547-
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
551+
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
548552
_ => self.can_begin_literal_maybe_minus(),
549553
}
550554
}
@@ -589,7 +593,7 @@ impl Token {
589593
match self.uninterpolate().kind {
590594
Literal(..) | BinOp(Minus) => true,
591595
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
592-
Interpolated(ref nt) => match &nt.0 {
596+
Interpolated(ref nt) => match &**nt {
593597
NtLiteral(_) => true,
594598
NtExpr(e) => match &e.kind {
595599
ast::ExprKind::Lit(_) => true,
@@ -610,7 +614,7 @@ impl Token {
610614
/// otherwise returns the original token.
611615
pub fn uninterpolate(&self) -> Cow<'_, Token> {
612616
match &self.kind {
613-
Interpolated(nt) => match &nt.0 {
617+
Interpolated(nt) => match &**nt {
614618
NtIdent(ident, is_raw) => {
615619
Cow::Owned(Token::new(Ident(ident.name, *is_raw), ident.span))
616620
}
@@ -627,7 +631,7 @@ impl Token {
627631
// We avoid using `Token::uninterpolate` here because it's slow.
628632
match &self.kind {
629633
&Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
630-
Interpolated(nt) => match &nt.0 {
634+
Interpolated(nt) => match &**nt {
631635
NtIdent(ident, is_raw) => Some((*ident, *is_raw)),
632636
_ => None,
633637
},
@@ -641,7 +645,7 @@ impl Token {
641645
// We avoid using `Token::uninterpolate` here because it's slow.
642646
match &self.kind {
643647
&Lifetime(name) => Some(Ident::new(name, self.span)),
644-
Interpolated(nt) => match &nt.0 {
648+
Interpolated(nt) => match &**nt {
645649
NtLifetime(ident) => Some(*ident),
646650
_ => None,
647651
},
@@ -668,7 +672,7 @@ impl Token {
668672
/// Returns `true` if the token is an interpolated path.
669673
fn is_whole_path(&self) -> bool {
670674
if let Interpolated(nt) = &self.kind
671-
&& let NtPath(..) = &nt.0
675+
&& let NtPath(..) = &**nt
672676
{
673677
return true;
674678
}
@@ -681,7 +685,7 @@ impl Token {
681685
/// (which happens while parsing the result of macro expansion)?
682686
pub fn is_whole_expr(&self) -> bool {
683687
if let Interpolated(nt) = &self.kind
684-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &nt.0
688+
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
685689
{
686690
return true;
687691
}
@@ -692,7 +696,7 @@ impl Token {
692696
/// Is the token an interpolated block (`$b:block`)?
693697
pub fn is_whole_block(&self) -> bool {
694698
if let Interpolated(nt) = &self.kind
695-
&& let NtBlock(..) = &nt.0
699+
&& let NtBlock(..) = &**nt
696700
{
697701
return true;
698702
}
@@ -857,6 +861,7 @@ pub enum Nonterminal {
857861
NtPat(P<ast::Pat>),
858862
NtExpr(P<ast::Expr>),
859863
NtTy(P<ast::Ty>),
864+
/// The span is for the identifier argument passed to the macro.
860865
NtIdent(Ident, IdentIsRaw),
861866
NtLifetime(Ident),
862867
NtLiteral(P<ast::Expr>),

compiler/rustc_ast/src/tokenstream.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,14 @@ impl TokenStream {
490490

491491
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
492492
match &token.kind {
493-
token::Interpolated(nt) if let token::NtIdent(ident, is_raw) = nt.0 => {
494-
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
493+
token::Interpolated(nt) if let token::NtIdent(ident, is_raw) = &**nt => {
494+
TokenTree::Token(Token::new(token::Ident(ident.name, *is_raw), ident.span), spacing)
495495
}
496496
token::Interpolated(nt) => TokenTree::Delimited(
497497
DelimSpan::from_single(token.span),
498498
DelimSpacing::new(Spacing::JointHidden, spacing),
499499
Delimiter::Invisible,
500-
TokenStream::from_nonterminal_ast(&nt.0).flattened(),
500+
TokenStream::from_nonterminal_ast(&nt).flattened(),
501501
),
502502
_ => TokenTree::Token(token.clone(), spacing),
503503
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
926926
}
927927
token::Eof => "<eof>".into(),
928928

929-
token::Interpolated(ref nt) => self.nonterminal_to_string(&nt.0).into(),
929+
token::Interpolated(ref nt) => self.nonterminal_to_string(&nt).into(),
930930
}
931931
}
932932

compiler/rustc_expand/src/mbe/diagnostics.rs

-6
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ pub(super) fn failed_to_match_macro<'cx>(
7373
&& (matches!(expected_token.kind, TokenKind::Interpolated(_))
7474
|| matches!(token.kind, TokenKind::Interpolated(_)))
7575
{
76-
if let TokenKind::Interpolated(node) = &expected_token.kind {
77-
err.span_label(node.1, "");
78-
}
79-
if let TokenKind::Interpolated(node) = &token.kind {
80-
err.span_label(node.1, "");
81-
}
8276
err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens");
8377
err.note("see <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment> for more information");
8478

compiler/rustc_expand/src/mbe/macro_parser.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ pub(crate) use ParseResult::*;
7575

7676
use crate::mbe::{macro_rules::Tracker, KleeneOp, TokenTree};
7777

78-
use rustc_ast::token::{self, DocComment, Nonterminal, NonterminalKind, Token};
78+
use rustc_ast::token::{self, DocComment, NonterminalKind, Token};
7979
use rustc_ast_pretty::pprust;
8080
use rustc_data_structures::fx::FxHashMap;
81-
use rustc_data_structures::sync::Lrc;
8281
use rustc_errors::ErrorGuaranteed;
8382
use rustc_lint_defs::pluralize;
8483
use rustc_parse::parser::{ParseNtResult, Parser};
@@ -392,7 +391,7 @@ pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
392391
#[derive(Debug, Clone)]
393392
pub(crate) enum NamedMatch {
394393
MatchedSeq(Vec<NamedMatch>),
395-
MatchedSingle(ParseNtResult<Lrc<(Nonterminal, Span)>>),
394+
MatchedSingle(ParseNtResult),
396395
}
397396

398397
/// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison)
@@ -686,11 +685,7 @@ impl TtParser {
686685
}
687686
Ok(nt) => nt,
688687
};
689-
mp.push_match(
690-
next_metavar,
691-
seq_depth,
692-
MatchedSingle(nt.map_nt(|nt| (Lrc::new((nt, span))))),
693-
);
688+
mp.push_match(next_metavar, seq_depth, MatchedSingle(nt));
694689
mp.idx += 1;
695690
} else {
696691
unreachable!()

compiler/rustc_expand/src/proc_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl MultiItemModifier for DeriveProcMacro {
127127
Annotatable::Stmt(stmt) => token::NtStmt(stmt),
128128
_ => unreachable!(),
129129
};
130-
TokenStream::token_alone(token::Interpolated(Lrc::new((nt, span))), DUMMY_SP)
130+
TokenStream::token_alone(token::Interpolated(Lrc::new(nt)), DUMMY_SP)
131131
} else {
132132
item.to_tokens()
133133
};

compiler/rustc_expand/src/proc_macro_server.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
259259
}));
260260
}
261261

262-
Interpolated(ref nt) if let NtIdent(ident, is_raw) = &nt.0 => {
262+
Interpolated(ref nt) if let NtIdent(ident, is_raw) = &**nt => {
263263
trees.push(TokenTree::Ident(Ident {
264264
sym: ident.name,
265265
is_raw: matches!(is_raw, IdentIsRaw::Yes),
@@ -268,14 +268,14 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
268268
}
269269

270270
Interpolated(nt) => {
271-
let stream = TokenStream::from_nonterminal_ast(&nt.0);
271+
let stream = TokenStream::from_nonterminal_ast(&nt);
272272
// A hack used to pass AST fragments to attribute and derive
273273
// macros as a single nonterminal token instead of a token
274274
// stream. Such token needs to be "unwrapped" and not
275275
// represented as a delimited group.
276276
// FIXME: It needs to be removed, but there are some
277277
// compatibility issues (see #73345).
278-
if crate::base::nt_pretty_printing_compatibility_hack(&nt.0, rustc.ecx.sess) {
278+
if crate::base::nt_pretty_printing_compatibility_hack(&nt, rustc.ecx.sess) {
279279
trees.extend(Self::from_internal((stream, rustc)));
280280
} else {
281281
trees.push(TokenTree::Group(Group {

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a> Parser<'a> {
363363
// We can't use `maybe_whole` here because it would bump in the `None`
364364
// case, which we don't want.
365365
if let token::Interpolated(nt) = &self.token.kind
366-
&& let token::NtMeta(attr_item) = &nt.0
366+
&& let token::NtMeta(attr_item) = &**nt
367367
{
368368
match attr_item.meta(attr_item.path.span) {
369369
Some(meta) => {

compiler/rustc_parse/src/parser/diagnostics.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -2372,9 +2372,9 @@ impl<'a> Parser<'a> {
23722372
// in a subsequent macro invocation (#71039).
23732373
let mut tok = self.token.clone();
23742374
let mut labels = vec![];
2375-
while let TokenKind::Interpolated(node) = &tok.kind {
2376-
let tokens = node.0.tokens();
2377-
labels.push(node.clone());
2375+
while let TokenKind::Interpolated(nt) = &tok.kind {
2376+
let tokens = nt.tokens();
2377+
labels.push(nt.clone());
23782378
if let Some(tokens) = tokens
23792379
&& let tokens = tokens.to_attr_token_stream()
23802380
&& let tokens = tokens.0.deref()
@@ -2387,27 +2387,20 @@ impl<'a> Parser<'a> {
23872387
}
23882388
let mut iter = labels.into_iter().peekable();
23892389
let mut show_link = false;
2390-
while let Some(node) = iter.next() {
2391-
let descr = node.0.descr();
2390+
while let Some(nt) = iter.next() {
2391+
let descr = nt.descr();
23922392
if let Some(next) = iter.peek() {
2393-
let next_descr = next.0.descr();
2393+
let next_descr = next.descr();
23942394
if next_descr != descr {
2395-
err.span_label(next.1, format!("this macro fragment matcher is {next_descr}"));
2396-
err.span_label(node.1, format!("this macro fragment matcher is {descr}"));
2395+
err.span_label(next.use_span(), format!("this is expected to be {next_descr}"));
23972396
err.span_label(
2398-
next.0.use_span(),
2399-
format!("this is expected to be {next_descr}"),
2400-
);
2401-
err.span_label(
2402-
node.0.use_span(),
2397+
nt.use_span(),
24032398
format!(
24042399
"this is interpreted as {}, but it is expected to be {}",
24052400
next_descr, descr,
24062401
),
24072402
);
24082403
show_link = true;
2409-
} else {
2410-
err.span_label(node.1, "");
24112404
}
24122405
}
24132406
}

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use thin_vec::{thin_vec, ThinVec};
4545
macro_rules! maybe_whole_expr {
4646
($p:expr) => {
4747
if let token::Interpolated(nt) = &$p.token.kind {
48-
match &nt.0 {
48+
match &**nt {
4949
token::NtExpr(e) | token::NtLiteral(e) => {
5050
let e = e.clone();
5151
$p.bump();

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,7 @@ impl<'a> Parser<'a> {
28412841

28422842
fn is_named_param(&self) -> bool {
28432843
let offset = match &self.token.kind {
2844-
token::Interpolated(nt) => match &nt.0 {
2844+
token::Interpolated(nt) => match &**nt {
28452845
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
28462846
_ => 0,
28472847
},

0 commit comments

Comments
 (0)