1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright 2023 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.
//go:build go1.19
// goversion can be pinned to anything strictly before 1.22.
package main
func main() {
test_init()
// Clones from cmd/compile/internal/loopvar/testdata .
range_esc_address()
range_esc_closure()
range_esc_method()
}
// pre-go1.22 all of i will have the same address.
var same = func(a [3]int) []*int {
var r []*int
for i := range a {
r = append(r, &i)
}
return r
}([3]int{})
func test_init() {
if len(same) != 3 {
panic(same)
}
for i := range same {
for j := range same {
if !(same[i] == same[j]) {
panic(same)
}
}
}
for i := range same {
if *(same[i]) != 2 {
panic(same)
}
}
}
func range_esc_address() {
// Clone of range_esc_address.go
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
sum := 0
var is []*int
for _, i := range ints {
for j := 0; j < 10; j++ {
if i == j { // 10 skips
continue
}
sum++
}
if i&1 == 0 {
is = append(is, &i)
}
}
bug := false
if sum != 100-10 {
println("wrong sum, expected", 90, ", saw ", sum)
bug = true
}
if len(is) != 5 {
println("wrong iterations, expected ", 5, ", saw", len(is))
bug = true
}
sum = 0
for _, pi := range is {
sum += *pi
}
if sum != 9+9+9+9+9 {
println("wrong sum, expected", 45, ", saw", sum)
bug = true
}
if bug {
panic("range_esc_address")
}
}
func range_esc_closure() {
// Clone of range_esc_closure.go
var ints = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
var is []func() int
sum := 0
for _, i := range ints {
for j := 0; j < 10; j++ {
if i == j { // 10 skips
continue
}
sum++
}
if i&1 == 0 {
is = append(is, func() int {
if i%17 == 15 {
i++
}
return i
})
}
}
bug := false
if sum != 100-10 {
println("wrong sum, expected", 90, ", saw", sum)
bug = true
}
if len(is) != 5 {
println("wrong iterations, expected ", 5, ", saw", len(is))
bug = true
}
sum = 0
for _, f := range is {
sum += f()
}
if sum != 9+9+9+9+9 {
println("wrong sum, expected ", 45, ", saw ", sum)
bug = true
}
if bug {
panic("range_esc_closure")
}
}
type I int
func (x *I) method() int {
return int(*x)
}
func range_esc_method() {
// Clone of range_esc_method.go
var ints = []I{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
sum := 0
var is []func() int
for _, i := range ints {
for j := 0; j < 10; j++ {
if int(i) == j { // 10 skips
continue
}
sum++
}
if i&1 == 0 {
is = append(is, i.method)
}
}
bug := false
if sum != 100-10 {
println("wrong sum, expected", 90, ", saw", sum)
bug = true
}
if len(is) != 5 {
println("wrong iterations, expected ", 5, ", saw", len(is))
bug = true
}
sum = 0
for _, m := range is {
sum += m()
}
if sum != 9+9+9+9+9 {
println("wrong sum, expected ", 45, ", saw ", sum)
bug = true
}
if bug {
panic("range_esc_method")
}
}
|