]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: move typeHash to environment.go
authorRobert Findley <rfindley@google.com>
Fri, 3 Sep 2021 15:17:37 +0000 (11:17 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 8 Sep 2021 16:31:42 +0000 (16:31 +0000)
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>
src/go/types/environment.go
src/go/types/subst.go

index f8c14c87bf708a1b9e8246a62d3554ebb329119f..93383efe1a2922df509b4cbb6dcfd4587f6044fc 100644 (file)
@@ -4,7 +4,10 @@
 
 package types
 
-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
@@ -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.
 // If no type exists for h and n is non-nil, n is recorded for h.
index d0ef07652f2299657c9ed9a6419be08135db2ad1..452ea5f75feaeb1f8030ad2ece55415985a4039c 100644 (file)
@@ -6,10 +6,7 @@
 
 package types
 
-import (
-       "bytes"
-       "go/token"
-)
+import "go/token"
 
 // TODO(rFindley) decide error codes for the errors in this file, and check
 //                if error spans can be improved
@@ -256,37 +253,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.