Skip to content

Commit b5d4767

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

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
@@ -524,11 +524,11 @@ impl Token {
524524
NtExpr(..) |
525525
NtBlock(..) |
526526
NtPath(..)),
527-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
528-
NonterminalKind::Block |
529-
NonterminalKind::Expr |
530-
NonterminalKind::Literal |
531-
NonterminalKind::Path
527+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
528+
NonterminalKind::Block |
529+
NonterminalKind::Expr |
530+
NonterminalKind::Literal |
531+
NonterminalKind::Path
532532
))) => true,
533533
_ => false,
534534
}
@@ -845,6 +845,15 @@ impl Token {
845845
}
846846
}
847847

848+
/// Is this an invisible open delimiter at the start of a token sequence
849+
/// from an expanded metavar?
850+
pub fn is_metavar_seq(&self) -> Option<NonterminalKind> {
851+
match self.kind {
852+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
853+
_ => None,
854+
}
855+
}
856+
848857
pub fn glue(&self, joint: &Token) -> Option<Token> {
849858
let kind = match self.kind {
850859
Eq => match joint.kind {
@@ -928,7 +937,6 @@ pub enum Nonterminal {
928937
/// Stuff inside brackets for attributes
929938
NtMeta(P<ast::AttrItem>),
930939
NtPath(P<ast::Path>),
931-
NtVis(P<ast::Visibility>),
932940
}
933941

934942
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1021,7 +1029,6 @@ impl Nonterminal {
10211029
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
10221030
NtMeta(attr_item) => attr_item.span(),
10231031
NtPath(path) => path.span,
1024-
NtVis(vis) => vis.span,
10251032
}
10261033
}
10271034

@@ -1038,7 +1045,6 @@ impl Nonterminal {
10381045
NtLifetime(..) => "lifetime",
10391046
NtMeta(..) => "attribute",
10401047
NtPath(..) => "path",
1041-
NtVis(..) => "visibility",
10421048
}
10431049
}
10441050
}
@@ -1072,7 +1078,6 @@ impl fmt::Debug for Nonterminal {
10721078
NtLiteral(..) => f.pad("NtLiteral(..)"),
10731079
NtMeta(..) => f.pad("NtMeta(..)"),
10741080
NtPath(..) => f.pad("NtPath(..)"),
1075-
NtVis(..) => f.pad("NtVis(..)"),
10761081
NtLifetime(..) => f.pad("NtLifetime(..)"),
10771082
}
10781083
}

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), // njn: ?
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
@@ -104,6 +104,41 @@ macro_rules! maybe_whole {
104104
};
105105
}
106106

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

14451488
if !self.eat_keyword(kw::Pub) {
14461489
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1621,6 +1664,9 @@ pub enum FlatToken {
16211664
#[derive(Clone, Debug)]
16221665
pub enum ParseNtResult<NtType> {
16231666
Tt(TokenTree),
1667+
Vis(P<ast::Visibility>),
1668+
1669+
/// This variant will eventually be removed, along with `Token::Interpolate`.
16241670
Nt(NtType),
16251671
}
16261672

@@ -1631,6 +1677,7 @@ impl<T> ParseNtResult<T> {
16311677
{
16321678
match self {
16331679
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
1680+
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16341681
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
16351682
}
16361683
}

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)