diff --git a/src/cmd/compile/internal/ir/name.go b/src/cmd/compile/internal/ir/name.go index 48fe572124..a2eec05013 100644 --- a/src/cmd/compile/internal/ir/name.go +++ b/src/cmd/compile/internal/ir/name.go @@ -404,9 +404,7 @@ func CaptureName(pos src.XPos, fn *Func, n *Name) *Name { if n.Op() != ONAME || n.Curfn == nil { return n // okay to use directly } - if n.IsClosureVar() && n.Sym().Name != ".dict" { - // Note: capturing dictionary closure variables is ok. This makes - // sure the generated code is correctly optimized. + if n.IsClosureVar() { base.FatalfAt(pos, "misuse of CaptureName on closure variable: %v", n) } diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index e8eee5290e..4ed1850597 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -1087,13 +1087,25 @@ func (subst *subster) node(n ir.Node) ir.Node { ir.FinishCaptureNames(oldfn.Pos(), saveNewf, newfn) 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 // param is the closure variable for the dictionary of the // outer function. Since the dictionary is shared, use the // same entries for startSubDict, dictLen, dictEntryMap. cinfo := &instInfo{ fun: newfn, - dictParam: cdict, + dictParam: ldict, gf: subst.info.gf, gfInfo: subst.info.gfInfo, startSubDict: subst.info.startSubDict, @@ -1110,7 +1122,7 @@ func (subst *subster) node(n ir.Node) ir.Node { outerinfo := subst.info subst.info = cinfo // 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.newf = saveNewf ir.CurFunc = saveNewf diff --git a/test/typeparam/issue47723.go b/test/typeparam/issue47723.go new file mode 100644 index 0000000000..9ef60402b2 --- /dev/null +++ b/test/typeparam/issue47723.go @@ -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]() +}