]> granicus.if.org Git - vim/commitdiff
patch 8.2.1396: Vim9: no error for unexpectedly returning a value v8.2.1396
authorBram Moolenaar <Bram@vim.org>
Sat, 8 Aug 2020 14:47:30 +0000 (16:47 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 8 Aug 2020 14:47:30 +0000 (16:47 +0200)
Problem:    Vim9: no error for unexpectedly returning a value.
Solution:   Only set the return type for lambda's.  Make using function type
            in a function reference work.

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

index d39584154bc5eabcf46e1a522073297b42edba53..4a020c9afa678c5cce2ecbfcb56122c6856acd0f 100644 (file)
@@ -503,16 +503,48 @@ def Test_error_in_nested_function()
 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'], '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:')
+  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'],
+        '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:')
 
   CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
   CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
   CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
+
+  CheckScriptFailure([
+        'vim9script',
+        'def FuncB()',
+        '  return 123',
+        'enddef',
+        'def FuncA()',
+        '   FuncB()',
+        'enddef',
+        'defcompile'], 'E1096:')
 enddef
 
 def Test_arg_type_wrong()
index 870df176600193b43e95471c78561e1675981c47..8649ca89654fa2ba1943125b874172604dc61a74 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1396,
 /**/
     1395,
 /**/
index 0664124fe4c123b8d687258b59380b8c56c60dbd..5f76bc2c03c21c214ee38eb9d3ac040b5f722a1d 100644 (file)
@@ -662,8 +662,10 @@ check_type(type_T *expected, type_T *actual, int give_msg)
                int i;
 
                for (i = 0; i < expected->tt_argcount; ++i)
-                   if (check_type(expected->tt_args[i], actual->tt_args[i],
-                                                               FALSE) == FAIL)
+                   // Allow for using "any" argument type, lambda's have them.
+                   if (actual->tt_args[i] != &t_any && check_type(
+                              expected->tt_args[i], actual->tt_args[i], FALSE)
+                                                                      == FAIL)
                    {
                        ret = FAIL;
                        break;
@@ -1537,7 +1539,7 @@ generate_NEWDICT(cctx_T *cctx, int count)
  * Generate an ISN_FUNCREF instruction.
  */
     static int
-generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
+generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
 {
     isn_T      *isn;
     garray_T   *stack = &cctx->ctx_type_stack;
@@ -1545,13 +1547,13 @@ generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
     RETURN_OK_IF_SKIP(cctx);
     if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
        return FAIL;
-    isn->isn_arg.funcref.fr_func = dfunc_idx;
+    isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
     isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
 
     if (ga_grow(stack, 1) == FAIL)
        return FAIL;
-    ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
-    // TODO: argument and return types
+    ((type_T **)stack->ga_data)[stack->ga_len] =
+              ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
     ++stack->ga_len;
 
     return OK;
@@ -1713,7 +1715,8 @@ generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
            }
        }
        if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
-           if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
+           if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
+                                                                      == FAIL)
                return FAIL;
     }
 
@@ -3338,7 +3341,12 @@ compile_lambda(char_u **arg, cctx_T *cctx)
     clear_evalarg(&evalarg, NULL);
 
     if (ufunc->uf_def_status == UF_COMPILED)
-       return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
+    {
+       // The return type will now be known.
+       set_function_type(ufunc);
+
+       return generate_FUNCREF(cctx, ufunc);
+    }
 
     func_ptr_unref(ufunc);
     return FAIL;
@@ -4982,7 +4990,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
                                                    TRUE, ufunc->uf_func_type);
        if (lvar == NULL)
            return NULL;
-       if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
+       if (generate_FUNCREF(cctx, ufunc) == FAIL)
            return NULL;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
     }