patch 9.1.0329: String interpolation fails for Dict type
Problem: String interpolation fails for Dict type
Solution: Support Dict data type properly, also support :put =Dict
(without having to convert it to string() first)
(Yegappan Lakshmanan)
fixes: #14529
closes: #14541
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/runtime/doc/change.txt b/runtime/doc/change.txt
index 7063cad..cd3d6ad 100644
--- a/runtime/doc/change.txt
+++ b/runtime/doc/change.txt
@@ -1,4 +1,4 @@
-*change.txt* For Vim version 9.1. Last change: 2023 Dec 19
+*change.txt* For Vim version 9.1. Last change: 2024 Apr 14
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1345,8 +1345,8 @@
The expression must evaluate to a String. A Number is always automatically
converted to a String. For the "p" and ":put" command, if the result is a
Float it's converted into a String. If the result is a List each element is
-turned into a String and used as a line. A Dictionary or FuncRef results in
-an error message (use string() to convert).
+turned into a String and used as a line. A Dictionary is converted into a
+String. A FuncRef results in an error message (use string() to convert).
If the "= register is used for the "p" command, the String is split up at <NL>
characters. If the String ends in a <NL>, it is regarded as a linewise
diff --git a/src/eval.c b/src/eval.c
index e4c8bae..d81ef17 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -575,7 +575,8 @@
/*
* Convert "tv" to a string.
- * When "convert" is TRUE convert a List into a sequence of lines.
+ * When "convert" is TRUE convert a List into a sequence of lines and a Dict
+ * into a textual representation of the Dict.
* Returns an allocated string (NULL when out of memory).
*/
char_u *
@@ -596,6 +597,8 @@
ga_append(&ga, NUL);
retval = (char_u *)ga.ga_data;
}
+ else if (convert && tv->v_type == VAR_DICT)
+ retval = dict2string(tv, get_copyID(), FALSE);
else
retval = vim_strsave(tv_get_string(tv));
return retval;
diff --git a/src/testdir/test_edit.vim b/src/testdir/test_edit.vim
index cdf2097..789e44c 100644
--- a/src/testdir/test_edit.vim
+++ b/src/testdir/test_edit.vim
@@ -1962,8 +1962,8 @@
let buf = RunVimInTerminal('', #{rows: 6, cols: 60})
- " trying to insert a dictionary produces an error
- call term_sendkeys(buf, "i\<C-R>={}\<CR>")
+ " trying to insert a blob produces an error
+ call term_sendkeys(buf, "i\<C-R>=0z\<CR>")
" ending Insert mode should put the cursor back on the ':'
call term_sendkeys(buf, ":\<Esc>")
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index c1869c1..1fa460d 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -950,6 +950,10 @@
endif
call assert_equal(0, tmp)
+ #" Dict interpolation
+ VAR d = {'a': 10, 'b': [1, 2]}
+ call assert_equal("{'a': 10, 'b': [1, 2]}", $'{d}')
+
#" Stray closing brace.
call assert_fails('echo $"moo}"', 'E1278:')
#" Undefined variable in expansion.
diff --git a/src/testdir/test_let.vim b/src/testdir/test_let.vim
index 17f78ea..fec0273 100644
--- a/src/testdir/test_let.vim
+++ b/src/testdir/test_let.vim
@@ -689,6 +689,13 @@
END
call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
+ " Evaluate a dictionary
+ let d1 = #{a: 10, b: 'ss', c: {}}
+ let code =<< eval trim END
+ let d2 = {d1}
+ END
+ call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code)
+
let code = 'xxx'
let code =<< eval trim END
let n = {5 +
diff --git a/src/testdir/test_put.vim b/src/testdir/test_put.vim
index 5b5c354..6f1e992 100644
--- a/src/testdir/test_put.vim
+++ b/src/testdir/test_put.vim
Binary files differ
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 0beaa44..4da368c 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -2984,6 +2984,16 @@
CODE
v9.CheckDefAndScriptSuccess(lines)
+ # Evaluate a dictionary
+ lines =<< trim CODE
+ var d1 = {'a': 10, 'b': [1, 2]}
+ var code =<< trim eval END
+ var d2 = {d1}
+ END
+ assert_equal(["var d2 = {'a': 10, 'b': [1, 2]}"], code)
+ CODE
+ v9.CheckDefAndScriptSuccess(lines)
+
lines =<< trim CODE
var code =<< eval trim END
var s = "{$SOME_ENV_VAR}"
diff --git a/src/version.c b/src/version.c
index 957475d..91a3d61 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 329,
+/**/
328,
/**/
327,
diff --git a/src/vim9execute.c b/src/vim9execute.c
index 2d128e0..b3e8939 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -1653,6 +1653,7 @@
case VAR_BOOL:
case VAR_NUMBER:
case VAR_FLOAT:
+ case VAR_DICT:
case VAR_BLOB: break;
case VAR_LIST:
diff --git a/src/vim9instr.c b/src/vim9instr.c
index a2179f3..48ebf1a 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -222,6 +222,7 @@
// conversion possible when tolerant
case VAR_LIST:
+ case VAR_DICT:
if (tolerant)
{
isntype = ISN_2STRING_ANY;
@@ -234,7 +235,6 @@
case VAR_BLOB:
case VAR_FUNC:
case VAR_PARTIAL:
- case VAR_DICT:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR: