From d8bdc6670bb1f8b87adab900a2ff8645e9d9373b Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 22 Feb 2020 15:24:06 +0100 Subject: [PATCH] fix: detect field names in struct pointer --- _test/composite7.go | 20 ++++++++++++++++++++ interp/type.go | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 _test/composite7.go diff --git a/_test/composite7.go b/_test/composite7.go new file mode 100644 index 000000000..dd037ded4 --- /dev/null +++ b/_test/composite7.go @@ -0,0 +1,20 @@ +package main + +type T struct { + name string +} + +var tab = []*T{{ + name: "foo", +}, { + name: "bar", +}} + +func main() { + println(len(tab)) + println(tab[0].name) +} + +// Output: +// 2 +// foo diff --git a/interp/type.go b/interp/type.go index d0a86b30d..a1a25427a 100644 --- a/interp/type.go +++ b/interp/type.go @@ -1017,7 +1017,12 @@ func isInterface(t *itype) bool { func isStruct(t *itype) bool { // Test first for a struct category, because a recursive interpreter struct may be // represented by an interface{} at reflect level. - return t.cat == structT || t.TypeOf().Kind() == reflect.Struct + if t.cat == structT { + return true + } + rt := t.TypeOf() + k := rt.Kind() + return k == reflect.Struct || (k == reflect.Ptr && rt.Elem().Kind() == reflect.Struct) } func isBool(t *itype) bool { return t.TypeOf().Kind() == reflect.Bool }