...

Source file src/cmd/compile/internal/loopvar/testdata/range_esc_minimal_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  				return i
    28  			})
    29  		}
    30  	}
    31  
    32  	bug := false
    33  	if sum != 100-10 {
    34  		fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
    35  		bug = true
    36  	}
    37  	sum = 0
    38  	for _, f := range is {
    39  		sum += f()
    40  	}
    41  	if sum != 2+4+6+8 {
    42  		fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
    43  		bug = true
    44  	}
    45  	if !bug {
    46  		fmt.Printf("PASS\n")
    47  	} else {
    48  		os.Exit(11)
    49  	}
    50  }
    51  

View as plain text