Skip to content

Commit 5f3dabb

Browse files
committed
cmd/compile: fix import of functions of multiple nested closure
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 <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Dan Scales <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 7af821a commit 5f3dabb

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/cmd/compile/internal/typecheck/iimport.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -986,15 +986,19 @@ func (r *importReader) node() ir.Node {
986986
fn.ClosureVars = cvars
987987
r.allClosureVars = append(r.allClosureVars, cvars...)
988988

989-
fn.Dcl = r.readFuncDcls(fn)
990-
body := r.stmtList()
989+
fn.Inl = &ir.Inline{}
990+
// Read in the Dcls and Body of the closure after temporarily
991+
// setting r.curfn to fn.
992+
r.funcBody(fn)
993+
fn.Dcl = fn.Inl.Dcl
994+
fn.Body = fn.Inl.Body
995+
fn.Inl = nil
996+
991997
ir.FinishCaptureNames(pos, r.curfn, fn)
992998

993999
clo := ir.NewClosureExpr(pos, fn)
9941000
fn.OClosure = clo
9951001

996-
fn.Body = body
997-
9981002
return clo
9991003

10001004
// case OPTRLIT:

test/fixedbugs/issue44335.dir/a.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Go Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file
4+
5+
package a
6+
7+
type W struct {
8+
M func(string) string
9+
}
10+
11+
func FM(m string) func(W) {
12+
return func(pw W) {
13+
pw.M = func(string) string {
14+
return m
15+
}
16+
}
17+
}

test/fixedbugs/issue44335.dir/b.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2021 The Go Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
package b
6+
7+
import "./a"
8+
9+
func F() {
10+
a.FM("")
11+
}

test/fixedbugs/issue44335.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir
2+
3+
// Copyright 2021 The Go Authors. All rights reserved. Use of this
4+
// source code is governed by a BSD-style license that can be found in
5+
// the LICENSE file.
6+
7+
package ignored

0 commit comments

Comments
 (0)