changeset 20029:8fb1cf4c44d5 v8.2.0570

patch 8.2.0570: Vim9: no error when omitting type from argument Commit: https://github.com/vim/vim/commit/6e949784be29bfaea6e49a9d8231481eae10fab6 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Apr 13 17:21:00 2020 +0200 patch 8.2.0570: Vim9: no error when omitting type from argument Problem: Vim9: no error when omitting type from argument. Solution: Enforce specifying argument types.
author Bram Moolenaar <Bram@vim.org>
date Mon, 13 Apr 2020 17:30:05 +0200
parents f0fb61802fe7
children dd4098fd958c
files src/ex_eval.c src/testdir/test_vim9_disassemble.vim src/testdir/test_vim9_expr.vim src/testdir/test_vim9_func.vim src/testdir/test_vim9_script.vim src/userfunc.c src/version.c
diffstat 7 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -2273,9 +2273,12 @@ rewind_conditionals(
  * ":endfunction" when not after a ":function"
  */
     void
-ex_endfunction(exarg_T *eap UNUSED)
+ex_endfunction(exarg_T *eap)
 {
-    emsg(_("E193: :endfunction not inside a function"));
+    if (eap->cmdidx == CMD_enddef)
+	emsg(_("E193: :enddef not inside a function"));
+    else
+	emsg(_("E193: :endfunction not inside a function"));
 }
 
 /*
--- a/src/testdir/test_vim9_disassemble.vim
+++ b/src/testdir/test_vim9_disassemble.vim
@@ -160,7 +160,7 @@ def Test_disassemble_new()
         res)
 enddef
 
-def FuncWithArg(arg)
+def FuncWithArg(arg: any)
   echo arg
 enddef
 
@@ -432,7 +432,7 @@ def Test_disassemble_lambda()
         instr)
 enddef
 
-def AndOr(arg): string
+def AndOr(arg: any): string
   if arg == 1 && arg != 2 || arg == 4
     return 'yes'
   endif
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -859,11 +859,11 @@ def Test_expr7_negate()
   assert_equal(88, --nr)
 enddef
 
-def Echo(arg): string
+def Echo(arg: any): string
   return arg
 enddef
 
-def s:EchoArg(arg): string
+def s:EchoArg(arg: any): string
   return arg
 enddef
 
@@ -991,6 +991,7 @@ def Test_expr7_trailing()
   assert_equal(123, d.key)
 enddef
 
+
 func Test_expr7_trailing_fails()
   call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107')
   call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274')
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -250,6 +250,7 @@ enddef
 def Test_arg_type_wrong()
   CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
   CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
+  CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
 enddef
 
 def Test_vim9script_call()
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -917,7 +917,7 @@ def Test_for_loop_fails()
   CheckDefFailure(['for # in range(5)'], 'E690:')
   CheckDefFailure(['for i In range(5)'], 'E690:')
   CheckDefFailure(['let x = 5', 'for x in range(5)'], 'E1023:')
-  CheckScriptFailure(['def Func(arg)', 'for arg in range(5)', 'enddef'], 'E1006:')
+  CheckScriptFailure(['def Func(arg: any)', 'for arg in range(5)', 'enddef'], 'E1006:')
   CheckDefFailure(['for i in "text"'], 'E1024:')
   CheckDefFailure(['for i in xxx'], 'E1001:')
   CheckDefFailure(['endfor'], 'E588:')
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -64,14 +64,16 @@ func_tbl_get(void)
 }
 
 /*
- * Get one function argument and an optional type: "arg: type".
+ * Get one function argument.
+ * If "argtypes" is not NULL also get the type: "arg: type".
  * Return a pointer to after the type.
  * When something is wrong return "arg".
  */
     static char_u *
 one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
 {
-    char_u *p = arg;
+    char_u	*p = arg;
+    char_u	*arg_copy = NULL;
 
     while (ASCII_ISALNUM(*p) || *p == '_')
 	++p;
@@ -87,7 +89,6 @@ one_function_arg(char_u *arg, garray_T *
 	return arg;
     if (newargs != NULL)
     {
-	char_u	*arg_copy;
 	int	c;
 	int	i;
 
@@ -119,14 +120,24 @@ one_function_arg(char_u *arg, garray_T *
     {
 	char_u *type = NULL;
 
+	if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
+	{
+	    semsg(_("E1059: No white space allowed before colon: %s"),
+					    arg_copy == NULL ? arg : arg_copy);
+	    p = skipwhite(p);
+	}
 	if (*p == ':')
 	{
 	    type = skipwhite(p + 1);
 	    p = skip_type(type);
 	    type = vim_strnsave(type, p - type);
 	}
-	else if (*skipwhite(p) == ':')
-	    emsg(_("E1059: No white space allowed before :"));
+	else if (*skipwhite(p) != '=')
+	{
+	    semsg(_("E1077: Missing argument type for %s"),
+					    arg_copy == NULL ? arg : arg_copy);
+	    return arg;
+	}
 	((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
     }
 
--- a/src/version.c
+++ b/src/version.c
@@ -739,6 +739,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    570,
+/**/
     569,
 /**/
     568,