From 770cff8b918f5754062aa991cfdd19988ce4a788 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 6 May 2020 15:24:12 +0300 Subject: [PATCH] compiler: allow to use `return` with no arguments --- pkg/compiler/func_scope.go | 8 +++++--- pkg/compiler/function_call_test.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index d4a0ca862..de9d039e7 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -65,9 +65,11 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool { } } case *ast.ReturnStmt: - switch n.Results[0].(type) { - case *ast.CallExpr: - return false + if len(n.Results) > 0 { + switch n.Results[0].(type) { + case *ast.CallExpr: + return false + } } case *ast.BinaryExpr: return false diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index 7c1f22367..179e0d136 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -121,7 +121,14 @@ func TestFunctionWithVoidReturn(t *testing.T) { return x + y } - func getSomeInteger() { } + func getSomeInteger() { %s } ` - eval(t, src, big.NewInt(6)) + t.Run("EmptyBody", func(t *testing.T) { + src := fmt.Sprintf(src, "") + eval(t, src, big.NewInt(6)) + }) + t.Run("SingleReturn", func(t *testing.T) { + src := fmt.Sprintf(src, "return") + eval(t, src, big.NewInt(6)) + }) }