fix(semantic): drive a macro expansion block by the repetition at its own depth - #10282
Draft
Conversation
5 tasks
Collaborator
Author
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Stacked on #10281.
A
$()expansion block whose only placeholder is bound to a deeper repetition than the block's own level expanded wrongly:Nothing was bound at the outer block's depth, so it fell back to driving on
$xtoo and both loops walked the same group.macro_rules!accepts this shape and expands it correctly at any depth (verified under rustc 1.96), so the fix is to iterate, not to reject. What rustc does reject —($($a:expr),*) => { $($a + $($a +)*)* 0 }, "attempted to repeat an expression containing no syntax variables matched as repeating at this depth" — is exactly our E2202, and the existingmax_path_len_in_block >= curr_rep_depthcheck is a faithful port of that rule.Change
A block nested in
dblocks drives the pattern repetition at depthd, reached by climbingrep_parentsfrom a placeholder's own repetition (repetition_at_depth).depthis threaded throughexpand_macro_rule_ex.This makes the
(is_iterated, pattern_depth)ranking from #10270 dead — a block at depthddrives a repetition no enclosing block can be iterating — sofind_repetition_driverloses it, andrepetition_group's parent lookup tightens to anexpect(the no-parent arm stays: every depth-1 block takes it).register_repetition_placeholdersno longer lets a shallower repetition overwrite a deeper binding. It re-runs on every iteration of the enclosing repetition, and a nested repetition matching zero times that iteration is never entered, so it would not restore what was overwritten. Without this the depth rule regressestwo_deeper_only!([(1)], [])into expanding not at all.Tests
Seven goldens in
expansion_test_data/inline_macros, every expected value cross-checked against rustc. Four fail at the parent commit; the other three are guards on the newexpectand on zero-match inputs, which have masked three separate bugs in this code already.