From a64fe5b21085438c739af2c82c32c252e113158b Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 15 Jan 2021 12:14:04 +0100 Subject: [PATCH] interp: fix detection of type recursivity If a struct contains several fields of the same temporary incomplete type, it could be detected incorrectly as a recursive struct. Pass a copy of defined types map to avoid this issue. Fixes #1007. --- _test/issue-1007.go | 40 ++++++++++++++++++++++++++++++++++++++++ interp/type.go | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 _test/issue-1007.go diff --git a/_test/issue-1007.go b/_test/issue-1007.go new file mode 100644 index 000000000..54d3c702f --- /dev/null +++ b/_test/issue-1007.go @@ -0,0 +1,40 @@ +package main + +type TypeA struct { + B TypeB +} + +type TypeB struct { + C1 *TypeC + C2 *TypeC +} + +type TypeC struct { + Val string + D *TypeD + D2 *TypeD +} + +type TypeD struct { + Name string +} + +func build() *TypeA { + return &TypeA{ + B: TypeB{ + C2: &TypeC{Val: "22"}, + }, + } +} + +func Bar(s string) string { + a := build() + return s + "-" + a.B.C2.Val +} + +func main() { + println(Bar("test")) +} + +// Output: +// test-22 diff --git a/interp/type.go b/interp/type.go index b92ef6bac..6fde115d8 100644 --- a/interp/type.go +++ b/interp/type.go @@ -1551,7 +1551,7 @@ func hasRecursiveStruct(t *itype, defined map[string]*itype) bool { defined[typ.path+"/"+typ.name] = typ for _, f := range typ.field { - if hasRecursiveStruct(f.typ, defined) { + if hasRecursiveStruct(f.typ, copyDefined(defined)) { return true } }