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
5package main
6
7import (
8	"cmd/compile/internal/loopvar/testdata/inlines/a"
9	"cmd/compile/internal/loopvar/testdata/inlines/b"
10	"cmd/compile/internal/loopvar/testdata/inlines/c"
11	"fmt"
12	"os"
13)
14
15func sum(s []*int) int {
16	sum := 0
17	for _, pi := range s {
18		sum += *pi
19	}
20	return sum
21}
22
23var t []*int
24
25func 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
34func 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}
54