From 0132b91127c3cf6e19e0e1db014a04219427171e Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Fri, 11 Jun 2021 03:09:26 -0700 Subject: [dev.typeparams] cmd/compile: refactor closure construction typecheck.tcClosure is complicated with many code flows because all of its callers setup the closure funcs in slightly different ways. E.g., it's non-obvious who's responsible for setting the underlying func's Sym or adding it to target.Decls, or how to write new code that constructs a closure without interfering with existing code. This CL refactors everything to use three common functions in package ir: NewClosureFunc (which handle creating the Func, Name, and ClosureExpr and wiring them together), NameClosure (which generates and assigns its unique Sym), and UseClosure (which handles adding the Func to target.Decls). Most IR builders can actually name the closure right away, but the legacy noder+typecheck path may not yet know the name of the enclosing function. In particular, for methods declared with aliased receiver parameters, we need to wait until after typechecking top-level declarations to know the method's true name. So they're left anonymous until typecheck. UseClosure does relatively little work today, but it serves as a useful spot to check that the code setting up closures got it right. It may also eventually serve as an optimization point for early lifting of trivial closures, which may or may not ultimately be beneficial. Change-Id: I7da1e93c70d268f575b12d6aaeb2336eb910a6f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/327051 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/typecheck/iimport.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'src/cmd/compile/internal/typecheck/iimport.go') diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 9054a83e6e..b1b3c27898 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -1283,12 +1283,7 @@ func (r *importReader) node() ir.Node { // All the remaining code below is similar to (*noder).funcLit(), but // with Dcls and ClosureVars lists already set up - fn := ir.NewFunc(pos) - fn.SetIsHiddenClosure(true) - fn.Nname = ir.NewNameAt(pos, ir.BlankNode.Sym()) - fn.Nname.Func = fn - fn.Nname.Ntype = ir.TypeNode(typ) - fn.Nname.Defn = fn + fn := ir.NewClosureFunc(pos, r.curfn) fn.Nname.SetType(typ) cvars := make([]*ir.Name, r.int64()) @@ -1321,18 +1316,10 @@ func (r *importReader) node() ir.Node { ir.FinishCaptureNames(pos, r.curfn, fn) - clo := ir.NewClosureExpr(pos, fn) - fn.OClosure = clo + clo := fn.OClosure if go117ExportTypes { clo.SetType(typ) } - if r.curfn.Type().HasTParam() { - // Generic functions aren't inlined, so give the closure a - // function name now, which is then available for use - // (after appending the type args) for each stenciling. - fn.Nname.SetSym(ClosureName(r.curfn)) - } - return clo case ir.OSTRUCTLIT: -- cgit v1.2.3-54-g00ecf