Skip to content

Commit 31943c1

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

File tree

10 files changed

+94
-39
lines changed

10 files changed

+94
-39
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
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
244243
}
@@ -252,7 +251,6 @@ impl HasTokens for Nonterminal {
252251
Nonterminal::NtTy(ty) => ty.tokens_mut(),
253252
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
254253
Nonterminal::NtPath(path) => path.tokens_mut(),
255-
Nonterminal::NtVis(vis) => vis.tokens_mut(),
256254
Nonterminal::NtBlock(block) => block.tokens_mut(),
257255
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
258256
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
849849
visit_lazy_tts(tokens, vis);
850850
}
851851
token::NtPath(path) => vis.visit_path(path),
852-
token::NtVis(visib) => vis.visit_vis(visib),
853852
}
854853
}
855854

compiler/rustc_ast/src/token.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,11 @@ impl Token {
520520
NtExpr(..) |
521521
NtBlock(..) |
522522
NtPath(..)),
523-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
524-
NonterminalKind::Block |
525-
NonterminalKind::Expr |
526-
NonterminalKind::Literal |
527-
NonterminalKind::Path
523+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
524+
NonterminalKind::Block |
525+
NonterminalKind::Expr |
526+
NonterminalKind::Literal |
527+
NonterminalKind::Path
528528
))) => true,
529529
_ => false,
530530
}
@@ -835,6 +835,15 @@ impl Token {
835835
}
836836
}
837837

838+
/// Is this an invisible open delimiter at the start of a token sequence
839+
/// from an expanded metavar?
840+
pub fn is_metavar_seq(&self) -> Option<NonterminalKind> {
841+
match self.kind {
842+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
843+
_ => None,
844+
}
845+
}
846+
838847
pub fn glue(&self, joint: &Token) -> Option<Token> {
839848
let kind = match self.kind {
840849
Eq => match joint.kind {
@@ -917,7 +926,6 @@ pub enum Nonterminal {
917926
/// Stuff inside brackets for attributes
918927
NtMeta(P<ast::AttrItem>),
919928
NtPath(P<ast::Path>),
920-
NtVis(P<ast::Visibility>),
921929
}
922930

923931
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1010,7 +1018,6 @@ impl Nonterminal {
10101018
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
10111019
NtMeta(attr_item) => attr_item.span(),
10121020
NtPath(path) => path.span,
1013-
NtVis(vis) => vis.span,
10141021
}
10151022
}
10161023

@@ -1027,7 +1034,6 @@ impl Nonterminal {
10271034
NtLifetime(..) => "lifetime",
10281035
NtMeta(..) => "attribute",
10291036
NtPath(..) => "path",
1030-
NtVis(..) => "visibility",
10311037
}
10321038
}
10331039
}
@@ -1061,7 +1067,6 @@ impl fmt::Debug for Nonterminal {
10611067
NtLiteral(..) => f.pad("NtLiteral(..)"),
10621068
NtMeta(..) => f.pad("NtMeta(..)"),
10631069
NtPath(..) => f.pad("NtPath(..)"),
1064-
NtVis(..) => f.pad("NtVis(..)"),
10651070
NtLifetime(..) => f.pad("NtLifetime(..)"),
10661071
}
10671072
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ impl TokenStream {
484484
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
485485
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
486486
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
487-
Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
488487
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
489488
}
490489
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
855855
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),
856856
token::NtLifetime(e) => e.to_string(),
857857
token::NtLiteral(e) => self.expr_to_string(e),
858-
token::NtVis(e) => self.vis_to_string(e),
859858
}
860859
}
861860

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::errors::{
66
use crate::mbe::macro_parser::{NamedMatch, NamedMatch::*};
77
use crate::mbe::{self, KleeneOp, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
9-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
9+
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, NonterminalKind, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_errors::{pluralize, Diag, PResult};
@@ -244,18 +244,31 @@ pub(super) fn transcribe<'a>(
244244
}
245245
}
246246

247-
// Replace the meta-var with the matched token tree from the invocation.
248247
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
249248
// Find the matched nonterminal from the macro invocation, and use it to replace
250249
// the meta-var.
251250
let ident = MacroRulesNormalizedIdent::new(original_ident);
252251
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
252+
let mut mk_delimited = |nt_kind, stream| {
253+
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
254+
// priorities.
255+
marker.visit_span(&mut sp);
256+
TokenTree::Delimited(
257+
DelimSpan::from_single(sp),
258+
DelimSpacing::new(Spacing::Alone, Spacing::Alone),
259+
Delimiter::Invisible(InvisibleOrigin::MetaVar(nt_kind)),
260+
stream,
261+
)
262+
};
253263
let tt = match cur_matched {
254264
MatchedSingle(ParseNtResult::Tt(tt)) => {
255265
// `tt`s are emitted into the output stream directly as "raw tokens",
256266
// without wrapping them into groups.
257267
maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker)
258268
}
269+
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
270+
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
271+
}
259272
MatchedSingle(ParseNtResult::Nt(nt)) => {
260273
// Other variables are emitted into the output stream as groups with
261274
// `Delimiter::Invisible` to maintain parsing priorities.

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,41 @@ macro_rules! maybe_whole {
103103
};
104104
}
105105

106+
/// Reparses an invisible-delimited sequence produced by expansion of a
107+
/// declarative macro metavariable. Will panic if called with a `self.token`
108+
/// that is not an `InvisibleOrigin::Metavar` invisible open delimiter.
109+
#[macro_export]
110+
macro_rules! reparse_metavar_seq {
111+
($p:expr, $nt_kind:expr, $nt_res:pat, $ret:expr) => {{
112+
let delim = token::Delimiter::Invisible(token::InvisibleOrigin::MetaVar($nt_kind));
113+
$p.expect(&token::OpenDelim(delim)).expect("no open delim when reparsing");
114+
// njn: parse_nonterminal collects token. Should this reparsing call
115+
// not do that?
116+
let Ok($nt_res) = $p.parse_nonterminal($nt_kind) else {
117+
panic!("failed to reparse");
118+
};
119+
$p.expect(&token::CloseDelim(delim)).expect("no close delim when reparsing");
120+
$ret
121+
}};
122+
}
123+
124+
/// Reparses an an invisible-delimited sequence produced by expansion of a
125+
/// declarative macro metavariable, if present.
126+
///
127+
/// `$nt_kind_pat` and `$nt_kind` are always syntactically identical in
128+
/// practice, but must be specified separately because one is a pattern and one
129+
/// is an expression. Which is annoying but hard to avoid.
130+
#[macro_export]
131+
macro_rules! maybe_reparse_metavar_seq {
132+
($p:expr, $nt_kind_pat:pat, $nt_kind:expr, $nt_res:pat, $ret:expr) => {
133+
if let Some($nt_kind_pat) = $p.token.is_metavar_seq() {
134+
Some(crate::reparse_metavar_seq!($p, $nt_kind, $nt_res, $ret))
135+
} else {
136+
None
137+
}
138+
};
139+
}
140+
106141
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
107142
#[macro_export]
108143
macro_rules! maybe_recover_from_interpolated_ty_qpath {
@@ -1423,7 +1458,15 @@ impl<'a> Parser<'a> {
14231458
/// so emit a proper diagnostic.
14241459
// Public for rustfmt usage.
14251460
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1426-
maybe_whole!(self, NtVis, |vis| vis.into_inner());
1461+
if let Some(vis) = maybe_reparse_metavar_seq!(
1462+
self,
1463+
NonterminalKind::Vis,
1464+
NonterminalKind::Vis,
1465+
ParseNtResult::Vis(vis),
1466+
vis
1467+
) {
1468+
return Ok(vis.into_inner());
1469+
}
14271470

14281471
if !self.eat_keyword(kw::Pub) {
14291472
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1604,6 +1647,9 @@ pub enum FlatToken {
16041647
#[derive(Clone, Debug)]
16051648
pub enum ParseNtResult<NtType> {
16061649
Tt(TokenTree),
1650+
Vis(P<ast::Visibility>),
1651+
1652+
/// This variant will eventually be removed, along with `Token::Interpolate`.
16071653
Nt(NtType),
16081654
}
16091655

@@ -1614,6 +1660,7 @@ impl<T> ParseNtResult<T> {
16141660
{
16151661
match self {
16161662
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
1663+
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16171664
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
16181665
}
16191666
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl<'a> Parser<'a> {
5656

5757
NtItem(_)
5858
| NtBlock(_)
59-
| NtVis(_)
6059
| NtLifetime(_) => false,
6160
}
6261
}
@@ -73,7 +72,7 @@ impl<'a> Parser<'a> {
7372
NonterminalKind::Ident => get_macro_ident(token).is_some(),
7473
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
7574
NonterminalKind::Vis => match token.kind {
76-
// The follow-set of :vis + "priv" keyword + interpolated/metavar-expansion
75+
// The follow-set of :vis + "priv" keyword + interpolated/metavar-expansion.
7776
token::Comma
7877
| token::Ident(..)
7978
| token::Interpolated(..)
@@ -84,8 +83,7 @@ impl<'a> Parser<'a> {
8483
token::OpenDelim(Delimiter::Brace) => true,
8584
token::Interpolated(nt) => match &nt.0 {
8685
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
87-
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_)
88-
| NtVis(_) => false,
86+
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
8987
},
9088
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9189
NonterminalKind::Block
@@ -224,8 +222,9 @@ impl<'a> Parser<'a> {
224222
}
225223
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
226224
NonterminalKind::Vis => {
227-
NtVis(P(self
228-
.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?))
225+
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
226+
this.parse_visibility(FollowedByType::Yes)
227+
})?)));
229228
}
230229
NonterminalKind::Lifetime => {
231230
if self.check_lifetime() {

tests/ui/macros/stringify.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -819,23 +819,19 @@ fn test_ty() {
819819
#[test]
820820
fn test_vis() {
821821
// VisibilityKind::Public
822-
c2!(vis, [ pub ], "pub ", "pub");
822+
c1!(vis, [ pub ], "pub");
823823

824824
// VisibilityKind::Restricted
825-
c2!(vis, [ pub(crate) ], "pub(crate) ", "pub(crate)");
826-
c2!(vis, [ pub(self) ], "pub(self) ", "pub(self)");
827-
c2!(vis, [ pub(super) ], "pub(super) ", "pub(super)");
828-
c2!(vis, [ pub(in crate) ], "pub(in crate) ", "pub(in crate)");
829-
c2!(vis, [ pub(in self) ], "pub(in self) ", "pub(in self)");
830-
c2!(vis, [ pub(in super) ], "pub(in super) ", "pub(in super)");
831-
c2!(vis, [ pub(in path::to) ], "pub(in path::to) ", "pub(in path::to)");
832-
c2!(vis, [ pub(in ::path::to) ], "pub(in ::path::to) ", "pub(in ::path::to)");
833-
c2!(vis, [ pub(in self::path::to) ], "pub(in self::path::to) ", "pub(in self::path::to)");
834-
c2!(vis,
835-
[ pub(in super::path::to) ],
836-
"pub(in super::path::to) ",
837-
"pub(in super::path::to)"
838-
);
825+
c1!(vis, [ pub(crate) ], "pub(crate)");
826+
c1!(vis, [ pub(self) ], "pub(self)");
827+
c1!(vis, [ pub(super) ], "pub(super)");
828+
c1!(vis, [ pub(in crate) ], "pub(in crate)");
829+
c1!(vis, [ pub(in self) ], "pub(in self)");
830+
c1!(vis, [ pub(in super) ], "pub(in super)");
831+
c1!(vis, [ pub(in path::to) ], "pub(in path::to)");
832+
c1!(vis, [ pub(in ::path::to) ], "pub(in ::path::to)");
833+
c1!(vis, [ pub(in self::path::to) ], "pub(in self::path::to)");
834+
c1!(vis, [ pub(in super::path::to) ], "pub(in super::path::to)");
839835

840836
// VisibilityKind::Inherited
841837
// This one is different because directly calling `vis!` does not work.

tests/ui/proc-macro/capture-macro-rules-invoke.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
1212
},
1313
]
1414
PRINT-BANG INPUT (DISPLAY): 1 + 1, { "a" }, let a = 1;, String, my_name, 'a, my_val = 30,
15-
std::option::Option, pub(in some::path) , [a b c], -30
15+
std::option::Option, pub(in some::path), [a b c], -30
1616
PRINT-BANG RE-COLLECTED (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
1717
std::option::Option, pub(in some::path), [a b c], -30
1818
PRINT-BANG DEEP-RE-COLLECTED (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,

0 commit comments

Comments
 (0)