changeset 23201:94608d7c3b55 v8.2.2146

patch 8.2.2146: Vim9: automatic conversion of number to string for dict key Commit: https://github.com/vim/vim/commit/9987fb0b4b591e6450fb1dfbe8f615f365057f2a Author: Bram Moolenaar <Bram@vim.org> Date: Tue Dec 15 21:41:56 2020 +0100 patch 8.2.2146: Vim9: automatic conversion of number to string for dict key Problem: Vim9: automatic conversion of number to string for dict key. Solution: Do not convert number to string. (closes https://github.com/vim/vim/issues/7474)
author Bram Moolenaar <Bram@vim.org>
date Tue, 15 Dec 2020 21:45:05 +0100
parents f26564daf7a8
children 71089c6b385c
files src/dict.c src/testdir/test_vim9_expr.vim src/version.c
diffstat 3 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/dict.c
+++ b/src/dict.c
@@ -951,6 +951,11 @@ eval_dict(char_u **arg, typval_T *rettv,
 	}
 	if (evaluate)
 	{
+	    if (vim9script && check_for_string(&tvkey) == FAIL)
+	    {
+		clear_tv(&tvkey);
+		goto failret;
+	    }
 	    key = tv_get_string_buf_chk(&tvkey, buf);
 	    if (key == NULL)
 	    {
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -1989,6 +1989,14 @@ def Test_expr7_dict()
 
   CheckDefFailure(['var x = ({'], 'E723:', 2)
   CheckDefExecFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1)
+
+  # no automatic conversion from number to string
+  lines =<< trim END
+      var n = 123
+      var d = {[n]: 1}
+  END
+  CheckDefFailure(lines, 'E1012:', 2)
+  CheckScriptFailure(['vim9script'] + lines, 'E928:', 3)
 enddef
 
 def Test_expr7_dict_vim9script()
--- 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 */
 /**/
+    2146,
+/**/
     2145,
 /**/
     2144,