From: Matthew Dempsky Date: Fri, 18 Mar 2022 02:50:47 +0000 (-0700) Subject: cmd/compile/internal/types: use Type.LinkString in TypeHash X-Git-Tag: go1.19beta1~221 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a900a176bf946ac220336c7c5dad107745fc9c41;p=gostls13.git 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 TryBot-Result: Gopher Robot Reviewed-by: David Chase Run-TryBot: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/types/fmt.go b/src/cmd/compile/internal/types/fmt.go index c0eeb8685f..c1138643b7 100644 --- a/src/cmd/compile/internal/types/fmt.go +++ b/src/cmd/compile/internal/types/fmt.go @@ -72,7 +72,6 @@ const ( fmtDebug fmtTypeID fmtTypeIDName - fmtTypeIDHash ) // Sym @@ -144,18 +143,6 @@ func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) { if q := pkgqual(s.Pkg, verb, mode); q != "" { b.WriteString(q) b.WriteByte('.') - switch mode { - case fmtTypeIDHash: - // If name is a generic instantiation, don't hash the instantiating types. - // This isn't great, but it is safe. If we hash the instantiating types, then - // we need to make sure they have just the package name. At this point, they - // either have "", or the whole package path, and it is hard to reconcile - // the two without depending on -p (which we might do someday). - // See issue 51250. - if i := strings.Index(name, "["); i >= 0 { - name = name[:i] - } - } } b.WriteString(name) } @@ -183,7 +170,7 @@ func pkgqual(pkg *Pkg, verb rune, mode fmtMode) string { case fmtDebug: return pkg.Name - case fmtTypeIDName, fmtTypeIDHash: + case fmtTypeIDName: // dcommontype, typehash return pkg.Name @@ -329,7 +316,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type if t == AnyType || t == ByteType || t == RuneType { // in %-T mode collapse predeclared aliases with their originals. switch mode { - case fmtTypeIDName, fmtTypeIDHash, fmtTypeID: + case fmtTypeIDName, fmtTypeID: t = Types[t.Kind()] default: sconv2(b, t.Sym(), 'S', mode) @@ -420,7 +407,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type case TPTR: b.WriteByte('*') switch mode { - case fmtTypeID, fmtTypeIDName, fmtTypeIDHash: + case fmtTypeID, fmtTypeIDName: if verb == 'S' { tconv2(b, t.Elem(), 'S', mode, visited) return @@ -482,7 +469,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type case IsExported(f.Sym.Name): sconv2(b, f.Sym, 'S', mode) default: - if mode != fmtTypeIDName && mode != fmtTypeIDHash { + if mode != fmtTypeIDName { mode = fmtTypeID } sconv2(b, f.Sym, 'v', mode) @@ -552,7 +539,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type b.WriteByte(byte(open)) fieldVerb := 'v' switch mode { - case fmtTypeID, fmtTypeIDName, fmtTypeIDHash, fmtGo: + case fmtTypeID, fmtTypeIDName, fmtGo: // no argument names on function signature, and no "noescape"/"nosplit" tags fieldVerb = 'S' } @@ -686,7 +673,7 @@ func fldconv(b *bytes.Buffer, f *Field, verb rune, mode fmtMode, visited map[*Ty if name == ".F" { name = "F" // Hack for toolstash -cmp. } - if !IsExported(name) && mode != fmtTypeIDName && mode != fmtTypeIDHash { + if !IsExported(name) && mode != fmtTypeIDName { name = sconv(s, 0, mode) // qualify non-exported names (used on structs, not on funarg) } } else { @@ -754,7 +741,7 @@ func FmtConst(v constant.Value, sharp bool) string { // TypeHash computes a hash value for type t to use in type switch statements. func TypeHash(t *Type) uint32 { - p := tconv(t, 0, fmtTypeIDHash) + p := t.LinkString() // Using SHA256 is overkill, but reduces accidental collisions. h := notsha256.Sum256([]byte(p))