summaryrefslogtreecommitdiffstats
path: root/test/escape_iface.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
commit43a123c1ae6613b3efeed291fa552ecd909d3acf (patch)
treefd92518b7024bc74031f78a1cf9e454b65e73665 /test/escape_iface.go
parentInitial commit. (diff)
downloadgolang-1.20-upstream.tar.xz
golang-1.20-upstream.zip
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/escape_iface.go')
-rw-r--r--test/escape_iface.go255
1 files changed, 255 insertions, 0 deletions
diff --git a/test/escape_iface.go b/test/escape_iface.go
new file mode 100644
index 0000000..9862281
--- /dev/null
+++ b/test/escape_iface.go
@@ -0,0 +1,255 @@
+// errorcheck -0 -m -l
+
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test escape analysis for interface conversions.
+
+package escape
+
+var sink interface{}
+
+type M interface {
+ M()
+}
+
+func mescapes(m M) { // ERROR "leaking param: m"
+ sink = m
+}
+
+func mdoesnotescape(m M) { // ERROR "m does not escape"
+}
+
+// Tests for type stored directly in iface and with value receiver method.
+type M0 struct {
+ p *int
+}
+
+func (M0) M() {
+}
+
+func efaceEscape0() {
+ {
+ i := 0
+ v := M0{&i}
+ var x M = v
+ _ = x
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M0{&i}
+ var x M = v
+ sink = x
+ }
+ {
+ i := 0
+ v := M0{&i}
+ var x M = v
+ v1 := x.(M0)
+ _ = v1
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M0{&i}
+ // BAD: v does not escape to heap here
+ var x M = v
+ v1 := x.(M0)
+ sink = v1
+ }
+ {
+ i := 0
+ v := M0{&i}
+ var x M = v
+ x.M() // ERROR "devirtualizing x.M"
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M0{&i}
+ var x M = v
+ mescapes(x)
+ }
+ {
+ i := 0
+ v := M0{&i}
+ var x M = v
+ mdoesnotescape(x)
+ }
+}
+
+// Tests for type stored indirectly in iface and with value receiver method.
+type M1 struct {
+ p *int
+ x int
+}
+
+func (M1) M() {
+}
+
+func efaceEscape1() {
+ {
+ i := 0
+ v := M1{&i, 0}
+ var x M = v // ERROR "v does not escape"
+ _ = x
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M1{&i, 0}
+ var x M = v // ERROR "v escapes to heap"
+ sink = x
+ }
+ {
+ i := 0
+ v := M1{&i, 0}
+ var x M = v // ERROR "v does not escape"
+ v1 := x.(M1)
+ _ = v1
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M1{&i, 0}
+ var x M = v // ERROR "v does not escape"
+ v1 := x.(M1)
+ sink = v1 // ERROR "v1 escapes to heap"
+ }
+ {
+ i := 0
+ v := M1{&i, 0}
+ var x M = v // ERROR "v does not escape"
+ x.M() // ERROR "devirtualizing x.M"
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := M1{&i, 0}
+ var x M = v // ERROR "v escapes to heap"
+ mescapes(x)
+ }
+ {
+ i := 0
+ v := M1{&i, 0}
+ var x M = v // ERROR "v does not escape"
+ mdoesnotescape(x)
+ }
+}
+
+// Tests for type stored directly in iface and with pointer receiver method.
+type M2 struct {
+ p *int
+}
+
+func (*M2) M() {
+}
+
+func efaceEscape2() {
+ {
+ i := 0
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ var x M = v
+ _ = x
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := &M2{&i} // ERROR "&M2{...} escapes to heap"
+ var x M = v
+ sink = x
+ }
+ {
+ i := 0
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ var x M = v
+ v1 := x.(*M2)
+ _ = v1
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := &M2{&i} // ERROR "&M2{...} escapes to heap"
+ // BAD: v does not escape to heap here
+ var x M = v
+ v1 := x.(*M2)
+ sink = v1
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ // BAD: v does not escape to heap here
+ var x M = v
+ v1 := x.(*M2)
+ sink = *v1
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ // BAD: v does not escape to heap here
+ var x M = v
+ v1, ok := x.(*M2)
+ sink = *v1
+ _ = ok
+ }
+ {
+ i := 0
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ var x M = v
+ x.M() // ERROR "devirtualizing x.M"
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ v := &M2{&i} // ERROR "&M2{...} escapes to heap"
+ var x M = v
+ mescapes(x)
+ }
+ {
+ i := 0
+ v := &M2{&i} // ERROR "&M2{...} does not escape"
+ var x M = v
+ mdoesnotescape(x)
+ }
+}
+
+type T1 struct {
+ p *int
+}
+
+type T2 struct {
+ T1 T1
+}
+
+func dotTypeEscape() *T2 { // #11931
+ var x interface{}
+ x = &T1{p: new(int)} // ERROR "new\(int\) escapes to heap" "&T1{...} does not escape"
+ return &T2{ // ERROR "&T2{...} escapes to heap"
+ T1: *(x.(*T1)),
+ }
+}
+
+func dotTypeEscape2() { // #13805, #15796
+ {
+ i := 0
+ j := 0
+ var v int
+ var ok bool
+ var x interface{} = i // ERROR "i does not escape"
+ var y interface{} = j // ERROR "j does not escape"
+
+ *(&v) = x.(int)
+ *(&v), *(&ok) = y.(int)
+ }
+ {
+ i := 0 // ERROR "moved to heap: i"
+ j := 0 // ERROR "moved to heap: j"
+ var ok bool
+ var x interface{} = &i
+ var y interface{} = &j
+
+ sink = x.(*int)
+ sink, *(&ok) = y.(*int)
+ }
+}
+
+func issue42279() {
+ type I interface{ M() }
+ type T struct{ I }
+
+ var i I = T{} // ERROR "T\{\} does not escape"
+ i.M() // ERROR "partially devirtualizing i.M to T"
+}