Skip to content

Commit

Permalink
interp: fix detection of type recursivity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvertes authored Jan 15, 2021
1 parent 5c59dc4 commit a64fe5b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions _test/issue-1007.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit a64fe5b

Please sign in to comment.