Source file test/escape_make_non_const.go
1 // errorcheck -0 -m 2 3 // Copyright 2025 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 package escape 8 9 const globalConstSize = 128 10 11 var globalVarSize = 128 12 13 //go:noinline 14 func testSlices() { 15 { 16 size := 128 17 _ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape" 18 } 19 20 { 21 s := 128 22 size := s 23 _ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape" 24 } 25 26 { 27 size := 128 28 _ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape" 29 } 30 31 { 32 s := 128 33 size := s 34 _ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape" 35 } 36 37 { 38 s1 := 128 39 s2 := 256 40 _ = make([]byte, s2, s1) // ERROR "make\(\[\]byte, s2, 128\) does not escape" 41 } 42 43 allocLen(256) // ERROR "make\(\[\]byte, 256\) does not escape" "inlining call" 44 allocCap(256) // ERROR "make\(\[\]byte, 0, 256\) does not escape" "inlining call" 45 _ = newT(256) // ERROR "make\(\[\]byte, 256\) does not escape" "inlining call" 46 47 { 48 size := globalConstSize 49 _ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape" 50 } 51 52 allocLen(globalConstSize) // ERROR "make\(\[\]byte, 128\) does not escape" "inlining call" 53 allocCap(globalConstSize) // ERROR "make\(\[\]byte, 0, 128\) does not escape" "inlining call" 54 _ = newT(globalConstSize) // ERROR "make\(\[\]byte, 128\) does not escape" "inlining call" 55 56 { 57 c := 128 58 s := 256 59 _ = make([]byte, s, c) // ERROR "make\(\[\]byte, s, 128\) does not escape" 60 } 61 62 { 63 s := 256 64 _ = make([]byte, s, globalConstSize) // ERROR "make\(\[\]byte, s, 128\) does not escape" 65 } 66 67 { 68 _ = make([]byte, globalVarSize) // ERROR "make\(\[\]byte, globalVarSize\) does not escape" 69 _ = make([]byte, globalVarSize, globalConstSize) // ERROR "make\(\[\]byte, globalVarSize, 128\) does not escape" 70 } 71 } 72 73 func allocLen(l int) []byte { // ERROR "can inline" 74 return make([]byte, l) // ERROR "escapes to heap" 75 } 76 77 func allocCap(l int) []byte { // ERROR "can inline" 78 return make([]byte, 0, l) // ERROR "escapes to heap" 79 } 80 81 type t struct { 82 s []byte 83 } 84 85 func newT(l int) t { // ERROR "can inline" 86 return t{make([]byte, l)} // ERROR "make.*escapes to heap" 87 } 88 89 //go:noinline 90 func testMaps() { 91 size := 128 92 _ = make(map[string]int, size) // ERROR "does not escape" 93 94 _ = allocMapLen(128) // ERROR "does not escape" "inlining call" 95 _ = newM(128) // ERROR "does not escape" "inlining call" 96 } 97 98 func allocMapLen(l int) map[string]int { // ERROR "can inline" 99 return make(map[string]int, l) // ERROR "escapes to heap" 100 } 101 102 type m struct { 103 m map[string]int 104 } 105 106 func newM(l int) m { // ERROR "can inline" 107 return m{make(map[string]int, l)} // ERROR "make.*escapes to heap" 108 } 109 110 //go:noinline 111 func testLenOfSliceLit() { 112 ints := []int{0, 1, 2, 3, 4, 5} // ERROR "\[\]int\{\.\.\.\} does not escape"' 113 _ = make([]int, len(ints)) // ERROR "make\(\[\]int, 6\) does not escape" 114 _ = allocLenOf(ints) // ERROR "inlining call", "make\(\[\]int, 6\) does not escape" 115 116 _ = make([]int, 2, len(ints)) // ERROR "make\(\[\]int, 2, 6\) does not escape" 117 _ = make([]int, len(ints), 2) // ERROR "make\(\[\]int, len\(ints\), 2\) does not escape" 118 _ = make([]int, 10, len(ints)) // ERROR "make\(\[\]int, 10, 6\) does not escape" 119 } 120 121 func allocLenOf(s []int) []int { // ERROR "can inline" "s does not escape" 122 return make([]int, len(s)) // ERROR "escapes to heap" 123 } 124