]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix inlining labeled switch statements
authorwdvxdr <wdvxdr1123@gmail.com>
Mon, 25 Oct 2021 09:54:11 +0000 (17:54 +0800)
committerDan Scales <danscales@google.com>
Mon, 25 Oct 2021 16:07:11 +0000 (16:07 +0000)
CL 357649 fixes inlining labeled FOR/RANGE loops,
we should do same translation for inlined SWITCH's label

Fixes #49145

Change-Id: I9a6f365f57e974271a1eb279b38e81f9b5148788
Reviewed-on: https://go-review.googlesource.com/c/go/+/358315
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/inline/inl.go
test/fixedbugs/issue49145.go [new file with mode: 0644]
test/fixedbugs/issue49145.out [new file with mode: 0644]

index fb6cf53155be572c58597a7ef03cd4d9ddfbc34b..1ae6a58de062766cc17ecb2afc85af91f44a904e 100644 (file)
@@ -1285,18 +1285,24 @@ func (subst *inlsubst) node(n ir.Node) ir.Node {
        ir.EditChildren(m, subst.edit)
 
        if subst.newclofn == nil {
-               // Translate any label on FOR or RANGE loops
-               if m.Op() == ir.OFOR {
+               // Translate any label on FOR, RANGE loops or SWITCH
+               switch m.Op() {
+               case ir.OFOR:
                        m := m.(*ir.ForStmt)
                        m.Label = translateLabel(m.Label)
                        return m
-               }
 
-               if m.Op() == ir.ORANGE {
+               case ir.ORANGE:
                        m := m.(*ir.RangeStmt)
                        m.Label = translateLabel(m.Label)
                        return m
+
+               case ir.OSWITCH:
+                       m := m.(*ir.SwitchStmt)
+                       m.Label = translateLabel(m.Label)
+                       return m
                }
+
        }
 
        switch m := m.(type) {
diff --git a/test/fixedbugs/issue49145.go b/test/fixedbugs/issue49145.go
new file mode 100644 (file)
index 0000000..d666681
--- /dev/null
@@ -0,0 +1,27 @@
+// run
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func f(j int) {
+loop:
+       switch j {
+       case 1:
+               break loop
+       default:
+               println(j)
+       }
+}
+
+func main() {
+loop:
+       for j := 0; j < 5; j++ {
+               f(j)
+               if j == 3 {
+                       break loop
+               }
+       }
+}
diff --git a/test/fixedbugs/issue49145.out b/test/fixedbugs/issue49145.out
new file mode 100644 (file)
index 0000000..1556c06
--- /dev/null
@@ -0,0 +1,3 @@
+0
+2
+3