Skip to content

Commit 31a11b9

Browse files
committed
patch 8.2.2323: Vim9: error when inferring type from empty dict/list
Problem: Vim9: error when inferring type from empty dict/list. Solution: When the member is t_unknown use t_any. (closes #7009)
1 parent 0186e58 commit 31a11b9

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/testdir/test_vim9_expr.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,16 @@ def Test_expr7_list_subscript()
29292929
lines = ['var l = [0, 1, 2]', 'echo l[g:astring : g:theone]']
29302930
CheckDefExecFailure(lines, 'E1012:')
29312931
CheckScriptFailure(['vim9script'] + lines, 'E1030:', 3)
2932+
2933+
lines =<< trim END
2934+
vim9script
2935+
var ld = []
2936+
def Func()
2937+
eval ld[0].key
2938+
enddef
2939+
defcompile
2940+
END
2941+
CheckScriptSuccess(lines)
29322942
enddef
29332943

29342944
def Test_expr7_dict_subscript()
@@ -2937,6 +2947,15 @@ def Test_expr7_dict_subscript()
29372947
var l = [{lnum: 2}, {lnum: 1}]
29382948
var res = l[0].lnum > l[1].lnum
29392949
assert_true(res)
2950+
2951+
var dd = {}
2952+
def Func1()
2953+
eval dd.key1.key2
2954+
enddef
2955+
def Func2()
2956+
eval dd['key1'].key2
2957+
enddef
2958+
defcompile
29402959
END
29412960
CheckScriptSuccess(lines)
29422961
enddef

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+
2323,
753755
/**/
754756
2322,
755757
/**/

src/vim9compile.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,10 @@ generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
18991899
}
19001900
// change dict type to dict member type
19011901
if (type->tt_type == VAR_DICT)
1902-
((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1902+
{
1903+
((type_T **)stack->ga_data)[stack->ga_len - 1] =
1904+
type->tt_member == &t_unknown ? &t_any : type->tt_member;
1905+
}
19031906

19041907
return OK;
19051908
}
@@ -3793,7 +3796,12 @@ compile_subscript(
37933796
return FAIL;
37943797
}
37953798
if ((*typep)->tt_type == VAR_DICT)
3799+
{
37963800
*typep = (*typep)->tt_member;
3801+
if (*typep == &t_unknown)
3802+
// empty dict was used
3803+
*typep = &t_any;
3804+
}
37973805
else
37983806
{
37993807
if (need_type(*typep, &t_dict_any, -2, cctx,
@@ -3831,7 +3839,12 @@ compile_subscript(
38313839
else
38323840
{
38333841
if ((*typep)->tt_type == VAR_LIST)
3842+
{
38343843
*typep = (*typep)->tt_member;
3844+
if (*typep == &t_unknown)
3845+
// empty list was used
3846+
*typep = &t_any;
3847+
}
38353848
if (generate_instr_drop(cctx,
38363849
vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
38373850
1) == FAIL)

0 commit comments

Comments
 (0)