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

Commit 08af67f

Browse files
nnethercotecompiler-errors
authored andcommitted
Remove NtVis.
We now use invisible delimiters for expanded `vis` fragments, instead of `Token::Interpolated`.
1 parent 9625b10 commit 08af67f

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
@@ -957,6 +957,15 @@ impl Token {
957957
}
958958
}
959959

960+
/// Is this an invisible open delimiter at the start of a token sequence
961+
/// from an expanded metavar?
962+
pub fn is_metavar_seq(&self) -> Option<MetaVarKind> {
963+
match self.kind {
964+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
965+
_ => None,
966+
}
967+
}
968+
960969
pub fn glue(&self, joint: &Token) -> Option<Token> {
961970
let kind = match self.kind {
962971
Eq => match joint.kind {
@@ -1060,7 +1069,6 @@ pub enum Nonterminal {
10601069
/// Stuff inside brackets for attributes
10611070
NtMeta(P<ast::AttrItem>),
10621071
NtPath(P<ast::Path>),
1063-
NtVis(P<ast::Visibility>),
10641072
}
10651073

10661074
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1157,7 +1165,6 @@ impl Nonterminal {
11571165
NtTy(ty) => ty.span,
11581166
NtMeta(attr_item) => attr_item.span(),
11591167
NtPath(path) => path.span,
1160-
NtVis(vis) => vis.span,
11611168
}
11621169
}
11631170

@@ -1172,7 +1179,6 @@ impl Nonterminal {
11721179
NtTy(..) => "type",
11731180
NtMeta(..) => "attribute",
11741181
NtPath(..) => "path",
1175-
NtVis(..) => "visibility",
11761182
}
11771183
}
11781184
}
@@ -1199,7 +1205,6 @@ impl fmt::Debug for Nonterminal {
11991205
NtLiteral(..) => f.pad("NtLiteral(..)"),
12001206
NtMeta(..) => f.pad("NtMeta(..)"),
12011207
NtPath(..) => f.pad("NtPath(..)"),
1202-
NtVis(..) => f.pad("NtVis(..)"),
12031208
}
12041209
}
12051210
}

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
@@ -1,7 +1,10 @@
11
use std::mem;
22

33
use rustc_ast::mut_visit::{self, MutVisitor};
4-
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, LitKind, Nonterminal, Token, TokenKind};
4+
use rustc_ast::token::{
5+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
6+
TokenKind,
7+
};
58
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
69
use rustc_ast::ExprKind;
710
use rustc_data_structures::fx::FxHashMap;
@@ -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
@@ -717,6 +717,43 @@ impl<'a> Parser<'a> {
717717
if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
718718
}
719719

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

14641505
if !self.eat_keyword(kw::Pub) {
14651506
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1684,7 +1725,8 @@ pub enum ParseNtResult {
16841725
Tt(TokenTree),
16851726
Ident(Ident, IdentIsRaw),
16861727
Lifetime(Ident, IdentIsRaw),
1728+
Vis(P<ast::Visibility>),
16871729

1688-
/// This case will eventually be removed, along with `Token::Interpolate`.
1730+
/// This variant will eventually be removed, along with `Token::Interpolate`.
16891731
Nt(Lrc<Nonterminal>),
16901732
}

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)