Source file test/escape6.go

     1  // errorcheck -0 -m -l
     2  
     3  // Copyright 2025 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  // Tests for escaping variable-sized allocations.
     8  // In particular, we need to make sure things assigned into
     9  // variable-sized allocations escape even when the variable-sized
    10  // allocations themselves don't escape.
    11  
    12  package foo
    13  
    14  type T string
    15  
    16  func f1(n int, v T) { // ERROR "leaking param: v"
    17  	s := make([]T, n) // ERROR "make\(\[\]T, n\) does not escape"
    18  	s[0] = v
    19  	g(s)
    20  }
    21  
    22  func f2(n int, v T) { // ERROR "leaking param: v"
    23  	s := make([]T, n) // ERROR "make\(\[\]T, n\) does not escape"
    24  	p := &s[0]
    25  	*p = v
    26  	g(s)
    27  }
    28  
    29  func f3(n int, v T) { // ERROR "leaking param: v"
    30  	s := make([]T, n) // ERROR "make\(\[\]T, n\) does not escape"
    31  	t := (*[4]T)(s)
    32  	t[0] = v
    33  	g(s)
    34  }
    35  
    36  // TODO: imprecise: this does not need to leak v.
    37  func f4(v T) { // ERROR "leaking param: v"
    38  	s := make([]T, 4) // ERROR "make\(\[\]T, 4\) does not escape"
    39  	s[0] = v
    40  	g(s)
    41  }
    42  
    43  // TODO: imprecise: this does not need to leak v.
    44  func f5(v T) { // ERROR "leaking param: v"
    45  	var b [4]T
    46  	s := b[:]
    47  	s[0] = v
    48  	g(s)
    49  }
    50  
    51  func f6(v T) { // ERROR "v does not escape"
    52  	var b [4]T
    53  	s := b[:]
    54  	b[0] = v
    55  	g(s)
    56  }
    57  
    58  func g(s []T) { // ERROR "s does not escape"
    59  }
    60  

View as plain text