Skip to content

Commit 8da7e52

Browse files
committed
Remove NtVis.
We now use invisible delimiters for expanded `vis` fragments, instead of `Token::Interpolated`. This slightly changes some pretty-printed output in tests, but not in any significant way.
1 parent 68d7597 commit 8da7e52

File tree

10 files changed

+83
-37
lines changed

10 files changed

+83
-37
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
@@ -816,7 +816,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
816816
visit_lazy_tts(tokens, vis);
817817
}
818818
token::NtPath(path) => vis.visit_path(path),
819-
token::NtVis(visib) => vis.visit_vis(visib),
820819
}
821820
}
822821

compiler/rustc_ast/src/token.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,6 @@ pub enum Nonterminal {
853853
/// Stuff inside brackets for attributes
854854
NtMeta(P<ast::AttrItem>),
855855
NtPath(P<ast::Path>),
856-
NtVis(P<ast::Visibility>),
857856
}
858857

859858
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -946,7 +945,6 @@ impl Nonterminal {
946945
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
947946
NtMeta(attr_item) => attr_item.span(),
948947
NtPath(path) => path.span,
949-
NtVis(vis) => vis.span,
950948
}
951949
}
952950
}
@@ -980,7 +978,6 @@ impl fmt::Debug for Nonterminal {
980978
NtLiteral(..) => f.pad("NtLiteral(..)"),
981979
NtMeta(..) => f.pad("NtMeta(..)"),
982980
NtPath(..) => f.pad("NtPath(..)"),
983-
NtVis(..) => f.pad("NtVis(..)"),
984981
NtLifetime(..) => f.pad("NtLifetime(..)"),
985982
}
986983
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ impl TokenStream {
474474
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
475475
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
476476
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
477-
Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
478477
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
479478
}
480479
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
742742
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
743743
token::NtLifetime(e) => e.to_string(),
744744
token::NtLiteral(e) => self.expr_to_string(e),
745-
token::NtVis(e) => self.vis_to_string(e),
746745
}
747746
}
748747

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 21 additions & 10 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, 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, InvisibleSource, NonterminalKind, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_errors::{pluralize, PResult};
@@ -211,31 +211,42 @@ pub(super) fn transcribe<'a>(
211211
}
212212
}
213213

214-
// Replace the meta-var with the matched token tree from the invocation.
215214
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
216215
// Find the matched nonterminal from the macro invocation, and use it to replace
217216
// the meta-var.
218217
let ident = MacroRulesNormalizedIdent::new(original_ident);
219218
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
220-
match cur_matched {
219+
let mut mk_delimited = |nt_kind, stream| {
220+
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
221+
// priorities.
222+
marker.visit_span(&mut sp);
223+
TokenTree::Delimited(
224+
DelimSpan::from_single(sp),
225+
Delimiter::Invisible(InvisibleSource::MetaVar(nt_kind)),
226+
stream,
227+
)
228+
};
229+
let tt = match cur_matched {
221230
MatchedSingle(ParseNtResult::Tt(tt)) => {
222231
// `tt`s are emitted into the output stream directly as "raw tokens",
223232
// without wrapping them into groups.
224-
result.push(tt.clone());
233+
tt.clone()
234+
}
235+
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
236+
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
225237
}
226238
MatchedSingle(ParseNtResult::Nt(nt)) => {
227-
// Other variables are emitted into the output stream as groups with
228-
// `Delimiter::Invisible` to maintain parsing priorities.
229-
// `Interpolated` is currently used for such groups in rustc parser.
239+
// `Interpolated` is currently used to maintain parsing priorities for
240+
// these cases, but will eventually be removed.
230241
marker.visit_span(&mut sp);
231-
result
232-
.push(TokenTree::token_alone(token::Interpolated(nt.clone()), sp));
242+
TokenTree::token_alone(token::Interpolated(nt.clone()), sp)
233243
}
234244
MatchedSeq(..) => {
235245
// We were unable to descend far enough. This is an error.
236246
return Err(cx.create_err(VarStillRepeating { span: sp, ident }));
237247
}
238-
}
248+
};
249+
result.push(tt);
239250
} else {
240251
// If we aren't able to match the meta-var, we push it back into the result but
241252
// with modified syntax context. (I believe this supports nested macros).

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
1818
pub use path::PathStyle;
1919

2020
use rustc_ast::ptr::P;
21-
use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind};
21+
use rustc_ast::token::{self, Delimiter, Nonterminal, NonterminalKind, Token, TokenKind};
2222
use rustc_ast::tokenstream::{AttributesData, DelimSpan, Spacing};
2323
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
2424
use rustc_ast::util::case::Case;
@@ -100,6 +100,41 @@ macro_rules! maybe_whole {
100100
};
101101
}
102102

103+
/// Reparses an invisible-delimited sequence produced by expansion of a
104+
/// declarative macro metavariable. Will panic if called with a `self.token`
105+
/// that is not an `InvisibleSource::Metavar` invisible open delimiter.
106+
#[macro_export]
107+
macro_rules! reparse_metavar_seq {
108+
($p:expr, $nt_kind:expr, $nt_res:pat, $ret:expr) => {{
109+
let delim = token::Delimiter::Invisible(token::InvisibleSource::MetaVar($nt_kind));
110+
$p.expect(&token::OpenDelim(delim)).expect("no open delim when reparsing");
111+
// njn: parse_nonterminal collects token. Should this reparsing call
112+
// not do that?
113+
let Ok($nt_res) = $p.parse_nonterminal($nt_kind) else {
114+
panic!("failed to reparse");
115+
};
116+
$p.expect(&token::CloseDelim(delim)).expect("no close delim when reparsing");
117+
$ret
118+
}};
119+
}
120+
121+
/// Reparses an an invisible-delimited sequence produced by expansion of a
122+
/// declarative macro metavariable, if present.
123+
///
124+
/// `$nt_kind_pat` and `$nt_kind` are always syntactically identical in
125+
/// practice, but must be specified separately because one is a pattern and one
126+
/// is an expression. Which is annoying but hard to avoid.
127+
#[macro_export]
128+
macro_rules! maybe_reparse_metavar_seq {
129+
($p:expr, $nt_kind_pat:pat, $nt_kind:expr, $nt_res:pat, $ret:expr) => {
130+
if let Some($nt_kind_pat) = $p.token.is_metavar_seq() {
131+
Some(crate::reparse_metavar_seq!($p, $nt_kind, $nt_res, $ret))
132+
} else {
133+
None
134+
}
135+
};
136+
}
137+
103138
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
104139
#[macro_export]
105140
macro_rules! maybe_recover_from_interpolated_ty_qpath {
@@ -1330,7 +1365,15 @@ impl<'a> Parser<'a> {
13301365
/// so emit a proper diagnostic.
13311366
// Public for rustfmt usage.
13321367
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1333-
maybe_whole!(self, NtVis, |x| x.into_inner());
1368+
if let Some(vis) = maybe_reparse_metavar_seq!(
1369+
self,
1370+
NonterminalKind::Vis,
1371+
NonterminalKind::Vis,
1372+
ParseNtResult::Vis(vis),
1373+
vis
1374+
) {
1375+
return Ok(vis.into_inner());
1376+
}
13341377

13351378
if !self.eat_keyword(kw::Pub) {
13361379
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1515,6 +1558,8 @@ pub enum FlatToken {
15151558
#[derive(Clone, Debug)]
15161559
pub enum ParseNtResult {
15171560
Tt(TokenTree),
1561+
Vis(P<ast::Visibility>),
15181562

1563+
/// This case will eventually be removed, along with `Token::Interpolate`.
15191564
Nt(Lrc<Nonterminal>),
15201565
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 3 additions & 5 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
}
@@ -84,8 +83,7 @@ impl<'a> Parser<'a> {
8483
token::OpenDelim(Delimiter::Brace) => true,
8584
token::Interpolated(nt) => match **nt {
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(InvisibleSource::MetaVar(k))) => match k {
9189
NonterminalKind::Block
@@ -224,9 +222,9 @@ impl<'a> Parser<'a> {
224222
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
225223
),
226224
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
227-
NonterminalKind::Vis => NtVis(
225+
NonterminalKind::Vis => return Ok(ParseNtResult::Vis(
228226
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
229-
),
227+
)),
230228
NonterminalKind::Lifetime => {
231229
if self.check_lifetime() {
232230
NtLifetime(self.expect_lifetime().ident)

tests/ui/macros/stringify.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -859,19 +859,19 @@ fn test_ty() {
859859
#[test]
860860
fn test_vis() {
861861
// VisibilityKind::Public
862-
assert_eq!(stringify_vis!(pub), "pub ");
862+
assert_eq!(stringify_vis!(pub), "pub");
863863

864864
// VisibilityKind::Restricted
865-
assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
866-
assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
867-
assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
868-
assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
869-
assert_eq!(stringify_vis!(pub(in self)), "pub(in self) ");
870-
assert_eq!(stringify_vis!(pub(in super)), "pub(in super) ");
871-
assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
872-
assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
873-
assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
874-
assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super::path::to) ");
865+
assert_eq!(stringify_vis!(pub(crate)), "pub(crate)");
866+
assert_eq!(stringify_vis!(pub(self)), "pub(self)");
867+
assert_eq!(stringify_vis!(pub(super)), "pub(super)");
868+
assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate)");
869+
assert_eq!(stringify_vis!(pub(in self)), "pub(in self)");
870+
assert_eq!(stringify_vis!(pub(in super)), "pub(in super)");
871+
assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path :: to)");
872+
assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in :: path :: to)");
873+
assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self :: path :: to)");
874+
assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super :: path :: to)");
875875

876876
// VisibilityKind::Inherited
877877
// Directly calling `stringify_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 INPUT (DEBUG): TokenStream [

0 commit comments

Comments
 (0)