2 // errorcheck -0 -l -live -wb=0
4 // Copyright 2014 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // liveness tests with inlining disabled.
16 func printpointer(**int)
19 func printintpointer(*int)
22 func printstringpointer(*string)
25 func printstring(string)
28 func printbytepointer(*byte)
34 printpointer(&x) // ERROR "live at call to printpointer: x$"
35 printpointer(&x) // ERROR "live at call to printpointer: x$"
40 printint(0) // nothing live here
44 printpointer(&x) // ERROR "live at call to printpointer: x$"
45 printpointer(&x) // ERROR "live at call to printpointer: x$"
49 // Because x and y are ambiguously live, they appear
50 // live throughout the function, to avoid being poisoned
51 // in GODEBUG=gcdead=1 mode.
53 printint(0) // ERROR "live at call to printint: x y$"
55 printint(0) // ERROR "live at call to printint: x y$"
61 printpointer(&x) // ERROR "live at call to printpointer: x y$"
62 printpointer(&x) // ERROR "live at call to printpointer: x y$"
65 printpointer(&y) // ERROR "live at call to printpointer: x y$"
66 printpointer(&y) // ERROR "live at call to printpointer: x y$"
68 printint(0) // ERROR "f3: x \(type \*int\) is ambiguously live$" "f3: y \(type \*int\) is ambiguously live$" "live at call to printint: x y$"
71 // The old algorithm treated x as live on all code that
72 // could flow to a return statement, so it included the
73 // function entry and code above the declaration of x
74 // but would not include an indirect use of x in an infinite loop.
75 // Check that these cases are handled correctly.
77 func f4(b1, b2 bool) { // x not live here
79 printint(0) // x not live here
86 printint(**z) // ERROR "live at call to printint: x z$"
88 printint(1) // ERROR "live at call to printint: x$"
92 printint(**z) // ERROR "live at call to printint: x z$"
107 printint(**z) // ERROR "f5: x \(type \*int\) is ambiguously live$" "f5: y \(type \*int\) is ambiguously live$" "live at call to printint: x y$"
110 // confusion about the _ result used to cause spurious "live at entry to f6: _".
112 func f6() (_, y string) {
117 // confusion about addressed results used to cause "live at entry to f7: x".
119 func f7() (x string) {
125 // ignoring block returns used to cause "live at entry to f8: x, y".
127 func f8() (x, y string) {
131 func g8() (string, string)
133 // ignoring block assignments used to cause "live at entry to f9: x"
141 return x != interface{}(99.0i) // ERROR "live at call to convT2E: x$"
144 // liveness formerly confused by UNDEF followed by RET,
145 // leading to "live at entry to f10: ~r1" (unnamed result).
151 // liveness formerly confused by select, thinking runtime.selectgo
152 // can return to next instruction; it always jumps elsewhere.
153 // note that you have to use at least two cases in the select
154 // to get a true select; smaller selects compile to optimized helper functions.
159 // this used to have a spurious "live at entry to f11a: ~r0"
161 select { // ERROR "live at call to newselect: autotmp_[0-9]+$" "live at call to selectgo: autotmp_[0-9]+$"
162 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
164 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
172 // At this point p is dead: the code here cannot
173 // get to the bottom of the function.
174 // This used to have a spurious "live at call to printint: p".
175 printint(1) // nothing live here!
176 select { // ERROR "live at call to newselect: autotmp_[0-9]+$" "live at call to selectgo: autotmp_[0-9]+$"
177 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
179 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
190 // Unlike previous, the cases in this select fall through,
191 // so we can get to the println, so p is not dead.
192 printint(1) // ERROR "live at call to printint: p$"
193 select { // ERROR "live at call to newselect: autotmp_[0-9]+ p$" "live at call to selectgo: autotmp_[0-9]+ p$"
194 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
195 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
202 // similarly, select{} does not fall through.
203 // this used to have a spurious "live at entry to f12: ~r0".
213 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
214 // this used to be missing the fact that s is live during the call to g13 (because it is
215 // needed for the call to h13).
219 s = h13(s, g13(s)) // ERROR "live at call to g13: s$"
222 func g13(string) string
223 func h13(string, string) string
225 // more incorrectly placed VARDEF.
229 printstringpointer(&x) // ERROR "live at call to printstringpointer: x$"
237 x = g15() // ERROR "live at call to g15: x$"
238 printstring(x) // ERROR "live at call to printstring: x$"
243 // Checking that various temporaries do not persist or cause
244 // ambiguously live values that must be zeroed.
245 // The exact temporary names are inconsequential but we are
246 // trying to check that there is only one at any given site,
247 // and also that none show up in "ambiguously live" messages.
253 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
255 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
256 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
259 var m2s map[string]*byte
260 var m2 map[[2]string]*byte
265 // value temporary only
267 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
269 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
270 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
274 // key temporary only
276 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
278 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
279 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
283 // key and value temporaries
285 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
287 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
288 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
294 // key temporary for mapaccess.
295 // temporary introduced by orderexpr.
298 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
300 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
301 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
308 // dest temporary for channel receive.
312 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
314 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
315 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
320 // src temporary for channel send
322 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
324 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
325 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
329 // key temporary for mapaccess using array literal key.
332 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
334 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
335 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
340 // key temporary for two-result map access using array literal key.
344 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
346 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
347 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
353 // key temporary for map access using array literal key.
354 // value temporary too.
356 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
358 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
359 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
362 // defer should not cause spurious ambiguously live variables
371 x = g15() // ERROR "live at call to g15: x$"
372 printstring(x) // ERROR "live at call to printstring: x$"
373 } // ERROR "live at call to deferreturn: x$"
377 // non-escaping ... slices passed to function call should die on return,
378 // so that the temporaries do not stack and do not cause ambiguously
383 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
385 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
386 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
391 func print26(...interface{})
393 // non-escaping closures passed to function call should die on return
398 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
400 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
401 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
405 // but defer does escape to later execution in the function
407 func f27defer(b bool) {
410 defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+$"
412 defer call27(func() { x++ }) // ERROR "f27defer: autotmp_[0-9]+ \(type struct { F uintptr; x \*int }\) is ambiguously live$" "live at call to deferproc: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$"
413 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ autotmp_[0-9]+$"
414 } // ERROR "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$"
416 // and newproc (go) escapes to the heap
421 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newproc: &x$"
423 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$"
430 // concatstring slice should die on return
432 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
436 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
438 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
439 printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
442 // map iterator should die on end of range loop
446 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
447 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
450 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
451 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
453 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
454 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
458 // copy of array of pointers should die at end of range loop
463 // two live temps during print(p):
464 // the copy of ptrarr and the internal iterator pointer.
466 for _, p := range ptrarr {
467 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
470 for _, p := range ptrarr {
471 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
473 for _, p := range ptrarr {
474 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
478 // conversion to interface should not leave temporary behind
480 func f31(b1, b2, b3 bool) {
482 g31("a") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to g31: autotmp_[0-9]+$"
485 h31("b") // ERROR "live at call to convT2E: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to h31: autotmp_[0-9]+$" "live at call to newobject: autotmp_[0-9]+$"
488 panic("asdf") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to gopanic: autotmp_[0-9]+$"
493 func g31(interface{})
494 func h31(...interface{})
496 // non-escaping partial functions passed to function call should die on return
500 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
508 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
510 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
511 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
517 // temporaries introduced during if conditions and && || expressions
518 // should die once the condition has been acted upon.
520 var m33 map[interface{}]int
523 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
533 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
541 if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
549 if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
557 if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
564 // select temps should disappear in the case bodies
568 func fc38() chan string
569 func fi38(int) *string
573 // we don't care what temps are printed on the lines with output.
574 // we care that the println lines have no live variables
575 // and therefore no output.
577 select { // ERROR "live at call to newselect: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to selectgo: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$"
578 case <-fc38(): // ERROR "live at call to selectrecv: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$"
580 case fc38() <- *fi38(1): // ERROR "live at call to fc38: autotmp_[0-9]+$" "live at call to fi38: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to selectsend: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$"
582 case *fi38(2) = <-fc38(): // ERROR "live at call to fc38: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to fi38: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to selectrecv: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$"
584 case *fi38(3), *fb38() = <-fc38(): // ERROR "live at call to fb38: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to fc38: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to fi38: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to selectrecv2: autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+ autotmp_[0-9]+$"
592 // issue 8097: mishandling of x = x during return.
594 func f39() (x []int) {
596 printnl() // ERROR "live at call to printnl: x$"
600 func f39a() (x []int) {
602 printnl() // ERROR "live at call to printnl: x$"
606 func f39b() (x [10]*int) {
608 x[0] = new(int) // ERROR "live at call to newobject: x$"
609 printnl() // ERROR "live at call to printnl: x$"
613 func f39c() (x [10]*int) {
615 x[0] = new(int) // ERROR "live at call to newobject: x$"
616 printnl() // ERROR "live at call to printnl: x$"
620 // issue 8142: lost 'addrtaken' bit on inlined variables.
621 // no inlining in this test, so just checking that non-inlined works.
629 ret.m = make(map[int]int) // ERROR "live at call to makemap: &ret$"
641 ret.m = make(map[int]int) // ERROR "live at call to makemap: autotmp_[0-9]+ ret$"
643 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ ret$"