all: use range-over-int in counting loops - #503
Merged
hajimehoshi merged 1 commit intoAug 29, 2026
Merged
Conversation
Go 1.22 added range over an integer, which reads better than the three-clause form and evaluates the limit once rather than on every iteration. Convert the loops that translate directly, most of them walking reflect fields or function arguments. The calls used as limits (NumIn, NumField, Len, NumCPU, numABIFields) are all pure, so evaluating them once does not change behavior. Loops that a range cannot express are left as they are: the 8-byte strides in copyStruct8ByteChunks, placeStack and addStruct, the field scan in getStruct on amd64 whose index is read after the loop, the compound condition in setStruct, and the one-based loops in objc that skip the self and _cmd arguments. wincallback.go generates the zcallback_*.s files; regenerating with the converted loops reproduces all eight of them byte for byte. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What issue is this addressing?
None; this is a standalone cleanup.
What type of issue is this addressing?
refactor (no behavior change)
What this PR does | solves
Go 1.22 added range over an integer. This converts the 37 three-clause
counting loops that translate directly, most of them walking reflect fields
or function arguments, and drops the index where the body never used it.
Found with
go fix -rangeint ./...run across the GOOS/GOARCH matrix so theper-architecture files were covered, then extended by hand to the cases that
analyzer skips conservatively:
NumIn,NumField,Len,NumCPUand
numABIFields. All are pure, so evaluating them once instead of onevery iteration does not change behavior.
func.goalready used this form.numFieldsin theplaceclosures ofstruct_amd64.goandstruct_arm64.go, assigned in two branches before the loop and neverinside it.
wincallback.go, which carries//go:build ignoreand so is neveranalyzed. It generates the
zcallback_*.sfiles; regenerating with theconverted loops reproduces all eight byte for byte.
Loops a range cannot express are left as they are: the 8-byte strides in
copyStruct8ByteChunks,placeStackandaddStruct; the field scan inamd64
getStruct, whose index is read after the loop; the compoundcondition in
setStruct; and the one-based loops inobjcthat skip thefirst argument, emitted separately as
encId.gofmt -s -lis clean,go vetis clean on 10 platforms andgo buildonall 15 that build today, and
go test ./...passes on darwin/arm64.Authored by Claude (Claude Code), on behalf of @hajimehoshi.