Permalink
Browse files

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>
  • Loading branch information...
tzneal committed Mar 17, 2016
1 parent a145378 commit e41f527f4d56a94b33ab73efaae3575760916194
Showing with 39 additions and 2 deletions.
  1. +1 −1 src/cmd/compile/internal/gc/fmt.go
  2. +2 −1 src/cmd/compile/internal/gc/inl.go
  3. +36 −0 test/inline.go
@@ -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
}
View
@@ -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
}
}

0 comments on commit e41f527

Please sign in to comment.