fix: prevent panic on inner attributes in a loop body inside a closure#6978
Open
shulaoda wants to merge 1 commit into
Conversation
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.
Summary
Closes #6209.
A block has nowhere to store attributes, so an inner attribute written in a loop body is attached to the loop expression with
AttrStyle::Inner.rewrite_closure_with_blockwraps the closure body in an artificial block and forwards the body's attributes to it. That assumes the|| #[attr] foo()shape, where the attributes precede the body, which is what the artificial block's span is widened to cover. When an inner attribute is forwarded instead,visit_blockemits it and pusheslast_pospast it. The loop statement that follows starts before that point, sopush_rewriteasks to format a backwards span.Filtering the body's attributes to the outer ones is a no-op for the shape the code was written for, since
rewrite_block_with_visitoralready narrows what it receives to inner attributes.Review notes
Inner attributes are still dropped from the output, so the expected output in the new tests does not contain them. That is a separate pre-existing bug (#5973) which affects loops with or without a closure and is not touched here.
#6010 fixes that one by plumbing the loop's inner attributes through
ControlFlow. I applied it on top of this branch to check the two compose: there is no panic and the attributes come back, so the only change is that the expected output added here will need updating when #6010 lands.