1
0
mirror of https://github.com/golang/go synced 2024-11-27 03:51:30 -07:00

cmd/compile/internal/ir: prevent NewClosureVar misuse

NewClosureVar should only be called to capture locally declared
variables in the enclosing function scope. This CL adds a check to
make sure it's used that way, in particular to make sure it's not
called to capture global variables.

This came up because for generic method values, we desugar the method
value into a function literal that captures the receiver value after
evaluating it. However, due to compiler backend limitations, for
package-scope generic method values we spill the receiver value into a
global variable rather than capturing it normally.

To prevent confusing backend issues when misusing NewClosureVar with
global variables, this CL adds an extra check.

Change-Id: I80f0f083dc80f70c7f0298020efe56dba00b67d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/422195
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2022-08-08 12:40:49 -07:00
parent b911771f12
commit bd901af30b

View File

@ -336,6 +336,14 @@ func (n *Name) Byval() bool {
// NewClosureVar returns a new closure variable for fn to refer to
// outer variable n.
func NewClosureVar(pos src.XPos, fn *Func, n *Name) *Name {
switch n.Class {
case PAUTO, PPARAM, PPARAMOUT, PAUTOHEAP:
// ok
default:
// Prevent mistaken capture of global variables.
base.Fatalf("NewClosureVar: %+v", n)
}
c := NewNameAt(pos, n.Sym())
c.Curfn = fn
c.Class = PAUTOHEAP