2015-02-24 10:19:01 -07:00
|
|
|
// errorcheck -0 -m
|
|
|
|
|
2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-02-24 10:19:01 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Test, using compiler diagnostic flags, that inlining is working.
|
|
|
|
// Compiles but does not run.
|
|
|
|
|
|
|
|
package foo
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
|
|
|
|
return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
|
|
|
|
}
|
|
|
|
|
|
|
|
func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
|
|
|
|
return unsafe.Pointer(uintptr(p) + x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
|
|
|
|
return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
|
|
|
|
}
|
2015-08-24 18:45:59 -06:00
|
|
|
|
|
|
|
//go:noinline
|
|
|
|
func g(x int) int {
|
|
|
|
return x + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func h(x int) int { // ERROR "can inline h"
|
|
|
|
return x + 2
|
|
|
|
}
|
2016-03-16 17:44:17 -06:00
|
|
|
|
|
|
|
func i(x int) int { // ERROR "can inline i"
|
|
|
|
const y = 2
|
|
|
|
return x + y
|
|
|
|
}
|
2016-03-16 20:29:17 -06:00
|
|
|
|
|
|
|
func j(x int) int { // ERROR "can inline j"
|
|
|
|
switch {
|
|
|
|
case x > 0:
|
|
|
|
return x + 2
|
|
|
|
default:
|
|
|
|
return x + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't currently inline functions with a break statement
|
|
|
|
func switchBreak(x, y int) int {
|
|
|
|
var n int
|
|
|
|
switch x {
|
|
|
|
case 0:
|
|
|
|
n = 1
|
|
|
|
Done:
|
|
|
|
switch y {
|
|
|
|
case 0:
|
|
|
|
n += 10
|
|
|
|
break Done
|
|
|
|
}
|
|
|
|
n = 2
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't currently inline functions with a type switch
|
|
|
|
func switchType(x interface{}) int { // ERROR "switchType x does not escape"
|
|
|
|
switch x.(type) {
|
|
|
|
case int:
|
|
|
|
return x.(int)
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|