src/cmd/compile/internal/typecheck/iexport.go | 3 +++ src/cmd/compile/internal/typecheck/iimport.go | 2 ++ test/typeparam/issue51423.dir/a.go | 17 +++++++++++++++++ test/typeparam/issue51423.dir/b.go | 11 +++++++++++ test/typeparam/issue51423.go | 7 +++++++ diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index ae3c41ca04304c9dc99b991c816f80dc35d65ddd..b7a251c71ffec1d520857d3bca45d4007b087d1f 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1851,7 +1851,10 @@ case ir.OCLOSURE: 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 ef91f550a5a1fe3460516a5ec70c69649e012966..28a50605aab444f8ef4c85eb3f155ac42a751271 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -1374,7 +1374,9 @@ 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 0000000000000000000000000000000000000000..e824d0e1654d604204d9c6500d8a0d56834001e7 --- /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 0000000000000000000000000000000000000000..2bad19fbdac9f62797a88fad53ded351023fe6d5 --- /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 0000000000000000000000000000000000000000..060a1214cc9a7060e2aa09632726f746a98eceb0 --- /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