File tree 2 files changed +35
-37
lines changed
2 files changed +35
-37
lines changed Original file line number Diff line number Diff line change 4
4
5
5
package types
6
6
7
- import "sync"
7
+ import (
8
+ "bytes"
9
+ "sync"
10
+ )
8
11
9
12
// An Environment is an opaque type checking environment. It may be used to
10
13
// share identical type instances across type-checked packages or calls to
@@ -26,7 +29,36 @@ func NewEnvironment() *Environment {
26
29
}
27
30
}
28
31
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
+ }
30
62
31
63
// typeForHash returns the recorded type for the type hash h, if it exists.
32
64
// If no type exists for h and n is non-nil, n is recorded for h.
Original file line number Diff line number Diff line change 6
6
7
7
package types
8
8
9
- import (
10
- "bytes"
11
- "go/token"
12
- )
9
+ import "go/token"
13
10
14
11
// TODO(rFindley) decide error codes for the errors in this file, and check
15
12
// if error spans can be improved
@@ -256,37 +253,6 @@ func (subst *subster) typ(typ Type) Type {
256
253
return typ
257
254
}
258
255
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
-
290
256
// typOrNil is like typ but if the argument is nil it is replaced with Typ[Invalid].
291
257
// A nil type may appear in pathological cases such as type T[P any] []func(_ T([]_))
292
258
// where an array/slice element is accessed before it is set up.
You can’t perform that action at this time.
0 commit comments