aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-09-08 13:37:34 -0700
committerRobert Griesemer <gri@golang.org>2021-09-08 22:41:22 +0000
commitccc927b8f6550cb638e78fd63eebf422fc3c3d8a (patch)
tree58e743e2c34f62f31473f53baa598cd86082bd17 /src/cmd
parent30e9bfbcefb9492d66bd56ea7df6d6426ae8a711 (diff)
downloadgo-ccc927b8f6550cb638e78fd63eebf422fc3c3d8a.tar.xz
cmd/compile/internal/types2: move typeHash to environment.go
This is a clean port of CL 347560. Change-Id: I0d56f5a818df1a66e603415d5198d909b0aef228 Reviewed-on: https://go-review.googlesource.com/c/go/+/348573 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/compile/internal/types2/environment.go37
-rw-r--r--src/cmd/compile/internal/types2/subst.go36
2 files changed, 36 insertions, 37 deletions
diff --git a/src/cmd/compile/internal/types2/environment.go b/src/cmd/compile/internal/types2/environment.go
index 070cf34243..816139bbb4 100644
--- a/src/cmd/compile/internal/types2/environment.go
+++ b/src/cmd/compile/internal/types2/environment.go
@@ -3,7 +3,10 @@
// license that can be found in the LICENSE file.
package types2
-import "sync"
+import (
+ "bytes"
+ "sync"
+)
// An Environment is an opaque type checking environment. It may be used to
// share identical type instances across type-checked packages or calls to
@@ -25,7 +28,37 @@ 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.
// If no type exists for h and n is non-nil, n is recorded for h.
func (env *Environment) typeForHash(h string, n *Named) *Named {
diff --git a/src/cmd/compile/internal/types2/subst.go b/src/cmd/compile/internal/types2/subst.go
index f86555594d..2032305fab 100644
--- a/src/cmd/compile/internal/types2/subst.go
+++ b/src/cmd/compile/internal/types2/subst.go
@@ -6,10 +6,7 @@
package types2
-import (
- "bytes"
- "cmd/compile/internal/syntax"
-)
+import "cmd/compile/internal/syntax"
type substMap map[*TypeParam]Type
@@ -253,37 +250,6 @@ func (subst *subster) typ(typ Type) Type {
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].
// 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.