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$"
48 func f3(b1, b2 bool) {
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$"
88 printint(1) // x not live here
92 printint(**z) // ERROR "live at call to printint: x$"
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.data x.type$"
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]+$"
191 sink = p // prevent stack allocation, otherwise p is rematerializeable
193 // Unlike previous, the cases in this select fall through,
194 // so we can get to the println, so p is not dead.
195 printint(1) // ERROR "live at call to printint: p$"
196 select { // ERROR "live at call to newselect: autotmp_[0-9]+ p$" "live at call to selectgo: autotmp_[0-9]+ p$"
197 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
198 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
205 // similarly, select{} does not fall through.
206 // this used to have a spurious "live at entry to f12: ~r0".
216 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
217 // this used to be missing the fact that s is live during the call to g13 (because it is
218 // needed for the call to h13).
222 s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
225 func g13(string) string
226 func h13(string, string) string
228 // more incorrectly placed VARDEF.
232 printstringpointer(&x) // ERROR "live at call to printstringpointer: x$"
240 x = g15() // ERROR "live at call to g15: x$"
241 printstring(x) // ERROR "live at call to printstring: x$"
246 // Checking that various temporaries do not persist or cause
247 // ambiguously live values that must be zeroed.
248 // The exact temporary names are inconsequential but we are
249 // trying to check that there is only one at any given site,
250 // and also that none show up in "ambiguously live" messages.
256 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
258 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
259 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
262 var m2s map[string]*byte
263 var m2 map[[2]string]*byte
268 // value temporary only
270 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
272 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
273 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
277 // key temporary only
279 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
281 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
282 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
286 // key and value temporaries
288 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
290 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
291 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
297 // key temporary for mapaccess.
298 // temporary introduced by orderexpr.
301 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
303 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
304 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
311 // dest temporary for channel receive.
315 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
317 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
318 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
323 // src temporary for channel send
325 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
327 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
328 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
332 // key temporary for mapaccess using array literal key.
335 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
337 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
338 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
343 // key temporary for two-result map access using array literal key.
347 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
349 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
350 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
356 // key temporary for map access using array literal key.
357 // value temporary too.
359 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
361 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
362 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
365 // defer should not cause spurious ambiguously live variables
374 x = g15() // ERROR "live at call to g15: x$"
375 printstring(x) // ERROR "live at call to printstring: x$"
376 } // ERROR "live at call to deferreturn: x$"
380 // non-escaping ... slices passed to function call should die on return,
381 // so that the temporaries do not stack and do not cause ambiguously
386 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
388 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
389 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
394 func print26(...interface{})
396 // non-escaping closures passed to function call should die on return
401 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
403 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
404 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
408 // but defer does escape to later execution in the function
410 func f27defer(b bool) {
413 defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+$"
415 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]+$"
416 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ autotmp_[0-9]+$"
417 } // ERROR "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$"
419 // and newproc (go) escapes to the heap
424 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newproc: &x$"
426 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$"
433 // concatstring slice should die on return
435 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
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]+$"
441 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 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]+$"
445 // map iterator should die on end of range loop
449 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
450 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]+$"
456 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
457 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
461 // copy of array of pointers should die at end of range loop
466 // two live temps during print(p):
467 // the copy of ptrarr and the internal iterator pointer.
469 for _, p := range ptrarr {
470 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]+$"
476 for _, p := range ptrarr {
477 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
481 // conversion to interface should not leave temporary behind
483 func f31(b1, b2, b3 bool) {
485 g31("a") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to g31: autotmp_[0-9]+$"
488 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]+$"
491 panic("asdf") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to gopanic: autotmp_[0-9]+$"
496 func g31(interface{})
497 func h31(...interface{})
499 // non-escaping partial functions passed to function call should die on return
503 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
511 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
513 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
514 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
520 // temporaries introduced during if conditions and && || expressions
521 // should die once the condition has been acted upon.
523 var m33 map[interface{}]int
526 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
536 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
544 if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
552 if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
560 if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
567 // select temps should disappear in the case bodies
571 func fc38() chan string
572 func fi38(int) *string
576 // we don't care what temps are printed on the lines with output.
577 // we care that the println lines have no live variables
578 // and therefore no output.
580 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]+$"
581 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]+$"
583 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]+$"
585 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]+$"
587 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]+$"
595 // issue 8097: mishandling of x = x during return.
597 func f39() (x []int) {
599 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+$"
603 func f39a() (x []int) {
605 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+$"
609 func f39b() (x [10]*int) {
611 x[0] = new(int) // ERROR "live at call to newobject: x$"
612 printnl() // ERROR "live at call to printnl: x$"
616 func f39c() (x [10]*int) {
618 x[0] = new(int) // ERROR "live at call to newobject: x$"
619 printnl() // ERROR "live at call to printnl: x$"
623 // issue 8142: lost 'addrtaken' bit on inlined variables.
624 // no inlining in this test, so just checking that non-inlined works.
632 ret.m = make(map[int]int) // ERROR "live at call to makemap: &ret$"
644 ret.m = make(map[int]int) // ERROR "live at call to makemap: autotmp_[0-9]+ ret$"
646 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ ret$"