...

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

View as plain text