Go version
go version go1.27-devel_c173b531 Wed Mar 25
Output of go env in your module/workspace:
What did you do?
While working on CL 759100, I noticed a bounds check that should be eliminated but isn't. Below is a simple reproducer:
package p
func f(s []byte) {
switch len(s) {
case 2:
_ = s[1]
fallthrough
case 1:
_ = s[0]
}
}
What did you see happen?
Running go build -gcflags='-d=ssa/check_bce/debug=1 reveals one bounds checks (for 0) remains:
package p
func f(s []byte) {
switch len(s) {
case 2:
_ = s[1]
fallthrough
case 1:
_ = s[0] // Found IsInBounds
}
}
(Godbolt link)
What did you expect to see?
I expected no bounds checks, since the indexing expression s[0] is evaluated only when len(s) is equal to either 2 or 1.
Apparently, the fallthrough statement is to blame. If I remove it, all bounds checks are gone (Godbolt link).
Go version
go version go1.27-devel_c173b531 Wed Mar 25
Output of
go envin your module/workspace:What did you do?
While working on CL 759100, I noticed a bounds check that should be eliminated but isn't. Below is a simple reproducer:
What did you see happen?
Running
go build -gcflags='-d=ssa/check_bce/debug=1reveals one bounds checks (for 0) remains:(Godbolt link)
What did you expect to see?
I expected no bounds checks, since the indexing expression
s[0]is evaluated only whenlen(s)is equal to either 2 or 1.Apparently, the
fallthroughstatement is to blame. If I remove it, all bounds checks are gone (Godbolt link).