golang/go

cmd/compile: extra bounds check when accessing string using index from range over its runes

Open

#76,354 opened on Nov 19, 2025

 (5 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (PR metrics pending)

Description

Go version

go version go1.26-devel_e1a12c7 Mon Nov 17 13:45:54 2025 -0800 linux/amd64

Output of go env in your module/workspace:

Workspace is `go.godbolt.org` on `x86-64 gc (tip)` as above.

What did you do?

Compile this simple example from the standard library:

func hasUpperCase(s string) bool {
	for i := range s {
		if 'A' <= s[i] && s[i] <= 'Z' {
			return true
		}
	}
	return false
}

(As an aside: is there a reason why the above doesn't iterate over the bytes of s instead?)

What did you see happen?

Extra bounds check on s[i], even though per-spec i should still always be 0 <= i < len(s) even on invalid UTF-8.

What did you expect to see?

No extra bounds checks.

This also appears elsewhere in the standard library, such as strings.ToValidUTF8, fmt.truncateString, and time.tzsetNum.

Contributor guide