changeset 21166:64f664f9b23a v8.2.1134

patch 8.2.1134: Vim9: getting a list member may not work Commit: https://github.com/vim/vim/commit/435d89789ef4dd329938edbe17c646db9f0ea772 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 5 16:42:13 2020 +0200 patch 8.2.1134: Vim9: getting a list member may not work Problem: Vim9: getting a list member may not work. Solution: Clear the list only after copying the item. (closes https://github.com/vim/vim/issues/6393)
author Bram Moolenaar <Bram@vim.org>
date Sun, 05 Jul 2020 16:45:03 +0200
parents b64bd9672ef0
children 61a9e8f67262
files src/testdir/test_vim9_expr.vim src/version.c src/vim9execute.c
diffstat 3 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -1141,6 +1141,11 @@ def Test_expr_member()
   call CheckDefExecFailure(["let d: dict<number>", "d = g:list_empty"], 'E1029: Expected dict but got list')
 enddef
 
+def Test_expr_index()
+  # getting the one member should clear the list only after getting the item
+  assert_equal('bbb', ['aaa', 'bbb', 'ccc'][1])
+enddef
+
 def Test_expr_member_vim9script()
   let lines =<< trim END
       vim9script
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1134,
+/**/
     1133,
 /**/
     1132,
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -2085,6 +2085,7 @@ call_def_function(
 		    list_T	*list;
 		    varnumber_T	n;
 		    listitem_T	*li;
+		    typval_T	temp_tv;
 
 		    // list index: list is at stack-2, index at stack-1
 		    tv = STACK_TV_BOT(-2);
@@ -2109,8 +2110,12 @@ call_def_function(
 			goto failed;
 		    }
 		    --ectx.ec_stack.ga_len;
-		    clear_tv(STACK_TV_BOT(-1));
-		    copy_tv(&li->li_tv, STACK_TV_BOT(-1));
+		    // Clear the list after getting the item, to avoid that it
+		    // make the item invalid.
+		    tv = STACK_TV_BOT(-1);
+		    temp_tv = *tv;
+		    copy_tv(&li->li_tv, tv);
+		    clear_tv(&temp_tv);
 		}
 		break;