]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: remove typeparam subscripts, use func/type prefix for uniqueness
authorDan Scales <danscales@google.com>
Wed, 29 Sep 2021 17:54:27 +0000 (10:54 -0700)
committerDan Scales <danscales@google.com>
Fri, 1 Oct 2021 18:18:46 +0000 (18:18 +0000)
In types1 and for the export format, we were using type param subscripts
coming from types2 (originally for debugging) to provide unique names.
We need unique full-names for type params in types1 to ensure consistent
references to type params in function/method bodies and type params
derived from translation from types2. We also currently need unique
names for type params in importer/iimport.go and gcimporter/iimport.go,
because there are no levels of scoping in the package symbol lookup and
pkgIndex table.

As a step to eliminate the typeparam subscripts (which have no
relation to the source code), we change so that the typeparams' unique
name is just prefixing the type param name with the name of the
enclosing generic function, type, or method.

We now no longer use types2.TypeString in types2-to-types1 translation,
so Typestring can be changed to eliminate the subscript, as needed.
Also, types2.TypeParam.SetId() is no longer needed and is eliminated.

We can decide later if we want to do the further step of adding scoping
to the importer/iimport.go and gcimporter/iimport.go, which could be
used to eliminate the type param "path" prefix from the export format.

Change-Id: I0e37795664be2c2e1869b8f9e93393b83fc56409
Reviewed-on: https://go-review.googlesource.com/c/go/+/353135
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/importer/iimport.go
src/cmd/compile/internal/noder/decl.go
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/noder/types.go
src/cmd/compile/internal/types2/typeparam.go
src/cmd/compile/internal/types2/typestring.go
src/go/internal/gcimporter/iimport.go

index b9d2ecbdb5b1b94cce6402add5b6bf4710e89f8a..cbc78539fd7987fa224bfaa7a1213df53391bc93 100644 (file)
@@ -362,13 +362,13 @@ func (r *importReader) obj(name string) {
                if r.p.exportVersion < iexportVersionGenerics {
                        errorf("unexpected type param type")
                }
-               name0, sub := parseSubscript(name)
-               tn := types2.NewTypeName(pos, r.currPkg, name0, nil)
-               t := types2.NewTypeParam(tn, nil)
-               if sub == 0 {
-                       errorf("missing subscript")
+               // Remove the "path" from the type param name that makes it unique
+               ix := strings.LastIndex(name, ".")
+               if ix < 0 {
+                       errorf("missing path for type param")
                }
-               t.SetId(sub)
+               tn := types2.NewTypeName(pos, r.currPkg, name[ix+1:], nil)
+               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}
@@ -752,23 +752,3 @@ func baseType(typ types2.Type) *types2.Named {
        n, _ := typ.(*types2.Named)
        return n
 }
-
-func parseSubscript(name string) (string, uint64) {
-       // Extract the subscript value from the type param name. We export
-       // and import the subscript value, so that all type params have
-       // unique names.
-       sub := uint64(0)
-       startsub := -1
-       for i, r := range name {
-               if '₀' <= r && r < '₀'+10 {
-                       if startsub == -1 {
-                               startsub = i
-                       }
-                       sub = sub*10 + uint64(r-'₀')
-               }
-       }
-       if startsub >= 0 {
-               name = name[:startsub]
-       }
-       return name, sub
-}
index c9ab31f20335d5ee8aa4df6ba37e85acdc30b1b8..f2dad9c30247f18f1eabdb52d315ff987f3d365a 100644 (file)
@@ -86,6 +86,17 @@ func (g *irgen) constDecl(out *ir.Nodes, decl *syntax.ConstDecl) {
 }
 
 func (g *irgen) funcDecl(out *ir.Nodes, decl *syntax.FuncDecl) {
+       // Set g.curDecl to the function name, as context for the type params declared
+       // during types2-to-types1 translation if this is a generic function.
+       g.curDecl = decl.Name.Value
+       obj2 := g.info.Defs[decl.Name]
+       recv := types2.AsSignature(obj2.Type()).Recv()
+       if recv != nil {
+               t2 := deref2(recv.Type())
+               // This is a method, so set g.curDecl to recvTypeName.methName instead.
+               g.curDecl = types2.AsNamed(t2).Obj().Name() + "." + g.curDecl
+       }
+
        fn := ir.NewFunc(g.pos(decl))
        fn.Nname, _ = g.def(decl.Name)
        fn.Nname.Func = fn
@@ -143,6 +154,9 @@ func (g *irgen) funcDecl(out *ir.Nodes, decl *syntax.FuncDecl) {
 }
 
 func (g *irgen) typeDecl(out *ir.Nodes, decl *syntax.TypeDecl) {
+       // Set g.curDecl to the type name, as context for the type params declared
+       // during types2-to-types1 translation if this is a generic type.
+       g.curDecl = decl.Name.Value
        if decl.Alias {
                name, _ := g.def(decl.Name)
                g.pragmaFlags(decl.Pragma, 0)
@@ -205,6 +219,9 @@ func (g *irgen) typeDecl(out *ir.Nodes, decl *syntax.TypeDecl) {
                methods := make([]*types.Field, otyp.NumMethods())
                for i := range methods {
                        m := otyp.Method(i)
+                       // Set g.curDecl to recvTypeName.methName, as context for the
+                       // method-specific type params in the receiver.
+                       g.curDecl = decl.Name.Value + "." + m.Name()
                        meth := g.obj(m)
                        methods[i] = types.NewField(meth.Pos(), g.selector(m), meth.Type())
                        methods[i].Nname = meth
index b0a4da3536ca028c1b75d9abb6a457b08d4d643e..645ac2c2140059a77a9b67e8cb235159f0b0e570 100644 (file)
@@ -170,6 +170,12 @@ type irgen struct {
        // avoid adding closures of generic functions/methods to the target.Decls
        // list.
        topFuncIsGeneric bool
+
+       // The context during type/function/method declarations that is used to
+       // uniquely name type parameters. We need unique names for type params so we
+       // can be sure they match up correctly between types2-to-types1 translation
+       // and types1 importing.
+       curDecl string
 }
 
 func (g *irgen) later(fn func()) {
index e1cfe4a9d8a2c5cdfd079ee384df3c48d1ea17a8..1a7cef4aa3e84ca149fa55d7c4e44f9abf8a1d36 100644 (file)
@@ -219,7 +219,10 @@ 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)
-               sym := pkg.Lookup(types2.TypeString(typ, func(*types2.Package) string { return "" }))
+               // Create the unique types1 name for a type param, using its context with a
+               // function, type, or method declaration.
+               nm := g.curDecl + "." + typ.Obj().Name()
+               sym := pkg.Lookup(nm)
                if sym.Def != nil {
                        // Make sure we use the same type param type for the same
                        // name, whether it is created during types1-import or
@@ -318,6 +321,10 @@ func (g *irgen) fillinMethods(typ *types2.Named, ntyp *types.Type) {
                                meth2 = ir.NewNameAt(meth.Pos(), newsym)
                                rparams := types2.AsSignature(m.Type()).RecvTypeParams()
                                tparams := make([]*types.Type, rparams.Len())
+                               // Set g.curDecl to be the method context, so type
+                               // params in the receiver of the method that we are
+                               // translating gets the right unique name.
+                               g.curDecl = typ.Obj().Name() + "." + m.Name()
                                for i := range tparams {
                                        tparams[i] = g.typ1(rparams.At(i))
                                }
index 3ec4a641a6a12b2bc70412415a246afd309f1ecc..6bc9dbc24d5c591b986b7c5016ab453788da21bd 100644 (file)
@@ -55,12 +55,6 @@ func (t *TypeParam) Index() int {
        return t.index
 }
 
-// SetId sets the unique id of a type param. Should only be used for type params
-// in imported generic types.
-func (t *TypeParam) SetId(id uint64) {
-       t.id = id
-}
-
 // Constraint returns the type constraint specified for t.
 func (t *TypeParam) Constraint() Type {
        return t.bound
index c1feaa97cca6dea3bfd38c67915191231afd9b69..61c8a9158cb2a07b139cf9d1dfe51bd331c82de5 100644 (file)
@@ -267,9 +267,6 @@ func (w *typeWriter) typ(typ Type) {
                        break
                }
                // Optionally write out package for typeparams (like Named).
-               // TODO(danscales): this is required for import/export, so
-               // we maybe need a separate function that won't be changed
-               // for debugging purposes.
                if t.obj.pkg != nil {
                        writePackage(w.buf, t.obj.pkg, w.qf)
                }
index e61a3a51a1196e3b157d432ce3ff9134c769013f..9aae2a31f38fad20d3bd1682c23275c2be41efb7 100644 (file)
@@ -18,6 +18,7 @@ import (
        "io"
        "math/big"
        "sort"
+       "strings"
 )
 
 type intReader struct {
@@ -353,16 +354,13 @@ func (r *importReader) obj(name string) {
                if r.p.exportVersion < iexportVersionGenerics {
                        errorf("unexpected type param type")
                }
-               name0, sub := parseSubscript(name)
-               tn := types.NewTypeName(pos, r.currPkg, name0, nil)
-               t := types.NewTypeParam(tn, nil)
-               if sub == 0 {
-                       errorf("missing subscript")
+               // Remove the "path" from the type param name that makes it unique
+               ix := strings.LastIndex(name, ".")
+               if ix < 0 {
+                       errorf("missing path for type param")
                }
-
-               // TODO(rfindley): can we use a different, stable ID?
-               // t.SetId(sub)
-
+               tn := types.NewTypeName(pos, r.currPkg, name[ix+1:], nil)
+               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}
@@ -743,23 +741,3 @@ func baseType(typ types.Type) *types.Named {
        n, _ := typ.(*types.Named)
        return n
 }
-
-func parseSubscript(name string) (string, uint64) {
-       // Extract the subscript value from the type param name. We export
-       // and import the subscript value, so that all type params have
-       // unique names.
-       sub := uint64(0)
-       startsub := -1
-       for i, r := range name {
-               if '₀' <= r && r < '₀'+10 {
-                       if startsub == -1 {
-                               startsub = i
-                       }
-                       sub = sub*10 + uint64(r-'₀')
-               }
-       }
-       if startsub >= 0 {
-               name = name[:startsub]
-       }
-       return name, sub
-}