...

Source file src/cmd/compile/internal/loopvar/testdata/range_esc_closure.go

Documentation: cmd/compile/internal/loopvar/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  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  var is []func() int
    13  
    14  var ints = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    15  
    16  func main() {
    17  	sum := 0
    18  	for _, i := range ints {
    19  		for j := 0; j < 10; j++ {
    20  			if i == j { // 10 skips
    21  				continue
    22  			}
    23  			sum++
    24  		}
    25  		if i&1 == 0 {
    26  			is = append(is, func() int {
    27  				if i%17 == 15 {
    28  					i++
    29  				}
    30  				return i
    31  			})
    32  		}
    33  	}
    34  
    35  	bug := false
    36  	if sum != 100-10 {
    37  		fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
    38  		bug = true
    39  	}
    40  	sum = 0
    41  	for _, f := range is {
    42  		sum += f()
    43  	}
    44  	if sum != 2+4+6+8 {
    45  		fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
    46  		bug = true
    47  	}
    48  	if !bug {
    49  		fmt.Printf("PASS\n")
    50  	} else {
    51  		os.Exit(11)
    52  	}
    53  }
    54  

View as plain text