github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/loopvar/testdata/inlines/main.go (about)

     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  	"github.com/go-asm/go/cmd/compile/loopvar/testdata/inlines/a"
     9  	"github.com/go-asm/go/cmd/compile/loopvar/testdata/inlines/b"
    10  	"github.com/go-asm/go/cmd/compile/loopvar/testdata/inlines/c"
    11  	"fmt"
    12  	"os"
    13  )
    14  
    15  func sum(s []*int) int {
    16  	sum := 0
    17  	for _, pi := range s {
    18  		sum += *pi
    19  	}
    20  	return sum
    21  }
    22  
    23  var t []*int
    24  
    25  func F() []*int {
    26  	var s []*int
    27  	for i, j := 0, 0; j < 10; i, j = i+1, j+1 {
    28  		s = append(s, &i)
    29  		t = append(s, &j)
    30  	}
    31  	return s
    32  }
    33  
    34  func main() {
    35  	f := F()
    36  	af := a.F()
    37  	bf, _ := b.F()
    38  	abf := a.Fb()
    39  	cf := c.F()
    40  
    41  	sf, saf, sbf, sabf, scf := sum(f), sum(af), sum(bf), sum(abf), sum(cf)
    42  
    43  	fmt.Printf("f, af, bf, abf, cf sums = %d, %d, %d, %d, %d\n", sf, saf, sbf, sabf, scf)
    44  
    45  	// Special failure just for use with hash searching, to prove it fires exactly once.
    46  	// To test: `gossahash -e loopvarhash go run .` in this directory.
    47  	// This is designed to fail in two different ways, because gossahash searches randomly
    48  	// it will find both failures over time.
    49  	if os.Getenv("GOCOMPILEDEBUG") != "" && (sabf == 45 || sf == 45) {
    50  		os.Exit(11)
    51  	}
    52  	os.Exit(0)
    53  }