Skip to content

Commit a900a17

Browse files
committed
cmd/compile/internal/types: use Type.LinkString in TypeHash
Now that Type.LinkString always returns a fully unique string ID, we can use it in TypeHash to avoid collisions between instantiations of the same generic type. Updates #51734. Change-Id: I38cb396c88259be7afa44bd4333124ca98666c3f Reviewed-on: https://go-review.googlesource.com/c/go/+/393716 Reviewed-by: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]>
1 parent d81dd12 commit a900a17

File tree

1 file changed

+7
-20
lines changed
  • src/cmd/compile/internal/types

1 file changed

+7
-20
lines changed

src/cmd/compile/internal/types/fmt.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const (
7272
fmtDebug
7373
fmtTypeID
7474
fmtTypeIDName
75-
fmtTypeIDHash
7675
)
7776

7877
// Sym
@@ -144,18 +143,6 @@ func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
144143
if q := pkgqual(s.Pkg, verb, mode); q != "" {
145144
b.WriteString(q)
146145
b.WriteByte('.')
147-
switch mode {
148-
case fmtTypeIDHash:
149-
// If name is a generic instantiation, don't hash the instantiating types.
150-
// This isn't great, but it is safe. If we hash the instantiating types, then
151-
// we need to make sure they have just the package name. At this point, they
152-
// either have "", or the whole package path, and it is hard to reconcile
153-
// the two without depending on -p (which we might do someday).
154-
// See issue 51250.
155-
if i := strings.Index(name, "["); i >= 0 {
156-
name = name[:i]
157-
}
158-
}
159146
}
160147
b.WriteString(name)
161148
}
@@ -183,7 +170,7 @@ func pkgqual(pkg *Pkg, verb rune, mode fmtMode) string {
183170
case fmtDebug:
184171
return pkg.Name
185172

186-
case fmtTypeIDName, fmtTypeIDHash:
173+
case fmtTypeIDName:
187174
// dcommontype, typehash
188175
return pkg.Name
189176

@@ -329,7 +316,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
329316
if t == AnyType || t == ByteType || t == RuneType {
330317
// in %-T mode collapse predeclared aliases with their originals.
331318
switch mode {
332-
case fmtTypeIDName, fmtTypeIDHash, fmtTypeID:
319+
case fmtTypeIDName, fmtTypeID:
333320
t = Types[t.Kind()]
334321
default:
335322
sconv2(b, t.Sym(), 'S', mode)
@@ -420,7 +407,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
420407
case TPTR:
421408
b.WriteByte('*')
422409
switch mode {
423-
case fmtTypeID, fmtTypeIDName, fmtTypeIDHash:
410+
case fmtTypeID, fmtTypeIDName:
424411
if verb == 'S' {
425412
tconv2(b, t.Elem(), 'S', mode, visited)
426413
return
@@ -482,7 +469,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
482469
case IsExported(f.Sym.Name):
483470
sconv2(b, f.Sym, 'S', mode)
484471
default:
485-
if mode != fmtTypeIDName && mode != fmtTypeIDHash {
472+
if mode != fmtTypeIDName {
486473
mode = fmtTypeID
487474
}
488475
sconv2(b, f.Sym, 'v', mode)
@@ -552,7 +539,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
552539
b.WriteByte(byte(open))
553540
fieldVerb := 'v'
554541
switch mode {
555-
case fmtTypeID, fmtTypeIDName, fmtTypeIDHash, fmtGo:
542+
case fmtTypeID, fmtTypeIDName, fmtGo:
556543
// no argument names on function signature, and no "noescape"/"nosplit" tags
557544
fieldVerb = 'S'
558545
}
@@ -686,7 +673,7 @@ func fldconv(b *bytes.Buffer, f *Field, verb rune, mode fmtMode, visited map[*Ty
686673
if name == ".F" {
687674
name = "F" // Hack for toolstash -cmp.
688675
}
689-
if !IsExported(name) && mode != fmtTypeIDName && mode != fmtTypeIDHash {
676+
if !IsExported(name) && mode != fmtTypeIDName {
690677
name = sconv(s, 0, mode) // qualify non-exported names (used on structs, not on funarg)
691678
}
692679
} else {
@@ -754,7 +741,7 @@ func FmtConst(v constant.Value, sharp bool) string {
754741

755742
// TypeHash computes a hash value for type t to use in type switch statements.
756743
func TypeHash(t *Type) uint32 {
757-
p := tconv(t, 0, fmtTypeIDHash)
744+
p := t.LinkString()
758745

759746
// Using SHA256 is overkill, but reduces accidental collisions.
760747
h := notsha256.Sum256([]byte(p))

0 commit comments

Comments
 (0)