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

Commit f484b6f

Browse files
committed
Introduce InvisibleOrigin on invisible delimiters.
It's not used meaningfully yet, but will be needed to get rid of interpolated tokens.
1 parent d98c6ea commit f484b6f

File tree

9 files changed

+58
-29
lines changed

9 files changed

+58
-29
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl MetaItemKind {
404404
tokens: &mut impl Iterator<Item = &'a TokenTree>,
405405
) -> Option<MetaItemKind> {
406406
match tokens.next() {
407-
Some(TokenTree::Delimited(.., Delimiter::Invisible, inner_tokens)) => {
407+
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
408408
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
409409
}
410410
Some(TokenTree::Token(token, _)) => {
@@ -542,7 +542,7 @@ impl NestedMetaItem {
542542
tokens.next();
543543
return Some(NestedMetaItem::Lit(lit));
544544
}
545-
Some(TokenTree::Delimited(.., Delimiter::Invisible, inner_tokens)) => {
545+
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
546546
tokens.next();
547547
return NestedMetaItem::from_tokens(&mut inner_tokens.trees().peekable());
548548
}

compiler/rustc_ast/src/token.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ pub enum BinOpToken {
4141
Shr,
4242
}
4343

44+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
45+
pub enum InvisibleOrigin {
46+
// From the expansion of a metavariable in a declarative macro.
47+
MetaVar(NonterminalKind),
48+
49+
// Converted from `proc_macro::Delimiter` in
50+
// `proc_macro::Delimiter::to_internal`, i.e. returned by a proc macro.
51+
ProcMacro,
52+
53+
// Converted from `TokenKind::Interpolated` in
54+
// `TokenStream::flatten_token`. Treated similarly to `ProcMacro`.
55+
FlattenToken,
56+
}
57+
4458
/// Describes how a sequence of token trees is delimited.
4559
/// Cannot use `proc_macro::Delimiter` directly because this
4660
/// structure should implement some additional traits.
@@ -58,7 +72,22 @@ pub enum Delimiter {
5872
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
5973
/// `$var * 3` where `$var` is `1 + 2`.
6074
/// Invisible delimiters might not survive roundtrip of a token stream through a string.
61-
Invisible,
75+
Invisible(InvisibleOrigin),
76+
}
77+
78+
impl Delimiter {
79+
// Should the parser skip these delimiters? Only happens for certain kinds
80+
// of invisible delimiters. Ideally this function will eventually disappear
81+
// and no invisible delimiters will be skipped.
82+
#[inline]
83+
pub fn skip(&self) -> bool {
84+
use InvisibleOrigin::*;
85+
match self {
86+
Delimiter::Parenthesis | Delimiter::Bracket | Delimiter::Brace => false,
87+
Delimiter::Invisible(MetaVar(_)) => false,
88+
Delimiter::Invisible(FlattenToken | ProcMacro) => true,
89+
}
90+
}
6291
}
6392

6493
// Note that the suffix is *not* considered when deciding the `LitKind` in this
@@ -873,7 +902,7 @@ impl PartialEq<TokenKind> for Token {
873902
}
874903
}
875904

876-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable)]
905+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
877906
pub enum NtPatKind {
878907
// Matches or-patterns. Was written using `pat` in edition 2021 or later.
879908
PatWithOr,
@@ -883,7 +912,7 @@ pub enum NtPatKind {
883912
PatParam { inferred: bool },
884913
}
885914

886-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable)]
915+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
887916
pub enum NtExprKind {
888917
// Matches expressions using the post-edition 2024. Was written using
889918
// `expr` in edition 2024 or later.
@@ -910,7 +939,7 @@ pub enum Nonterminal {
910939
NtVis(P<ast::Visibility>),
911940
}
912941

913-
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable)]
942+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
914943
pub enum NonterminalKind {
915944
Item,
916945
Block,

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use crate::ast::{AttrStyle, StmtKind};
1717
use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
18-
use crate::token::{self, Delimiter, Nonterminal, Token, TokenKind};
18+
use crate::token::{self, Delimiter, InvisibleOrigin, Nonterminal, Token, TokenKind};
1919
use crate::AttrVec;
2020

2121
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -490,13 +490,13 @@ impl TokenStream {
490490
token::NtLifetime(ident) => TokenTree::Delimited(
491491
DelimSpan::from_single(token.span),
492492
DelimSpacing::new(Spacing::JointHidden, spacing),
493-
Delimiter::Invisible,
493+
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
494494
TokenStream::token_alone(token::Lifetime(ident.name), ident.span),
495495
),
496496
token::Interpolated(ref nt) => TokenTree::Delimited(
497497
DelimSpan::from_single(token.span),
498498
DelimSpacing::new(Spacing::JointHidden, spacing),
499-
Delimiter::Invisible,
499+
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
500500
TokenStream::from_nonterminal_ast(&nt).flattened(),
501501
),
502502
_ => TokenTree::Token(token.clone(), spacing),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
928928
token::CloseDelim(Delimiter::Bracket) => "]".into(),
929929
token::OpenDelim(Delimiter::Brace) => "{".into(),
930930
token::CloseDelim(Delimiter::Brace) => "}".into(),
931-
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible) => {
932-
"".into()
933-
}
931+
token::OpenDelim(Delimiter::Invisible(_))
932+
| token::CloseDelim(Delimiter::Invisible(_)) => "".into(),
934933
token::Pound => "#".into(),
935934
token::Dollar => "$".into(),
936935
token::Question => "?".into(),

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
707707
&& let mbe::TokenTree::Token(bang) = bang
708708
&& let TokenKind::Not = bang.kind
709709
&& let mbe::TokenTree::Delimited(.., del) = args
710-
&& del.delim != Delimiter::Invisible
710+
&& !del.delim.skip()
711711
{
712712
true
713713
} else {

compiler/rustc_expand/src/mbe/quoted.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ fn parse_tree<'a>(
189189
// during parsing.
190190
let mut next = outer_trees.next();
191191
let mut trees: Box<dyn Iterator<Item = &tokenstream::TokenTree>>;
192-
if let Some(tokenstream::TokenTree::Delimited(.., Delimiter::Invisible, tts)) = next {
193-
trees = Box::new(tts.trees());
194-
next = trees.next();
195-
} else {
196-
trees = Box::new(outer_trees);
192+
match next {
193+
Some(tokenstream::TokenTree::Delimited(.., delim, tts)) if delim.skip() => {
194+
trees = Box::new(tts.trees());
195+
next = trees.next();
196+
}
197+
_ => trees = Box::new(outer_trees),
197198
}
198199

199200
match next {

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl FromInternal<token::Delimiter> for Delimiter {
3636
token::Delimiter::Parenthesis => Delimiter::Parenthesis,
3737
token::Delimiter::Brace => Delimiter::Brace,
3838
token::Delimiter::Bracket => Delimiter::Bracket,
39-
token::Delimiter::Invisible => Delimiter::None,
39+
token::Delimiter::Invisible(_) => Delimiter::None,
4040
}
4141
}
4242
}
@@ -47,7 +47,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
4747
Delimiter::Parenthesis => token::Delimiter::Parenthesis,
4848
Delimiter::Brace => token::Delimiter::Brace,
4949
Delimiter::Bracket => token::Delimiter::Bracket,
50-
Delimiter::None => token::Delimiter::Invisible,
50+
Delimiter::None => token::Delimiter::Invisible(token::InvisibleOrigin::ProcMacro),
5151
}
5252
}
5353
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl TokenCursor {
285285
spacing,
286286
delim,
287287
));
288-
if delim != Delimiter::Invisible {
288+
if !delim.skip() {
289289
return (Token::new(token::OpenDelim(delim), sp.open), spacing.open);
290290
}
291291
// No open delimiter to return; continue on to the next iteration.
@@ -294,7 +294,7 @@ impl TokenCursor {
294294
} else if let Some((tree_cursor, span, spacing, delim)) = self.stack.pop() {
295295
// We have exhausted this token stream. Move back to its parent token stream.
296296
self.tree_cursor = tree_cursor;
297-
if delim != Delimiter::Invisible {
297+
if !delim.skip() {
298298
return (Token::new(token::CloseDelim(delim), span.close), spacing.close);
299299
}
300300
// No close delimiter to return; continue on to the next iteration.
@@ -1105,7 +1105,7 @@ impl<'a> Parser<'a> {
11051105
}
11061106
debug_assert!(!matches!(
11071107
next.0.kind,
1108-
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
1108+
token::OpenDelim(delim) | token::CloseDelim(delim) if delim.skip()
11091109
));
11101110
self.inlined_bump_with(next)
11111111
}
@@ -1119,17 +1119,17 @@ impl<'a> Parser<'a> {
11191119
}
11201120

11211121
if let Some(&(_, span, _, delim)) = self.token_cursor.stack.last()
1122-
&& delim != Delimiter::Invisible
1122+
&& !delim.skip()
11231123
{
11241124
// We are not in the outermost token stream, and the token stream
11251125
// we are in has non-skipped delimiters. Look for skipped
11261126
// delimiters in the lookahead range.
11271127
let tree_cursor = &self.token_cursor.tree_cursor;
1128-
let all_normal = (0..dist).all(|i| {
1128+
let any_skip = (0..dist).any(|i| {
11291129
let token = tree_cursor.look_ahead(i);
1130-
!matches!(token, Some(TokenTree::Delimited(.., Delimiter::Invisible, _)))
1130+
matches!(token, Some(TokenTree::Delimited(.., delim, _)) if delim.skip())
11311131
});
1132-
if all_normal {
1132+
if !any_skip {
11331133
// There were no skipped delimiters. Do lookahead by plain indexing.
11341134
return match tree_cursor.look_ahead(dist - 1) {
11351135
Some(tree) => {
@@ -1160,7 +1160,7 @@ impl<'a> Parser<'a> {
11601160
token = cursor.next().0;
11611161
if matches!(
11621162
token.kind,
1163-
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
1163+
token::OpenDelim(delim) | token::CloseDelim(delim) if delim.skip()
11641164
) {
11651165
continue;
11661166
}

src/tools/rustfmt/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ fn delim_token_to_str(
588588
("{ ", " }")
589589
}
590590
}
591-
Delimiter::Invisible => unreachable!(),
591+
Delimiter::Invisible(_) => unreachable!(),
592592
};
593593
if use_multiple_lines {
594594
let indent_str = shape.indent.to_string_with_newline(context.config);

0 commit comments

Comments
 (0)