changeset 23276:b79cdad3ea2e v8.2.2184

patch 8.2.2184: Vim9: no error when using "2" for a line number Commit: https://github.com/vim/vim/commit/9a963377b4811e4e0419ec8825856ff4b01331ac Author: Bram Moolenaar <Bram@vim.org> Date: Mon Dec 21 21:58:46 2020 +0100 patch 8.2.2184: Vim9: no error when using "2" for a line number Problem: Vim9: no error when using "2" for a line number. Solution: Give an error message if the line number is invalid. (closes https://github.com/vim/vim/issues/7492)
author Bram Moolenaar <Bram@vim.org>
date Mon, 21 Dec 2020 22:00:03 +0100
parents cd907d92a6be
children 798b339d466f
files src/evalfunc.c src/testdir/test_cursor_func.vim src/testdir/test_vim9_builtin.vim src/typval.c src/version.c
diffstat 5 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -2615,6 +2615,8 @@ f_cursor(typval_T *argvars, typval_T *re
     else
     {
 	line = tv_get_lnum(argvars);
+	if (line < 0)
+	    semsg(_(e_invarg2), tv_get_string(&argvars[0]));
 	col = (long)tv_get_number_chk(&argvars[1], NULL);
 	if (argvars[2].v_type != VAR_UNKNOWN)
 	    coladd = (long)tv_get_number_chk(&argvars[2], NULL);
--- a/src/testdir/test_cursor_func.vim
+++ b/src/testdir/test_cursor_func.vim
@@ -30,7 +30,7 @@ func Test_move_cursor()
   call cursor(1, 1, 1)
   call assert_equal([1, 1, 1], getcurpos()[1:3])
 
-  call assert_equal(-1, cursor(-1, -1))
+  call assert_fails('call cursor(-1, -1)', 'E475:')
 
   quit!
 endfunc
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -185,6 +185,20 @@ def Test_count()
   count('ABC ABC ABC', 'b', false)->assert_equal(0)
 enddef
 
+def Test_cursor()
+  new
+  setline(1, range(4))
+  cursor(2, 1)
+  assert_equal(2, getcurpos()[1])
+  cursor('$', 1)
+  assert_equal(4, getcurpos()[1])
+
+  var lines =<< trim END
+    cursor('2', 1)
+  END
+  CheckDefExecAndScriptFailure(lines, 'E475:')
+enddef
+
 def Test_executable()
   assert_false(executable(""))
   assert_false(executable(test_null_string()))
--- a/src/typval.c
+++ b/src/typval.c
@@ -1536,11 +1536,11 @@ eval_env_var(char_u **arg, typval_T *ret
     linenr_T
 tv_get_lnum(typval_T *argvars)
 {
-    linenr_T	lnum = 0;
+    linenr_T	lnum = -1;
 
     if (argvars[0].v_type != VAR_STRING || !in_vim9script())
 	lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
-    if (lnum == 0)  // no valid number, try using arg like line()
+    if (lnum <= 0)  // no valid number, try using arg like line()
     {
 	int	fnum;
 	pos_T	*fp = var2fpos(&argvars[0], TRUE, &fnum);
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2184,
+/**/
     2183,
 /**/
     2182,