aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2022-03-10 23:41:56 +0700
committerDmitri Shuralyov <dmitshur@golang.org>2022-03-14 17:19:04 +0000
commit4b9b25a21dbf4d1f9c7dc2845c1feb67a4447c1c (patch)
tree57eea405a8408abdc8a69cdde345ac51bd5dfcd4
parentd69d093c77250a58c648756db42bbad8ad823770 (diff)
downloadgo-4b9b25a21dbf4d1f9c7dc2845c1feb67a4447c1c.tar.gz
go-4b9b25a21dbf4d1f9c7dc2845c1feb67a4447c1c.zip
[release-branch.go1.18] cmd/compile: fix re-export closure
For hidden closure built during stenciling to implement a function instantiation, the function may come from other package, not local package, which causes the ICE for code that re-export the hidden closure after inlining. To fix it, use the closure package for export writer when writing out the closure itself. Fixes #51423 Change-Id: I23b067ba14e2d602a0fc3b2e99bd9317afbe53ff Reviewed-on: https://go-review.googlesource.com/c/go/+/391574 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit 9743e9b6d8e150639f509fc64e4bc5f24ecce562) Reviewed-on: https://go-review.googlesource.com/c/go/+/391774 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
-rw-r--r--src/cmd/compile/internal/typecheck/iexport.go3
-rw-r--r--src/cmd/compile/internal/typecheck/iimport.go2
-rw-r--r--test/typeparam/issue51423.dir/a.go17
-rw-r--r--test/typeparam/issue51423.dir/b.go11
-rw-r--r--test/typeparam/issue51423.go7
5 files changed, 40 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go
index ae3c41ca04..b7a251c71f 100644
--- a/src/cmd/compile/internal/typecheck/iexport.go
+++ b/src/cmd/compile/internal/typecheck/iexport.go
@@ -1851,7 +1851,10 @@ func (w *exportWriter) expr(n ir.Node) {
n := n.(*ir.ClosureExpr)
w.op(ir.OCLOSURE)
w.pos(n.Pos())
+ old := w.currPkg
+ w.setPkg(n.Type().Pkg(), true)
w.signature(n.Type())
+ w.setPkg(old, true)
// Write out id for the Outer of each conditional variable. The
// conditional variable itself for this closure will be re-created
diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go
index ef91f550a5..28a50605aa 100644
--- a/src/cmd/compile/internal/typecheck/iimport.go
+++ b/src/cmd/compile/internal/typecheck/iimport.go
@@ -1374,7 +1374,9 @@ func (r *importReader) node() ir.Node {
case ir.OCLOSURE:
//println("Importing CLOSURE")
pos := r.pos()
+ r.setPkg()
typ := r.signature(nil, nil)
+ r.setPkg()
// All the remaining code below is similar to (*noder).funcLit(), but
// with Dcls and ClosureVars lists already set up
diff --git a/test/typeparam/issue51423.dir/a.go b/test/typeparam/issue51423.dir/a.go
new file mode 100644
index 0000000000..e824d0e165
--- /dev/null
+++ b/test/typeparam/issue51423.dir/a.go
@@ -0,0 +1,17 @@
+// Copyright 2022 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 Comparator[T any] func(v1, v2 T) int
+
+func CompareInt[T ~int](a, b T) int {
+ if a < b {
+ return -1
+ }
+ if a == b {
+ return 0
+ }
+ return 1
+}
diff --git a/test/typeparam/issue51423.dir/b.go b/test/typeparam/issue51423.dir/b.go
new file mode 100644
index 0000000000..2bad19fbda
--- /dev/null
+++ b/test/typeparam/issue51423.dir/b.go
@@ -0,0 +1,11 @@
+package b
+
+import "./a"
+
+func C() a.Comparator[int] {
+ return a.CompareInt[int]
+}
+
+func main() {
+ _ = C()(1, 2)
+}
diff --git a/test/typeparam/issue51423.go b/test/typeparam/issue51423.go
new file mode 100644
index 0000000000..060a1214cc
--- /dev/null
+++ b/test/typeparam/issue51423.go
@@ -0,0 +1,7 @@
+// compiledir -G=3
+
+// Copyright 2022 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