Skip to content

Commit c7981d6

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

File tree

8 files changed

+110
-18
lines changed

8 files changed

+110
-18
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
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

-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
916916
visit_lazy_tts(vis, tokens);
917917
}
918918
token::NtPath(path) => vis.visit_path(path),
919-
token::NtVis(visib) => vis.visit_vis(visib),
920919
}
921920
}
922921

compiler/rustc_ast/src/token.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,15 @@ impl Token {
969969
}
970970
}
971971

972+
/// Is this an invisible open delimiter at the start of a token sequence
973+
/// from an expanded metavar?
974+
pub fn is_metavar_seq(&self) -> Option<MetaVarKind> {
975+
match self.kind {
976+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
977+
_ => None,
978+
}
979+
}
980+
972981
pub fn glue(&self, joint: &Token) -> Option<Token> {
973982
let kind = match self.kind {
974983
Eq => match joint.kind {
@@ -1072,7 +1081,6 @@ pub enum Nonterminal {
10721081
/// Stuff inside brackets for attributes
10731082
NtMeta(P<ast::AttrItem>),
10741083
NtPath(P<ast::Path>),
1075-
NtVis(P<ast::Visibility>),
10761084
}
10771085

10781086
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1169,7 +1177,6 @@ impl Nonterminal {
11691177
NtTy(ty) => ty.span,
11701178
NtMeta(attr_item) => attr_item.span(),
11711179
NtPath(path) => path.span,
1172-
NtVis(vis) => vis.span,
11731180
}
11741181
}
11751182

@@ -1184,7 +1191,6 @@ impl Nonterminal {
11841191
NtTy(..) => "type",
11851192
NtMeta(..) => "attribute",
11861193
NtPath(..) => "path",
1187-
NtVis(..) => "visibility",
11881194
}
11891195
}
11901196
}
@@ -1211,7 +1217,6 @@ impl fmt::Debug for Nonterminal {
12111217
NtLiteral(..) => f.pad("NtLiteral(..)"),
12121218
NtMeta(..) => f.pad("NtMeta(..)"),
12131219
NtPath(..) => f.pad("NtPath(..)"),
1214-
NtVis(..) => f.pad("NtVis(..)"),
12151220
}
12161221
}
12171222
}

compiler/rustc_ast/src/tokenstream.rs

-1
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

+34-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use std::sync::Arc;
33

44
use rustc_ast::ExprKind;
55
use rustc_ast::mut_visit::{self, MutVisitor};
6-
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, LitKind, Nonterminal, Token, TokenKind};
6+
use rustc_ast::token::{
7+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
8+
TokenKind,
9+
};
710
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
811
use rustc_data_structures::fx::FxHashMap;
912
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
@@ -274,6 +277,33 @@ pub(super) fn transcribe<'a>(
274277
// some of the unnecessary whitespace.
275278
let ident = MacroRulesNormalizedIdent::new(original_ident);
276279
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
280+
// We wrap the tokens in invisible delimiters, unless they are already wrapped
281+
// in invisible delimiters with the same `MetaVarKind`. Because some proc
282+
// macros can't multiple layers of invisible delimiters of the same
283+
// `MetaVarKind`. This loses some span info, though it hopefully won't matter.
284+
let mut mk_delimited = |mv_kind, mut stream: TokenStream| {
285+
if stream.len() == 1 {
286+
let tree = stream.iter().next().unwrap();
287+
if let TokenTree::Delimited(_, _, delim, inner) = tree
288+
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(mvk)) = delim
289+
&& mv_kind == *mvk
290+
{
291+
stream = inner.clone();
292+
}
293+
}
294+
295+
// Emit as a token stream within `Delimiter::Invisible` to maintain
296+
// parsing priorities.
297+
marker.visit_span(&mut sp);
298+
// Both the open delim and close delim get the same span, which covers the
299+
// `$foo` in the decl macro RHS.
300+
TokenTree::Delimited(
301+
DelimSpan::from_single(sp),
302+
DelimSpacing::new(Spacing::Alone, Spacing::Alone),
303+
Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)),
304+
stream,
305+
)
306+
};
277307
let tt = match cur_matched {
278308
MatchedSingle(ParseNtResult::Tt(tt)) => {
279309
// `tt`s are emitted into the output stream directly as "raw tokens",
@@ -292,6 +322,9 @@ pub(super) fn transcribe<'a>(
292322
let kind = token::NtLifetime(*ident, *is_raw);
293323
TokenTree::token_alone(kind, sp)
294324
}
325+
MatchedSingle(ParseNtResult::Vis(vis)) => {
326+
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
327+
}
295328
MatchedSingle(ParseNtResult::Nt(nt)) => {
296329
// Other variables are emitted into the output stream as groups with
297330
// `Delimiter::Invisible` to maintain parsing priorities.

compiler/rustc_parse/src/parser/mod.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,43 @@ impl<'a> Parser<'a> {
721721
if !self.eat_keyword(exp) { 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(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(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())
@@ -1455,7 +1492,11 @@ impl<'a> Parser<'a> {
14551492
/// so emit a proper diagnostic.
14561493
// Public for rustfmt usage.
14571494
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1458-
maybe_whole!(self, NtVis, |vis| vis.into_inner());
1495+
if let Some(vis) = self
1496+
.eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
1497+
{
1498+
return Ok(vis);
1499+
}
14591500

14601501
if !self.eat_keyword(exp!(Pub)) {
14611502
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
@@ -1683,7 +1724,8 @@ pub enum ParseNtResult {
16831724
Tt(TokenTree),
16841725
Ident(Ident, IdentIsRaw),
16851726
Lifetime(Ident, IdentIsRaw),
1727+
Vis(P<ast::Visibility>),
16861728

1687-
/// This case will eventually be removed, along with `Token::Interpolate`.
1729+
/// This variant will eventually be removed, along with `Token::Interpolate`.
16881730
Nt(Arc<Nonterminal>),
16891731
}

compiler/rustc_parse/src/parser/nonterminal.rs

+6-7
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
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ check-pass
2+
//
3+
// A test case where a `block` fragment specifier is interpreted as an `expr`
4+
// fragment specifier. It's an interesting case for the handling of invisible
5+
// delimiters.
6+
7+
macro_rules! m_expr {
8+
($e:expr) => { const _CURRENT: u32 = $e; };
9+
}
10+
11+
macro_rules! m_block {
12+
($b:block) => ( m_expr!($b); );
13+
}
14+
15+
fn main() {
16+
m_block!({ 1 });
17+
}

0 commit comments

Comments
 (0)