Source file test/escape_iface_data.go

     1  // errorcheck -0 -d=escapedebug=1
     2  
     3  // Copyright 2024 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 the data word used for interface conversions
     8  // that might otherwise allocate.
     9  
    10  package dataword
    11  
    12  var sink interface{}
    13  
    14  func string1() {
    15  	sink = "abc" // ERROR "using global for interface value"
    16  }
    17  
    18  func string2() {
    19  	v := "abc"
    20  	sink = v // ERROR "using global for interface value"
    21  }
    22  
    23  func string3() {
    24  	sink = "" // ERROR "using global for interface value"
    25  }
    26  
    27  func string4() {
    28  	v := ""
    29  	sink = v // ERROR "using global for interface value"
    30  }
    31  
    32  func string5() {
    33  	var a any = "abc" // ERROR "using global for interface value"
    34  	_ = a
    35  }
    36  
    37  func string6() {
    38  	var a any
    39  	v := "abc"
    40  	a = v // ERROR "using global for interface value"
    41  	_ = a
    42  }
    43  
    44  // string7 can be inlined.
    45  func string7(v string) {
    46  	sink = v
    47  }
    48  
    49  func string8() {
    50  	v0 := "abc"
    51  	v := v0
    52  	string7(v) // ERROR "using global for interface value"
    53  }
    54  
    55  func string9() {
    56  	v0 := "abc"
    57  	v := v0
    58  	f := func() {
    59  		string7(v)
    60  	}
    61  	f() // ERROR "using global for interface value"
    62  }
    63  
    64  func string10() {
    65  	v0 := "abc"
    66  	v := v0
    67  	f := func() {
    68  		f2 := func() {
    69  			string7(v)
    70  		}
    71  		f2()
    72  	}
    73  	f() // ERROR "using global for interface value"
    74  }
    75  
    76  func string11() {
    77  	v0 := "abc"
    78  	v := v0
    79  	defer func() {
    80  		string7(v) // ERROR "using global for interface value"
    81  	}()
    82  }
    83  
    84  func integer1() {
    85  	sink = 42 // ERROR "using global for interface value"
    86  }
    87  
    88  func integer2() {
    89  	v := 42
    90  	sink = v // ERROR "using global for interface value"
    91  }
    92  
    93  func integer3() {
    94  	sink = 0 // ERROR "using global for interface value"
    95  }
    96  
    97  func integer4a() {
    98  	v := 0
    99  	sink = v // ERROR "using global for interface value"
   100  }
   101  
   102  func integer4b() {
   103  	v := uint8(0)
   104  	sink = v // ERROR "using global for single-byte interface value"
   105  }
   106  
   107  func integer5() {
   108  	var a any = 42 // ERROR "using global for interface value"
   109  	_ = a
   110  }
   111  
   112  func integer6() {
   113  	var a any
   114  	v := 42
   115  	a = v // ERROR "using global for interface value"
   116  	_ = a
   117  }
   118  
   119  func integer7(v int) {
   120  	sink = v
   121  }
   122  
   123  type M interface{ M() }
   124  
   125  type MyInt int
   126  
   127  func (m MyInt) M() {}
   128  
   129  func escapes(m M) {
   130  	sink = m
   131  }
   132  
   133  func named1a() {
   134  	sink = MyInt(42) // ERROR "using global for interface value"
   135  }
   136  
   137  func named1b() {
   138  	escapes(MyInt(42)) // ERROR "using global for interface value"
   139  }
   140  
   141  func named2a() {
   142  	v := MyInt(0)
   143  	sink = v // ERROR "using global for interface value"
   144  }
   145  
   146  func named2b() {
   147  	v := MyInt(42)
   148  	escapes(v) // ERROR "using global for interface value"
   149  }
   150  
   151  func named2c() {
   152  	v := 42
   153  	sink = MyInt(v) // ERROR "using global for interface value"
   154  }
   155  
   156  func named2d() {
   157  	v := 42
   158  	escapes(MyInt(v)) // ERROR "using global for interface value"
   159  }
   160  func named3a() {
   161  	sink = MyInt(42) // ERROR "using global for interface value"
   162  }
   163  
   164  func named3b() {
   165  	escapes(MyInt(0)) // ERROR "using global for interface value"
   166  }
   167  
   168  func named4a() {
   169  	v := MyInt(0)
   170  	sink = v // ERROR "using global for interface value"
   171  }
   172  
   173  func named4b() {
   174  	v := MyInt(0)
   175  	escapes(v) // ERROR "using global for interface value"
   176  }
   177  
   178  func named4c() {
   179  	v := 0
   180  	sink = MyInt(v) // ERROR "using global for interface value"
   181  }
   182  
   183  func named4d() {
   184  	v := 0
   185  	escapes(MyInt(v)) // ERROR "using global for interface value"
   186  }
   187  
   188  func named5() {
   189  	var a any = MyInt(42) // ERROR "using global for interface value"
   190  	_ = a
   191  }
   192  
   193  func named6() {
   194  	var a any
   195  	v := MyInt(42)
   196  	a = v // ERROR "using global for interface value"
   197  	_ = a
   198  }
   199  
   200  func named7a(v MyInt) {
   201  	sink = v
   202  }
   203  
   204  func named7b(v MyInt) {
   205  	escapes(v)
   206  }
   207  
   208  type S struct{ a, b int64 }
   209  
   210  func struct1() {
   211  	sink = S{1, 1} // ERROR "using global for interface value"
   212  }
   213  
   214  func struct2() {
   215  	v := S{1, 1}
   216  	sink = v // ERROR "using global for interface value"
   217  }
   218  
   219  func struct3() {
   220  	sink = S{} // ERROR "using global for zero value interface value"
   221  }
   222  
   223  func struct4() {
   224  	v := S{}
   225  	sink = v // ERROR "using global for zero value interface value"
   226  }
   227  
   228  func struct5() {
   229  	var a any = S{1, 1} // ERROR "using global for interface value"
   230  	_ = a
   231  }
   232  
   233  func struct6() {
   234  	var a any
   235  	v := S{1, 1}
   236  	a = v // ERROR "using global for interface value"
   237  	_ = a
   238  }
   239  
   240  func struct7(v S) {
   241  	sink = v
   242  }
   243  
   244  func emptyStruct1() {
   245  	sink = struct{}{} // ERROR "using global for zero-sized interface value"
   246  }
   247  
   248  func emptyStruct2() {
   249  	v := struct{}{}
   250  	sink = v // ERROR "using global for zero-sized interface value"
   251  }
   252  
   253  func emptyStruct3(v struct{}) { // ERROR "using global for zero-sized interface value"
   254  	sink = v
   255  }
   256  
   257  // Some light emulation of conditional debug printing (such as in #53465).
   258  
   259  func Printf(format string, args ...any) {
   260  	for _, arg := range args {
   261  		sink = arg
   262  	}
   263  }
   264  
   265  var enabled = true
   266  
   267  func debugf(format string, args ...interface{}) {
   268  	if enabled {
   269  		Printf(format, args...)
   270  	}
   271  }
   272  
   273  //go:noinline
   274  func debugf2(format string, args ...interface{}) {
   275  	if enabled {
   276  		Printf(format, args...)
   277  	}
   278  }
   279  
   280  func f1() {
   281  	v := 1000
   282  	debugf("hello %d", v) // ERROR "using global for interface value"
   283  }
   284  
   285  func f2() {
   286  	v := 1000
   287  	debugf2("hello %d", v) // ERROR "using global for interface value"
   288  }
   289  
   290  //go:noinline
   291  func f3(i int) {
   292  	debugf("hello %d", i)
   293  }
   294  
   295  func f4() {
   296  	f3(1000)
   297  }
   298  

View as plain text