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