Home | History | Annotate | Download | only in test
      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
     13 	s []nih
     14 	y [1024]byte // Prevent write decomposition
     15 }
     16 
     17 type t2 struct {
     18 	x *ih
     19 	s []ih
     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
     35 
     36 	v1s []t1
     37 	v2s []t2
     38 )
     39 
     40 func f() {
     41 	// Test direct writes
     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"
     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 }
     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 }
     61