Skip to content

Remove NtItem and NtStmt #138083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
More denesting of invisibly-delimited groups.
This time when converting them to proc-macro `Group` form.
  • Loading branch information
nnethercote committed Mar 7, 2025
commit 3378ee1a4f2ddfc4b740b70d286fc32d81aca005
20 changes: 19 additions & 1 deletion compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre

while let Some(tree) = iter.next() {
let (Token { kind, span }, joint) = match tree.clone() {
tokenstream::TokenTree::Delimited(span, _, delim, stream) => {
tokenstream::TokenTree::Delimited(span, _, mut delim, mut stream) => {
// We used to have an alternative behaviour for crates that
// needed it: a hack used to pass AST fragments to
// attribute and derive macros as a single nonterminal
Expand All @@ -131,6 +131,24 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
rustc.psess(),
);
}

// In `mk_delimited` we avoid nesting invisible delimited
// of the same `MetaVarKind`. Here we do the same but
// ignore the `MetaVarKind` because it is discarded when we
// convert it to a `Group`.
while let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim {
if stream.len() == 1
&& let tree = stream.iter().next().unwrap()
&& let tokenstream::TokenTree::Delimited(_, _, delim2, stream2) = tree
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim2
{
delim = *delim2;
stream = stream2.clone();
} else {
break;
}
}

trees.push(TokenTree::Group(Group {
delimiter: pm::Delimiter::from_internal(delim),
stream: Some(stream),
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/proc-macro/nodelim-groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ macro_rules! expand_it {
fn main() {
expand_it!(1 + (25) + 1);
expand_it!(("hello".len()) ("world".len()));
f();
}

// The key thing here is to produce a single `None`-delimited `Group`, even
// though there is multiple levels of macros.
macro_rules! m5 { ($e:expr) => { print_bang_consume!($e) }; }
macro_rules! m4 { ($e:expr) => { m5!($e); } }
macro_rules! m3 { ($e:expr) => { m4!($e); } }
macro_rules! m2 { ($e:expr) => { m3!($e); } }
macro_rules! m1 { ($e:expr) => { m2!($e); } }

fn f() {
m1!(123);
}
15 changes: 15 additions & 0 deletions tests/ui/proc-macro/nodelim-groups.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,18 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
span: $DIR/nodelim-groups.rs:16:52: 16:59 (#8),
},
]
PRINT-BANG INPUT (DISPLAY): 123
PRINT-BANG INPUT (DEBUG): TokenStream [
Group {
delimiter: None,
stream: TokenStream [
Literal {
kind: Integer,
symbol: "123",
suffix: None,
span: $DIR/nodelim-groups.rs:34:9: 34:12 (#0),
},
],
span: $DIR/nodelim-groups.rs:27:54: 27:56 (#16),
},
]
Loading