Mercurial > vim
diff src/vim9compile.c @ 21387:8d58cbb07a12 v8.2.1244
patch 8.2.1244: Vim9: in lambda index assumes a list
Commit: https://github.com/vim/vim/commit/6802cce407b8b181bcf3eafe701fa9f8a91f7a73
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 19 15:49:49 2020 +0200
patch 8.2.1244: Vim9: in lambda index assumes a list
Problem: Vim9: in lambda index assumes a list.
Solution: Use the value type to decide about list or dict. (closes https://github.com/vim/vim/issues/6479)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 19 Jul 2020 16:00:04 +0200 |
parents | 54a304e4dc57 |
children | 8b882afa8ed2 |
line wrap: on
line diff
--- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -3748,6 +3748,7 @@ compile_subscript( { garray_T *stack = &cctx->ctx_type_stack; type_T **typep; + vartype_T vtype; // list index: list[123] // dict member: dict[key] @@ -3773,22 +3774,38 @@ compile_subscript( } *arg = *arg + 1; + // We can index a list and a dict. If we don't know the type + // we can use the index value type. + // TODO: If we don't know use an instruction to figure it out at + // runtime. typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; - if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any) + vtype = (*typep)->tt_type; + if (*typep == &t_any) + { + type_T *valtype = ((type_T **)stack->ga_data) + [stack->ga_len - 1]; + if (valtype == &t_string) + vtype = VAR_DICT; + } + if (vtype == VAR_DICT) + { + if ((*typep)->tt_type == VAR_DICT) + *typep = (*typep)->tt_member; + else if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) + == FAIL) + return FAIL; + if (may_generate_2STRING(-1, cctx) == FAIL) + return FAIL; + if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) + return FAIL; + } + else if (vtype == VAR_LIST || *typep == &t_any) { if ((*typep)->tt_type == VAR_LIST) *typep = (*typep)->tt_member; if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) return FAIL; } - else if ((*typep)->tt_type == VAR_DICT) - { - *typep = (*typep)->tt_member; - if (may_generate_2STRING(-1, cctx) == FAIL) - return FAIL; - if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) - return FAIL; - } else { emsg(_(e_listdictblobreq)); @@ -5386,7 +5403,7 @@ compile_assignment(char_u *arg, exarg_T } stacktype = stack->ga_len == 0 ? &t_void - : ((type_T **)stack->ga_data)[stack->ga_len - 1]; + : ((type_T **)stack->ga_data)[stack->ga_len - 1]; if (lvar != NULL && (is_decl || !has_type)) { if (new_local && !has_type)