Skip to content

Commit

Permalink
fix: make a copy of defined before detecting recursivness
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Jul 7, 2020
1 parent 9d4685d commit bc2b224
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 26 additions & 0 deletions _test/struct54.go
@@ -0,0 +1,26 @@
package main

type S struct {
t *T
}

func newS() *S {
return &S{
t: &T{u: map[string]*U{}},
}
}

type T struct {
u map[string]*U
}

type U struct {
a int
}

func main() {
s := newS()
_ = s

println("ok")
}
10 changes: 9 additions & 1 deletion interp/type.go
Expand Up @@ -1260,7 +1260,7 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
if defined[name] != nil && defined[name].rtype != nil {
return defined[name].rtype
}
if t.val != nil && t.val.cat == structT && t.val.rtype == nil && hasRecursiveStruct(t.val, defined) {
if t.val != nil && t.val.cat == structT && t.val.rtype == nil && hasRecursiveStruct(t.val, copyDefined(defined)) {
// Replace reference to self (direct or indirect) by an interface{} to handle
// recursive types with reflect.
typ := *t.val
Expand Down Expand Up @@ -1367,6 +1367,14 @@ func (t *itype) implements(it *itype) bool {
return t.methods().contains(it.methods())
}

func copyDefined(m map[string]*itype) map[string]*itype {
n := make(map[string]*itype, len(m))
for k, v := range m {
n[k] = v
}
return n
}

// hasRecursiveStruct determines if a struct is a recursion or a recursion
// intermediate. A recursion intermediate is a struct that contains a recursive
// struct.
Expand Down

0 comments on commit bc2b224

Please sign in to comment.