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.
This commit is contained in:
Marc Vertes
2021-01-15 12:14:04 +01:00
committed by GitHub
parent 5c59dc425f
commit a64fe5b210
2 changed files with 41 additions and 1 deletions

40
_test/issue-1007.go Normal file
View File

@@ -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

View File

@@ -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
}
}