blob: 5ace8d6793f21bdb87252f7425143fd31f9b454c [file] [log] [blame]
Austin Clements316e3032017-10-24 16:34:33 -04001// errorcheck -+ -0 -l -d=wb
2
3// Copyright 2016 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Test write barrier elimination for notinheap.
8
9package p
10
11type t1 struct {
12 x *nih
Austin Clements3a446d82017-11-02 12:37:25 -040013 s []nih
Austin Clements316e3032017-10-24 16:34:33 -040014 y [1024]byte // Prevent write decomposition
15}
16
17type t2 struct {
18 x *ih
Austin Clements3a446d82017-11-02 12:37:25 -040019 s []ih
Austin Clements316e3032017-10-24 16:34:33 -040020 y [1024]byte
21}
22
23//go:notinheap
24type nih struct {
25 x uintptr
26}
27
28type ih struct { // In-heap type
29 x uintptr
30}
31
32var (
33 v1 t1
34 v2 t2
Austin Clementsb78b54f2017-10-24 17:11:32 -040035
36 v1s []t1
37 v2s []t2
Austin Clements316e3032017-10-24 16:34:33 -040038)
39
40func f() {
41 // Test direct writes
Austin Clements3a446d82017-11-02 12:37:25 -040042 v1.x = nil // no barrier
43 v2.x = nil // ERROR "write barrier"
44 v1.s = []nih(nil) // no barrier
45 v2.s = []ih(nil) // ERROR "write barrier"
Austin Clements316e3032017-10-24 16:34:33 -040046}
47
48func g() {
49 // Test aggregate writes
50 v1 = t1{x: nil} // no barrier
51 v2 = t2{x: nil} // ERROR "write barrier"
52}
Austin Clementsb78b54f2017-10-24 17:11:32 -040053
54func h() {
55 // Test copies and appends.
56 copy(v1s, v1s[1:]) // no barrier
57 copy(v2s, v2s[1:]) // ERROR "write barrier"
58 _ = append(v1s, v1s...) // no barrier
59 _ = append(v2s, v2s...) // ERROR "write barrier"
60}
Austin Clements6454a092018-12-05 15:23:26 -050061
62// Slice clearing
63
64var (
65 sliceIH []*ih
66 sliceNIH []*nih
67)
68
69func sliceClear() {
70 for i := range sliceIH {
71 sliceIH[i] = nil // ERROR "write barrier"
72 }
73 for i := range sliceNIH {
74 sliceNIH[i] = nil // no barrier
75 }
76}