changeset 22602:2c77ec32deeb v8.2.1849

patch 8.2.1849: Vim9: garbage collection frees block-local variables Commit: https://github.com/vim/vim/commit/ed234f24f3a6d697ba9b786d0bc74d4682bfdf47 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Oct 15 20:42:20 2020 +0200 patch 8.2.1849: Vim9: garbage collection frees block-local variables Problem: Vim9: garbage collection frees block-local variables. Solution: Mark all script variables as used.
author Bram Moolenaar <Bram@vim.org>
date Thu, 15 Oct 2020 20:45:05 +0200
parents e4ba57ce600b
children d2c45a169cf2
files src/evalvars.c src/testdir/test_vim9_script.vim src/version.c
diffstat 3 files changed, 41 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -303,12 +303,24 @@ garbage_collect_vimvars(int copyID)
     int
 garbage_collect_scriptvars(int copyID)
 {
-    int		i;
-    int		abort = FALSE;
+    int		    i;
+    int		    idx;
+    int		    abort = FALSE;
+    scriptitem_T    *si;
 
     for (i = 1; i <= script_items.ga_len; ++i)
+    {
 	abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
 
+	si = SCRIPT_ITEM(i);
+	for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
+	{
+	    svar_T    *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
+
+	    abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
+	}
+    }
+
     return abort;
 }
 
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -253,31 +253,47 @@ enddef
 def Test_block_local_vars()
   var lines =<< trim END
       vim9script
+      v:testing = 1
       if true
-        var text = 'hello'
-        def SayHello(): string
+        var text = ['hello']
+        def SayHello(): list<string>
           return text
         enddef
         def SetText(v: string)
-          text = v
+          text = [v]
         enddef
       endif
 
       if true
-        var text = 'again'
-        def SayAgain(): string
+        var text = ['again']
+        def SayAgain(): list<string>
           return text
         enddef
       endif
+
+      # test that the "text" variables are not cleaned up
+      test_garbagecollect_now()
+
       defcompile
 
-      assert_equal('hello', SayHello())
-      assert_equal('again', SayAgain())
+      assert_equal(['hello'], SayHello())
+      assert_equal(['again'], SayAgain())
 
       SetText('foobar')
-      assert_equal('foobar', SayHello())
+      assert_equal(['foobar'], SayHello())
+
+      call writefile(['ok'], 'Xdidit')
+      qall!
   END
-  CheckScriptSuccess(lines)
+
+  # need to execute this with a separate Vim instance to avoid the current
+  # context gets garbage collected.
+  writefile(lines, 'Xscript')
+  RunVim([], [], '-S Xscript')
+  assert_equal(['ok'], readfile('Xdidit'))
+
+  delete('Xscript')
+  delete('Xdidit')
 enddef
 
 func g:NoSuchFunc()
--- 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 */
 /**/
+    1849,
+/**/
     1848,
 /**/
     1847,