diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index bf12ba803b..9bd1f626fe 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1418,6 +1418,12 @@ func (w *exportWriter) funcExt(n *ir.Name) { w.uint64(1 + uint64(n.Func.Inl.Cost)) w.bool(n.Func.Inl.CanDelayResults) if n.Func.ExportInline() || n.Type().HasTParam() { + if n.Type().HasTParam() { + // If this generic function/method is from another + // package, but we didn't use for instantiation in + // this package, we may not yet have imported it. + ImportedBody(n.Func) + } w.p.doInline(n) } diff --git a/test/typeparam/issue49667.dir/a.go b/test/typeparam/issue49667.dir/a.go new file mode 100644 index 0000000000..3b1889f699 --- /dev/null +++ b/test/typeparam/issue49667.dir/a.go @@ -0,0 +1,12 @@ +// 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 A[T any] struct { +} + +func (a A[T]) F() { + _ = a +} diff --git a/test/typeparam/issue49667.dir/b.go b/test/typeparam/issue49667.dir/b.go new file mode 100644 index 0000000000..e7f781e4af --- /dev/null +++ b/test/typeparam/issue49667.dir/b.go @@ -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" + +type B[T any] struct { + _ a.A[T] +} diff --git a/test/typeparam/issue49667.dir/main.go b/test/typeparam/issue49667.dir/main.go new file mode 100644 index 0000000000..fccefe023f --- /dev/null +++ b/test/typeparam/issue49667.dir/main.go @@ -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 main + +import "b" + +func main() { + var _ b.B[int] +} diff --git a/test/typeparam/issue49667.go b/test/typeparam/issue49667.go new file mode 100644 index 0000000000..76930e5e4f --- /dev/null +++ b/test/typeparam/issue49667.go @@ -0,0 +1,7 @@ +// rundir -G=3 + +// 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