Skip to content

Fix asm sym operand parsing for parenthesized expr fragments#21588

Merged
ChayimFriedman2 merged 1 commit intorust-lang:masterfrom
railgun-0402:fix/21582-asm-sym-accept-expr
Mar 23, 2026
Merged

Fix asm sym operand parsing for parenthesized expr fragments#21588
ChayimFriedman2 merged 1 commit intorust-lang:masterfrom
railgun-0402:fix/21582-asm-sym-accept-expr

Conversation

@railgun-0402
Copy link
Copy Markdown
Contributor

@railgun-0402 railgun-0402 commented Feb 4, 2026

Fixes #21582

Summary

When capturing a path via $e:expr, macro expansion treats it as Fragment::Expr and wraps it in parentheses to preserve operator precedence.
This results in sym (generic::<i32>), but the existing parser expected a path immediately after sym and failed when seeing (, leading to a parse failure and a bogus typed-hole diagnostic.

Changes

  • Parse the operand after sym as an expression
  • During lowering, extract a path from the expression (so parenthesized forms are handled correctly)

Why this approach

This keeps parsing more permissive while keeping the semantic requirement (“sym expects a path”) enforced during lowering.

Testing

AI Usage

AI disclosure:

I used ChatGPT to help draft the PR description and clarify the root-cause explanation.
The implementation and tests were written and validated by me.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 4, 2026
@ChayimFriedman2
Copy link
Copy Markdown
Contributor

Please note that we require disclosure of AI usage, if you used them.

@oxalica
Copy link
Copy Markdown
Contributor

oxalica commented Feb 4, 2026

I'm not sure if it is good to overly relax the syntax to accept any sym <expr>, which is not accepted by rustc currently. Note that rustc will reject it if you write sym (func) directly (see also syntax reference).

@railgun-0402
Copy link
Copy Markdown
Contributor Author

Good point — I agree we shouldn’t accept a broader sym syntax than rustc.

The problematic case here is $e:expr in macro expansion, where the operand can be wrapped in invisible delimiters for precedence, even though users didn’t write parentheses.

I’ll adjust the change to keep the syntax strict (path-only), and only handle the invisible-delimiter wrapping so that sym (func) written directly is still rejected, matching rustc. I’ll also add a regression test for sym (func) to ensure we stay compatible.

@railgun-0402
Copy link
Copy Markdown
Contributor Author

Hi @oxalica — thanks for the earlier feedback!

I revised the approach to avoid relaxing sym syntax beyond rustc.
Instead of accepting sym <expr>, this PR keeps the operand path-only and fixes the macro-expansion behavior that introduced the problematic parentheses.

  • In the macro transcriber, we avoid wrapping path-like expr fragments (e.g. generic::<i32>) in parentheses, since that breaks asm! sym operands coming from $e:expr.
    (Conservative heuristic: requires :: and only allows path-like tokens.)
  • Added a regression test for the $e:expr + sym $e case (issue Invisible delimiters after asm sym operand causes parsing errors #21582) to ensure no bogus typed-hole diagnostic.

Could you please take another look at the transcriber heuristic and the test coverage?
Thanks!

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

No no no. If we're accepting that we should patch something until we fix macros properly (which I'm not sure is a good idea but maybe it is), it is definitely not a good idea to do that in macro_rules general handling. Change the parser to accept parentheses inside sym instead:

} else if p.eat_contextual_kw(T![sym]) {
dir_spec.abandon(p);
paths::type_path(p);
op.complete(p, ASM_SYM);
op_n.complete(p, ASM_OPERAND_NAMED);

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

@railgun-0402 railgun-0402 force-pushed the fix/21582-asm-sym-accept-expr branch from 7eef93d to 6322c04 Compare March 23, 2026 14:20
@railgun-0402
Copy link
Copy Markdown
Contributor Author

railgun-0402 commented Mar 23, 2026

Squashed!

squashed my commits

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Mar 23, 2026
Merged via the queue into rust-lang:master with commit f869e68 Mar 23, 2026
17 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invisible delimiters after asm sym operand causes parsing errors

4 participants