]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: eliminate typeHashing global variable
authorRobert Griesemer <gri@golang.org>
Sun, 29 Aug 2021 01:19:36 +0000 (18:19 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 30 Aug 2021 22:07:49 +0000 (22:07 +0000)
Instead, keep track of hashing mode with a typeWriter field.
Introduce a new constructor (newTypeHasher) to set the mode.

Change-Id: Ie69cc0382532c75973794326be15c884b7fdcb76
Reviewed-on: https://go-review.googlesource.com/c/go/+/345929
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/subst.go
src/cmd/compile/internal/types2/typestring.go

index 18a9e393007b1c752dc98c3bd4e018b1d3ae4b95..c67538d4f0fa23e6714f689916ebd9b2c8f6d654 100644 (file)
@@ -253,8 +253,6 @@ func (subst *subster) typ(typ Type) Type {
        return typ
 }
 
-var typeHashing = 0
-
 // typeHash returns a string representation of typ, which can be used as an exact
 // type hash: types that are identical produce identical string representations.
 // If typ is a *Named type and targs is not empty, typ is printed as if it were
@@ -263,19 +261,16 @@ func typeHash(typ Type, targs []Type) string {
        assert(typ != nil)
        var buf bytes.Buffer
 
-       assert(typeHashing == 0)
-       typeHashing++
-       w := newTypeWriter(&buf, nil)
+       h := newTypeHasher(&buf)
        if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
                // Don't use WriteType because we need to use the provided targs
                // and not any targs that might already be with the *Named type.
-               w.typeName(named.obj)
-               w.typeList(targs)
+               h.typeName(named.obj)
+               h.typeList(targs)
        } else {
                assert(targs == nil)
-               w.typ(typ)
+               h.typ(typ)
        }
-       typeHashing--
 
        if debug {
                // there should be no instance markers in type hashes
index 2110b46498fb159e755e2aa7c3149080b4b47119..3582d183a8ed3d8796b81f74ab3db8ae69799080 100644 (file)
@@ -62,10 +62,15 @@ type typeWriter struct {
        buf  *bytes.Buffer
        seen map[Type]bool
        qf   Qualifier
+       hash bool
 }
 
 func newTypeWriter(buf *bytes.Buffer, qf Qualifier) *typeWriter {
-       return &typeWriter{buf, make(map[Type]bool), qf}
+       return &typeWriter{buf, make(map[Type]bool), qf, false}
+}
+
+func newTypeHasher(buf *bytes.Buffer) *typeWriter {
+       return &typeWriter{buf, make(map[Type]bool), nil, true}
 }
 
 func (w *typeWriter) byte(b byte)                               { w.buf.WriteByte(b) }
@@ -207,7 +212,7 @@ func (w *typeWriter) typ(typ Type) {
                // types. Write them to aid debugging, but don't write
                // them when we need an instance hash: whether a type
                // is fully expanded or not doesn't matter for identity.
-               if typeHashing == 0 && t.instPos != nil {
+               if !w.hash && t.instPos != nil {
                        w.byte(instanceMarker)
                }
                w.typeName(t.obj)
@@ -291,7 +296,7 @@ func (w *typeWriter) tParamList(list []*TypeParam) {
 
 func (w *typeWriter) typeName(obj *TypeName) {
        if obj == nil {
-               assert(typeHashing == 0) // we need an object for type hashing
+               assert(!w.hash) // we need an object for type hashing
                w.string("<Named w/o object>")
                return
        }
@@ -300,7 +305,7 @@ func (w *typeWriter) typeName(obj *TypeName) {
        }
        w.string(obj.name)
 
-       if typeHashing != 0 {
+       if w.hash {
                // For local defined types, use the (original!) TypeName's scope
                // numbers to disambiguate.
                if typ, _ := obj.typ.(*Named); typ != nil {
@@ -334,7 +339,7 @@ func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
                                w.string(", ")
                        }
                        // parameter names are ignored for type identity and thus type hashes
-                       if typeHashing == 0 && v.name != "" {
+                       if !w.hash && v.name != "" {
                                w.string(v.name)
                                w.byte(' ')
                        }
@@ -382,8 +387,8 @@ func (w *typeWriter) signature(sig *Signature) {
        }
 
        w.byte(' ')
-       if n == 1 && (typeHashing != 0 || sig.results.vars[0].name == "") {
-               // single unnamed result (if typeHashing, name must be ignored)
+       if n == 1 && (w.hash || sig.results.vars[0].name == "") {
+               // single unnamed result (if type hashing, name must be ignored)
                w.typ(sig.results.vars[0].typ)
                return
        }