1
0
forked from aniani/vim

patch 8.2.4589: cannot index the g: dictionary

Problem:    Cannot index the g: dictionary.
Solution:   Recognize using "g:[key]". (closes #9969)
This commit is contained in:
Bram Moolenaar 2022-03-18 19:44:48 +00:00
parent f35fd8e5d4
commit 2e17fef225
5 changed files with 29 additions and 9 deletions

View File

@ -929,7 +929,8 @@ get_lval(
if (vim9script) if (vim9script)
{ {
// "a: type" is declaring variable "a" with a type, not "a:". // "a: type" is declaring variable "a" with a type, not "a:".
if (p == name + 2 && p[-1] == ':') // However, "g:[key]" is indexing a dictionary.
if (p == name + 2 && p[-1] == ':' && *p != '[')
{ {
--p; --p;
lp->ll_name_end = p; lp->ll_name_end = p;

View File

@ -3523,12 +3523,14 @@ find_ex_command(
return eap->cmd; return eap->cmd;
} }
if (p != eap->cmd && ( if ((p != eap->cmd && (
// "varname[]" is an expression. // "varname[]" is an expression.
*p == '[' *p == '['
// "varname.key" is an expression. // "varname.key" is an expression.
|| (*p == '.' && (ASCII_ISALPHA(p[1]) || (*p == '.'
|| p[1] == '_')))) && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
// g:[key] is an expression
|| STRNCMP(eap->cmd, "g:[", 3) == 0)
{ {
char_u *after = eap->cmd; char_u *after = eap->cmd;

View File

@ -1116,6 +1116,14 @@ def Test_assignment_dict()
END END
v9.CheckDefAndScriptSuccess(lines) v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END
var key = 'foo'
g:[key] = 'value'
assert_equal('value', g:foo)
unlet g:foo
END
v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END lines =<< trim END
var dd = {one: 1} var dd = {one: 1}
dd.one) = 2 dd.one) = 2

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
4589,
/**/ /**/
4588, 4588,
/**/ /**/

View File

@ -1000,7 +1000,12 @@ generate_loadvar(
break; break;
case dest_global: case dest_global:
if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
{
if (name[2] == NUL)
generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
else
generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
}
else else
generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type); generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
break; break;
@ -2413,17 +2418,19 @@ may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
// Recognize an assignment if we recognize the variable // Recognize an assignment if we recognize the variable
// name: // name:
// "g:var = expr"
// "local = expr" where "local" is a local var.
// "script = expr" where "script" is a script-local var.
// "import = expr" where "import" is an imported var
// "&opt = expr" // "&opt = expr"
// "$ENV = expr" // "$ENV = expr"
// "@r = expr" // "@r = expr"
// "g:var = expr"
// "g:[key] = expr"
// "local = expr" where "local" is a local var.
// "script = expr" where "script" is a script-local var.
// "import = expr" where "import" is an imported var
if (*eap->cmd == '&' if (*eap->cmd == '&'
|| *eap->cmd == '$' || *eap->cmd == '$'
|| *eap->cmd == '@' || *eap->cmd == '@'
|| ((len) > 2 && eap->cmd[1] == ':') || ((len) > 2 && eap->cmd[1] == ':')
|| STRNCMP(eap->cmd, "g:[", 3) == 0
|| variable_exists(eap->cmd, len, cctx)) || variable_exists(eap->cmd, len, cctx))
{ {
*line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);