github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/test/inline.go (about)

     1  // errorcheck -0 -m
     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, using compiler diagnostic flags, that inlining is working.
     8  // Compiles but does not run.
     9  
    10  package foo
    11  
    12  import "unsafe"
    13  
    14  func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
    15  	return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
    16  }
    17  
    18  func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
    19  	return unsafe.Pointer(uintptr(p) + x)
    20  }
    21  
    22  func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
    23  	return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
    24  }
    25  
    26  //go:noinline
    27  func g(x int) int {
    28  	return x + 1
    29  }
    30  
    31  func h(x int) int { // ERROR "can inline h"
    32  	return x + 2
    33  }
    34  
    35  func i(x int) int { // ERROR "can inline i"
    36  	const y = 2
    37  	return x + y
    38  }
    39  
    40  func j(x int) int { // ERROR "can inline j"
    41  	switch {
    42  	case x > 0:
    43  		return x + 2
    44  	default:
    45  		return x + 1
    46  	}
    47  }
    48  
    49  // can't currently inline functions with a break statement
    50  func switchBreak(x, y int) int {
    51  	var n int
    52  	switch x {
    53  	case 0:
    54  		n = 1
    55  	Done:
    56  		switch y {
    57  		case 0:
    58  			n += 10
    59  			break Done
    60  		}
    61  		n = 2
    62  	}
    63  	return n
    64  }
    65  
    66  // can't currently inline functions with a type switch
    67  func switchType(x interface{}) int { // ERROR "switchType x does not escape"
    68  	switch x.(type) {
    69  	case int:
    70  		return x.(int)
    71  	default:
    72  		return 0
    73  	}
    74  }