Skip to content

Commit 41eb0dc

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

File tree

7 files changed

+79
-19
lines changed

7 files changed

+79
-19
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ impl HasTokens for Nonterminal {
206206
Nonterminal::NtTy(ty) => ty.tokens(),
207207
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
208208
Nonterminal::NtPath(path) => path.tokens(),
209-
Nonterminal::NtVis(vis) => vis.tokens(),
210209
Nonterminal::NtBlock(block) => block.tokens(),
211210
}
212211
}
@@ -219,7 +218,6 @@ impl HasTokens for Nonterminal {
219218
Nonterminal::NtTy(ty) => ty.tokens_mut(),
220219
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
221220
Nonterminal::NtPath(path) => path.tokens_mut(),
222-
Nonterminal::NtVis(vis) => vis.tokens_mut(),
223221
Nonterminal::NtBlock(block) => block.tokens_mut(),
224222
}
225223
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
824824
visit_lazy_tts(vis, tokens);
825825
}
826826
token::NtPath(path) => vis.visit_path(path),
827-
token::NtVis(visib) => vis.visit_vis(visib),
828827
}
829828
}
830829

compiler/rustc_ast/src/token.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,15 @@ impl Token {
963963
}
964964
}
965965

966+
/// Is this an invisible open delimiter at the start of a token sequence
967+
/// from an expanded metavar?
968+
pub fn is_metavar_seq(&self) -> Option<MetaVarKind> {
969+
match self.kind {
970+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
971+
_ => None,
972+
}
973+
}
974+
966975
pub fn glue(&self, joint: &Token) -> Option<Token> {
967976
let kind = match self.kind {
968977
Eq => match joint.kind {
@@ -1066,7 +1075,6 @@ pub enum Nonterminal {
10661075
/// Stuff inside brackets for attributes
10671076
NtMeta(P<ast::AttrItem>),
10681077
NtPath(P<ast::Path>),
1069-
NtVis(P<ast::Visibility>),
10701078
}
10711079

10721080
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1163,7 +1171,6 @@ impl Nonterminal {
11631171
NtTy(ty) => ty.span,
11641172
NtMeta(attr_item) => attr_item.span(),
11651173
NtPath(path) => path.span,
1166-
NtVis(vis) => vis.span,
11671174
}
11681175
}
11691176

@@ -1178,7 +1185,6 @@ impl Nonterminal {
11781185
NtTy(..) => "type",
11791186
NtMeta(..) => "attribute",
11801187
NtPath(..) => "path",
1181-
NtVis(..) => "visibility",
11821188
}
11831189
}
11841190
}
@@ -1205,7 +1211,6 @@ impl fmt::Debug for Nonterminal {
12051211
NtLiteral(..) => f.pad("NtLiteral(..)"),
12061212
NtMeta(..) => f.pad("NtMeta(..)"),
12071213
NtPath(..) => f.pad("NtPath(..)"),
1208-
NtVis(..) => f.pad("NtVis(..)"),
12091214
}
12101215
}
12111216
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ impl TokenStream {
472472
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
473473
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
474474
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
475-
Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
476475
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
477476
}
478477
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::mem;
22

33
use rustc_ast::ExprKind;
44
use rustc_ast::mut_visit::{self, MutVisitor};
5-
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, LitKind, Nonterminal, Token, TokenKind};
5+
use rustc_ast::token::{
6+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
7+
TokenKind,
8+
};
69
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
710
use rustc_data_structures::fx::FxHashMap;
811
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
@@ -252,7 +255,6 @@ pub(super) fn transcribe<'a>(
252255
}
253256
}
254257

255-
// Replace the meta-var with the matched token tree from the invocation.
256258
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
257259
// Find the matched nonterminal from the macro invocation, and use it to replace
258260
// the meta-var.
@@ -272,6 +274,19 @@ pub(super) fn transcribe<'a>(
272274
// some of the unnecessary whitespace.
273275
let ident = MacroRulesNormalizedIdent::new(original_ident);
274276
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
277+
let mut mk_delimited = |mv_kind, stream| {
278+
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
279+
// priorities.
280+
marker.visit_span(&mut sp);
281+
// Both the open delim and close delim get the same span, which covers the
282+
// `$foo` in the decl macro RHS.
283+
TokenTree::Delimited(
284+
DelimSpan::from_single(sp),
285+
DelimSpacing::new(Spacing::Alone, Spacing::Alone),
286+
Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)),
287+
stream,
288+
)
289+
};
275290
let tt = match cur_matched {
276291
MatchedSingle(ParseNtResult::Tt(tt)) => {
277292
// `tt`s are emitted into the output stream directly as "raw tokens",
@@ -288,6 +303,9 @@ pub(super) fn transcribe<'a>(
288303
let kind = token::NtLifetime(*ident, *is_raw);
289304
TokenTree::token_alone(kind, sp)
290305
}
306+
MatchedSingle(ParseNtResult::Vis(vis)) => {
307+
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
308+
}
291309
MatchedSingle(ParseNtResult::Nt(nt)) => {
292310
// Other variables are emitted into the output stream as groups with
293311
// `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
@@ -721,6 +721,43 @@ impl<'a> Parser<'a> {
721721
if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
722722
}
723723

724+
/// Consume a sequence produced by a metavar expansion, if present.
725+
fn eat_metavar_seq<T>(
726+
&mut self,
727+
mv_kind: MetaVarKind,
728+
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
729+
) -> Option<T> {
730+
self.eat_metavar_seq_with_matcher(|mvk| mvk == mv_kind, f)
731+
}
732+
733+
/// A slightly more general form of `eat_metavar_seq`, for use with the
734+
/// `MetaVarKind` variants that have parameters, where an exact match isn't
735+
/// desired.
736+
fn eat_metavar_seq_with_matcher<T>(
737+
&mut self,
738+
match_mv_kind: impl Fn(MetaVarKind) -> bool,
739+
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
740+
) -> Option<T> {
741+
if let token::OpenDelim(delim) = self.token.kind
742+
&& let Delimiter::Invisible(token::InvisibleOrigin::MetaVar(mv_kind)) = delim
743+
&& match_mv_kind(mv_kind)
744+
{
745+
self.bump();
746+
let res = f(self).expect("failed to reparse {mv_kind:?}");
747+
if let token::CloseDelim(delim) = self.token.kind
748+
&& let Delimiter::Invisible(token::InvisibleOrigin::MetaVar(mv_kind)) = delim
749+
&& match_mv_kind(mv_kind)
750+
{
751+
self.bump();
752+
Some(res)
753+
} else {
754+
panic!("no close delim when reparsing {mv_kind:?}");
755+
}
756+
} else {
757+
None
758+
}
759+
}
760+
724761
/// Is the given keyword `kw` followed by a non-reserved identifier?
725762
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
726763
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
@@ -1462,7 +1499,11 @@ impl<'a> Parser<'a> {
14621499
/// so emit a proper diagnostic.
14631500
// Public for rustfmt usage.
14641501
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1465-
maybe_whole!(self, NtVis, |vis| vis.into_inner());
1502+
if let Some(vis) = self
1503+
.eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
1504+
{
1505+
return Ok(vis);
1506+
}
14661507

14671508
if !self.eat_keyword(kw::Pub) {
14681509
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1700,7 +1741,8 @@ pub enum ParseNtResult {
17001741
Tt(TokenTree),
17011742
Ident(Ident, IdentIsRaw),
17021743
Lifetime(Ident, IdentIsRaw),
1744+
Vis(P<ast::Visibility>),
17031745

1704-
/// This case will eventually be removed, along with `Token::Interpolate`.
1746+
/// This variant will eventually be removed, along with `Token::Interpolate`.
17051747
Nt(Lrc<Nonterminal>),
17061748
}

compiler/rustc_parse/src/parser/nonterminal.rs

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

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

@@ -88,7 +86,7 @@ impl<'a> Parser<'a> {
8886
NonterminalKind::Ident => get_macro_ident(token).is_some(),
8987
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
9088
NonterminalKind::Vis => match token.kind {
91-
// The follow-set of :vis + "priv" keyword + interpolated
89+
// The follow-set of :vis + "priv" keyword + interpolated/metavar-expansion.
9290
token::Comma
9391
| token::Ident(..)
9492
| token::NtIdent(..)
@@ -102,7 +100,7 @@ impl<'a> Parser<'a> {
102100
token::NtLifetime(..) => true,
103101
token::Interpolated(nt) => match &**nt {
104102
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
105-
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) | NtVis(_) => false,
103+
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) => false,
106104
},
107105
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
108106
MetaVarKind::Block
@@ -208,8 +206,9 @@ impl<'a> Parser<'a> {
208206
}
209207
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(ForceCollect::Yes)?)),
210208
NonterminalKind::Vis => {
211-
NtVis(P(self
212-
.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?))
209+
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
210+
this.parse_visibility(FollowedByType::Yes)
211+
})?)));
213212
}
214213
NonterminalKind::Lifetime => {
215214
// We want to keep `'keyword` parsing, just like `keyword` is still

0 commit comments

Comments
 (0)