cmd/compile/internal/importer: key tparams by Package instead of pkgname

The importer type param index used package name type parameter key,
causing type parameters to be reused/overwritten if two packages in the
import graph had the same combination of (name, declaration name, type
parameter name).

Fix this by instead using the *Package in the key.

Fixes #51836

Change-Id: I881ceaf3cf7c1ab4e0835962350feb552e79b233
Reviewed-on: https://go-review.googlesource.com/c/go/+/394219
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Findley 2022-03-21 16:08:38 -04:00
parent 212bda0669
commit fd1b5904ae
6 changed files with 45 additions and 6 deletions

View file

@ -53,7 +53,7 @@ const (
)
type ident struct {
pkg string
pkg *types2.Package
name string
}
@ -402,7 +402,7 @@ func (r *importReader) obj(name string) {
t := types2.NewTypeParam(tn, nil)
// To handle recursive references to the typeparam within its
// bound, save the partial type in tparamIndex before reading the bounds.
id := ident{r.currPkg.Name(), name}
id := ident{r.currPkg, name}
r.p.tparamIndex[id] = t
var implicit bool
@ -687,7 +687,7 @@ func (r *importReader) doType(base *types2.Named) types2.Type {
errorf("unexpected type param type")
}
pkg, name := r.qualifiedIdent()
id := ident{pkg.Name(), name}
id := ident{pkg, name}
if t, ok := r.p.tparamIndex[id]; ok {
// We're already in the process of importing this typeparam.
return t

View file

@ -53,7 +53,7 @@ const (
)
type ident struct {
pkg string
pkg *types.Package
name string
}
@ -393,7 +393,7 @@ func (r *importReader) obj(name string) {
t := types.NewTypeParam(tn, nil)
// To handle recursive references to the typeparam within its
// bound, save the partial type in tparamIndex before reading the bounds.
id := ident{r.currPkg.Name(), name}
id := ident{r.currPkg, name}
r.p.tparamIndex[id] = t
var implicit bool
@ -676,7 +676,7 @@ func (r *importReader) doType(base *types.Named) types.Type {
errorf("unexpected type param type")
}
pkg, name := r.qualifiedIdent()
id := ident{pkg.Name(), name}
id := ident{pkg, name}
if t, ok := r.p.tparamIndex[id]; ok {
// We're already in the process of importing this typeparam.
return t

View file

@ -0,0 +1,8 @@
// 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 T[K any] struct {
}

View file

@ -0,0 +1,13 @@
// 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
import (
"./a"
)
type T[K any] struct {
t a.T[K]
}

View file

@ -0,0 +1,11 @@
// 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 p
import (
a "./aa"
)
var Foo a.T[int]

View file

@ -0,0 +1,7 @@
// compiledir -s
// 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