Patch 8.2.3954
Problem: Vim9: no error for shadowing if script var is declared later.
Solution: Check argument names when compiling a function.
Files: src/vim9compile.c, src/testdir/test_vim9_func.vim,
src/testdir/test_vim9_assign.vim
*** ../vim-8.2.3953/src/vim9compile.c 2021-12-30 17:09:01.491005356 +0000
--- src/vim9compile.c 2021-12-31 14:03:01.242174061 +0000
***************
*** 2393,2398 ****
--- 2393,2426 ----
return NOTDONE;
}
+ /*
+ * Check if arguments of "ufunc" shadow variables in "cctx".
+ * Return OK or FAIL.
+ */
+ static int
+ check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
+ {
+ int i;
+ char_u *arg;
+ int r = OK;
+
+ // Make sure arguments are not found when compiling a second time.
+ ufunc->uf_args_visible = 0;
+
+ // Check for arguments shadowing variables from the context.
+ for (i = 0; i < ufunc->uf_args.ga_len; ++i)
+ {
+ arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
+ if (check_defined(arg, STRLEN(arg), cctx, TRUE) == FAIL)
+ {
+ r = FAIL;
+ break;
+ }
+ }
+ ufunc->uf_args_visible = ufunc->uf_args.ga_len;
+ return r;
+ }
+
/*
* Add a function to the list of :def functions.
***************
*** 2525,2530 ****
--- 2553,2561 ----
estack_push_ufunc(ufunc, 1);
estack_compiling = TRUE;
+ if (check_args_shadowing(ufunc, &cctx) == FAIL)
+ goto erret;
+
if (ufunc->uf_def_args.ga_len > 0)
{
int count = ufunc->uf_def_args.ga_len;
*** ../vim-8.2.3953/src/testdir/test_vim9_func.vim 2021-12-30 17:09:01.491005356 +0000
--- src/testdir/test_vim9_func.vim 2021-12-31 13:39:25.252572525 +0000
***************
*** 951,956 ****
--- 951,957 ----
END
CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
+ # argument name declared earlier is found when declaring a function
lines =<< trim END
vim9script
var name = 'piet'
***************
*** 960,965 ****
--- 961,977 ----
END
CheckScriptFailure(lines, 'E1168:')
+ # argument name declared later is only found when compiling
+ lines =<< trim END
+ vim9script
+ def FuncOne(name: string)
+ echo nr
+ enddef
+ var name = 'piet'
+ END
+ CheckScriptSuccess(lines)
+ CheckScriptFailure(lines + ['defcompile'], 'E1168:')
+
lines =<< trim END
vim9script
def FuncOne(nr: number)
*** ../vim-8.2.3953/src/testdir/test_vim9_assign.vim 2021-12-30 13:28:56.812073798 +0000
--- src/testdir/test_vim9_assign.vim 2021-12-31 13:42:52.968136780 +0000
***************
*** 853,860 ****
var nres: any
var sres: any
! def Func(n: number, s = '')
! nres = n
sres = s
enddef
--- 853,860 ----
var nres: any
var sres: any
! def Func(nr: number, s = '')
! nres = nr
sres = s
enddef
***************
*** 869,875 ****
lines =<< trim END
vim9script
! def Func(n: number, s = '')
enddef
var n: number
--- 869,875 ----
lines =<< trim END
vim9script
! def Func(nr: number, s = '')
enddef
var n: number
*** ../vim-8.2.3953/src/version.c 2021-12-31 12:59:49.976653460 +0000
--- src/version.c 2021-12-31 13:35:09.364999405 +0000
***************
*** 751,752 ****
--- 751,754 ----
{ /* Add new patch number below this line */
+ /**/
+ 3954,
/**/
--
"Hit any key to continue" does _not_ mean you can hit the on/off button!
/// Bram Moolenaar -- Br...@Moolenaar.net --
http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features --
http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims --
http://ICCF-Holland.org ///