Austin Clements | 316e303 | 2017-10-24 16:34:33 -0400 | [diff] [blame] | 1 | // 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 | |
| 9 | package p |
| 10 | |
| 11 | type t1 struct { |
| 12 | x *nih |
Austin Clements | 3a446d8 | 2017-11-02 12:37:25 -0400 | [diff] [blame] | 13 | s []nih |
Austin Clements | 316e303 | 2017-10-24 16:34:33 -0400 | [diff] [blame] | 14 | y [1024]byte // Prevent write decomposition |
| 15 | } |
| 16 | |
| 17 | type t2 struct { |
| 18 | x *ih |
Austin Clements | 3a446d8 | 2017-11-02 12:37:25 -0400 | [diff] [blame] | 19 | s []ih |
Austin Clements | 316e303 | 2017-10-24 16:34:33 -0400 | [diff] [blame] | 20 | y [1024]byte |
| 21 | } |
| 22 | |
| 23 | //go:notinheap |
| 24 | type nih struct { |
| 25 | x uintptr |
| 26 | } |
| 27 | |
| 28 | type ih struct { // In-heap type |
| 29 | x uintptr |
| 30 | } |
| 31 | |
| 32 | var ( |
| 33 | v1 t1 |
| 34 | v2 t2 |
Austin Clements | b78b54f | 2017-10-24 17:11:32 -0400 | [diff] [blame] | 35 | |
| 36 | v1s []t1 |
| 37 | v2s []t2 |
Austin Clements | 316e303 | 2017-10-24 16:34:33 -0400 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | func f() { |
| 41 | // Test direct writes |
Austin Clements | 3a446d8 | 2017-11-02 12:37:25 -0400 | [diff] [blame] | 42 | 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 Clements | 316e303 | 2017-10-24 16:34:33 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func g() { |
| 49 | // Test aggregate writes |
| 50 | v1 = t1{x: nil} // no barrier |
| 51 | v2 = t2{x: nil} // ERROR "write barrier" |
| 52 | } |
Austin Clements | b78b54f | 2017-10-24 17:11:32 -0400 | [diff] [blame] | 53 | |
| 54 | func 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 Clements | 6454a09 | 2018-12-05 15:23:26 -0500 | [diff] [blame] | 61 | |
| 62 | // Slice clearing |
| 63 | |
| 64 | var ( |
| 65 | sliceIH []*ih |
| 66 | sliceNIH []*nih |
| 67 | ) |
| 68 | |
| 69 | func 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 | } |