From 32636cd1ffc6cd9ef81d09b8320d2aaad4a21117 Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Tue, 18 Jan 2022 12:55:17 -0800 Subject: cmd/compile: make sure multiple blank typeparams remain unique In a method declaration "func (f *Foo[_, _]) String() string { ... }", the two blank typeparams have the same name, but our current design with types1 needs unique names for type params. Similarly, for export/import, we need unique names to keep the type params straight in generic types and connect the proper type param with the proper constraint. We make blank type params unique by changing them to $1, $2, etc in noder.typ0() via typecheck.TparamExportName(). We then revert $ back to _ during type2 import via typecheck.TparamName(). We similarly revert during gcimporter import. We don't need/want to revert in the types1 importer, since we want unique names for type params. Rob Findley has made a similar change to x/tools (and we tried to make the source code changes similar for the gcimporter and types2 importer changes). Fixes #50419 Change-Id: I855cc3d90d06bcf59541ed0c879e9a0e4ede45bb Reviewed-on: https://go-review.googlesource.com/c/go/+/379194 Reviewed-by: Robert Griesemer Trust: Dan Scales --- src/cmd/compile/internal/noder/types.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/cmd/compile/internal/noder') diff --git a/src/cmd/compile/internal/noder/types.go b/src/cmd/compile/internal/noder/types.go index 3f3c9566ca..e7ce4c1089 100644 --- a/src/cmd/compile/internal/noder/types.go +++ b/src/cmd/compile/internal/noder/types.go @@ -239,10 +239,13 @@ func (g *irgen) typ0(typ types2.Type) *types.Type { // Save the name of the type parameter in the sym of the type. // Include the types2 subscript in the sym name pkg := g.tpkg(typ) - // Create the unique types1 name for a type param, using its context with a - // function, type, or method declaration. + // Create the unique types1 name for a type param, using its context + // with a function, type, or method declaration. Also, map blank type + // param names to a unique name based on their type param index. The + // unique blank names will be exported, but will be reverted during + // types2 and gcimporter import. assert(g.curDecl != "") - nm := g.curDecl + "." + typ.Obj().Name() + nm := typecheck.TparamExportName(g.curDecl, typ.Obj().Name(), typ.Index()) sym := pkg.Lookup(nm) if sym.Def != nil { // Make sure we use the same type param type for the same -- cgit v1.3-5-g9baa