1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:24:51 -07:00

go/types: move typeHash to environment.go

This is a pure code move, with no other changes.

Change-Id: Id31f1f960d3208dc614556de89bf39b7ca77df3a
Reviewed-on: https://go-review.googlesource.com/c/go/+/347560
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Robert Findley 2021-09-03 11:17:37 -04:00
parent a1a6540bf1
commit cb9ccd494b
2 changed files with 35 additions and 37 deletions

View File

@ -4,7 +4,10 @@
package types package types
import "sync" import (
"bytes"
"sync"
)
// An Environment is an opaque type checking environment. It may be used to // An Environment is an opaque type checking environment. It may be used to
// share identical type instances across type-checked packages or calls to // share identical type instances across type-checked packages or calls to
@ -26,7 +29,36 @@ func NewEnvironment() *Environment {
} }
} }
// TODO(rfindley): move Environment.typeHash here. // 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.
func (env *Environment) typeHash(typ Type, targs []Type) string {
assert(env != nil)
assert(typ != nil)
var buf bytes.Buffer
h := newTypeHasher(&buf, env)
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.typeList(targs)
} else {
assert(targs == nil)
h.typ(typ)
}
if debug {
// there should be no instance markers in type hashes
for _, b := range buf.Bytes() {
assert(b != instanceMarker)
}
}
return buf.String()
}
// typeForHash returns the recorded type for the type hash h, if it exists. // typeForHash returns the recorded type for the type hash h, if it exists.
// If no type exists for h and n is non-nil, n is recorded for h. // If no type exists for h and n is non-nil, n is recorded for h.

View File

@ -6,10 +6,7 @@
package types package types
import ( import "go/token"
"bytes"
"go/token"
)
// TODO(rFindley) decide error codes for the errors in this file, and check // TODO(rFindley) decide error codes for the errors in this file, and check
// if error spans can be improved // if error spans can be improved
@ -256,37 +253,6 @@ func (subst *subster) typ(typ Type) Type {
return typ return typ
} }
// 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.
func (env *Environment) typeHash(typ Type, targs []Type) string {
assert(env != nil)
assert(typ != nil)
var buf bytes.Buffer
h := newTypeHasher(&buf, env)
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.typeList(targs)
} else {
assert(targs == nil)
h.typ(typ)
}
if debug {
// there should be no instance markers in type hashes
for _, b := range buf.Bytes() {
assert(b != instanceMarker)
}
}
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].
// A nil type may appear in pathological cases such as type T[P any] []func(_ T([]_)) // A nil type may appear in pathological cases such as type T[P any] []func(_ T([]_))
// where an array/slice element is accessed before it is set up. // where an array/slice element is accessed before it is set up.