]> granicus.if.org Git - vim/commitdiff
patch 8.2.1131: Vim9: error message for returning a value is not clear v8.2.1131
authorBram Moolenaar <Bram@vim.org>
Sun, 5 Jul 2020 13:52:19 +0000 (15:52 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 5 Jul 2020 13:52:19 +0000 (15:52 +0200)
Problem:    Vim9: error message for returning a value in a function that does
            not return anything is not clear.
Solution:   Add a specific message.

src/testdir/test_vim9_func.vim
src/version.c
src/vim9compile.c

index 6a03eddc1d53e1414dc5484cc79e6b8dcb1a1664..583a232287c142fe2faff4665101009e89530b4e 100644 (file)
@@ -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:')
 
index c94988faf05c990e7accf7734f283deb76ac5ec3..8f85bb7bf03aabfaba2975b55a8924d819cf409b 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1131,
 /**/
     1130,
 /**/
index b4209a1fee92869e9197b1e434a6ae619d7bd858..e5a801a28ea871fb930f92437658124c62cb8594 100644 (file)
@@ -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
     {