go/types: don't print instance markers for type hashes

This is a port of CL 345891 to go/types.

Change-Id: I5abcb9c9c5110923a743f0c47d9b34b2baabab68
Reviewed-on: https://go-review.googlesource.com/c/go/+/346555
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Robert Findley 2021-08-31 17:59:05 -04:00
parent 2d98a4b4bc
commit 3c8c9e1e44
2 changed files with 12 additions and 13 deletions

View File

@ -259,28 +259,23 @@ func (subst *subster) typ(typ Type) Type {
var instanceHashing = 0 var instanceHashing = 0
func instantiatedHash(typ *Named, targs []Type) string { func instantiatedHash(typ *Named, targs []Type) string {
var buf bytes.Buffer
assert(instanceHashing == 0) assert(instanceHashing == 0)
instanceHashing++ instanceHashing++
var buf bytes.Buffer
w := newTypeWriter(&buf, nil) w := newTypeWriter(&buf, nil)
w.typeName(typ.obj) w.typeName(typ.obj)
w.typeList(targs) w.typeList(targs)
instanceHashing-- instanceHashing--
// With respect to the represented type, whether a if debug {
// type is fully expanded or stored as instance // there should be no instance markers in type hashes
// does not matter - they are the same types. for _, b := range buf.Bytes() {
// Remove the instanceMarkers printed for instances. assert(b != instanceMarker)
res := buf.Bytes()
i := 0
for _, b := range res {
if b != instanceMarker {
res[i] = b
i++
} }
} }
return string(res[:i]) return buf.String()
} }
// typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid]. // typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid].

View File

@ -204,7 +204,11 @@ func (w *typeWriter) typ(typ Type) {
} }
case *Named: case *Named:
if t.instPos != nil { // Instance markers indicate unexpanded instantiated
// 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 instanceHashing == 0 && t.instPos != nil {
w.byte(instanceMarker) w.byte(instanceMarker)
} }
w.typeName(t.obj) w.typeName(t.obj)