]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: avoid generating unreachable branch for select cases
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Wed, 26 Jan 2022 12:23:11 +0000 (20:23 +0800)
committerKeith Randall <khr@golang.org>
Tue, 1 Mar 2022 18:13:15 +0000 (18:13 +0000)
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>

src/cmd/compile/internal/walk/select.go

index fde8f5089590fbc2984a6fb2c98d2d1e0e0412f0..5cea66f5ff64a0f440f34493ccec1ea607be5ce0 100644 (file)
@@ -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)
        }
 
@@ -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)
        }