patch 8.2.2090: Vim9: dict does not accept a key in quotes
Problem: Vim9: dict does not accept a key in quotes.
Solution: Recognize a key in single or double quotes.
diff --git a/src/dict.c b/src/dict.c
index 819f5fa..311b003 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -801,7 +801,7 @@
* Return FAIL when there is no valid key.
*/
static int
-get_literal_key(char_u **arg, typval_T *tv)
+get_literal_key_tv(char_u **arg, typval_T *tv)
{
char_u *p = skip_literal_key(*arg);
@@ -815,6 +815,47 @@
}
/*
+ * Get a literal key for a Vim9 dict:
+ * {"name": value},
+ * {'name': value},
+ * {name: value} use "name" as a literal key
+ * Return the key in allocated memory or NULL in the case of an error.
+ * "arg" is advanced to just after the key.
+ */
+ char_u *
+get_literal_key(char_u **arg)
+{
+ char_u *key;
+ char_u *end;
+ typval_T rettv;
+
+ if (**arg == '\'')
+ {
+ if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
+ return NULL;
+ key = rettv.vval.v_string;
+ }
+ else if (**arg == '"')
+ {
+ if (eval_string(arg, &rettv, TRUE) == FAIL)
+ return NULL;
+ key = rettv.vval.v_string;
+ }
+ else
+ {
+ end = skip_literal_key(*arg);
+ if (end == *arg)
+ {
+ semsg(_(e_invalid_key_str), *arg);
+ return NULL;
+ }
+ key = vim_strnsave(*arg, end - *arg);
+ *arg = end;
+ }
+ return key;
+}
+
+/*
* Allocate a variable for a Dictionary and fill it from "*arg".
* "*arg" points to the "{".
* "literal" is TRUE for #{key: val}
@@ -864,11 +905,18 @@
{
int has_bracket = vim9script && **arg == '[';
- if (literal || (vim9script && !has_bracket))
+ if (literal)
{
- if (get_literal_key(arg, &tvkey) == FAIL)
+ if (get_literal_key_tv(arg, &tvkey) == FAIL)
goto failret;
}
+ else if (vim9script && !has_bracket)
+ {
+ tvkey.vval.v_string = get_literal_key(arg);
+ if (tvkey.vval.v_string == NULL)
+ goto failret;
+ tvkey.v_type = VAR_STRING;
+ }
else
{
if (has_bracket)