summaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/loopvar/testdata/inlines/main.go
blob: 46fcee1a6db426f4c21bff4c6203868967ef3920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"cmd/compile/internal/loopvar/testdata/inlines/a"
	"cmd/compile/internal/loopvar/testdata/inlines/b"
	"cmd/compile/internal/loopvar/testdata/inlines/c"
	"fmt"
	"os"
)

func sum(s []*int) int {
	sum := 0
	for _, pi := range s {
		sum += *pi
	}
	return sum
}

var t []*int

func F() []*int {
	var s []*int
	for i, j := 0, 0; j < 10; i, j = i+1, j+1 {
		s = append(s, &i)
		t = append(s, &j)
	}
	return s
}

func main() {
	f := F()
	af := a.F()
	bf, _ := b.F()
	abf := a.Fb()
	cf := c.F()

	sf, saf, sbf, sabf, scf := sum(f), sum(af), sum(bf), sum(abf), sum(cf)

	fmt.Printf("f, af, bf, abf, cf sums = %d, %d, %d, %d, %d\n", sf, saf, sbf, sabf, scf)

	// Special failure just for use with hash searching, to prove it fires exactly once.
	// To test: `gossahash -e loopvarhash go run .` in this directory.
	// This is designed to fail in two different ways, because gossahash searches randomly
	// it will find both failures over time.
	if os.Getenv("GOCOMPILEDEBUG") != "" && (sabf == 45 || sf == 45) {
		os.Exit(11)
	}
	os.Exit(0)
}