...

Source file src/cmd/compile/internal/loopvar/testdata/range_esc_method.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  type I int
    13  
    14  func (x *I) method() int {
    15  	return int(*x)
    16  }
    17  
    18  var ints = []I{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    19  
    20  func main() {
    21  	sum := 0
    22  	var is []func() int
    23  	for _, i := range ints {
    24  		for j := 0; j < 10; j++ {
    25  			if int(i) == j { // 10 skips
    26  				continue
    27  			}
    28  			sum++
    29  		}
    30  		if i&1 == 0 {
    31  			is = append(is, i.method)
    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 _, m := range is {
    42  		sum += m()
    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