From b18a7f200c746229f05cea7c2ded163bb7740224 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 20 Aug 2020 15:35:02 +0300 Subject: [PATCH] compiler: calculate local variables properly In case when right-hand side of an assignment is a function, left-hand side should be used. --- pkg/compiler/func_scope.go | 2 +- pkg/compiler/global_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index aba84a5b9..7a9e09ed6 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -124,7 +124,7 @@ func (c *funcScope) countLocals() int { } case *ast.AssignStmt: if n.Tok == token.DEFINE { - size += len(n.Rhs) + size += len(n.Lhs) } case *ast.ReturnStmt, *ast.IfStmt: size++ diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index 2be4caf30..e4f29f89b 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -38,6 +38,18 @@ func TestMultiDeclaration(t *testing.T) { eval(t, src, big.NewInt(6)) } +func TestCountLocal(t *testing.T) { + src := `package foo + func Main() int { + a, b, c, d := f() + return a + b + c + d + } + func f() (int, int, int, int) { + return 1, 2, 3, 4 + }` + eval(t, src, big.NewInt(10)) +} + func TestMultiDeclarationLocal(t *testing.T) { src := `package foo func Main() int {