changeset 21441:78d97ee2c707 v8.2.1271

patch 8.2.1271: Vim9: Error for Funcref function argument type Commit: https://github.com/vim/vim/commit/0f60e80f9b6d778e460b4dc8333cd8e17c1b620b Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jul 22 20:16:11 2020 +0200 patch 8.2.1271: Vim9: Error for Funcref function argument type Problem: Vim9: Error for Funcref function argument type. Solution: Find the actual function type if possible. (issue https://github.com/vim/vim/issues/6507)
author Bram Moolenaar <Bram@vim.org>
date Wed, 22 Jul 2020 20:30:04 +0200
parents bd0a3a73f3da
children 0c134036ff70
files src/testdir/test_vim9_func.vim src/version.c src/vim9compile.c
diffstat 3 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -270,6 +270,19 @@ def Test_call_funcref()
     assert_equal(123, Funcref())
   END
   CheckScriptSuccess(lines)
+
+  lines =<< trim END
+    vim9script
+    def RetNumber(): number
+      return 123
+    enddef
+    def Bar(F: func: number): number
+      return F()
+    enddef
+    let Funcref = function('RetNumber')
+    assert_equal(123, Bar(Funcref))
+  END
+  CheckScriptSuccess(lines)
 enddef
 
 let SomeFunc = function('len')
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1271,
+/**/
     1270,
 /**/
     1269,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -599,6 +599,28 @@ check_argtype(type_T *expected, typval_T
 	member.tt_member = &t_any;
 	actual.tt_member = &member;
     }
+    else if (actual_tv->v_type == VAR_FUNC || actual_tv->v_type == VAR_PARTIAL)
+    {
+	char_u	*name = NULL;
+	ufunc_T *ufunc = NULL;
+
+	if (actual_tv->v_type == VAR_PARTIAL)
+	{
+	    if (actual_tv->vval.v_partial->pt_func != NULL)
+		ufunc = actual_tv->vval.v_partial->pt_func;
+	    else
+		name = actual_tv->vval.v_partial->pt_name;
+	}
+	else
+	    name = actual_tv->vval.v_string;
+	if (name != NULL)
+	    // TODO: how about a builtin function?
+	    ufunc = find_func(name, FALSE, NULL);
+	if (ufunc != NULL && ufunc->uf_func_type != NULL)
+	    actual = *ufunc->uf_func_type;
+	else
+	    actual.tt_member = &t_any;
+    }
     else
 	actual.tt_member = &t_any;
     return check_type(expected, &actual, TRUE);