Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d9488dc

Browse files
committed
Remove NtVis.
We now use invisible delimiters for expanded `vis` fragments, instead of `Token::Interpolated`.
1 parent 5b0c39b commit d9488dc

File tree

7 files changed

+76
-19
lines changed

7 files changed

+76
-19
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ impl HasTokens for Nonterminal {
238238
Nonterminal::NtTy(ty) => ty.tokens(),
239239
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
240240
Nonterminal::NtPath(path) => path.tokens(),
241-
Nonterminal::NtVis(vis) => vis.tokens(),
242241
Nonterminal::NtBlock(block) => block.tokens(),
243242
}
244243
}
@@ -251,7 +250,6 @@ impl HasTokens for Nonterminal {
251250
Nonterminal::NtTy(ty) => ty.tokens_mut(),
252251
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
253252
Nonterminal::NtPath(path) => path.tokens_mut(),
254-
Nonterminal::NtVis(vis) => vis.tokens_mut(),
255253
Nonterminal::NtBlock(block) => block.tokens_mut(),
256254
}
257255
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
843843
visit_lazy_tts(tokens, vis);
844844
}
845845
token::NtPath(path) => vis.visit_path(path),
846-
token::NtVis(visib) => vis.visit_vis(visib),
847846
}
848847
}
849848

compiler/rustc_ast/src/token.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@ impl Token {
859859
}
860860
}
861861

862+
/// Is this an invisible open delimiter at the start of a token sequence
863+
/// from an expanded metavar?
864+
pub fn is_metavar_seq(&self) -> Option<NonterminalKind> {
865+
match self.kind {
866+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
867+
_ => None,
868+
}
869+
}
870+
862871
pub fn glue(&self, joint: &Token) -> Option<Token> {
863872
let kind = match self.kind {
864873
Eq => match joint.kind {
@@ -941,7 +950,6 @@ pub enum Nonterminal {
941950
/// Stuff inside brackets for attributes
942951
NtMeta(P<ast::AttrItem>),
943952
NtPath(P<ast::Path>),
944-
NtVis(P<ast::Visibility>),
945953
}
946954

947955
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1046,7 +1054,6 @@ impl Nonterminal {
10461054
NtTy(ty) => ty.span,
10471055
NtMeta(attr_item) => attr_item.span(),
10481056
NtPath(path) => path.span,
1049-
NtVis(vis) => vis.span,
10501057
}
10511058
}
10521059

@@ -1061,7 +1068,6 @@ impl Nonterminal {
10611068
NtTy(..) => "type",
10621069
NtMeta(..) => "attribute",
10631070
NtPath(..) => "path",
1064-
NtVis(..) => "visibility",
10651071
}
10661072
}
10671073
}
@@ -1088,7 +1094,6 @@ impl fmt::Debug for Nonterminal {
10881094
NtLiteral(..) => f.pad("NtLiteral(..)"),
10891095
NtMeta(..) => f.pad("NtMeta(..)"),
10901096
NtPath(..) => f.pad("NtPath(..)"),
1091-
NtVis(..) => f.pad("NtVis(..)"),
10921097
}
10931098
}
10941099
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ impl TokenStream {
477477
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
478478
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
479479
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
480-
Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
481480
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
482481
}
483482
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::mbe::metavar_expr::{MetaVarExprConcatElem, RAW_IDENT_ERR};
77
use crate::mbe::{self, KleeneOp, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
99
use rustc_ast::token::IdentIsRaw;
10-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
10+
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, NonterminalKind, Token, TokenKind};
1111
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_errors::{pluralize, Diag, DiagCtxtHandle, PResult};
@@ -248,7 +248,6 @@ pub(super) fn transcribe<'a>(
248248
}
249249
}
250250

251-
// Replace the meta-var with the matched token tree from the invocation.
252251
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
253252
// Find the matched nonterminal from the macro invocation, and use it to replace
254253
// the meta-var.
@@ -268,6 +267,19 @@ pub(super) fn transcribe<'a>(
268267
// some of the unnecessary whitespace.
269268
let ident = MacroRulesNormalizedIdent::new(original_ident);
270269
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
270+
let mut mk_delimited = |nt_kind, stream| {
271+
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
272+
// priorities.
273+
marker.visit_span(&mut sp);
274+
// Both the open delim and close delim get the same span, which covers the
275+
// `$foo` in the decl macro RHS.
276+
TokenTree::Delimited(
277+
DelimSpan::from_single(sp),
278+
DelimSpacing::new(Spacing::Alone, Spacing::Alone),
279+
Delimiter::Invisible(InvisibleOrigin::MetaVar(nt_kind)),
280+
stream,
281+
)
282+
};
271283
let tt = match cur_matched {
272284
MatchedSingle(ParseNtResult::Tt(tt)) => {
273285
// `tt`s are emitted into the output stream directly as "raw tokens",
@@ -284,6 +296,9 @@ pub(super) fn transcribe<'a>(
284296
let kind = token::NtLifetime(*ident);
285297
TokenTree::token_alone(kind, sp)
286298
}
299+
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
300+
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
301+
}
287302
MatchedSingle(ParseNtResult::Nt(nt)) => {
288303
// Other variables are emitted into the output stream as groups with
289304
// `Delimiter::Invisible` to maintain parsing priorities.

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ macro_rules! maybe_whole {
118118
};
119119
}
120120

121+
/// Reparses an invisible-delimited sequence produced by expansion of a
122+
/// declarative macro metavariable. Will panic if called with a `self.token`
123+
/// that is not an `InvisibleOrigin::Metavar` invisible open delimiter.
124+
#[macro_export]
125+
macro_rules! reparse_metavar_seq {
126+
($p:expr, $nt_kind:expr, $nt_res:pat, $ret:expr) => {{
127+
let delim = token::Delimiter::Invisible(token::InvisibleOrigin::MetaVar($nt_kind));
128+
$p.expect(&token::OpenDelim(delim)).expect("no open delim when reparsing");
129+
let Ok($nt_res) = $p.parse_nonterminal($nt_kind) else {
130+
panic!("failed to reparse");
131+
};
132+
$p.expect(&token::CloseDelim(delim)).expect("no close delim when reparsing");
133+
$ret
134+
}};
135+
}
136+
137+
/// Reparses an an invisible-delimited sequence produced by expansion of a
138+
/// declarative macro metavariable, if present.
139+
///
140+
/// `$nt_kind_pat` and `$nt_kind` are always syntactically identical in
141+
/// practice, but must be specified separately because one is a pattern and one
142+
/// is an expression. Which is annoying but hard to avoid.
143+
#[macro_export]
144+
macro_rules! maybe_reparse_metavar_seq {
145+
($p:expr, $nt_kind_pat:pat, $nt_kind:expr, $nt_res:pat, $ret:expr) => {
146+
if let Some($nt_kind_pat) = $p.token.is_metavar_seq() {
147+
Some(crate::reparse_metavar_seq!($p, $nt_kind, $nt_res, $ret))
148+
} else {
149+
None
150+
}
151+
};
152+
}
153+
121154
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
122155
#[macro_export]
123156
macro_rules! maybe_recover_from_interpolated_ty_qpath {
@@ -1412,7 +1445,15 @@ impl<'a> Parser<'a> {
14121445
/// so emit a proper diagnostic.
14131446
// Public for rustfmt usage.
14141447
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1415-
maybe_whole!(self, NtVis, |vis| vis.into_inner());
1448+
if let Some(vis) = maybe_reparse_metavar_seq!(
1449+
self,
1450+
NonterminalKind::Vis,
1451+
NonterminalKind::Vis,
1452+
ParseNtResult::Vis(vis),
1453+
vis
1454+
) {
1455+
return Ok(vis.into_inner());
1456+
}
14161457

14171458
if !self.eat_keyword(kw::Pub) {
14181459
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1638,7 +1679,8 @@ pub enum ParseNtResult {
16381679
Tt(TokenTree),
16391680
Ident(Ident, IdentIsRaw),
16401681
Lifetime(Ident),
1682+
Vis(P<ast::Visibility>),
16411683

1642-
/// This case will eventually be removed, along with `Token::Interpolate`.
1684+
/// This variant will eventually be removed, along with `Token::Interpolate`.
16431685
Nt(Lrc<Nonterminal>),
16441686
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ impl<'a> Parser<'a> {
5555
| NtMeta(_)
5656
| NtPath(_) => true,
5757

58-
NtItem(_)
59-
| NtBlock(_)
60-
| NtVis(_) => false,
58+
NtItem(_) | NtBlock(_) => false,
6159
}
6260
}
6361

@@ -78,7 +76,7 @@ impl<'a> Parser<'a> {
7876
NonterminalKind::Ident => get_macro_ident(token).is_some(),
7977
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
8078
NonterminalKind::Vis => match token.kind {
81-
// The follow-set of :vis + "priv" keyword + interpolated
79+
// The follow-set of :vis + "priv" keyword + interpolated/metavar-expansion.
8280
token::Comma
8381
| token::Ident(..)
8482
| token::NtIdent(..)
@@ -92,7 +90,7 @@ impl<'a> Parser<'a> {
9290
token::NtLifetime(..) => true,
9391
token::Interpolated(nt) => match &**nt {
9492
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
95-
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) | NtVis(_) => false,
93+
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) => false,
9694
},
9795
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9896
NonterminalKind::Block
@@ -227,8 +225,9 @@ impl<'a> Parser<'a> {
227225
}
228226
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
229227
NonterminalKind::Vis => {
230-
NtVis(P(self
231-
.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?))
228+
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
229+
this.parse_visibility(FollowedByType::Yes)
230+
})?)));
232231
}
233232
NonterminalKind::Lifetime => {
234233
return if self.check_lifetime() {

0 commit comments

Comments
 (0)