diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index 5c202c8aa..b66c8e1dd 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -139,16 +139,18 @@ func (c *funcScope) stackSize() int64 { // newVariable creates a new local variable or argument in the scope of the function. func (c *funcScope) newVariable(t varType, name string) int { - c.i++ + var n int switch t { case varLocal: - c.locals[name] = c.i + n = len(c.locals) + c.locals[name] = n case varArgument: - c.arguments[name] = c.i + n = len(c.arguments) + c.arguments[name] = n default: panic("invalid type") } - return c.i + return n } // newLocal creates a new local variable into the scope of the function. diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index c9d9ca04c..446ac0b9a 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -169,3 +169,18 @@ func TestFunctionWithMultipleArgumentNames(t *testing.T) { }` eval(t, src, big.NewInt(3)) } + +func TestLocalsCount(t *testing.T) { + src := `package foo + func f(a, b, c int) int { + sum := a + for i := 0; i < c; i++ { + sum += b + } + return sum + } + func Main() int { + return f(1, 2, 3) + }` + eval(t, src, big.NewInt(7)) +}