Skip to content

Commit fc74d03

Browse files
committed
patch 8.2.2000: Vim9: dict.key assignment not implemented yet
Problem: Vim9: dict.key assignment not implemented yet. Solution: Implement dict.key assignment. (closes #7312)
1 parent e6329e4 commit fc74d03

3 files changed

Lines changed: 35 additions & 12 deletions

File tree

src/testdir/test_vim9_assign.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ def Test_assignment_dict()
408408

409409
# overwrite
410410
dict3['key'] = 'another'
411+
assert_equal(dict3, #{key: 'another'})
412+
dict3.key = 'yet another'
413+
assert_equal(dict3, #{key: 'yet another'})
414+
415+
var lines =<< trim END
416+
var dd = #{one: 1}
417+
dd.one) = 2
418+
END
419+
CheckDefFailure(lines, 'E15:', 2)
411420

412421
# empty key can be used
413422
var dd = {}
@@ -418,7 +427,7 @@ def Test_assignment_dict()
418427
var somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'}
419428

420429
# assignment to script-local dict
421-
var lines =<< trim END
430+
lines =<< trim END
422431
vim9script
423432
var test: dict<any> = {}
424433
def FillDict(): dict<any>

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2000,
753755
/**/
754756
1999,
755757
/**/

src/vim9compile.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,14 +5384,14 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
53845384
member_type = type;
53855385
if (var_end > var_start + varlen)
53865386
{
5387-
// Something follows after the variable: "var[idx]".
5387+
// Something follows after the variable: "var[idx]" or "var.key".
53885388
if (is_decl)
53895389
{
53905390
emsg(_(e_cannot_use_index_when_declaring_variable));
53915391
goto theend;
53925392
}
53935393

5394-
if (var_start[varlen] == '[')
5394+
if (var_start[varlen] == '[' || var_start[varlen] == '.')
53955395
{
53965396
has_index = TRUE;
53975397
if (type->tt_member == NULL)
@@ -5635,21 +5635,33 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
56355635
{
56365636
int r;
56375637

5638-
// Compile the "idx" in "var[idx]".
5638+
// Compile the "idx" in "var[idx]" or "key" in "var.key".
56395639
if (new_local)
56405640
--cctx->ctx_locals.ga_len;
5641-
p = skipwhite(var_start + varlen + 1);
5642-
r = compile_expr0(&p, cctx);
5641+
p = var_start + varlen;
5642+
if (*p == '[')
5643+
{
5644+
p = skipwhite(p + 1);
5645+
r = compile_expr0(&p, cctx);
5646+
if (r == OK && *skipwhite(p) != ']')
5647+
{
5648+
// this should not happen
5649+
emsg(_(e_missbrac));
5650+
r = FAIL;
5651+
}
5652+
}
5653+
else // if (*p == '.')
5654+
{
5655+
char_u *key_end = to_name_end(p + 1, TRUE);
5656+
char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5657+
5658+
r = generate_PUSHS(cctx, key);
5659+
}
56435660
if (new_local)
56445661
++cctx->ctx_locals.ga_len;
56455662
if (r == FAIL)
56465663
goto theend;
5647-
if (*skipwhite(p) != ']')
5648-
{
5649-
// this should not happen
5650-
emsg(_(e_missbrac));
5651-
goto theend;
5652-
}
5664+
56535665
if (type == &t_any)
56545666
{
56555667
type_T *idx_type = ((type_T **)stack->ga_data)[

0 commit comments

Comments
 (0)