golang.org/x/tools@v0.21.0/internal/refactor/inline/testdata/tailcall.txtar (about)

     1  Reduction of parameterless tail-call to functions.
     2  
     3  1. a0 (sum) is reduced, despite the complexity of the callee.
     4  
     5  2. a1 (conflict) is not reduced, because the caller and callee have
     6     intersecting sets of labels.
     7  
     8  3. a2 (usesResult) is not reduced, because it refers to a result variable.
     9  
    10  -- go.mod --
    11  module testdata
    12  go 1.12
    13  
    14  -- a/a0.go --
    15  package a
    16  
    17  func _() int {
    18  	return sum(1, 2) //@ inline(re"sum", sum)
    19  }
    20  
    21  func sum(lo, hi int) int {
    22  	total := 0
    23  start:
    24  	for i := lo; i <= hi; i++ {
    25  		total += i
    26  		if i == 6 {
    27  			goto start
    28  		} else if i == 7 {
    29  			return -1
    30  		}
    31  	}
    32  	return total
    33  }
    34  
    35  -- sum --
    36  package a
    37  
    38  func _() int {
    39  	total := 0
    40  start:
    41  	for i := 1; i <= 2; i++ {
    42  		total += i
    43  		if i == 6 {
    44  			goto start
    45  		} else if i == 7 {
    46  			return -1
    47  		}
    48  	}
    49  	return total //@ inline(re"sum", sum)
    50  }
    51  
    52  func sum(lo, hi int) int {
    53  	total := 0
    54  start:
    55  	for i := lo; i <= hi; i++ {
    56  		total += i
    57  		if i == 6 {
    58  			goto start
    59  		} else if i == 7 {
    60  			return -1
    61  		}
    62  	}
    63  	return total
    64  }
    65  
    66  -- a/a1.go --
    67  package a
    68  
    69  func _() int {
    70  	hello:
    71  	return conflict(1, 2) //@ inline(re"conflict", conflict)
    72  	goto hello
    73  }
    74  
    75  func conflict(lo, hi int) int {
    76  hello:
    77  	return lo + hi
    78  }
    79  
    80  -- conflict --
    81  package a
    82  
    83  func _() int {
    84  hello:
    85  	return func() int {
    86  	hello:
    87  		return 1 + 2
    88  	}() //@ inline(re"conflict", conflict)
    89  	goto hello
    90  }
    91  
    92  func conflict(lo, hi int) int {
    93  hello:
    94  	return lo + hi
    95  }
    96  
    97  -- a/a2.go --
    98  package a
    99  
   100  func _() int {
   101  	return usesResult(1, 2) //@ inline(re"usesResult", usesResult)
   102  }
   103  
   104  func usesResult(lo, hi int) (z int) {
   105  	z = y + x
   106  	return
   107  }
   108  
   109  -- usesResult --
   110  package a
   111  
   112  func _() int {
   113  	return func() (z int) { z = y + x; return }() //@ inline(re"usesResult", usesResult)
   114  }
   115  
   116  func usesResult(lo, hi int) (z int) {
   117  	z = y + x
   118  	return
   119  }
   120