fix: correct branch action in parenthesis expression

This commit is contained in:
Marc Vertes
2020-03-26 12:10:04 +01:00
committed by GitHub
parent 7327ff2811
commit e78650d359
2 changed files with 27 additions and 4 deletions

15
_test/if7.go Normal file
View File

@@ -0,0 +1,15 @@
package main
func main() {
a := 0
b := false
if (b) {
a = 1
} else {
a = -1
}
println(a)
}
// Output:
// -1

View File

@@ -1004,10 +1004,7 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
}
}
if sym.kind == varSym && sym.typ != nil && sym.typ.TypeOf().Kind() == reflect.Bool {
switch n.anc.kind {
case ifStmt0, ifStmt1, ifStmt2, ifStmt3, forStmt1, forStmt2, forStmt3, forStmt4:
n.gen = branch
}
fixBranch(n)
}
}
if n.sym != nil {
@@ -1668,6 +1665,17 @@ func genRun(nod *node) error {
return err
}
// FixBranch sets the branch action to the identExpr node if it is a bool
// used in a conditional expression.
func fixBranch(n *node) {
switch n.anc.kind {
case ifStmt0, ifStmt1, ifStmt2, ifStmt3, forStmt1, forStmt2, forStmt3, forStmt4:
n.gen = branch
case parenExpr:
fixBranch(n.anc)
}
}
// GetDefault return the index of default case clause in a switch statement, or -1.
func getDefault(n *node) int {
for i, c := range n.lastChild().child {