Skip to content

Commit

Permalink
cmd/compile: ensure generic function is loaded when it needs to be re…
Browse files Browse the repository at this point in the history
…-exported

In the case where we need to re-export a generic function/method from
another package in the export data of the current package, make sure it
is loaded before trying to write it out.

Fixed #49667

Change-Id: I177754bb762689f34cf5c8ad246d43f1cdbbf195
Reviewed-on: https://go-review.googlesource.com/c/go/+/365837
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
danscales committed Nov 20, 2021
1 parent d2f4c93 commit be18cd5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cmd/compile/internal/typecheck/iexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions test/typeparam/issue49667.dir/a.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions test/typeparam/issue49667.dir/b.go
Original file line number Diff line number Diff line change
@@ -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]
}
11 changes: 11 additions & 0 deletions test/typeparam/issue49667.dir/main.go
Original file line number Diff line number Diff line change
@@ -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]
}
7 changes: 7 additions & 0 deletions test/typeparam/issue49667.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit be18cd5

Please sign in to comment.