]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/importer: key tparams by Package instead of pkgname
authorRobert Findley <rfindley@google.com>
Mon, 21 Mar 2022 20:08:38 +0000 (16:08 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 22 Mar 2022 21:38:10 +0000 (21:38 +0000)
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>
src/cmd/compile/internal/importer/iimport.go
src/go/internal/gcimporter/iimport.go
test/typeparam/issue51836.dir/a.go [new file with mode: 0644]
test/typeparam/issue51836.dir/aa.go [new file with mode: 0644]
test/typeparam/issue51836.dir/p.go [new file with mode: 0644]
test/typeparam/issue51836.go [new file with mode: 0644]

index bed4fbb0160d72918793106b5c56ef7b17a82b1d..576036bdce37b1259b9e32f138d8113c842ef7cc 100644 (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
index bff1c09cc9714dd017356a91257d726cded21129..f9eaa0b10cc5bf98185d8d11601b2d543c493feb 100644 (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
diff --git a/test/typeparam/issue51836.dir/a.go b/test/typeparam/issue51836.dir/a.go
new file mode 100644 (file)
index 0000000..e9223c9
--- /dev/null
@@ -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 (file)
index 0000000..d774be2
--- /dev/null
@@ -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 (file)
index 0000000..98197ae
--- /dev/null
@@ -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 (file)
index 0000000..c755e74
--- /dev/null
@@ -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