]> granicus.if.org Git - vim/commitdiff
patch 8.2.4138: Vim9: no error for return with argument when invalid v8.2.4138
authorBram Moolenaar <Bram@vim.org>
Tue, 18 Jan 2022 18:46:07 +0000 (18:46 +0000)
committerBram Moolenaar <Bram@vim.org>
Tue, 18 Jan 2022 18:46:07 +0000 (18:46 +0000)
Problem:    Vim9: no error for return with argument when the function does not
            return anything.
Solution:   Give an error for the invalid argument. (issue #9497)

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

index 75d0dd0b65e81dfe03d37f933aad897d90726103..f495ddfd78aef3ac44546764a29f949585ba8e41 100644 (file)
@@ -341,6 +341,20 @@ def Test_return_something()
   ReturnString()->assert_equal('string')
   ReturnNumber()->assert_equal(123)
   assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
+
+  var lines =<< trim END
+      vim9script
+
+      def Msg()
+          echomsg 'in Msg()...'
+      enddef
+
+      def Func()
+        return Msg()
+      enddef
+      defcompile
+  END
+  CheckScriptFailure(lines, 'E1096:')
 enddef
 
 def Test_check_argument_type()
index 67e9ca5ff318c91c287a07100e1fe9ba0c8aba47..6198d410609209892ec39c46aa8b2c4bdf400a98 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4138,
 /**/
     4137,
 /**/
index 5beeab82e82c9fbb7abfda60528bd35eae198bb2..83ff6991bd725ea9bc9d7a0eca10b6807d49d5bd 100644 (file)
@@ -2196,6 +2196,11 @@ compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
 
     if (*p != NUL && *p != '|' && *p != '\n')
     {
+       if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID)
+       {
+           emsg(_(e_returning_value_in_function_without_return_type));
+           return NULL;
+       }
        if (legacy)
        {
            int save_flags = cmdmod.cmod_flags;
@@ -2231,13 +2236,6 @@ compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *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(_(e_returning_value_in_function_without_return_type));
-                   return NULL;
-               }
                if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
                                                0, cctx, FALSE, FALSE) == FAIL)
                    return NULL;