diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index ff0eeb5bf..0a17a782e 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -582,6 +582,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { for _, spec := range n.Specs { switch t := spec.(type) { case *ast.ValueSpec: + multiRet := n.Tok == token.VAR && len(t.Values) != 0 && len(t.Names) != len(t.Values) for _, id := range t.Names { if id.Name != "_" { if c.scope == nil { @@ -590,12 +591,16 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { } else { c.scope.newLocal(id.Name) } - c.registerDebugVariable(id.Name, t.Type) + if !multiRet { + c.registerDebugVariable(id.Name, t.Type) + } } } for i := range t.Names { if len(t.Values) != 0 { - ast.Walk(c, t.Values[i]) + if i == 0 || !multiRet { + ast.Walk(c, t.Values[i]) + } } else { c.emitDefault(c.typeOf(t.Type)) } diff --git a/pkg/compiler/vardecl_test.go b/pkg/compiler/vardecl_test.go new file mode 100644 index 000000000..607cd98fa --- /dev/null +++ b/pkg/compiler/vardecl_test.go @@ -0,0 +1,31 @@ +package compiler_test + +import ( + "math/big" + "testing" +) + +func TestGenDeclWithMultiRet(t *testing.T) { + t.Run("global var decl", func(t *testing.T) { + src := `package foo + func Main() int { + var a, b = f() + return a + b + } + func f() (int, int) { + return 1, 2 + }` + eval(t, src, big.NewInt(3)) + }) + t.Run("local var decl", func(t *testing.T) { + src := `package foo + var a, b = f() + func Main() int { + return a + b + } + func f() (int, int) { + return 1, 2 + }` + eval(t, src, big.NewInt(3)) + }) +}