...

Source file src/golang.org/x/tools/go/ssa/interp/testdata/rangevarlifetime_old.go

Documentation: golang.org/x/tools/go/ssa/interp/testdata

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

View as plain text