Skip to content

fix: don't panic when a lifetime is passed to an ident metavariable#22922

Open
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_don_t_panic_when_a_macro_expansion_contains_a_lone_
Open

fix: don't panic when a lifetime is passed to an ident metavariable#22922
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_don_t_panic_when_a_macro_expansion_contains_a_lone_

Conversation

@shulaoda

@shulaoda shulaoda commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Closes #22801

Passing a lifetime to a macro that expects an ident, like m!('a) for ($t:ident), panics with Next token must be ident. rustc just reports no rules expected 'a. The matcher consumes the ' before the ident match fails, and the consumed token is still bound to the fragment, so the expansion ends up holding a ' with nothing after it.

to_parser_input assumed a ' is always followed by an ident. It is the point where every expansion, including proc macro output we don't control, becomes a syntax tree, so it now pushes an ERROR token instead of panicking.

The expect_* helpers consume a token before checking what it is, so a failed match still advances past it. match_meta_var then binds everything consumed since the savepoint, which for m!('a) is the bare ' that later trips the panic. Returning Fragment::Empty on failure makes the caller take the push_missing branch it already has.

The same bug made @--1.0 expand to a stray - - when matched against $lit:literal, hence the snapshot change in minus_belongs_to_literal.

🤖 AI-assisted: understanding the issue, locating the cause, and writing the test.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2026
Comment on lines +33 to +36
_ => {
res.push(ERROR, ctx_edition(punct.span.ctx));
continue;
}

@Veykril Veykril Jul 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the right spot to fix this. We are calling this function with an invalid state, so we need to fix this up in the caller somewhere. It seems like we are incorrectly recovering somewhere in the fragment parsing causing us to end up with a lone '

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out!

@shulaoda
shulaoda marked this pull request as draft July 26, 2026 14:40
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2026
@shulaoda
shulaoda force-pushed the 07-26-fix_don_t_panic_when_a_macro_expansion_contains_a_lone_ branch from 5eef988 to 9699be7 Compare July 26, 2026 15:05
@shulaoda
shulaoda marked this pull request as ready for review July 26, 2026 15:07
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2026
@shulaoda shulaoda changed the title fix: don't panic when a macro expansion contains a lone ' fix: don't panic when a lifetime is passed to an ident metavariable Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: Next token must be ident macro with lifetime

3 participants