Source file test/escape_unique.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  // Test escape analysis for unique.
     8  
     9  package escape
    10  
    11  import "unique"
    12  
    13  type T string
    14  
    15  func f1(s string) unique.Handle[string] { // ERROR "s does not escape$"
    16  	return unique.Make(s)
    17  }
    18  
    19  func f1a(s []byte) unique.Handle[string] { // ERROR "s does not escape$"
    20  	return unique.Make(string(s)) // ERROR "string\(s\) does not escape$"
    21  }
    22  
    23  func gen[S ~string](s S) unique.Handle[S] {
    24  	return unique.Make(s)
    25  }
    26  
    27  func f2(s T) unique.Handle[T] { // ERROR "s does not escape$"
    28  	return unique.Make(s)
    29  }
    30  
    31  func f3(s T) unique.Handle[T] { // ERROR "s does not escape$"
    32  	return gen(s)
    33  }
    34  
    35  type pair struct {
    36  	s1 string
    37  	s2 string
    38  }
    39  
    40  func f4(s1 string, s2 string) unique.Handle[pair] { // ERROR "s1 does not escape$" "s2 does not escape$"
    41  	return unique.Make(pair{s1, s2})
    42  }
    43  
    44  type viaInterface struct {
    45  	s any
    46  }
    47  
    48  func f5(s string) unique.Handle[viaInterface] { // ERROR "leaking param: s$"
    49  	return unique.Make(viaInterface{s}) // ERROR "s escapes to heap$"
    50  }
    51  
    52  var sink any
    53  
    54  func f6(s string) unique.Handle[string] { // ERROR "leaking param: s$"
    55  	sink = s // ERROR "s escapes to heap$"
    56  	return unique.Make(s)
    57  }
    58  
    59  func f6a(s []byte) unique.Handle[string] { // ERROR "leaking param: s$"
    60  	sink = s                      // ERROR "s escapes to heap$"
    61  	return unique.Make(string(s)) // ERROR "string\(s\) does not escape$"
    62  }
    63  

View as plain text