github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/transform/testdata/allocs2.go (about)

     1  package main
     2  
     3  func main() {
     4  	n1 := 5
     5  	derefInt(&n1)
     6  
     7  	// This should eventually be modified to not escape.
     8  	n2 := 6 // OUT: object allocated on the heap: escapes at line 9
     9  	returnIntPtr(&n2)
    10  
    11  	s1 := make([]int, 3)
    12  	readIntSlice(s1)
    13  
    14  	s2 := [3]int{}
    15  	readIntSlice(s2[:])
    16  
    17  	// This should also be modified to not escape.
    18  	s3 := make([]int, 3) // OUT: object allocated on the heap: escapes at line 19
    19  	returnIntSlice(s3)
    20  
    21  	useSlice(make([]int, getUnknownNumber())) // OUT: object allocated on the heap: size is not constant
    22  
    23  	s4 := make([]byte, 300) // OUT: object allocated on the heap: object size 300 exceeds maximum stack allocation size 256
    24  	readByteSlice(s4)
    25  
    26  	s5 := make([]int, 4) // OUT: object allocated on the heap: escapes at line 27
    27  	_ = append(s5, 5)
    28  
    29  	s6 := make([]int, 3)
    30  	s7 := []int{1, 2, 3}
    31  	copySlice(s6, s7)
    32  
    33  	c1 := getComplex128() // OUT: object allocated on the heap: escapes at line 34
    34  	useInterface(c1)
    35  
    36  	n3 := 5
    37  	func() int {
    38  		return n3
    39  	}()
    40  
    41  	callVariadic(3, 5, 8) // OUT: object allocated on the heap: escapes at line 41
    42  
    43  	s8 := []int{3, 5, 8} // OUT: object allocated on the heap: escapes at line 44
    44  	callVariadic(s8...)
    45  
    46  	n4 := 3 // OUT: object allocated on the heap: escapes at line 48
    47  	n5 := 7 // OUT: object allocated on the heap: escapes at line 48
    48  	func() {
    49  		n4 = n5
    50  	}()
    51  	println(n4, n5)
    52  }
    53  
    54  func derefInt(x *int) int {
    55  	return *x
    56  }
    57  
    58  func returnIntPtr(x *int) *int {
    59  	return x
    60  }
    61  
    62  func readIntSlice(s []int) int {
    63  	return s[1]
    64  }
    65  
    66  func readByteSlice(s []byte) byte {
    67  	return s[1]
    68  }
    69  
    70  func returnIntSlice(s []int) []int {
    71  	return s
    72  }
    73  
    74  func getUnknownNumber() int
    75  
    76  func copySlice(out, in []int) {
    77  	copy(out, in)
    78  }
    79  
    80  func getComplex128() complex128
    81  
    82  func useInterface(interface{})
    83  
    84  func callVariadic(...int)
    85  
    86  func useSlice([]int)