Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4aa97b

Browse files
committedMar 5, 2025·
More denesting of invisibly-delimited groups.
This time when converting them to proc-macro `Group` form.
1 parent a604115 commit d4aa97b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed
 

‎compiler/rustc_expand/src/proc_macro_server.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
115115

116116
while let Some(tree) = iter.next() {
117117
let (Token { kind, span }, joint) = match tree.clone() {
118-
tokenstream::TokenTree::Delimited(span, _, delim, stream) => {
118+
tokenstream::TokenTree::Delimited(span, _, mut delim, mut stream) => {
119119
// We used to have an alternative behaviour for crates that
120120
// needed it: a hack used to pass AST fragments to
121121
// attribute and derive macros as a single nonterminal
@@ -131,6 +131,24 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
131131
rustc.psess(),
132132
);
133133
}
134+
135+
// In `mk_delimited` we avoid nesting invisible delimited
136+
// of the same `MetaVarKind`. Here we do the same but
137+
// ignore the `MetaVarKind` because it is discarded when we
138+
// convert it to a `Group`.
139+
while let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim {
140+
if stream.len() == 1
141+
&& let tree = stream.iter().next().unwrap()
142+
&& let tokenstream::TokenTree::Delimited(_, _, delim2, stream2) = tree
143+
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim2
144+
{
145+
delim = *delim2;
146+
stream = stream2.clone();
147+
} else {
148+
break;
149+
}
150+
}
151+
134152
trees.push(TokenTree::Group(Group {
135153
delimiter: pm::Delimiter::from_internal(delim),
136154
stream: Some(stream),

‎tests/ui/proc-macro/nodelim-groups.rs

+13
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,17 @@ macro_rules! expand_it {
1919
fn main() {
2020
expand_it!(1 + (25) + 1);
2121
expand_it!(("hello".len()) ("world".len()));
22+
f();
23+
}
24+
25+
// The key thing here is to produce a single `None`-delimited `Group`, even
26+
// though there is multiple levels of macros.
27+
macro_rules! m5 { ($e:expr) => { print_bang_consume!($e) }; }
28+
macro_rules! m4 { ($e:expr) => { m5!($e); } }
29+
macro_rules! m3 { ($e:expr) => { m4!($e); } }
30+
macro_rules! m2 { ($e:expr) => { m3!($e); } }
31+
macro_rules! m1 { ($e:expr) => { m2!($e); } }
32+
33+
fn f() {
34+
m1!(123);
2235
}

‎tests/ui/proc-macro/nodelim-groups.stdout

+15
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,18 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
165165
span: $DIR/nodelim-groups.rs:16:52: 16:59 (#8),
166166
},
167167
]
168+
PRINT-BANG INPUT (DISPLAY): 123
169+
PRINT-BANG INPUT (DEBUG): TokenStream [
170+
Group {
171+
delimiter: None,
172+
stream: TokenStream [
173+
Literal {
174+
kind: Integer,
175+
symbol: "123",
176+
suffix: None,
177+
span: $DIR/nodelim-groups.rs:34:9: 34:12 (#0),
178+
},
179+
],
180+
span: $DIR/nodelim-groups.rs:27:54: 27:56 (#16),
181+
},
182+
]

0 commit comments

Comments
 (0)
Please sign in to comment.