diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index eefb6174d8..30c2589ef2 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -5759,6 +5759,28 @@ def Test_multikey_dict_in_block() unlet g:TestDict enddef +" Test for overriding a block level variable with a new script level variable +" and referring to it in a function. +def Test_block_var_override_with_script_var() + var lines =<< trim END + vim9script + + if true + var lines = ['a'] + lines->filter((_, _) => true) + endif + + var lines = [] + + def Fx() + lines->add('b') + enddef + Fx() + assert_equal(['b'], lines) + END + v9.CheckSourceSuccess(lines) +enddef + " Test for using the type() function with void def Test_type_func_with_void() var lines =<< trim END diff --git a/src/version.c b/src/version.c index c07927ba2b..63d8715aa5 100644 --- a/src/version.c +++ b/src/version.c @@ -734,6 +734,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 347, /**/ 346, /**/ diff --git a/src/vim9compile.c b/src/vim9compile.c index c9976160e4..ea56f4ec5b 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -253,6 +253,11 @@ find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack) { int idx; + if (ufunc->uf_block_depth == 0 && sav->sav_block_id == 0) + // If the function was defined at the script level (not inside a + // block), script-scope variables are always visible. + return sav; + // Go over the blocks that this function was defined in. If the // variable block ID matches it was visible to the function. for (idx = 0; idx < ufunc->uf_block_depth; ++idx)