Skip to content

Commit ff3ae45

Browse files
committed
test: add regress test cases for self-assignment
Cherry pointed out this case in review for CL 136496. That CL was slightly too aggressive, and I likely would have made the same mistake if I tried it myself. Updates golang#27772. Change-Id: I1fafabb9f8d9aba0494aa71333a4e17cf1bac5c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/172421 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 52d9ce8 commit ff3ae45

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/escape_selfassign.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// errorcheck -0 -m -l
2+
3+
// Copyright 2019 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+
// Test escape analysis for self assignments.
8+
9+
package escape
10+
11+
type S struct {
12+
i int
13+
pi *int
14+
}
15+
16+
var sink S
17+
18+
func f(p *S) { // ERROR "leaking param: p"
19+
p.pi = &p.i
20+
sink = *p
21+
}
22+
23+
// BAD: "leaking param: p" is too conservative
24+
func g(p *S) { // ERROR "leaking param: p"
25+
p.pi = &p.i
26+
}
27+
28+
func h() {
29+
var s S // ERROR "moved to heap: s"
30+
g(&s)
31+
sink = s
32+
}

0 commit comments

Comments
 (0)