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

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

View as plain text