Skip to content

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

@fice-t

Description

@fice-t

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.help wanted

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions