Repository navigation
#3061 slice 3: fix-ceremony turns an index loop that only reads its list into an element loop - #3074
Merged
Merged
Conversation
…ist into an element loop `for i in 0..xs.len():` whose every use of `i` is `xs[i]` read becomes `for x in xs:` with each `xs[i]` the element. Sema counts the counter's uses while the loop body is checked (every use; the ones that index the loop's own list; a write through one), and the loop rewrites only when the counts agree and nothing was written. The element's name is the list's singular (`parts` → `part`), else `<name>_item`; the re-check after --apply catches a collision. test/internals/ceremony_fix_loops_test.w pins the rewrite and the three shapes that stay.
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.
#3061, slice 3: index loops.
for i in 0..xs.len():whose every use ofiis a read ofxs[i]becomesfor x in xs:with eachxs[i]the element.The fact is Sema's: while a loop over
0..xs.len()(a plain list name, exclusive range) checks its body, every use of the counter is counted, the uses that index that list are counted, and a write through the counter (xs[i] = …) marks the loop; the rewrite happens only when the two counts agree and nothing was written. A loop that usesias a number, indexes a second list, or writes through it stays. The element's name is the list's singular (parts→part,xs→x), else<name>_item; the re-check after--applycatches a collision.test/internals/ceremony_fix_loops_test.w: one rewrite out of four loops, the text as expected, the rewritten program checks. Slice 4 is the typed census patterns (.clone()on a str,&xat a&T); the direction (Eric, 2026-11-08) is that this tool becomes a user-facingwith fmt.