Skip to content

Commit cb9ccd4

Browse files
committed
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 <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent a1a6540 commit cb9ccd4

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/go/types/environment.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package types
66

7-
import "sync"
7+
import (
8+
"bytes"
9+
"sync"
10+
)
811

912
// An Environment is an opaque type checking environment. It may be used to
1013
// share identical type instances across type-checked packages or calls to
@@ -26,7 +29,36 @@ func NewEnvironment() *Environment {
2629
}
2730
}
2831

29-
// TODO(rfindley): move Environment.typeHash here.
32+
// typeHash returns a string representation of typ, which can be used as an exact
33+
// type hash: types that are identical produce identical string representations.
34+
// If typ is a *Named type and targs is not empty, typ is printed as if it were
35+
// instantiated with targs.
36+
func (env *Environment) typeHash(typ Type, targs []Type) string {
37+
assert(env != nil)
38+
assert(typ != nil)
39+
var buf bytes.Buffer
40+
41+
h := newTypeHasher(&buf, env)
42+
if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
43+
// Don't use WriteType because we need to use the provided targs
44+
// and not any targs that might already be with the *Named type.
45+
h.typePrefix(named)
46+
h.typeName(named.obj)
47+
h.typeList(targs)
48+
} else {
49+
assert(targs == nil)
50+
h.typ(typ)
51+
}
52+
53+
if debug {
54+
// there should be no instance markers in type hashes
55+
for _, b := range buf.Bytes() {
56+
assert(b != instanceMarker)
57+
}
58+
}
59+
60+
return buf.String()
61+
}
3062

3163
// typeForHash returns the recorded type for the type hash h, if it exists.
3264
// If no type exists for h and n is non-nil, n is recorded for h.

src/go/types/subst.go

+1-35
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
package types
88

9-
import (
10-
"bytes"
11-
"go/token"
12-
)
9+
import "go/token"
1310

1411
// TODO(rFindley) decide error codes for the errors in this file, and check
1512
// if error spans can be improved
@@ -256,37 +253,6 @@ func (subst *subster) typ(typ Type) Type {
256253
return typ
257254
}
258255

259-
// typeHash returns a string representation of typ, which can be used as an exact
260-
// type hash: types that are identical produce identical string representations.
261-
// If typ is a *Named type and targs is not empty, typ is printed as if it were
262-
// instantiated with targs.
263-
func (env *Environment) typeHash(typ Type, targs []Type) string {
264-
assert(env != nil)
265-
assert(typ != nil)
266-
var buf bytes.Buffer
267-
268-
h := newTypeHasher(&buf, env)
269-
if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
270-
// Don't use WriteType because we need to use the provided targs
271-
// and not any targs that might already be with the *Named type.
272-
h.typePrefix(named)
273-
h.typeName(named.obj)
274-
h.typeList(targs)
275-
} else {
276-
assert(targs == nil)
277-
h.typ(typ)
278-
}
279-
280-
if debug {
281-
// there should be no instance markers in type hashes
282-
for _, b := range buf.Bytes() {
283-
assert(b != instanceMarker)
284-
}
285-
}
286-
287-
return buf.String()
288-
}
289-
290256
// typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid].
291257
// A nil type may appear in pathological cases such as type T[P any] []func(_ T([]_))
292258
// where an array/slice element is accessed before it is set up.

0 commit comments

Comments
 (0)