hash structs via the bundled and altered typeutil.hash

As spotted by the protobuf package via check-third-party.sh,
the two structs below are identical:

    type alias1 = int64
    type Struct1 struct { Alias alias1 }

    type alias2 = int64
    type Struct2 struct { Alias alias2 }

Our previous approach with stripStructTags dealt with struct tags,
but it did not deal with aliases, which are now present in go/types
thanks to the new alias tracking.

The new approach properly ignores struct tags and unaliases any
type aliases, resulting in correct hashes of any type.
This commit is contained in:
Daniel Martí
2025-01-17 00:57:19 +00:00
committed by Paul Scheduikat
parent 8ca3d0adcf
commit e0dbea2b3d
2 changed files with 30 additions and 55 deletions
+18 -4
View File
@@ -1,4 +1,5 @@
exec garble build
go build
exec garble -debugdir=/tmp/test build
exec ./main
cmp stdout main.stdout
@@ -45,12 +46,15 @@ var _ privateInterface = T("")
type StructUnnamed = struct {
Foo int
Bar struct {
Bar []*struct {
Nested *[]string
NestedTagged string
NestedAlias int64 // we can skip the alias too
}
Named lib3.Named
lib3.StructEmbed
Tagged string // no field tag
Alias int64 // we can skip the alias too
}
var _ = lib1.Struct1(lib2.Struct2{})
@@ -83,14 +87,19 @@ import "test/main/lib3"
type Struct1 struct {
Foo int
Bar struct {
Bar []*struct {
Nested *[]string
NestedTagged string `json:"tagged1"`
NestedAlias alias1
}
Named lib3.Named
lib3.StructEmbed
Tagged string `json:"tagged1"`
Alias alias1
}
type alias1 = int64
-- lib2/lib2.go --
package lib2
@@ -98,13 +107,18 @@ import "test/main/lib3"
type Struct2 struct {
Foo int
Bar struct {
Bar []*struct {
Nested *[]string
NestedTagged string `json:"tagged2"`
NestedAlias alias2
}
Named lib3.Named
lib3.StructEmbed
Tagged string `json:"tagged2"`
Alias alias2
}
type alias2 = int64
-- lib3/lib3.go --
package lib3