Skip to content

Commit 5cd1b84

Browse files
committed
go/types: eliminate typeHashing global variable
This is a port of CL 345929 to go/types. It is also a step toward making instantiation concurrency-safe. Also fix some whitespace in instantiate.go. Updates #47910 Change-Id: Icdeb227cb83eee15da6db90daab294c8c55db601 Reviewed-on: https://go-review.googlesource.com/c/go/+/346557 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 0df6df1 commit 5cd1b84

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/go/types/instantiate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ func (check *Checker) instance(pos token.Pos, typ Type, targs []Type) Type {
121121
case *Named:
122122
h := typeHash(t, targs)
123123
if check != nil {
124-
// typ may already have been instantiated with identical type arguments. In
125-
// that case, re-use the existing instance.
124+
// typ may already have been instantiated with identical type arguments.
125+
// In that case, re-use the existing instance.
126126
if named := check.typMap[h]; named != nil {
127127
return named
128128
}
@@ -135,6 +135,7 @@ func (check *Checker) instance(pos token.Pos, typ Type, targs []Type) Type {
135135
check.typMap[h] = named
136136
}
137137
return named
138+
138139
case *Signature:
139140
tparams := t.TParams()
140141
if !check.validateTArgLen(pos, tparams.Len(), len(targs)) {

src/go/types/subst.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ func (subst *subster) typ(typ Type) Type {
256256
return typ
257257
}
258258

259-
var typeHashing = 0
260-
261259
// typeHash returns a string representation of typ, which can be used as an exact
262260
// type hash: types that are identical produce identical string representations.
263261
// If typ is a *Named type and targs is not empty, typ is printed as if it were
@@ -266,19 +264,16 @@ func typeHash(typ Type, targs []Type) string {
266264
assert(typ != nil)
267265
var buf bytes.Buffer
268266

269-
assert(typeHashing == 0)
270-
typeHashing++
271-
w := newTypeWriter(&buf, nil)
267+
h := newTypeHasher(&buf)
272268
if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
273269
// Don't use WriteType because we need to use the provided targs
274270
// and not any targs that might already be with the *Named type.
275-
w.typeName(named.obj)
276-
w.typeList(targs)
271+
h.typeName(named.obj)
272+
h.typeList(targs)
277273
} else {
278274
assert(targs == nil)
279-
w.typ(typ)
275+
h.typ(typ)
280276
}
281-
typeHashing--
282277

283278
if debug {
284279
// there should be no instance markers in type hashes

src/go/types/typestring.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@ type typeWriter struct {
6363
buf *bytes.Buffer
6464
seen map[Type]bool
6565
qf Qualifier
66+
hash bool
6667
}
6768

6869
func newTypeWriter(buf *bytes.Buffer, qf Qualifier) *typeWriter {
69-
return &typeWriter{buf, make(map[Type]bool), qf}
70+
return &typeWriter{buf, make(map[Type]bool), qf, false}
71+
}
72+
73+
func newTypeHasher(buf *bytes.Buffer) *typeWriter {
74+
return &typeWriter{buf, make(map[Type]bool), nil, true}
7075
}
7176

7277
func (w *typeWriter) byte(b byte) { w.buf.WriteByte(b) }
@@ -208,7 +213,7 @@ func (w *typeWriter) typ(typ Type) {
208213
// types. Write them to aid debugging, but don't write
209214
// them when we need an instance hash: whether a type
210215
// is fully expanded or not doesn't matter for identity.
211-
if typeHashing == 0 && t.instPos != nil {
216+
if !w.hash && t.instPos != nil {
212217
w.byte(instanceMarker)
213218
}
214219
w.typeName(t.obj)
@@ -292,7 +297,7 @@ func (w *typeWriter) tParamList(list []*TypeParam) {
292297

293298
func (w *typeWriter) typeName(obj *TypeName) {
294299
if obj == nil {
295-
assert(typeHashing == 0) // we need an object for type hashing
300+
assert(!w.hash) // we need an object for type hashing
296301
w.string("<Named w/o object>")
297302
return
298303
}
@@ -301,7 +306,7 @@ func (w *typeWriter) typeName(obj *TypeName) {
301306
}
302307
w.string(obj.name)
303308

304-
if typeHashing != 0 {
309+
if w.hash {
305310
// For local defined types, use the (original!) TypeName's scope
306311
// numbers to disambiguate.
307312
if typ, _ := obj.typ.(*Named); typ != nil {
@@ -335,7 +340,7 @@ func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
335340
w.string(", ")
336341
}
337342
// parameter names are ignored for type identity and thus type hashes
338-
if typeHashing == 0 && v.name != "" {
343+
if !w.hash && v.name != "" {
339344
w.string(v.name)
340345
w.byte(' ')
341346
}
@@ -383,8 +388,8 @@ func (w *typeWriter) signature(sig *Signature) {
383388
}
384389

385390
w.byte(' ')
386-
if n == 1 && (typeHashing != 0 || sig.results.vars[0].name == "") {
387-
// single unnamed result (if typeHashing, name must be ignored)
391+
if n == 1 && (w.hash || sig.results.vars[0].name == "") {
392+
// single unnamed result (if type hashing, name must be ignored)
388393
w.typ(sig.results.vars[0].typ)
389394
return
390395
}

0 commit comments

Comments
 (0)