Source file test/notinheap2.go

     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  // Global variables are okay.
    17  
    18  var x nih
    19  
    20  // Stack variables are not okay.
    21  
    22  func f() {
    23  	var y nih // ERROR "nih is incomplete \(or unallocatable\); stack allocation disallowed"
    24  	x = y
    25  }
    26  
    27  // Heap allocation is not okay.
    28  
    29  var y *nih
    30  var y2 *struct{ x nih }
    31  var y3 *[1]nih
    32  var z []nih
    33  var w []nih
    34  var n int
    35  var sink interface{}
    36  
    37  type embed1 struct { // implicitly notinheap
    38  	x nih
    39  }
    40  
    41  type embed2 [1]nih // implicitly notinheap
    42  
    43  type embed3 struct { // implicitly notinheap
    44  	x [1]nih
    45  }
    46  
    47  // Type aliases inherit the go:notinheap-ness of the type they alias.
    48  type nihAlias = nih
    49  
    50  type embedAlias1 struct { // implicitly notinheap
    51  	x nihAlias
    52  }
    53  type embedAlias2 [1]nihAlias // implicitly notinheap
    54  
    55  func g() {
    56  	y = new(nih)              // ERROR "can't be allocated in Go"
    57  	y2 = new(struct{ x nih }) // ERROR "can't be allocated in Go"
    58  	y3 = new([1]nih)          // ERROR "can't be allocated in Go"
    59  	z = make([]nih, 1)        // ERROR "can't be allocated in Go"
    60  	z = append(z, x)          // ERROR "can't be allocated in Go"
    61  
    62  	sink = new(embed1)      // ERROR "can't be allocated in Go"
    63  	sink = new(embed2)      // ERROR "can't be allocated in Go"
    64  	sink = new(embed3)      // ERROR "can't be allocated in Go"
    65  	sink = new(embedAlias1) // ERROR "can't be allocated in Go"
    66  	sink = new(embedAlias2) // ERROR "can't be allocated in Go"
    67  
    68  	// Test for special case of OMAKESLICECOPY
    69  	x := make([]nih, n) // ERROR "can't be allocated in Go"
    70  	copy(x, z)
    71  	z = x
    72  }
    73  
    74  // Writes don't produce write barriers.
    75  
    76  var p *nih
    77  
    78  //go:nowritebarrier
    79  func h() {
    80  	y.next = p.next
    81  }
    82  

View as plain text