Source file src/cmd/compile/internal/test/testdata/reproducible/race.go

     1  // Copyright 2026 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  // Constructs that reach -race-specific code in the concurrent backend.
     6  
     7  package p
     8  
     9  type S struct {
    10  	a, b int
    11  	s    string
    12  	arr  [4]int64
    13  }
    14  
    15  var (
    16  	g  S
    17  	gs []S
    18  )
    19  
    20  // Multi-field structs are not SSA-able under -race.
    21  func Copy(dst, src *S) { *dst = *src }
    22  
    23  func CopyArrays(xs, ys [][8]int) {
    24  	for i := range xs {
    25  		xs[i] = ys[i]
    26  	}
    27  }
    28  
    29  func Equal(x, y S) bool { return x == y }
    30  
    31  func Select(c1, c2 chan S) S {
    32  	select {
    33  	case v := <-c1:
    34  		return v
    35  	case c2 <- g:
    36  	}
    37  	return S{}
    38  }
    39  
    40  func Defer(p *S) (r int) {
    41  	defer func() { r += p.a }()
    42  	return p.b
    43  }
    44  
    45  func Closure(xs []S) func() int {
    46  	n := 0
    47  	return func() int {
    48  		for _, x := range xs {
    49  			n += x.a
    50  		}
    51  		return n
    52  	}
    53  }
    54  
    55  func Swap[T any](p *T, v T) T {
    56  	old := *p
    57  	*p = v
    58  	return old
    59  }
    60  
    61  func UseSwap() {
    62  	var a S
    63  	Swap(&a, g)
    64  	var b [4]string
    65  	Swap(&b, b)
    66  }
    67  
    68  //go:norace
    69  func NoRace(p *S) { p.a++ }
    70  
    71  func CallNoRace(p *S) {
    72  	NoRace(p)
    73  	p.b++
    74  }
    75  
    76  func Map(m map[string]S, k string) S {
    77  	v := m[k]
    78  	m[k] = g
    79  	return v
    80  }
    81  
    82  func Append(s S) { gs = append(gs, s) }
    83  

View as plain text