aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Findley <rfindley@google.com>2022-03-21 16:08:38 -0400
committerCherry Mui <cherryyz@google.com>2022-04-04 19:57:18 +0000
commit69bc821a010e60a40d6575e793e14b3e616b005b (patch)
tree9ffce9895f50a9a2bb74d4b8fa734512b6f65f07
parentc258e9d07dce0ef156d5a9c930bc15e3a0276599 (diff)
downloadgo-69bc821a010e60a40d6575e793e14b3e616b005b.tar.gz
go-69bc821a010e60a40d6575e793e14b3e616b005b.zip
[release-branch.go1.18] cmd/compile/internal/importer: key tparams by Package instead of pkgname
The importer type param index used package name as type parameter key, causing type parameters to be reused/overwritten if two packages in the import graph had the same combination of (package name, declaration name, type parameter name). Fix this by instead using the *Package in the key. Note: -G=3 was added to typeparam/issue51836.go, as it is necessary for 1.18 but not for tip. For #51836 Fixes #51847 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> (cherry picked from commit fd1b5904ae7cc76eefd94d315f92265da5b81b14) Reviewed-on: https://go-review.googlesource.com/c/go/+/394854
-rw-r--r--src/cmd/compile/internal/importer/iimport.go6
-rw-r--r--src/go/internal/gcimporter/iimport.go6
-rw-r--r--test/typeparam/issue51836.dir/a.go8
-rw-r--r--test/typeparam/issue51836.dir/aa.go13
-rw-r--r--test/typeparam/issue51836.dir/p.go11
-rw-r--r--test/typeparam/issue51836.go7
6 files changed, 45 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/importer/iimport.go b/src/cmd/compile/internal/importer/iimport.go
index bed4fbb016..576036bdce 100644
--- a/src/cmd/compile/internal/importer/iimport.go
+++ b/src/cmd/compile/internal/importer/iimport.go
@@ -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
diff --git a/src/go/internal/gcimporter/iimport.go b/src/go/internal/gcimporter/iimport.go
index bff1c09cc9..f9eaa0b10c 100644
--- a/src/go/internal/gcimporter/iimport.go
+++ b/src/go/internal/gcimporter/iimport.go
@@ -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
diff --git a/test/typeparam/issue51836.dir/a.go b/test/typeparam/issue51836.dir/a.go
new file mode 100644
index 0000000000..e9223c9aa8
--- /dev/null
+++ b/test/typeparam/issue51836.dir/a.go
@@ -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 {
+}
diff --git a/test/typeparam/issue51836.dir/aa.go b/test/typeparam/issue51836.dir/aa.go
new file mode 100644
index 0000000000..d774be282e
--- /dev/null
+++ b/test/typeparam/issue51836.dir/aa.go
@@ -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]
+}
diff --git a/test/typeparam/issue51836.dir/p.go b/test/typeparam/issue51836.dir/p.go
new file mode 100644
index 0000000000..98197ae0fd
--- /dev/null
+++ b/test/typeparam/issue51836.dir/p.go
@@ -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]
diff --git a/test/typeparam/issue51836.go b/test/typeparam/issue51836.go
new file mode 100644
index 0000000000..bf9e4212d0
--- /dev/null
+++ b/test/typeparam/issue51836.go
@@ -0,0 +1,7 @@
+// compiledir -s -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