mirror of
https://github.com/golang/go
synced 2024-11-02 09:28:34 +00:00
cmd/compile: allow inlining of functions with switch statements
Allow inlining of functions with switch statements as long as they don't contain a break or type switch. Fixes #13071 Change-Id: I057be351ea4584def1a744ee87eafa5df47a7f6d Reviewed-on: https://go-review.googlesource.com/20824 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
a14537816e
commit
e41f527f4d
3 changed files with 39 additions and 2 deletions
|
@ -888,7 +888,7 @@ func stmtfmt(n *Node) string {
|
|||
f += fmt.Sprintf(" %v;", n.Ninit.First())
|
||||
}
|
||||
if n.Left != nil {
|
||||
f += Nconv(n.Left, 0)
|
||||
f += fmt.Sprintf(" %s ", Nconv(n.Left, 0))
|
||||
}
|
||||
|
||||
f += fmt.Sprintf(" { %v }", n.List)
|
||||
|
|
|
@ -214,10 +214,11 @@ func ishairy(n *Node, budget *int) bool {
|
|||
ORANGE,
|
||||
OFOR,
|
||||
OSELECT,
|
||||
OSWITCH,
|
||||
OTYPESW,
|
||||
OPROC,
|
||||
ODEFER,
|
||||
ODCLTYPE, // can't print yet
|
||||
OBREAK,
|
||||
ORETJMP:
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -36,3 +36,39 @@ func i(x int) int { // ERROR "can inline i"
|
|||
const y = 2
|
||||
return x + y
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue