Skip to content

Commit 2caa159

Browse files
committed
patch 8.2.1339: Vim9: assigning to global dict variable doesn't work
Problem: Vim9: assigning to global dict variable doesn't work. Solution: Guess variable type based in index type. (issue #6591)
1 parent 8e4c8c8 commit 2caa159

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

src/testdir/test_vim9_script.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ def Test_assignment_dict()
274274
FillDict()
275275
END
276276
call CheckScriptFailure(lines, 'E1103:')
277+
278+
# assignment to global dict
279+
lines =<< trim END
280+
vim9script
281+
g:test = {}
282+
def FillDict(): dict<any>
283+
g:test['a'] = 43
284+
return g:test
285+
enddef
286+
assert_equal(#{a: 43}, FillDict())
287+
END
288+
call CheckScriptSuccess(lines)
289+
290+
# assignment to buffer dict
291+
lines =<< trim END
292+
vim9script
293+
b:test = {}
294+
def FillDict(): dict<any>
295+
b:test['a'] = 43
296+
return b:test
297+
enddef
298+
assert_equal(#{a: 43}, FillDict())
299+
END
300+
call CheckScriptSuccess(lines)
277301
enddef
278302

279303
def Test_assignment_local()

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1339,
757759
/**/
758760
1338,
759761
/**/

src/vim9compile.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5486,11 +5486,9 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
54865486
{
54875487
has_index = TRUE;
54885488
if (type->tt_member == NULL)
5489-
{
5490-
semsg(_("E1088: cannot use an index on %s"), name);
5491-
goto theend;
5492-
}
5493-
member_type = type->tt_member;
5489+
member_type = &t_any;
5490+
else
5491+
member_type = type->tt_member;
54945492
}
54955493
else
54965494
{
@@ -5719,6 +5717,18 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
57195717
emsg(_(e_missbrac));
57205718
goto theend;
57215719
}
5720+
if (type == &t_any)
5721+
{
5722+
type_T *idx_type = ((type_T **)stack->ga_data)[
5723+
stack->ga_len - 1];
5724+
// Index on variable of unknown type: guess the type from the
5725+
// index type: number is dict, otherwise dict.
5726+
// TODO: should do the assignment at runtime
5727+
if (idx_type->tt_type == VAR_NUMBER)
5728+
type = &t_list_any;
5729+
else
5730+
type = &t_dict_any;
5731+
}
57225732
if (type->tt_type == VAR_DICT
57235733
&& may_generate_2STRING(-1, cctx) == FAIL)
57245734
goto theend;

0 commit comments

Comments
 (0)