]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0347: Vim9: script-local variable not found v9.2.0347
authorYegappan Lakshmanan <yegappan@yahoo.com>
Tue, 14 Apr 2026 17:02:21 +0000 (17:02 +0000)
committerChristian Brabandt <cb@256bit.org>
Tue, 14 Apr 2026 17:02:21 +0000 (17:02 +0000)
Problem:  Vim9: script-local variable not found after function call
          (Mao-Yining)
Solution: Accept a script local variable in a function which overrides a
          previous block-scope variable (Yegappan Lakshmanan)

fixes:  #19959
closes: #19963

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/testdir/test_vim9_script.vim
src/version.c
src/vim9compile.c

index eefb6174d84d4020b90dbba421c8207f8234d807..30c2589ef235dca477dee6c432f88710156772df 100644 (file)
@@ -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
index c07927ba2b372622232cf9e8ed89ee87007528f9..63d8715aa5d0aa8c926fe5adf959b4deb313db95 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    347,
 /**/
     346,
 /**/
index c9976160e4581ee734d9e8e65b9208f147a2e22f..ea56f4ec5b841a27eacd13b8fd6780aedd2c8114 100644 (file)
@@ -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)