Skip to content

Commit 34416d7

Browse files
mdempskygopherbot
authored andcommitted
cmd/compile: fix escape analysis of string min/max
When I was plumbing min/max support through the compiler, I was thinking mostly about numeric argument types. As a result, I forgot that escape analysis would need to be aware that min/max can operate on string values, which contain pointers. Fixes #64565. Change-Id: I36127ce5a2da942401910fa0f9de922726c9f94d Reviewed-on: https://go-review.googlesource.com/c/go/+/547715 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Mauri de Souza Meneguzzo <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ed30ee6 commit 34416d7

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/cmd/compile/internal/escape/call.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,17 @@ func (e *escape) call(ks []hole, call ir.Node) {
155155
e.discard(call.X)
156156
e.discard(call.Y)
157157

158-
case ir.ODELETE, ir.OMAX, ir.OMIN, ir.OPRINT, ir.OPRINTLN, ir.ORECOVERFP:
158+
case ir.ODELETE, ir.OPRINT, ir.OPRINTLN, ir.ORECOVERFP:
159159
call := call.(*ir.CallExpr)
160-
for i := range call.Args {
161-
e.discard(call.Args[i])
160+
for _, arg := range call.Args {
161+
e.discard(arg)
162+
}
163+
e.discard(call.RType)
164+
165+
case ir.OMIN, ir.OMAX:
166+
call := call.(*ir.CallExpr)
167+
for _, arg := range call.Args {
168+
argument(ks[0], arg)
162169
}
163170
e.discard(call.RType)
164171

test/escape_calls.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ func bar() {
5252
s := "string"
5353
f([]string{s}) // ERROR "\[\]string{...} escapes to heap"
5454
}
55+
56+
func strmin(a, b, c string) string { // ERROR "leaking param: a to result ~r0 level=0" "leaking param: b to result ~r0 level=0" "leaking param: c to result ~r0 level=0"
57+
return min(a, b, c)
58+
}
59+
func strmax(a, b, c string) string { // ERROR "leaking param: a to result ~r0 level=0" "leaking param: b to result ~r0 level=0" "leaking param: c to result ~r0 level=0"
60+
return max(a, b, c)
61+
}

test/fixedbugs/issue64565.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
m := "0"
11+
for _, c := range "321" {
12+
m = max(string(c), m)
13+
println(m)
14+
}
15+
}

test/fixedbugs/issue64565.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3
2+
3
3+
3

0 commit comments

Comments
 (0)