Source file test/escape_level.go

     1  // errorcheck -0 -m -l
     2  
     3  // Copyright 2015 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 indirection level computation in escape analysis.
     8  
     9  package escape
    10  
    11  var sink interface{}
    12  
    13  func level0() {
    14  	i := 0     // ERROR "moved to heap: i"
    15  	p0 := &i   // ERROR "moved to heap: p0"
    16  	p1 := &p0  // ERROR "moved to heap: p1"
    17  	p2 := &p1  // ERROR "moved to heap: p2"
    18  	sink = &p2
    19  }
    20  
    21  func level1() {
    22  	i := 0    // ERROR "moved to heap: i"
    23  	p0 := &i  // ERROR "moved to heap: p0"
    24  	p1 := &p0 // ERROR "moved to heap: p1"
    25  	p2 := &p1
    26  	sink = p2
    27  }
    28  
    29  func level2() {
    30  	i := 0     // ERROR "moved to heap: i"
    31  	p0 := &i   // ERROR "moved to heap: p0"
    32  	p1 := &p0
    33  	p2 := &p1
    34  	sink = *p2
    35  }
    36  
    37  func level3() {
    38  	i := 0      // ERROR "moved to heap: i"
    39  	p0 := &i
    40  	p1 := &p0
    41  	p2 := &p1
    42  	sink = **p2
    43  }
    44  
    45  func level4() {
    46  	i := 0     // ERROR "moved to heap: i"
    47  	p0 := &i   // ERROR "moved to heap: p0"
    48  	p1 := &p0
    49  	p2 := p1   // ERROR "moved to heap: p2"
    50  	sink = &p2
    51  }
    52  
    53  func level5() {
    54  	i := 0    // ERROR "moved to heap: i"
    55  	p0 := &i  // ERROR "moved to heap: p0"
    56  	p1 := &p0
    57  	p2 := p1
    58  	sink = p2
    59  }
    60  
    61  func level6() {
    62  	i := 0    // ERROR "moved to heap: i"
    63  	p0 := &i
    64  	p1 := &p0
    65  	p2 := p1
    66  	sink = *p2
    67  }
    68  
    69  func level7() {
    70  	i := 0    // ERROR "moved to heap: i"
    71  	p0 := &i
    72  	p1 := &p0
    73  	// note *p1 == &i
    74  	p2 := *p1  // ERROR "moved to heap: p2"
    75  	sink = &p2
    76  }
    77  
    78  func level8() {
    79  	i := 0    // ERROR "moved to heap: i"
    80  	p0 := &i
    81  	p1 := &p0
    82  	p2 := *p1
    83  	sink = p2
    84  }
    85  
    86  func level9() {
    87  	i := 0
    88  	p0 := &i
    89  	p1 := &p0
    90  	p2 := *p1
    91  	sink = *p2 // ERROR "\*p2 escapes to heap"
    92  }
    93  
    94  func level10() {
    95  	i := 0
    96  	p0 := &i
    97  	p1 := *p0
    98  	p2 := &p1
    99  	sink = *p2 // ERROR "\*p2 escapes to heap"
   100  }
   101  
   102  func level11() {
   103  	i := 0
   104  	p0 := &i
   105  	p1 := &p0
   106  	p2 := **p1 // ERROR "moved to heap: p2"
   107  	sink = &p2
   108  }
   109  

View as plain text