Skip to content

Commit fd1b590

Browse files
committedMar 22, 2022
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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 212bda0 commit fd1b590

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed
 

Diff for: ‎src/cmd/compile/internal/importer/iimport.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
)
5454

5555
type ident struct {
56-
pkg string
56+
pkg *types2.Package
5757
name string
5858
}
5959

@@ -402,7 +402,7 @@ func (r *importReader) obj(name string) {
402402
t := types2.NewTypeParam(tn, nil)
403403
// To handle recursive references to the typeparam within its
404404
// bound, save the partial type in tparamIndex before reading the bounds.
405-
id := ident{r.currPkg.Name(), name}
405+
id := ident{r.currPkg, name}
406406
r.p.tparamIndex[id] = t
407407

408408
var implicit bool
@@ -687,7 +687,7 @@ func (r *importReader) doType(base *types2.Named) types2.Type {
687687
errorf("unexpected type param type")
688688
}
689689
pkg, name := r.qualifiedIdent()
690-
id := ident{pkg.Name(), name}
690+
id := ident{pkg, name}
691691
if t, ok := r.p.tparamIndex[id]; ok {
692692
// We're already in the process of importing this typeparam.
693693
return t

Diff for: ‎src/go/internal/gcimporter/iimport.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
)
5454

5555
type ident struct {
56-
pkg string
56+
pkg *types.Package
5757
name string
5858
}
5959

@@ -393,7 +393,7 @@ func (r *importReader) obj(name string) {
393393
t := types.NewTypeParam(tn, nil)
394394
// To handle recursive references to the typeparam within its
395395
// bound, save the partial type in tparamIndex before reading the bounds.
396-
id := ident{r.currPkg.Name(), name}
396+
id := ident{r.currPkg, name}
397397
r.p.tparamIndex[id] = t
398398

399399
var implicit bool
@@ -676,7 +676,7 @@ func (r *importReader) doType(base *types.Named) types.Type {
676676
errorf("unexpected type param type")
677677
}
678678
pkg, name := r.qualifiedIdent()
679-
id := ident{pkg.Name(), name}
679+
id := ident{pkg, name}
680680
if t, ok := r.p.tparamIndex[id]; ok {
681681
// We're already in the process of importing this typeparam.
682682
return t

Diff for: ‎test/typeparam/issue51836.dir/a.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type T[K any] struct {
8+
}

Diff for: ‎test/typeparam/issue51836.dir/aa.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
import (
8+
"./a"
9+
)
10+
11+
type T[K any] struct {
12+
t a.T[K]
13+
}

Diff for: ‎test/typeparam/issue51836.dir/p.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
import (
8+
a "./aa"
9+
)
10+
11+
var Foo a.T[int]

Diff for: ‎test/typeparam/issue51836.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir -s
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

0 commit comments

Comments
 (0)