modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/escape_struct_return.go (about) 1 // errorcheck -0 -m -l 2 3 // Copyright 2015 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 function parameters. 8 9 package foo 10 11 var Ssink *string 12 13 type U struct { 14 _sp *string 15 _spp **string 16 } 17 18 func A(sp *string, spp **string) U { // ERROR "leaking param: sp to result ~r2 level=0$" "leaking param: spp to result ~r2 level=0$" 19 return U{sp, spp} 20 } 21 22 func B(spp **string) U { // ERROR "leaking param: spp to result ~r1 level=0$" "leaking param: spp to result ~r1 level=1$" 23 return U{*spp, spp} 24 } 25 26 func tA1() { 27 s := "cat" 28 sp := &s // ERROR "tA1 &s does not escape$" 29 spp := &sp // ERROR "tA1 &sp does not escape$" 30 u := A(sp, spp) 31 _ = u 32 println(s) 33 } 34 35 func tA2() { 36 s := "cat" 37 sp := &s // ERROR "tA2 &s does not escape$" 38 spp := &sp // ERROR "tA2 &sp does not escape$" 39 u := A(sp, spp) 40 println(*u._sp) 41 } 42 43 func tA3() { 44 s := "cat" 45 sp := &s // ERROR "tA3 &s does not escape$" 46 spp := &sp // ERROR "tA3 &sp does not escape$" 47 u := A(sp, spp) 48 println(**u._spp) 49 } 50 51 func tB1() { 52 s := "cat" 53 sp := &s // ERROR "tB1 &s does not escape$" 54 spp := &sp // ERROR "tB1 &sp does not escape$" 55 u := B(spp) 56 _ = u 57 println(s) 58 } 59 60 func tB2() { 61 s := "cat" 62 sp := &s // ERROR "tB2 &s does not escape$" 63 spp := &sp // ERROR "tB2 &sp does not escape$" 64 u := B(spp) 65 println(*u._sp) 66 } 67 68 func tB3() { 69 s := "cat" 70 sp := &s // ERROR "tB3 &s does not escape$" 71 spp := &sp // ERROR "tB3 &sp does not escape$" 72 u := B(spp) 73 println(**u._spp) 74 }