For generic functions, we can export untransformed OKEY nodes, and the
key identifier is written as an ONONAME. But in this case, we do not
want to call Resolve() on the identifier, since we may resolve to a
global type (as happens in this issue) or other global symbol with the
same name, if it exists. We just want to keep the key identifier as an
Ident node.
To solve this, I added an extra bool when exporting an ONONAME entry,
which indicates if this entry is for a key or for a global (external)
symbol. When the bool is true (this is for a key), we avoid calling
Resolve().
Fixes #49497
Change-Id: Ic8fa93d37bcad2110e0e0d060080b733e07e35d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/363074
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
n := n.(*ir.Name)
if (n.Class == ir.PEXTERN || n.Class == ir.PFUNC) && !ir.IsBlank(n) {
w.op(ir.ONONAME)
+ // Indicate that this is not an OKEY entry.
+ w.bool(false)
w.qualifiedIdent(n)
if go117ExportTypes {
w.typ(n.Type())
case ir.ONONAME:
w.op(ir.ONONAME)
- // This should only be for OKEY nodes in generic functions
+ // This can only be for OKEY nodes in generic functions. Mark it
+ // as a key entry.
+ w.bool(true)
s := n.Sym()
w.string(s.Name)
w.pkg(s.Pkg)
return n
case ir.ONONAME:
+ isKey := r.bool()
n := r.qualifiedIdent()
if go117ExportTypes {
- n2 := Resolve(n)
+ var n2 ir.Node = n
+ // Key ONONAME entries should not be resolved - they should
+ // stay as identifiers.
+ if !isKey {
+ n2 = Resolve(n)
+ }
typ := r.typ()
if n2.Type() == nil {
n2.SetType(typ)
--- /dev/null
+// 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
+
+func F[T any]() A[T] {
+ var x A[T]
+ return x
+}
+
+type A[T any] struct {
+ b B[T]
+}
+
+func (a A[T]) M() C[T] {
+ return C[T]{
+ B: a.b,
+ }
+}
+
+type B[T any] struct{}
+
+type C[T any] struct {
+ B B[T]
+}
--- /dev/null
+// 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 "a"
+
+func main() {
+ a.F[string]()
+}
--- /dev/null
+// 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