mirror of
https://github.com/golang/go.git
synced 2025-05-06 08:03:03 +00:00
cmd/compile: copy captured dictionary var to local var
When starting a closure that needs a dictionary, copy the closure variable to a local variable. This lets child closures capture that dictionary variable correctly. This is a better fix for #47684, which does not cause problems like #47723. Fixes #47723 Update #47684 Change-Id: Ib5d9ffc68a5142e28daa7d0d75683e7a35508540 Reviewed-on: https://go-review.googlesource.com/c/go/+/343871 Trust: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
This commit is contained in:
parent
aeec6dbfe0
commit
8486ced8b0
@ -404,9 +404,7 @@ func CaptureName(pos src.XPos, fn *Func, n *Name) *Name {
|
|||||||
if n.Op() != ONAME || n.Curfn == nil {
|
if n.Op() != ONAME || n.Curfn == nil {
|
||||||
return n // okay to use directly
|
return n // okay to use directly
|
||||||
}
|
}
|
||||||
if n.IsClosureVar() && n.Sym().Name != ".dict" {
|
if n.IsClosureVar() {
|
||||||
// Note: capturing dictionary closure variables is ok. This makes
|
|
||||||
// sure the generated code is correctly optimized.
|
|
||||||
base.FatalfAt(pos, "misuse of CaptureName on closure variable: %v", n)
|
base.FatalfAt(pos, "misuse of CaptureName on closure variable: %v", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1087,13 +1087,25 @@ func (subst *subster) node(n ir.Node) ir.Node {
|
|||||||
ir.FinishCaptureNames(oldfn.Pos(), saveNewf, newfn)
|
ir.FinishCaptureNames(oldfn.Pos(), saveNewf, newfn)
|
||||||
newfn.ClosureVars = append(newfn.ClosureVars, subst.namelist(oldfn.ClosureVars)...)
|
newfn.ClosureVars = append(newfn.ClosureVars, subst.namelist(oldfn.ClosureVars)...)
|
||||||
|
|
||||||
|
// Copy that closure variable to a local one.
|
||||||
|
// Note: this allows the dictionary to be captured by child closures.
|
||||||
|
// See issue 47723.
|
||||||
|
ldict := ir.NewNameAt(x.Pos(), subst.info.gf.Sym().Pkg.Lookup(".dict"))
|
||||||
|
typed(types.Types[types.TUINTPTR], ldict)
|
||||||
|
ldict.Class = ir.PAUTO
|
||||||
|
ldict.Curfn = newfn
|
||||||
|
newfn.Dcl = append(newfn.Dcl, ldict)
|
||||||
|
as := ir.NewAssignStmt(x.Pos(), ldict, cdict)
|
||||||
|
as.SetTypecheck(1)
|
||||||
|
newfn.Body.Append(as)
|
||||||
|
|
||||||
// Create inst info for the instantiated closure. The dict
|
// Create inst info for the instantiated closure. The dict
|
||||||
// param is the closure variable for the dictionary of the
|
// param is the closure variable for the dictionary of the
|
||||||
// outer function. Since the dictionary is shared, use the
|
// outer function. Since the dictionary is shared, use the
|
||||||
// same entries for startSubDict, dictLen, dictEntryMap.
|
// same entries for startSubDict, dictLen, dictEntryMap.
|
||||||
cinfo := &instInfo{
|
cinfo := &instInfo{
|
||||||
fun: newfn,
|
fun: newfn,
|
||||||
dictParam: cdict,
|
dictParam: ldict,
|
||||||
gf: subst.info.gf,
|
gf: subst.info.gf,
|
||||||
gfInfo: subst.info.gfInfo,
|
gfInfo: subst.info.gfInfo,
|
||||||
startSubDict: subst.info.startSubDict,
|
startSubDict: subst.info.startSubDict,
|
||||||
@ -1110,7 +1122,7 @@ func (subst *subster) node(n ir.Node) ir.Node {
|
|||||||
outerinfo := subst.info
|
outerinfo := subst.info
|
||||||
subst.info = cinfo
|
subst.info = cinfo
|
||||||
// Make sure type of closure function is set before doing body.
|
// Make sure type of closure function is set before doing body.
|
||||||
newfn.Body = subst.list(oldfn.Body)
|
newfn.Body.Append(subst.list(oldfn.Body)...)
|
||||||
subst.info = outerinfo
|
subst.info = outerinfo
|
||||||
subst.newf = saveNewf
|
subst.newf = saveNewf
|
||||||
ir.CurFunc = saveNewf
|
ir.CurFunc = saveNewf
|
||||||
|
23
test/typeparam/issue47723.go
Normal file
23
test/typeparam/issue47723.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// run -gcflags=-G=3
|
||||||
|
|
||||||
|
// 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[_ any]() int {
|
||||||
|
var a [1]int
|
||||||
|
_ = func() int {
|
||||||
|
return func() int {
|
||||||
|
return 0
|
||||||
|
}()
|
||||||
|
}()
|
||||||
|
return a[func() int {
|
||||||
|
return 0
|
||||||
|
}()]
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
f[int]()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user