1 // errorcheckwithauto -0 -l -live -wb=0
2 // +build !ppc64,!ppc64le
3 // ppc64 needs a better tighten pass to make f18 pass
5 // Copyright 2014 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
9 // liveness tests with inlining disabled.
17 func printpointer(**int)
20 func printintpointer(*int)
23 func printstringpointer(*string)
26 func printstring(string)
29 func printbytepointer(*byte)
35 printpointer(&x) // ERROR "live at call to printpointer: x$"
36 printpointer(&x) // ERROR "live at call to printpointer: x$"
41 printint(0) // nothing live here
45 printpointer(&x) // ERROR "live at call to printpointer: x$"
46 printpointer(&x) // ERROR "live at call to printpointer: x$"
49 func f3(b1, b2 bool) {
50 // Here x and y are ambiguously live. In previous go versions they
51 // were marked as live throughout the function to avoid being
52 // poisoned in GODEBUG=gcdead=1 mode; this is now no longer the
63 printpointer(&x) // ERROR "live at call to printpointer: x$"
64 printpointer(&x) // ERROR "live at call to printpointer: x$"
67 printpointer(&y) // ERROR "live at call to printpointer: y$"
68 printpointer(&y) // ERROR "live at call to printpointer: y$"
70 printint(0) // ERROR "f3: x \(type \*int\) is ambiguously live$" "f3: y \(type \*int\) is ambiguously live$" "live at call to printint: x y$"
73 // The old algorithm treated x as live on all code that
74 // could flow to a return statement, so it included the
75 // function entry and code above the declaration of x
76 // but would not include an indirect use of x in an infinite loop.
77 // Check that these cases are handled correctly.
79 func f4(b1, b2 bool) { // x not live here
81 printint(0) // x not live here
88 printint(**z) // ERROR "live at call to printint: x$"
90 printint(1) // x not live here
94 printint(**z) // ERROR "live at call to printint: x$"
109 printint(**z) // ERROR "f5: x \(type \*int\) is ambiguously live$" "f5: y \(type \*int\) is ambiguously live$" "live at call to printint: x y$"
112 // confusion about the _ result used to cause spurious "live at entry to f6: _".
114 func f6() (_, y string) {
119 // confusion about addressed results used to cause "live at entry to f7: x".
121 func f7() (x string) {
127 // ignoring block returns used to cause "live at entry to f8: x, y".
129 func f8() (x, y string) {
133 func g8() (string, string)
135 // ignoring block assignments used to cause "live at entry to f9: x"
143 y := interface{}(99.0i) // ERROR "live at call to convT2E: x.data x.type$"
144 i9 = y // make y escape so the line above has to call convT2E
148 // liveness formerly confused by UNDEF followed by RET,
149 // leading to "live at entry to f10: ~r1" (unnamed result).
155 // liveness formerly confused by select, thinking runtime.selectgo
156 // can return to next instruction; it always jumps elsewhere.
157 // note that you have to use at least two cases in the select
158 // to get a true select; smaller selects compile to optimized helper functions.
163 // this used to have a spurious "live at entry to f11a: ~r0"
165 select { // ERROR "live at call to newselect: autotmp_[0-9]+$" "live at call to selectgo: autotmp_[0-9]+$"
166 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
168 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
176 // At this point p is dead: the code here cannot
177 // get to the bottom of the function.
178 // This used to have a spurious "live at call to printint: p".
179 printint(1) // nothing live here!
180 select { // ERROR "live at call to newselect: autotmp_[0-9]+$" "live at call to selectgo: autotmp_[0-9]+$"
181 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
183 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+$"
195 sink = p // prevent stack allocation, otherwise p is rematerializeable
197 // Unlike previous, the cases in this select fall through,
198 // so we can get to the println, so p is not dead.
199 printint(1) // ERROR "live at call to printint: p$"
200 select { // ERROR "live at call to newselect: autotmp_[0-9]+ p$" "live at call to selectgo: autotmp_[0-9]+ p$"
201 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
202 case <-c: // ERROR "live at call to selectrecv: autotmp_[0-9]+ p$"
209 // similarly, select{} does not fall through.
210 // this used to have a spurious "live at entry to f12: ~r0".
220 // incorrectly placed VARDEF annotations can cause missing liveness annotations.
221 // this used to be missing the fact that s is live during the call to g13 (because it is
222 // needed for the call to h13).
226 s = h13(s, g13(s)) // ERROR "live at call to g13: s.ptr$"
229 func g13(string) string
230 func h13(string, string) string
232 // more incorrectly placed VARDEF.
236 printstringpointer(&x) // ERROR "live at call to printstringpointer: x$"
244 x = g15() // ERROR "live at call to g15: x$"
245 printstring(x) // ERROR "live at call to printstring: x$"
250 // Checking that various temporaries do not persist or cause
251 // ambiguously live values that must be zeroed.
252 // The exact temporary names are inconsequential but we are
253 // trying to check that there is only one at any given site,
254 // and also that none show up in "ambiguously live" messages.
260 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
262 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
263 delete(m, "hi") // ERROR "live at call to mapdelete: autotmp_[0-9]+$"
266 var m2s map[string]*byte
267 var m2 map[[2]string]*byte
272 // value temporary only
274 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
276 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
277 m2[x2] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
281 // key temporary only
283 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
285 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
286 m2s["x"] = bp // ERROR "live at call to mapassign1: autotmp_[0-9]+$"
290 // key and value temporaries
292 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
294 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
295 m2s["x"] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
301 // key temporary for mapaccess.
302 // temporary introduced by orderexpr.
305 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
307 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
308 z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
315 // dest temporary for channel receive.
319 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
321 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
322 z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
327 // src temporary for channel send
329 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
331 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
332 ch <- nil // ERROR "live at call to chansend1: autotmp_[0-9]+$"
336 // key temporary for mapaccess using array literal key.
339 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
341 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
342 z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
347 // key temporary for two-result map access using array literal key.
351 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
353 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
354 z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
360 // key temporary for map access using array literal key.
361 // value temporary too.
363 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
365 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
366 m2[[2]string{"x", "y"}] = nil // ERROR "live at call to mapassign1: autotmp_[0-9]+ autotmp_[0-9]+$"
369 // defer should not cause spurious ambiguously live variables
378 x = g15() // ERROR "live at call to g15: x$"
379 printstring(x) // ERROR "live at call to printstring: x$"
380 } // ERROR "live at call to deferreturn: x$"
384 // non-escaping ... slices passed to function call should die on return,
385 // so that the temporaries do not stack and do not cause ambiguously
390 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
392 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
393 print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
398 func print26(...interface{})
400 // non-escaping closures passed to function call should die on return
405 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
407 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
408 call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
412 // but defer does escape to later execution in the function
414 func f27defer(b bool) {
417 defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+$"
419 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]+$"
420 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ autotmp_[0-9]+$"
421 } // ERROR "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$"
423 // and newproc (go) escapes to the heap
428 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$" "live at call to newproc: &x$"
430 go call27(func() { x++ }) // ERROR "live at call to newobject: &x$"
437 // concatstring slice should die on return
439 var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
443 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 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]+$"
446 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]+$"
449 // map iterator should die on end of range loop
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]+$"
457 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
458 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
460 for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
461 printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
465 // copy of array of pointers should die at end of range loop
470 // two live temps during print(p):
471 // the copy of ptrarr and the internal iterator pointer.
473 for _, p := range ptrarr {
474 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
477 for _, p := range ptrarr {
478 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
480 for _, p := range ptrarr {
481 printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
485 // conversion to interface should not leave temporary behind
487 func f31(b1, b2, b3 bool) {
489 g31("a") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to g31: autotmp_[0-9]+$"
492 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]+$"
495 panic("asdf") // ERROR "live at call to convT2E: autotmp_[0-9]+$" "live at call to gopanic: autotmp_[0-9]+$"
500 func g31(interface{})
501 func h31(...interface{})
503 // non-escaping partial functions passed to function call should die on return
507 func (t *T32) Inc() { // ERROR "live at entry to \(\*T32\).Inc: t$"
515 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
517 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
518 call32(t32.Inc) // ERROR "live at call to call32: autotmp_[0-9]+$"
524 // temporaries introduced during if conditions and && || expressions
525 // should die once the condition has been acted upon.
527 var m33 map[interface{}]int
530 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
540 if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
548 if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
556 if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
564 if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
571 // select temps should disappear in the case bodies
575 func fc38() chan string
576 func fi38(int) *string
580 // we don't care what temps are printed on the lines with output.
581 // we care that the println lines have no live variables
582 // and therefore no output.
584 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]+$"
585 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]+$"
587 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]+$"
589 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]+$"
591 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]+$"
599 // issue 8097: mishandling of x = x during return.
601 func f39() (x []int) {
603 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+$"
607 func f39a() (x []int) {
609 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+$"
613 func f39b() (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 func f39c() (x [10]*int) {
622 x[0] = new(int) // ERROR "live at call to newobject: x$"
623 printnl() // ERROR "live at call to printnl: x$"
627 // issue 8142: lost 'addrtaken' bit on inlined variables.
628 // no inlining in this test, so just checking that non-inlined works.
636 ret.m = make(map[int]int) // ERROR "live at call to makemap: &ret$"
648 ret.m = make(map[int]int) // ERROR "live at call to makemap: autotmp_[0-9]+ ret$"
650 printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ ret$"
654 func ddd1(x, y *int) { // ERROR "live at entry to ddd1: x y$"
655 ddd2(x, y) // ERROR "live at call to ddd2: autotmp_[0-9]+$"
657 // Note: no autotmp live at printnl. See issue 16996.
659 func ddd2(a ...*int) { // ERROR "live at entry to ddd2: a$"
663 // issue 16016: autogenerated wrapper should have arguments live
666 func (*T) Foo(ptr *int) {}
668 type R struct{ *T } // ERRORAUTO "live at entry to \(\*R\)\.Foo: \.this ptr" "live at entry to R\.Foo: \.this ptr"