fix: correct control flow graph for some switch statements

This commit is contained in:
Marc Vertes
2020-03-09 18:20:04 +01:00
committed by GitHub
parent d29b0a48ff
commit 6e33f89146
5 changed files with 73 additions and 15 deletions

15
_test/switch23.go Normal file
View File

@@ -0,0 +1,15 @@
package main
func getType() string { return "T1" }
func main() {
switch getType() {
case "T1":
println("T1")
default:
println("default")
}
}
// Output:
// T1

16
_test/switch24.go Normal file
View File

@@ -0,0 +1,16 @@
package main
func main() {
a := 3
switch a + 2 {
case 5:
println(5)
default:
println("default")
}
println("bye")
}
// Output:
// 5
// bye

18
_test/switch25.go Normal file
View File

@@ -0,0 +1,18 @@
package main
func main() {
a := 2
switch {
case a == 1:
println(1)
case a == 2:
println(2)
default:
println("default")
}
println("bye")
}
// Output:
// 2
// bye

18
_test/switch26.go Normal file
View File

@@ -0,0 +1,18 @@
package main
func main() {
a := 1
switch a := 2; {
case a == 1:
println(1)
case a == 2:
println(2)
default:
println("default")
}
println(a)
}
// Output:
// 2
// 1

View File

@@ -1326,14 +1326,8 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
}
c := clauses[l-1]
c.tnext = c.lastChild().start
if n.child[0].action == aAssign &&
(n.child[0].child[0].kind != typeAssertExpr || len(n.child[0].child[0].child) > 1) {
// switch init statement is defined
n.start = n.child[0].start
n.child[0].tnext = sbn.start
} else {
n.start = sbn.start
}
n.start = n.child[0].start
n.child[0].tnext = sbn.start
sc = sc.pop()
case switchIfStmt: // like an if-else chain
@@ -1360,16 +1354,13 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
// If last case body statement is a fallthrough, then jump to next case body
if i < l-1 && len(body.child) > 0 && body.lastChild().kind == fallthroughtStmt {
body.tnext = clauses[i+1].lastChild().start
} else {
body.tnext = n
}
}
sbn.start = clauses[0].start
if n.child[0].action == aAssign {
// switch init statement is defined
n.start = n.child[0].start
n.child[0].tnext = sbn.start
} else {
n.start = sbn.start
}
n.start = n.child[0].start
n.child[0].tnext = sbn.start
sc = sc.pop()
case typeAssertExpr: