Skip to content

Commit b98ce3b

Browse files
committed
cmd/compile: import empty closure function correctly
On import, make sure that an empty closure is represented as a single empty block statement. Otherwise, the closure is dropped. Block statements are not exported explicitly, so must recreate on import. Fixes #44330 Change-Id: I061598f0f859dd71d2d0cbd10c77cdd81525d1f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/297569 Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Trust: Dan Scales <[email protected]>
1 parent 97bdac0 commit b98ce3b

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,11 @@ func (r *importReader) node() ir.Node {
992992
r.funcBody(fn)
993993
fn.Dcl = fn.Inl.Dcl
994994
fn.Body = fn.Inl.Body
995+
if len(fn.Body) == 0 {
996+
// An empty closure must be represented as a single empty
997+
// block statement, else it will be dropped.
998+
fn.Body = []ir.Node{ir.NewBlockStmt(src.NoXPos, nil)}
999+
}
9951000
fn.Inl = nil
9961001

9971002
ir.FinishCaptureNames(pos, r.curfn, fn)

test/fixedbugs/issue44330.dir/a.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Table struct {
8+
ColumnSeparator bool
9+
RowSeparator bool
10+
11+
// ColumnResizer is called on each Draw. Can be used for custom column sizing.
12+
ColumnResizer func()
13+
}
14+
15+
func NewTable() *Table {
16+
return &Table{
17+
ColumnSeparator: true,
18+
RowSeparator: true,
19+
ColumnResizer: func() {},
20+
}
21+
}

test/fixedbugs/issue44330.dir/b.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 main
6+
7+
import (
8+
"./a"
9+
)
10+
11+
type Term struct {
12+
top *a.Table
13+
}
14+
15+
//go:noinline
16+
func NewFred() *Term {
17+
table := a.NewTable()
18+
return &Term{top: table}
19+
}
20+
21+
func main() {
22+
NewFred()
23+
}

test/fixedbugs/issue44330.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir
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)