]> granicus.if.org Git - vim/commitdiff
patch 8.2.1722: Vim9: cannot assign a lambda to a variable of type function v8.2.1722
authorBram Moolenaar <Bram@vim.org>
Mon, 21 Sep 2020 19:48:21 +0000 (21:48 +0200)
committerBram Moolenaar <Bram@vim.org>
Mon, 21 Sep 2020 19:48:21 +0000 (21:48 +0200)
Problem:    Vim9: cannot assign a lambda to a variable of type function.
Solution:   Allow for assigning a partial to a variable of type function.
            (Naruhiko Nishino, closes #6996)

src/testdir/test_vim9_expr.vim
src/version.c
src/vim9type.c

index 411ed7017484ec81960ff2758212101657422bcc..7900dd794376e2ee5bab4e56720b7b1f641d75ea 100644 (file)
@@ -1687,6 +1687,18 @@ def Test_expr7_lambda_vim9script()
        ->map({_, v -> synIDattr(v, 'name')})->len()})
   END
   CheckScriptSuccess(lines)
+
+  # check if assign a lambda to a variable which type is func or any.
+  lines =<< trim END
+      vim9script
+      let FuncRef = {->123}
+      assert_equal(123, FuncRef())
+      let FuncRef_Func: func = {->123}
+      assert_equal(123, FuncRef_Func())
+      let FuncRef_Any: any = {->123}
+      assert_equal(123, FuncRef_Any())
+  END
+  CheckScriptSuccess(lines)
 enddef
 
 def Test_epxr7_funcref()
index 29199d2cdf04ca7d767be1a038887f66e814b61c..7cc9e7bebd72582f258165b5c64ba9f403365367 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1722,
 /**/
     1721,
 /**/
index a7ffd32ed556e00603264f6b82a5501a9883c6f4..c7e3dde32cf2cc2dc20fe2345bff55b2c57cba3f 100644 (file)
@@ -463,7 +463,11 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
            && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
 
     {
-       if (expected->tt_type != actual->tt_type)
+       // tt_type should match, except that a "partial" can be assigned to a
+       // variable with type "func".
+       if (!(expected->tt_type == actual->tt_type
+                   || (expected->tt_type == VAR_FUNC
+                                          && actual->tt_type == VAR_PARTIAL)))
        {
            if (expected->tt_type == VAR_BOOL
                                        && (actual->tt_flags & TTFLAG_BOOL_OK))