]> granicus.if.org Git - vim/commitdiff
patch 8.2.0519: Vim9: return type not properly checked v8.2.0519
authorBram Moolenaar <Bram@vim.org>
Sun, 5 Apr 2020 20:14:54 +0000 (22:14 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 5 Apr 2020 20:14:54 +0000 (22:14 +0200)
Problem:    Vim9: return type not properly checked.
Solution:   Check type properly, also at runtime.

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

index c1221686283f4d9ee7057969b1c82da681654e21..74653b13e146f951716456848b71cb6ecc4bb912 100644 (file)
@@ -380,6 +380,10 @@ def FuncOneArgRetNumber(arg: number): number
   return arg
 enddef
 
+def FuncOneArgRetAny(arg: any): any
+  return arg
+enddef
+
 def Test_func_type()
   let Ref1: func()
   funcResult = 0
@@ -417,5 +421,20 @@ def Test_func_type_fails()
   CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1013: type mismatch, expected func() but got func(number): number')
 enddef
 
+def Test_func_return_type()
+  let nr: number
+  nr = FuncNoArgRetNumber()
+  assert_equal(1234, nr)
+
+  nr = FuncOneArgRetAny(122)
+  assert_equal(122, nr)
+
+  let str: string
+  str = FuncOneArgRetAny('yes')
+  assert_equal('yes', str)
+
+  CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1013: type mismatch, expected string but got number')
+enddef
+
 
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index f420990b9c637ecdeb92b7c1f6d67d41e037eefc..6e9f0061654c8f513fb38d25480e25f55a578708 100644 (file)
@@ -738,6 +738,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    519,
 /**/
     518,
 /**/
index 98936dd66068954ac7b1c8d65e5c8b23fa002e8f..0dfe5a1634f320b37a3eaeff0b9152740788d10a 100644 (file)
@@ -2432,7 +2432,7 @@ check_type(type_T *expected, type_T *actual, int give_msg)
        if (ret == FAIL && give_msg)
            type_mismatch(expected, actual);
     }
-    return OK;
+    return ret;
 }
 
 /*
@@ -2444,7 +2444,7 @@ check_type(type_T *expected, type_T *actual, int give_msg)
     static int
 need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
 {
-    if (check_type(expected, actual, FALSE))
+    if (check_type(expected, actual, FALSE) == OK)
        return OK;
     if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
     {
@@ -4069,7 +4069,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
                        lvar->lv_type = stacktype;
                }
            }
-           else if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
+           else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
                goto theend;
        }
        else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)