From: Dan Scales Date: Wed, 20 Oct 2021 20:56:15 +0000 (-0700) Subject: cmd/compile: fix inlining of labeled for loops X-Git-Tag: go1.18beta1~805 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b8da7e4c4c9e97b3d1ebfb07506acdd61277b988;p=gostls13.git cmd/compile: fix inlining of labeled for loops There is already a mechanism using inlgen to rename labels insided inlined functions so that they are unique and don't clash with loops in the outer function. This is used for OLABEL and OGOTO. Now that we are doing inlining of OFOR loops, we need to do this translation for OBREAK, OCONTINUE, and OFOR. I also added the translation for ORANGE loops, in anticipation of a CL that will allow inlining of ORANGE for loops. Fixes #49100 Change-Id: I2ccddc3350370825c386965f4a1e4bc54d3c369b Reviewed-on: https://go-review.googlesource.com/c/go/+/357649 Run-TryBot: Dan Scales TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky Trust: Dan Scales --- diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index a2268a5465..fb6cf53155 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -1223,7 +1223,7 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { // Don't do special substitutions if inside a closure break } - // Since we don't handle bodies with closures, + // Because of the above test for subst.newclofn, // this return is guaranteed to belong to the current inlined function. n := n.(*ir.ReturnStmt) init := subst.list(n.Init()) @@ -1251,7 +1251,7 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { typecheck.Stmts(init) return ir.NewBlockStmt(base.Pos, init) - case ir.OGOTO: + case ir.OGOTO, ir.OBREAK, ir.OCONTINUE: if subst.newclofn != nil { // Don't do special substitutions if inside a closure break @@ -1260,8 +1260,7 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { m := ir.Copy(n).(*ir.BranchStmt) m.SetPos(subst.updatedPos(m.Pos())) *m.PtrInit() = nil - p := fmt.Sprintf("%s·%d", n.Label.Name, inlgen) - m.Label = typecheck.Lookup(p) + m.Label = translateLabel(n.Label) return m case ir.OLABEL: @@ -1273,8 +1272,7 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { m := ir.Copy(n).(*ir.LabelStmt) m.SetPos(subst.updatedPos(m.Pos())) *m.PtrInit() = nil - p := fmt.Sprintf("%s·%d", n.Label.Name, inlgen) - m.Label = typecheck.Lookup(p) + m.Label = translateLabel(n.Label) return m case ir.OCLOSURE: @@ -1286,6 +1284,21 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { m.SetPos(subst.updatedPos(m.Pos())) ir.EditChildren(m, subst.edit) + if subst.newclofn == nil { + // Translate any label on FOR or RANGE loops + if m.Op() == ir.OFOR { + m := m.(*ir.ForStmt) + m.Label = translateLabel(m.Label) + return m + } + + if m.Op() == ir.ORANGE { + m := m.(*ir.RangeStmt) + m.Label = translateLabel(m.Label) + return m + } + } + switch m := m.(type) { case *ir.AssignStmt: if lhs, ok := m.X.(*ir.Name); ok && lhs.Defn == &subst.defnMarker { @@ -1302,6 +1315,16 @@ func (subst *inlsubst) node(n ir.Node) ir.Node { return m } +// translateLabel makes a label from an inlined function (if non-nil) be unique by +// adding "·inlgen". +func translateLabel(l *types.Sym) *types.Sym { + if l == nil { + return nil + } + p := fmt.Sprintf("%s·%d", l.Name, inlgen) + return typecheck.Lookup(p) +} + func (subst *inlsubst) updatedPos(xpos src.XPos) src.XPos { if subst.noPosUpdate { return xpos diff --git a/test/fixedbugs/issue49100.go b/test/fixedbugs/issue49100.go new file mode 100644 index 0000000000..3a2e972751 --- /dev/null +++ b/test/fixedbugs/issue49100.go @@ -0,0 +1,27 @@ +// run + +// Copyright 2015 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: + for i := 0; i < 4; i++ { + if i == 1 { + continue loop + } + println(j, i) + } +} + +func main() { +loop: + for j := 0; j < 5; j++ { + f(j) + if j == 3 { + break loop + } + } +} diff --git a/test/fixedbugs/issue49100.out b/test/fixedbugs/issue49100.out new file mode 100644 index 0000000000..326d41365a --- /dev/null +++ b/test/fixedbugs/issue49100.out @@ -0,0 +1,12 @@ +0 0 +0 2 +0 3 +1 0 +1 2 +1 3 +2 0 +2 2 +2 3 +3 0 +3 2 +3 3