Repository navigation
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.
Fixes mypyc/mypyc#1233
forloops over lists, tuples, strings, bytes, dicts andrange()(also insideenumerate(),zip()andreversed()) read the iterable, or the end of the range, on every iteration. When it was a local variable, the loop read the variable's register, so assigning to the variable in the loop body changed the iteration:for i in range(n): n = 0ran once,for x in a: a = [9]stopped after the first item, and a loop over a tuple read past the end of the new tuple, which could segfault.Now, if the loop target or body assigns to the local variable that holds the iterable or the end of the range, its value is copied to a new register before the loop. A small AST visitor finds these assignments: plain, augmented and starred assignments,
:=,del,fortargets,with ... as,except ... asandmatchcapture patterns. Other loops generate the same IR as before. Copying unconditionally would add a copy to every loop over a local variable and change the IR of 23 existing test cases.The comprehension form,
[x for x in t if (t := (9,))], is fixed separately by #22133.