Skip to content

Commit

Permalink
interp: fix append on variadic recursive struct
Browse files Browse the repository at this point in the history
Fixes #1065. Improves #1058.
  • Loading branch information
mvertes authored Mar 26, 2021
1 parent 9926767 commit 2b1d6f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions _test/issue-1065.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

type AST struct {
Num int
Children []AST
}

func newAST(num int, root AST, children ...AST) AST {
return AST{num, append([]AST{root}, children...)}
}

func main() {
ast := newAST(1, AST{}, AST{})
fmt.Println(ast)
}

// Output:
// {1 [{0 []} {0 []}]}
2 changes: 1 addition & 1 deletion interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2947,7 +2947,7 @@ func _append(n *node) {
if len(n.child) == 3 {
c1, c2 := n.child[1], n.child[2]
if (c1.typ.cat == valueT || c2.typ.cat == valueT) && c1.typ.rtype == c2.typ.rtype ||
c2.typ.cat == arrayT && c2.typ.val.id() == n.typ.val.id() ||
(c2.typ.cat == arrayT || c2.typ.cat == variadicT) && c2.typ.val.id() == n.typ.val.id() ||
isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) {
appendSlice(n)
return
Expand Down
4 changes: 4 additions & 0 deletions interp/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (check typecheck) assignment(n *node, typ *itype, context string) error {
return nil
}

if typ.isRecursive() || typ.val != nil && typ.val.isRecursive() {
return nil
}

if !n.typ.assignableTo(typ) {
if context == "" {
return n.cfgErrorf("cannot use type %s as type %s", n.typ.id(), typ.id())
Expand Down

0 comments on commit 2b1d6f0

Please sign in to comment.