]> granicus.if.org Git - vim/commitdiff
patch 8.2.2018: Vim9: script variable not found from lambda v8.2.2018
authorBram Moolenaar <Bram@vim.org>
Thu, 19 Nov 2020 20:47:56 +0000 (21:47 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 19 Nov 2020 20:47:56 +0000 (21:47 +0100)
Problem:    Vim9: script variable not found from lambda.
Solution:   In a lambda also check the script hashtab for a variable without a
            scope. (closes #7329)

src/evalvars.c
src/testdir/test_vim9_func.vim
src/version.c

index 9c05c57a01054682806dedb5ca12b06752aa3a09..75893bb27cc4d5640f3b2f5a05d40b9c4e11bfdf 100644 (file)
@@ -2628,7 +2628,28 @@ find_var(char_u *name, hashtab_T **htp, int no_autoload)
        return ret;
 
     // Search in parent scope for lambda
-    return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
+    ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
+    if (ret != NULL)
+       return ret;
+
+    // in Vim9 script items without a scope can be script-local
+    if (in_vim9script() && name[0] != NUL && name[1] != ':')
+    {
+       ht = get_script_local_ht();
+       if (ht != NULL)
+       {
+           ret = find_var_in_ht(ht, *name, varname,
+                                                  no_autoload || htp != NULL);
+           if (ret != NULL)
+           {
+               if (htp != NULL)
+                   *htp = ht;
+               return ret;
+           }
+       }
+    }
+
+    return NULL;
 }
 
 /*
index 8d67b6f0beca41d6b089c039712f8309a7f7d536..5c937b65777c0a9029328a2edefd67ef8d571b05 100644 (file)
@@ -1476,6 +1476,15 @@ def Test_line_continuation_in_def()
   Line_continuation_in_def('.')->assert_equal('full')
 enddef
 
+def Test_script_var_in_lambda()
+  var lines =<< trim END
+      vim9script
+      var script = 'test'
+      assert_equal(['test'], map(['one'], {-> script}))
+  END
+  CheckScriptSuccess(lines)
+enddef
+
 def Line_continuation_in_lambda(): list<string>
   var x = range(97, 100)
       ->map({_, v -> nr2char(v)
index 03f8a3f705daf6f9c2f5b758e4ddcb69fd9ee830..ebfe94c6d7c0df7253ac37c8c56bc22cf729f64c 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2018,
 /**/
     2017,
 /**/