Skip to content

Commit f703dc5

Browse files
randall77gopherbot
authored andcommitted
cmd/compile: add missing StringLen rule in prove
(StringLen (StringMake _ x)) == x, just like the rules we currently have for slices. This helps propagate string length knowledge to places which need it. Change-Id: Ifdcf6d1f2d430c1c4bbac32e0ea74c188eae998e Reviewed-on: https://go-review.googlesource.com/c/go/+/682777 Reviewed-by: Daniel Morsing <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Jorropo <[email protected]>
1 parent 394d0be commit f703dc5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/cmd/compile/internal/ssa/prove.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,10 @@ func addLocalFacts(ft *factsTable, b *Block) {
22672267
// the mod instruction executes (and thus panics if the
22682268
// modulus is 0). See issue 67625.
22692269
ft.update(b, v, v.Args[1], unsigned, lt)
2270+
case OpStringLen:
2271+
if v.Args[0].Op == OpStringMake {
2272+
ft.update(b, v, v.Args[0].Args[1], signed, eq)
2273+
}
22702274
case OpSliceLen:
22712275
if v.Args[0].Op == OpSliceMake {
22722276
ft.update(b, v, v.Args[0].Args[1], signed, eq)

test/prove.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,23 @@ func transitiveProofsThroughOverflowingUnsignedSub(x, y, z uint64) {
23022302
}
23032303
}
23042304

2305+
func resliceString(s string) byte {
2306+
if len(s) >= 4 {
2307+
s = s[2:] // ERROR "Proved IsSliceInBounds" "Proved slicemask not needed"
2308+
s = s[1:] // ERROR "Proved IsSliceInBounds" "Proved slicemask not needed"
2309+
return s[0] // ERROR "Proved IsInBounds"
2310+
}
2311+
return 0
2312+
}
2313+
func resliceBytes(b []byte) byte {
2314+
if len(b) >= 4 {
2315+
b = b[2:] // ERROR "Proved IsSliceInBounds" "Proved slicemask not needed"
2316+
b = b[1:] // ERROR "Proved IsSliceInBounds" "Proved slicemask not needed"
2317+
return b[0] // ERROR "Proved IsInBounds"
2318+
}
2319+
return 0
2320+
}
2321+
23052322
//go:noinline
23062323
func useInt(a int) {
23072324
}

0 commit comments

Comments
 (0)