Skip to content

Commit d310b2a

Browse files
committed
cmd/compile: set correct Defn for inlined vars
Currently, when copying definition node of an inlined var, we do not update var Defn field to point to new copied node. That causes all inlined vars point to the same Defn, and ir.StaticValue can not find inlined var in the lhs of its definition. clovar creates new ONAME node for local variables or params of closure inside inlined function, by copying most of the old node fields. So the new Node.Defn is not modified, its lhs still refer to old node instead of new one. To fix this, we need to do two things: - In subst.clovar, set a dummy Defn node for inlvar - During subst.node, when seeing OAS/OAS2 nodes, after substituting, we check if any node in lhs has the dummy Defn, then set it to the current OAS/OAS2 node. Fixes #45606 Change-Id: Ib517b753a7643756dcd61d36deae60f1a0fc53c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/312630 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 1b0a031 commit d310b2a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ func mkinlcall(n *ir.CallExpr, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]b
10021002
retvars: retvars,
10031003
delayretvars: delayretvars,
10041004
inlvars: inlvars,
1005+
defnMarker: ir.NilExpr{},
10051006
bases: make(map[*src.PosBase]*src.PosBase),
10061007
newInlIndex: newIndex,
10071008
fn: fn,
@@ -1103,6 +1104,10 @@ type inlsubst struct {
11031104
delayretvars bool
11041105

11051106
inlvars map[*ir.Name]*ir.Name
1107+
// defnMarker is used to mark a Node for reassignment.
1108+
// inlsubst.clovar set this during creating new ONAME.
1109+
// inlsubst.node will set the correct Defn for inlvar.
1110+
defnMarker ir.NilExpr
11061111

11071112
// bases maps from original PosBase to PosBase with an extra
11081113
// inlined call frame.
@@ -1160,7 +1165,11 @@ func (subst *inlsubst) clovar(n *ir.Name) *ir.Name {
11601165
m := &ir.Name{}
11611166
*m = *n
11621167
m.Curfn = subst.newclofn
1163-
if n.Defn != nil && n.Defn.Op() == ir.ONAME {
1168+
1169+
switch defn := n.Defn.(type) {
1170+
case nil:
1171+
// ok
1172+
case *ir.Name:
11641173
if !n.IsClosureVar() {
11651174
base.FatalfAt(n.Pos(), "want closure variable, got: %+v", n)
11661175
}
@@ -1182,7 +1191,13 @@ func (subst *inlsubst) clovar(n *ir.Name) *ir.Name {
11821191
if subst.inlvars[n.Defn.(*ir.Name)] != nil {
11831192
m.Defn = subst.node(n.Defn)
11841193
}
1194+
case *ir.AssignStmt, *ir.AssignListStmt:
1195+
// Mark node for reassignment at the end of inlsubst.node.
1196+
m.Defn = &subst.defnMarker
1197+
default:
1198+
base.FatalfAt(n.Pos(), "unexpected Defn: %+v", defn)
11851199
}
1200+
11861201
if n.Outer != nil {
11871202
// Either the outer variable is defined in function being inlined,
11881203
// and we will replace it with the substituted variable, or it is
@@ -1406,6 +1421,20 @@ func (subst *inlsubst) node(n ir.Node) ir.Node {
14061421
m := ir.Copy(n)
14071422
m.SetPos(subst.updatedPos(m.Pos()))
14081423
ir.EditChildren(m, subst.edit)
1424+
1425+
switch m := m.(type) {
1426+
case *ir.AssignStmt:
1427+
if lhs, ok := m.X.(*ir.Name); ok && lhs.Defn == &subst.defnMarker {
1428+
lhs.Defn = m
1429+
}
1430+
case *ir.AssignListStmt:
1431+
for _, lhs := range m.Lhs {
1432+
if lhs, ok := lhs.(*ir.Name); ok && lhs.Defn == &subst.defnMarker {
1433+
lhs.Defn = m
1434+
}
1435+
}
1436+
}
1437+
14091438
return m
14101439
}
14111440

test/fixedbugs/issue45606.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
func x() {
10+
func() func() {
11+
return func() {
12+
f := func() {}
13+
g, _ := f, 0
14+
g()
15+
}
16+
}()()
17+
}

0 commit comments

Comments
 (0)