From f43d01bdc789328437e88927c2e2ecf4a8648390 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 6 Aug 2019 11:40:23 +0200 Subject: [PATCH] compiler: make struct types more unique There are a lot more fields that are important when comparing structs with each other. Take them into account when building the unique ID per struct type. Example code that differs between the compilers: https://play.golang.org/p/nDX4tSHOf_T --- compiler/interface.go | 9 ++++++++- testdata/interface.go | 12 ++++++++++++ testdata/interface.txt | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler/interface.go b/compiler/interface.go index 94a60f3d..beda21a6 100644 --- a/compiler/interface.go +++ b/compiler/interface.go @@ -161,7 +161,14 @@ func getTypeCodeName(t types.Type) string { panic("cgo unions are not allowed in interfaces") } for i := 0; i < t.NumFields(); i++ { - elems[i] = getTypeCodeName(t.Field(i).Type()) + embedded := "" + if t.Field(i).Embedded() { + embedded = "#" + } + elems[i] = embedded + t.Field(i).Name() + ":" + getTypeCodeName(t.Field(i).Type()) + if t.Tag(i) != "" { + elems[i] += "`" + t.Tag(i) + "`" + } } return "struct:" + "{" + strings.Join(elems, ",") + "}" default: diff --git a/testdata/interface.go b/testdata/interface.go index 69c3b32c..88b0944b 100644 --- a/testdata/interface.go +++ b/testdata/interface.go @@ -11,6 +11,10 @@ func main() { printItf(*thing) printItf(thing) printItf(Stringer(thing)) + printItf(struct{ n int }{}) + printItf(struct { + n int `foo:"bar"` + }{}) printItf(Number(3)) array := Array([4]uint32{1, 7, 11, 13}) printItf(array) @@ -47,6 +51,14 @@ func printItf(val interface{}) { println("is Thing:", val.String()) case *Thing: println("is *Thing:", val.String()) + case struct{ i int }: + println("is struct{i int}") + case struct{ n int }: + println("is struct{n int}") + case struct { + n int `foo:"bar"` + }: + println("is struct{n int `foo:\"bar\"`}") case Foo: println("is Foo:", val) default: diff --git a/testdata/interface.txt b/testdata/interface.txt index 3f398974..83ee80b0 100644 --- a/testdata/interface.txt +++ b/testdata/interface.txt @@ -7,6 +7,8 @@ is Foo: 18 is Thing: foo is *Thing: foo is *Thing: foo +is struct{n int} +is struct{n int `foo:"bar"`} is Doubler: 6 is Tuple: 1 7 11 13 Array len: 4