golang/go

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

Open

#76,354 创建于 2025年11月19日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)Go (133,883 star) (19,008 fork)batch import
BugReportNeedsInvestigationPerformancecompiler/runtimehelp wanted

描述

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.

贡献者指南