1// Copyright 2022 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//go:build !compiler_bootstrap
6
7package test
8
9// The racecompile builder only builds packages, but does not build
10// or run tests. This is a non-test file to hold cases that (used
11// to) trigger compiler data races, so they will be exercised on
12// the racecompile builder.
13//
14// This package is not imported so functions here are not included
15// in the actual compiler.
16
17// Issue 55357: data race when building multiple instantiations of
18// generic closures with _ parameters.
19func Issue55357() {
20	type U struct {
21		A int
22		B string
23		C string
24	}
25	var q T55357[U]
26	q.Count()
27	q.List()
28
29	type M struct {
30		A int64
31		B uint32
32		C uint32
33	}
34	var q2 T55357[M]
35	q2.Count()
36	q2.List()
37}
38
39type T55357[T any] struct{}
40
41//go:noinline
42func (q *T55357[T]) do(w, v bool, fn func(bk []byte, v T) error) error {
43	return nil
44}
45
46func (q *T55357[T]) Count() (n int, rerr error) {
47	err := q.do(false, false, func(kb []byte, _ T) error {
48		n++
49		return nil
50	})
51	return n, err
52}
53
54func (q *T55357[T]) List() (list []T, rerr error) {
55	var l []T
56	err := q.do(false, true, func(_ []byte, v T) error {
57		l = append(l, v)
58		return nil
59	})
60	if err != nil {
61		return nil, err
62	}
63	return l, nil
64}
65