changeset 21578:7417cb54cb24 v8.2.1339

patch 8.2.1339: Vim9: assigning to global dict variable doesn't work Commit: https://github.com/vim/vim/commit/2caa1594e72be7a876c21ed5c2df252d3537cfa7 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 1 15:53:19 2020 +0200 patch 8.2.1339: Vim9: assigning to global dict variable doesn't work Problem: Vim9: assigning to global dict variable doesn't work. Solution: Guess variable type based in index type. (issue https://github.com/vim/vim/issues/6591)
author Bram Moolenaar <Bram@vim.org>
date Sat, 01 Aug 2020 16:00:04 +0200
parents c17665b3de40
children 30c6a9fb9e8d
files src/testdir/test_vim9_script.vim src/version.c src/vim9compile.c
diffstat 3 files changed, 41 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -274,6 +274,30 @@ def Test_assignment_dict()
     FillDict()
   END
   call CheckScriptFailure(lines, 'E1103:')
+
+  # assignment to global dict
+  lines =<< trim END
+    vim9script
+    g:test = {}
+    def FillDict(): dict<any>
+      g:test['a'] = 43
+      return g:test
+    enddef
+    assert_equal(#{a: 43}, FillDict())
+  END
+  call CheckScriptSuccess(lines)
+
+  # assignment to buffer dict
+  lines =<< trim END
+    vim9script
+    b:test = {}
+    def FillDict(): dict<any>
+      b:test['a'] = 43
+      return b:test
+    enddef
+    assert_equal(#{a: 43}, FillDict())
+  END
+  call CheckScriptSuccess(lines)
 enddef
 
 def Test_assignment_local()
--- 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 */
 /**/
+    1339,
+/**/
     1338,
 /**/
     1337,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5486,11 +5486,9 @@ compile_assignment(char_u *arg, exarg_T 
 	    {
 		has_index = TRUE;
 		if (type->tt_member == NULL)
-		{
-		    semsg(_("E1088: cannot use an index on %s"), name);
-		    goto theend;
-		}
-		member_type = type->tt_member;
+		    member_type = &t_any;
+		else
+		    member_type = type->tt_member;
 	    }
 	    else
 	    {
@@ -5719,6 +5717,18 @@ compile_assignment(char_u *arg, exarg_T 
 		emsg(_(e_missbrac));
 		goto theend;
 	    }
+	    if (type == &t_any)
+	    {
+		type_T	    *idx_type = ((type_T **)stack->ga_data)[
+							    stack->ga_len - 1];
+		// Index on variable of unknown type: guess the type from the
+		// index type: number is dict, otherwise dict.
+		// TODO: should do the assignment at runtime
+		if (idx_type->tt_type == VAR_NUMBER)
+		    type = &t_list_any;
+		else
+		    type = &t_dict_any;
+	    }
 	    if (type->tt_type == VAR_DICT
 		    && may_generate_2STRING(-1, cctx) == FAIL)
 		goto theend;