github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/test/writebarrier.go (about)

     1  // errorcheck -0 -l -d=wb
     2  
     3  // Copyright 2015 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 where write barriers are and are not emitted.
     8  
     9  package p
    10  
    11  import "unsafe"
    12  
    13  func f(x **byte, y *byte) {
    14  	*x = y // ERROR "write barrier"
    15  
    16  	z := y // no barrier
    17  	*x = z // ERROR "write barrier"
    18  }
    19  
    20  func f1(x *[]byte, y []byte) {
    21  	*x = y // ERROR "write barrier"
    22  
    23  	z := y // no barrier
    24  	*x = z // ERROR "write barrier"
    25  }
    26  
    27  func f1a(x *[]byte, y *[]byte) {
    28  	*x = *y // ERROR "write barrier"
    29  
    30  	z := *y // no barrier
    31  	*x = z  // ERROR "write barrier"
    32  }
    33  
    34  func f2(x *interface{}, y interface{}) {
    35  	*x = y // ERROR "write barrier"
    36  
    37  	z := y // no barrier
    38  	*x = z // ERROR "write barrier"
    39  }
    40  
    41  func f2a(x *interface{}, y *interface{}) {
    42  	*x = *y // ERROR "write barrier"
    43  
    44  	z := y // no barrier
    45  	*x = z // ERROR "write barrier"
    46  }
    47  
    48  func f3(x *string, y string) {
    49  	*x = y // ERROR "write barrier"
    50  
    51  	z := y // no barrier
    52  	*x = z // ERROR "write barrier"
    53  }
    54  
    55  func f3a(x *string, y *string) {
    56  	*x = *y // ERROR "write barrier"
    57  
    58  	z := *y // no barrier
    59  	*x = z  // ERROR "write barrier"
    60  }
    61  
    62  func f4(x *[2]string, y [2]string) {
    63  	*x = y // ERROR "write barrier"
    64  
    65  	z := y // no barrier
    66  	*x = z // ERROR "write barrier"
    67  }
    68  
    69  func f4a(x *[2]string, y *[2]string) {
    70  	*x = *y // ERROR "write barrier"
    71  
    72  	z := *y // no barrier
    73  	*x = z  // ERROR "write barrier"
    74  }
    75  
    76  type T struct {
    77  	X *int
    78  	Y int
    79  	M map[int]int
    80  }
    81  
    82  func f5(t, u *T) {
    83  	t.X = &u.Y // ERROR "write barrier"
    84  }
    85  
    86  func f6(t *T) {
    87  	t.M = map[int]int{1: 2} // ERROR "write barrier"
    88  }
    89  
    90  func f7(x, y *int) []*int {
    91  	var z [3]*int
    92  	i := 0
    93  	z[i] = x // ERROR "write barrier"
    94  	i++
    95  	z[i] = y // ERROR "write barrier"
    96  	i++
    97  	return z[:i]
    98  }
    99  
   100  func f9(x *interface{}, v *byte) {
   101  	*x = v // ERROR "write barrier"
   102  }
   103  
   104  func f10(x *byte, f func(interface{})) {
   105  	f(x)
   106  }
   107  
   108  func f11(x *unsafe.Pointer, y unsafe.Pointer) {
   109  	*x = unsafe.Pointer(uintptr(y) + 1) // ERROR "write barrier"
   110  }
   111  
   112  func f12(x []*int, y *int) []*int {
   113  	// write barrier for storing y in x's underlying array
   114  	x = append(x, y) // ERROR "write barrier"
   115  	return x
   116  }
   117  
   118  func f12a(x []int, y int) []int {
   119  	// y not a pointer, so no write barriers in this function
   120  	x = append(x, y)
   121  	return x
   122  }
   123  
   124  func f13(x []int, y *[]int) {
   125  	*y = append(x, 1) // ERROR "write barrier"
   126  }
   127  
   128  func f14(y *[]int) {
   129  	*y = append(*y, 1) // ERROR "write barrier"
   130  }
   131  
   132  type T1 struct {
   133  	X *int
   134  }
   135  
   136  func f15(x []T1, y T1) []T1 {
   137  	return append(x, y) // ERROR "write barrier"
   138  }
   139  
   140  type T8 struct {
   141  	X [8]*int
   142  }
   143  
   144  func f16(x []T8, y T8) []T8 {
   145  	return append(x, y) // ERROR "write barrier"
   146  }