- Notifications
You must be signed in to change notification settings - Fork18.6k
Description
Go version
go version go1.26-devel_337f7b1 Sat Nov 22 06:13:24 2025 -0800 linux/amd64
Output ofgo env in your module/workspace:
Workspace is`go.godbolt.org` on`x86-64 gc (tip)` as above.
What did you do?
[Code+output testedhere]
Compile these simplified examples:
functestReslice0(s []byte,kint)byte {ifk<0||k>=len(s) {return0 }s=s[k:]// Proved IsSliceInBoundsreturns[0]// bounds check!}functestReslice1(s []byte,kint)byte {ifk<0||k>len(s)-1 {return0 }s=s[k:]// Proved IsSliceInBounds + slicemask not needed (by limit)returns[0]// Proved IsInBounds}
What did you see happen?
testReslice0 has an extra bounds check (and slice mask) thattestReslice1 does not have.
What did you expect to see?
testReslice0 should not contain an extra bounds check, as0 <= k < len(s), meanings after slicing offk elements must still contain an element left fors[0] to succeed.
testReslice1 contains the following debug prove output thattestReslice0 does not have:
<source>:4:30: x+d >= w; x:v13 b2 delta:-1 w:-1 d:signed<source>:4:30: x+d >= w; x:v13 b2 delta:-1 w:0 d:signed<source>:7:10: Proved slicemask not needed (by limit)It appears that when there is no subtraction involved, the limits are not detected and propagated to the slice mask operation and later. This may be due to optimizations added by the recently introduceddetectSliceLenRelation (@dr2chase) and/ordetectSubRelations (@randall77) that don't have an equivalent without the subtraction fromlen(s).
Solving this would, for example, eliminate an extra bounds check in the runtime'sdecoderune.