Skip to content

#3061 slice 3: fix-ceremony turns an index loop that only reads its list into an element loop - #3074

Merged
ehartford merged 1 commit into
mainfrom
fix-ceremony-3
Nov 7, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-ceremony-3

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

#3061, slice 3: index loops. for i in 0..xs.len(): whose every use of i is a read of xs[i] becomes for x in xs: with each xs[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 uses i as 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 --apply catches 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, &x at a &T); the direction (Eric, 2026-11-08) is that this tool becomes a user-facing with fmt.

…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.
@ehartford
ehartford added this pull request to stack #3076 November 7, 2026 20:17
@ehartford
ehartford marked this pull request as ready for review November 7, 2026 20:37
@ehartford
ehartford merged commit 14bb988 into main Nov 7, 2026
@ehartford
ehartford deleted the fix-ceremony-3 branch November 7, 2026 20:37
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.

1 participant