]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: remove need for instance (struct)
authorRobert Griesemer <gri@golang.org>
Thu, 26 Aug 2021 04:48:21 +0000 (21:48 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 26 Aug 2021 17:19:04 +0000 (17:19 +0000)
instance was only used to hold the instantiation position for
lazy instantiation (and encode the fact that we have a lazy
instantiation). Just use a (pointer to a) syntax.Pos instead.

We could use a syntax.Pos (no pointer) and rely on the fact
that we have a known position (or fake position, if need be)
to indicate lazy instantiation. But using a pointer leads to
a smaller Named struct.

Change-Id: I441a839a125f453ad6c501de1ce499b72a2f67a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/345177
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types2/instantiate.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/typestring.go

index b78ac3bea3e22bed90f9103fbd47e4a56f813574..f9cde24dfc803b9ba154fd08c7fa6bcf183e2845 100644 (file)
@@ -136,7 +136,7 @@ func (check *Checker) instance(pos syntax.Pos, typ Type, targs []Type) Type {
                tname := NewTypeName(pos, t.obj.pkg, t.obj.name, nil)
                named := check.newNamed(tname, t, nil, nil, nil) // methods and tparams are set when named is loaded
                named.targs = NewTypeList(targs)
-               named.instance = &instance{pos}
+               named.instPos = &pos
                if check != nil {
                        check.typMap[h] = named
                }
index ccb1f265be9371171e7ead5b37d9f314cfc223a2..b4074aa3dc2877d387b7a9eb9425b8c7a0645240 100644 (file)
@@ -17,7 +17,7 @@ type Named struct {
        orig       *Named      // original, uninstantiated type
        fromRHS    Type        // type (on RHS of declaration) this *Named type is derived from (for cycle reporting)
        underlying Type        // possibly a *Named during setup; never a *Named once set up completely
-       instance   *instance   // position information for lazy instantiation, or nil
+       instPos    *syntax.Pos // position information for lazy instantiation, or nil
        tparams    *TParamList // type parameters, or nil
        targs      *TypeList   // type arguments (after instantiation), or nil
        methods    []*Func     // methods declared for this type (not the method set of this type); signatures are type-checked lazily
@@ -240,24 +240,16 @@ func (n *Named) setUnderlying(typ Type) {
        }
 }
 
-// instance holds position information for use in lazy instantiation.
-//
-// TODO(rfindley): instance is probably unnecessary now. See if it can be
-// eliminated.
-type instance struct {
-       pos syntax.Pos // position of type instantiation; for error reporting only
-}
-
 // expand ensures that the underlying type of n is instantiated.
 // The underlying type will be Typ[Invalid] if there was an error.
 func (n *Named) expand(typMap map[string]*Named) *Named {
-       if n.instance != nil {
+       if n.instPos != nil {
                // n must be loaded before instantiation, in order to have accurate
                // tparams. This is done implicitly by the call to n.TParams, but making it
                // explicit is harmless: load is idempotent.
                n.load()
                var u Type
-               if n.check.validateTArgLen(n.instance.pos, n.tparams.Len(), n.targs.Len()) {
+               if n.check.validateTArgLen(*n.instPos, n.tparams.Len(), n.targs.Len()) {
                        if typMap == nil {
                                if n.check != nil {
                                        typMap = n.check.typMap
@@ -270,13 +262,13 @@ func (n *Named) expand(typMap map[string]*Named) *Named {
                                        typMap = map[string]*Named{h: n}
                                }
                        }
-                       u = n.check.subst(n.instance.pos, n.orig.underlying, makeSubstMap(n.TParams().list(), n.targs.list()), typMap)
+                       u = n.check.subst(*n.instPos, n.orig.underlying, makeSubstMap(n.TParams().list(), n.targs.list()), typMap)
                } else {
                        u = Typ[Invalid]
                }
                n.underlying = u
                n.fromRHS = u
-               n.instance = nil
+               n.instPos = nil
        }
        return n
 }
index 9980408593f7fd1558766cd823cc2f8e709d5257..1775fc66773dd4cc5a408b25af8aced6ceae7381 100644 (file)
@@ -195,7 +195,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
                }
 
        case *Named:
-               if t.instance != nil {
+               if t.instPos != nil {
                        buf.WriteByte(instanceMarker)
                }
                writeTypeName(buf, t.obj, qf)