diff options
Diffstat (limited to 'test/escape2.go')
-rw-r--r-- | test/escape2.go | 155 |
1 files changed, 144 insertions, 11 deletions
diff --git a/test/escape2.go b/test/escape2.go index be89c2d84..28251aa98 100644 --- a/test/escape2.go +++ b/test/escape2.go @@ -80,7 +80,7 @@ func foo12(yyy **int) { // ERROR "leaking param: yyy" xxx = yyy } -// Must treat yyy as leaking because *yyy leaks, and the escape analysis +// Must treat yyy as leaking because *yyy leaks, and the escape analysis // summaries in exported metadata do not distinguish these two cases. func foo13(yyy **int) { // ERROR "leaking param: yyy" *xxx = *yyy @@ -135,7 +135,7 @@ func (b *Bar) Leak() *int { // ERROR "leaking param: b" return &b.i // ERROR "&b.i escapes to heap" } -func (b *Bar) AlsoNoLeak() *int { // ERROR "b does not escape" +func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param b content to result ~r0" return b.ii } @@ -149,7 +149,7 @@ func (b Bar) LeaksToo() *int { // ERROR "leaking param: b" return b.ii } -func (b *Bar) LeaksABit() *int { // ERROR "b does not escape" +func (b *Bar) LeaksABit() *int { // ERROR "leaking param b content to result ~r0" v := 0 // ERROR "moved to heap: v" b.ii = &v // ERROR "&v escapes" return b.ii @@ -182,7 +182,7 @@ func (b *Bar2) Leak() []int { // ERROR "leaking param: b" return b.i[:] // ERROR "b.i escapes to heap" } -func (b *Bar2) AlsoNoLeak() []int { // ERROR "b does not escape" +func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param b content to result ~r0" return b.ii[0:1] } @@ -1294,15 +1294,15 @@ func F4(x []byte) func G() { var buf1 [10]byte F1(buf1[:]) // ERROR "buf1 does not escape" - + var buf2 [10]byte // ERROR "moved to heap: buf2" - F2(buf2[:]) // ERROR "buf2 escapes to heap" + F2(buf2[:]) // ERROR "buf2 escapes to heap" var buf3 [10]byte F3(buf3[:]) // ERROR "buf3 does not escape" - + var buf4 [10]byte // ERROR "moved to heap: buf4" - F4(buf4[:]) // ERROR "buf4 escapes to heap" + F4(buf4[:]) // ERROR "buf4 escapes to heap" } type Tm struct { @@ -1314,9 +1314,9 @@ func (t *Tm) M() { // ERROR "t does not escape" func foo141() { var f func() - + t := new(Tm) // ERROR "escapes to heap" - f = t.M // ERROR "t.M does not escape" + f = t.M // ERROR "t.M does not escape" _ = f } @@ -1324,7 +1324,7 @@ var gf func() func foo142() { t := new(Tm) // ERROR "escapes to heap" - gf = t.M // ERROR "t.M escapes to heap" + gf = t.M // ERROR "t.M escapes to heap" } // issue 3888. @@ -1357,3 +1357,136 @@ func foo144() { //go:noescape func foo144b(*int) + +// issue 7313: for loop init should not be treated as "in loop" + +type List struct { + Next *List +} + +func foo145(l List) { // ERROR "l does not escape" + var p *List + for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape" + } +} + +func foo146(l List) { // ERROR "l does not escape" + var p *List + p = &l // ERROR "&l does not escape" + for ; p.Next != nil; p = p.Next { + } +} + +func foo147(l List) { // ERROR "l does not escape" + var p *List + p = &l // ERROR "&l does not escape" + for p.Next != nil { + p = p.Next + } +} + +func foo148(l List) { // ERROR " l does not escape" + for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape" + } +} + +// related: address of variable should have depth of variable, not of loop + +func foo149(l List) { // ERROR " l does not escape" + var p *List + for { + for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape" + } + } +} + +// issue 7934: missed ... if element type had no pointers + +var save150 []byte + +func foo150(x ...byte) { // ERROR "leaking param: x" + save150 = x +} + +func bar150() { + foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" +} + +// issue 7931: bad handling of slice of array + +var save151 *int + +func foo151(x *int) { // ERROR "leaking param: x" + save151 = x +} + +func bar151() { + var a [64]int // ERROR "moved to heap: a" + a[4] = 101 + foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap" +} + +func bar151b() { + var a [10]int // ERROR "moved to heap: a" + b := a[:] // ERROR "a escapes to heap" + foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap" +} + +func bar151c() { + var a [64]int // ERROR "moved to heap: a" + a[4] = 101 + foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap" +} + +func bar151d() { + var a [10]int // ERROR "moved to heap: a" + b := a[:] // ERROR "a escapes to heap" + foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap" +} + +// issue 8120 + +type U struct { + s *string +} + +func (u *U) String() *string { // ERROR "leaking param u content to result ~r0" + return u.s +} + +type V struct { + s *string +} + +func NewV(u U) *V { // ERROR "leaking param: u" + return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape" +} + +func foo152() { + a := "a" // ERROR "moved to heap: a" + u := U{&a} // ERROR "&a escapes to heap" + v := NewV(u) + println(v) +} + +// issue 8176 - &x in type switch body not marked as escaping + +func foo153(v interface{}) *int { // ERROR "leaking param: v" + switch x := v.(type) { + case int: // ERROR "moved to heap: x" + return &x // ERROR "&x escapes to heap" + } + panic(0) +} + +// issue 8185 - &result escaping into result + +func f() (x int, y *int) { // ERROR "moved to heap: x" + y = &x // ERROR "&x escapes to heap" + return +} + +func g() (x interface{}) { // ERROR "moved to heap: x" + x = &x // ERROR "&x escapes to heap" + return +} |