interp: fix append on variadic recursive struct

Fixes #1065. Improves #1058.
This commit is contained in:
Marc Vertes
2021-03-26 09:34:03 +01:00
committed by GitHub
parent 992676722d
commit 2b1d6f0e7a
3 changed files with 25 additions and 1 deletions

20
_test/issue-1065.go Normal file
View File

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

View File

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

View File

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