Skip to content

Commit

Permalink
cmd/compile: avoid generating unreachable branch for select cases
Browse files Browse the repository at this point in the history
Fixes #50823

Change-Id: I1c12e875b840eecadefb0d9e044ff2a268ccfbaa
Reviewed-on: https://go-review.googlesource.com/c/go/+/380894
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
zhouguangyuan0718 authored and randall77 committed Mar 1, 2022
1 parent d81464e commit d9fd920
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/cmd/compile/internal/walk/select.go
Expand Up @@ -239,21 +239,28 @@ func walkSelectCases(cases []*ir.CommClause) []ir.Node {

// dispatch cases
dispatch := func(cond ir.Node, cas *ir.CommClause) {
cond = typecheck.Expr(cond)
cond = typecheck.DefaultLit(cond, nil)

r := ir.NewIfStmt(base.Pos, cond, nil, nil)
var list ir.Nodes

if n := cas.Comm; n != nil && n.Op() == ir.OSELRECV2 {
n := n.(*ir.AssignListStmt)
if !ir.IsBlank(n.Lhs[1]) {
x := ir.NewAssignStmt(base.Pos, n.Lhs[1], recvOK)
r.Body.Append(typecheck.Stmt(x))
list.Append(typecheck.Stmt(x))
}
}

r.Body.Append(cas.Body.Take()...)
r.Body.Append(ir.NewBranchStmt(base.Pos, ir.OBREAK, nil))
list.Append(cas.Body.Take()...)
list.Append(ir.NewBranchStmt(base.Pos, ir.OBREAK, nil))

var r ir.Node
if cond != nil {
cond = typecheck.Expr(cond)
cond = typecheck.DefaultLit(cond, nil)
r = ir.NewIfStmt(base.Pos, cond, list, nil)
} else {
r = ir.NewBlockStmt(base.Pos, list)
}

init = append(init, r)
}

Expand All @@ -263,6 +270,10 @@ func walkSelectCases(cases []*ir.CommClause) []ir.Node {
}
for i, cas := range casorder {
ir.SetPos(cas)
if i == len(casorder)-1 {
dispatch(nil, cas)
break
}
dispatch(ir.NewBinaryExpr(base.Pos, ir.OEQ, chosen, ir.NewInt(int64(i))), cas)
}

Expand Down

0 comments on commit d9fd920

Please sign in to comment.