]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix import of functions of multiple nested closure
authorDan Scales <danscales@google.com>
Fri, 19 Feb 2021 17:40:35 +0000 (09:40 -0800)
committerDan Scales <danscales@google.com>
Tue, 23 Feb 2021 01:53:51 +0000 (01:53 +0000)
For import of functions with closures, the connections among closure
variables are constructed on-the-fly via CaptureName(). For multiple
nested closures, we need to temporarily set r.curfn to each closure we
construct, so that the processing of closure variables will be correct
for any nested closure inside that closure.

Fixes #44335

Change-Id: I34f99e2822250542528ff6b2232bf36756140868
Reviewed-on: https://go-review.googlesource.com/c/go/+/294212
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/typecheck/iimport.go
test/fixedbugs/issue44335.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue44335.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue44335.go [new file with mode: 0644]

index 7b5b113b158d20abea4599b4479ef31169faac75..29090a91788bbb57d22910cad01a77be14ce87cd 100644 (file)
@@ -986,15 +986,19 @@ func (r *importReader) node() ir.Node {
                fn.ClosureVars = cvars
                r.allClosureVars = append(r.allClosureVars, cvars...)
 
-               fn.Dcl = r.readFuncDcls(fn)
-               body := r.stmtList()
+               fn.Inl = &ir.Inline{}
+               // Read in the Dcls and Body of the closure after temporarily
+               // setting r.curfn to fn.
+               r.funcBody(fn)
+               fn.Dcl = fn.Inl.Dcl
+               fn.Body = fn.Inl.Body
+               fn.Inl = nil
+
                ir.FinishCaptureNames(pos, r.curfn, fn)
 
                clo := ir.NewClosureExpr(pos, fn)
                fn.OClosure = clo
 
-               fn.Body = body
-
                return clo
 
        // case OPTRLIT:
diff --git a/test/fixedbugs/issue44335.dir/a.go b/test/fixedbugs/issue44335.dir/a.go
new file mode 100644 (file)
index 0000000..2c9c217
--- /dev/null
@@ -0,0 +1,17 @@
+// 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 a
+
+type W struct {
+       M func(string) string
+}
+
+func FM(m string) func(W) {
+       return func(pw W) {
+               pw.M = func(string) string {
+                       return m
+               }
+       }
+}
diff --git a/test/fixedbugs/issue44335.dir/b.go b/test/fixedbugs/issue44335.dir/b.go
new file mode 100644 (file)
index 0000000..e72c2ab
--- /dev/null
@@ -0,0 +1,11 @@
+// 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 b
+
+import "./a"
+
+func F() {
+       a.FM("")
+}
diff --git a/test/fixedbugs/issue44335.go b/test/fixedbugs/issue44335.go
new file mode 100644 (file)
index 0000000..d406838
--- /dev/null
@@ -0,0 +1,7 @@
+// compiledir
+
+// 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 ignored