| 12
 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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 
 | // errorcheck -0 -l -d=wb
// Copyright 2015 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.
// Test where write barriers are and are not emitted.
package p
import "unsafe"
func f(x **byte, y *byte) {
	*x = y // no barrier (dead store)
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f1(x *[]byte, y []byte) {
	*x = y // no barrier (dead store)
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f1a(x *[]byte, y *[]byte) {
	*x = *y // ERROR "write barrier"
	z := *y // no barrier
	*x = z  // ERROR "write barrier"
}
func f2(x *interface{}, y interface{}) {
	*x = y // no barrier (dead store)
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f2a(x *interface{}, y *interface{}) {
	*x = *y // no barrier (dead store)
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f3(x *string, y string) {
	*x = y // no barrier (dead store)
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f3a(x *string, y *string) {
	*x = *y // ERROR "write barrier"
	z := *y // no barrier
	*x = z  // ERROR "write barrier"
}
func f4(x *[2]string, y [2]string) {
	*x = y // ERROR "write barrier"
	z := y // no barrier
	*x = z // ERROR "write barrier"
}
func f4a(x *[2]string, y *[2]string) {
	*x = *y // ERROR "write barrier"
	z := *y // no barrier
	*x = z  // ERROR "write barrier"
}
type T struct {
	X *int
	Y int
	M map[int]int
}
func f5(t, u *T) {
	t.X = &u.Y // ERROR "write barrier"
}
func f6(t *T) {
	t.M = map[int]int{1: 2} // ERROR "write barrier"
}
func f7(x, y *int) []*int {
	var z [3]*int
	i := 0
	z[i] = x // ERROR "write barrier"
	i++
	z[i] = y // ERROR "write barrier"
	i++
	return z[:i]
}
func f9(x *interface{}, v *byte) {
	*x = v // ERROR "write barrier"
}
func f10(x *byte, f func(interface{})) {
	f(x)
}
func f11(x *unsafe.Pointer, y unsafe.Pointer) {
	*x = unsafe.Pointer(uintptr(y) + 1) // ERROR "write barrier"
}
func f12(x []*int, y *int) []*int {
	// write barrier for storing y in x's underlying array
	x = append(x, y) // ERROR "write barrier"
	return x
}
func f12a(x []int, y int) []int {
	// y not a pointer, so no write barriers in this function
	x = append(x, y)
	return x
}
func f13(x []int, y *[]int) {
	*y = append(x, 1) // ERROR "write barrier"
}
func f14(y *[]int) {
	*y = append(*y, 1) // ERROR "write barrier"
}
type T1 struct {
	X *int
}
func f15(x []T1, y T1) []T1 {
	return append(x, y) // ERROR "write barrier"
}
type T8 struct {
	X [8]*int
}
func f16(x []T8, y T8) []T8 {
	return append(x, y) // ERROR "write barrier"
}
func t1(i interface{}) **int {
	// From issue 14306, make sure we have write barriers in a type switch
	// where the assigned variable escapes.
	switch x := i.(type) {
	case *int: // ERROR "write barrier"
		return &x
	}
	switch y := i.(type) {
	case **int: // no write barrier here
		return y
	}
	return nil
}
type T17 struct {
	f func(*T17)
}
func f17(x *T17) {
	// Originally from golang.org/issue/13901, but the hybrid
	// barrier requires both to have barriers.
	x.f = f17                      // ERROR "write barrier"
	x.f = func(y *T17) { *y = *x } // ERROR "write barrier"
}
type T18 struct {
	a []int
	s string
}
func f18(p *T18, x *[]int) {
	p.a = p.a[:5]    // no barrier
	*x = (*x)[0:5]   // no barrier
	p.a = p.a[3:5]   // ERROR "write barrier"
	p.a = p.a[1:2:3] // ERROR "write barrier"
	p.s = p.s[8:9]   // ERROR "write barrier"
	*x = (*x)[3:5]   // ERROR "write barrier"
}
func f19(x, y *int, i int) int {
	// Constructing a temporary slice on the stack should not
	// require any write barriers. See issue 14263.
	a := []*int{x, y} // no barrier
	return *a[i]
}
func f20(x, y *int, i int) []*int {
	// ... but if that temporary slice escapes, then the
	// write barriers are necessary.
	a := []*int{x, y} // ERROR "write barrier"
	return a
}
var x21 *int
var y21 struct {
	x *int
}
var z21 int
// f21x: Global -> heap pointer updates must have write barriers.
func f21a(x *int) {
	x21 = x   // ERROR "write barrier"
	y21.x = x // ERROR "write barrier"
}
func f21b(x *int) {
	x21 = &z21   // ERROR "write barrier"
	y21.x = &z21 // ERROR "write barrier"
}
func f21c(x *int) {
	y21 = struct{ x *int }{x} // ERROR "write barrier"
}
func f22(x *int) (y *int) {
	// pointer write on stack should have no write barrier.
	// this is a case that the frontend failed to eliminate.
	p := &y
	*p = x // no barrier
	return
}
type T23 struct {
	p *int
	a int
}
var t23 T23
var i23 int
// f23x: zeroing global needs write barrier for the hybrid barrier.
func f23a() {
	t23 = T23{} // ERROR "write barrier"
}
func f23b() {
	// also test partial assignments
	t23 = T23{a: 1} // ERROR "write barrier"
}
func f23c() {
	t23 = T23{} // no barrier (dead store)
	// also test partial assignments
	t23 = T23{p: &i23} // ERROR "write barrier"
}
var g int
func f24() **int {
	p := new(*int)
	*p = &g // no write barrier here
	return p
}
func f25() []string {
	return []string{"abc", "def", "ghi"} // no write barrier here
}
type T26 struct {
	a, b, c int
	d, e, f *int
}
var g26 int
func f26(p *int) *T26 { // see issue 29573
	return &T26{
		a: 5,
		b: 6,
		c: 7,
		d: &g26, // no write barrier: global ptr
		e: nil,  // no write barrier: nil ptr
		f: p,    // ERROR "write barrier"
	}
}
func f27(p *int) []interface{} {
	return []interface{}{
		nil,         // no write barrier: zeroed memory, nil ptr
		(*T26)(nil), // no write barrier: zeroed memory, type ptr & nil ptr
		&g26,        // no write barrier: zeroed memory, type ptr & global ptr
		7,           // no write barrier: zeroed memory, type ptr & global ptr
		p,           // ERROR "write barrier"
	}
}
var g28 [256]uint64
func f28() []interface{} {
	return []interface{}{
		false,      // no write barrier
		true,       // no write barrier
		0,          // no write barrier
		1,          // no write barrier
		uint8(127), // no write barrier
		int8(-4),   // no write barrier
		&g28[5],    // no write barrier
	}
}
 |