From 6b3f4d388fe5602172f45361f438edf54699b953 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 15 Nov 2021 15:28:12 -0800 Subject: [PATCH] cmd/compile/internal/types2: re-use type hashing logic in Context.typeHash This CL is clean port of CL 362800 from go/types to types2. Change-Id: I66443b5a82b3a9c2f608a0fe012fbb099db996f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/364155 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/context.go | 26 +++++++------------ .../compile/internal/types2/instantiate.go | 4 +-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/cmd/compile/internal/types2/context.go b/src/cmd/compile/internal/types2/context.go index b6fd9822b27..93a0cb8d40d 100644 --- a/src/cmd/compile/internal/types2/context.go +++ b/src/cmd/compile/internal/types2/context.go @@ -36,28 +36,22 @@ func NewContext() *Context { } } -// 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 -// instantiated with targs. The result is guaranteed to not contain blanks (" "). +// typeHash returns a string representation of typ instantiated with targs, +// which can be used as an exact type hash: types that are identical produce +// identical string representations. If targs is not empty, typ is printed as +// if it were instantiated with targs. The result is guaranteed to not contain +// blanks (" "). func (ctxt *Context) typeHash(typ Type, targs []Type) string { assert(ctxt != nil) assert(typ != nil) var buf bytes.Buffer h := newTypeHasher(&buf, ctxt) - // Caution: don't use asNamed here. TypeHash may be called for unexpanded - // types. We don't need anything other than name and type arguments below, - // which do not require expansion. - 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. - h.typePrefix(named) - h.typeName(named.obj) + h.typ(typ) + if len(targs) > 0 { + // TODO(rfindley): consider asserting on isGeneric(typ) here, if and when + // isGeneric handles *Signature types. h.typeList(targs) - } else { - assert(targs == nil) - h.typ(typ) } return strings.Replace(buf.String(), " ", "#", -1) // ReplaceAll is not available in Go1.4 @@ -65,7 +59,7 @@ func (ctxt *Context) typeHash(typ Type, targs []Type) string { // lookup returns an existing instantiation of orig with targs, if it exists. // Otherwise, it returns nil. -func (ctxt *Context) lookup(h string, orig *Named, targs []Type) Type { +func (ctxt *Context) lookup(h string, orig Type, targs []Type) Type { ctxt.mu.Lock() defer ctxt.mu.Unlock() diff --git a/src/cmd/compile/internal/types2/instantiate.go b/src/cmd/compile/internal/types2/instantiate.go index 9408fa43d97..299d63dc60e 100644 --- a/src/cmd/compile/internal/types2/instantiate.go +++ b/src/cmd/compile/internal/types2/instantiate.go @@ -60,8 +60,8 @@ func (check *Checker) instance(pos syntax.Pos, orig Type, targs []Type, ctxt *Co h = ctxt.typeHash(orig, targs) // typ may already have been instantiated with identical type arguments. In // that case, re-use the existing instance. - if named := ctxt.lookup(h, orig, targs); named != nil { - return named + if inst := ctxt.lookup(h, orig, targs); inst != nil { + return inst } } tname := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)