summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-07-05 15:52:19 +0200
committerBram Moolenaar <Bram@vim.org>2020-07-05 15:52:19 +0200
commit05a5551a86e013e35d1dfa10fd0d811c587f9c88 (patch)
treefdff5341055cc148712c4603f39a02c2a3f951d2
parent788123c00c4b7acc4e6ba3e9f1cc8b175ae9aae8 (diff)
downloadvim-git-05a5551a86e013e35d1dfa10fd0d811c587f9c88.tar.gz
patch 8.2.1131: Vim9: error message for returning a value is not clearv8.2.1131
Problem: Vim9: error message for returning a value in a function that does not return anything is not clear. Solution: Add a specific message.
-rw-r--r--src/testdir/test_vim9_func.vim6
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c12
3 files changed, 16 insertions, 4 deletions
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index 6a03eddc1..583a23228 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -286,14 +286,14 @@ enddef
def Test_error_in_nested_function()
" Error in called function requires unwinding the call stack.
- assert_fails('call FuncWithForwardCall()', 'E1013')
+ assert_fails('call FuncWithForwardCall()', 'E1096')
enddef
def Test_return_type_wrong()
CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef', 'defcompile'], 'expected number but got string')
CheckScriptFailure(['def Func(): string', 'return 1', 'enddef', 'defcompile'], 'expected string but got number')
- CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef', 'defcompile'], 'expected void but got string')
- CheckScriptFailure(['def Func()', 'return "a"', 'enddef', 'defcompile'], 'expected void but got string')
+ CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type')
+ CheckScriptFailure(['def Func()', 'return "a"', 'enddef', 'defcompile'], 'E1096: Returning a value in a function without a return type')
CheckScriptFailure(['def Func(): number', 'return', 'enddef', 'defcompile'], 'E1003:')
diff --git a/src/version.c b/src/version.c
index c94988faf..8f85bb7bf 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1131,
+/**/
1130,
/**/
1129,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index b4209a1fe..e5a801a28 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -4568,9 +4568,19 @@ compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (set_return_type)
cctx->ctx_ufunc->uf_ret_type = stack_type;
- else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
+ else
+ {
+ if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
+ && stack_type->tt_type != VAR_VOID
+ && stack_type->tt_type != VAR_UNKNOWN)
+ {
+ emsg(_("E1096: Returning a value in a function without a return type"));
+ return NULL;
+ }
+ if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
== FAIL)
return NULL;
+ }
}
else
{