github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/test/notinheap2.go (about)

     1  // errorcheck -+
     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 walk errors for go:notinheap.
     8  
     9  package p
    10  
    11  //go:notinheap
    12  type nih struct {
    13  	next *nih
    14  }
    15  
    16  // Globals and stack variables are okay.
    17  
    18  var x nih
    19  
    20  func f() {
    21  	var y nih
    22  	x = y
    23  }
    24  
    25  // Heap allocation is not okay.
    26  
    27  var y *nih
    28  var z []nih
    29  
    30  func g() {
    31  	y = new(nih)       // ERROR "heap allocation disallowed"
    32  	z = make([]nih, 1) // ERROR "heap allocation disallowed"
    33  	z = append(z, x)   // ERROR "heap allocation disallowed"
    34  }
    35  
    36  // Writes don't produce write barriers.
    37  
    38  var p *nih
    39  
    40  //go:nowritebarrier
    41  func h() {
    42  	y.next = p.next
    43  }