To: vim_dev@googlegroups.com Subject: Patch 8.2.1244 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1244 Problem: Vim9: in lambda index assumes a list. Solution: Use the value type to decide about list or dict. (closes #6479) Files: src/vim9compile.c, src/testdir/test_vim9_expr.vim *** ../vim-8.2.1243/src/vim9compile.c 2020-07-19 14:41:54.625623029 +0200 --- src/vim9compile.c 2020-07-19 15:44:04.880960297 +0200 *************** *** 3748,3753 **** --- 3748,3754 ---- { garray_T *stack = &cctx->ctx_type_stack; type_T **typep; + vartype_T vtype; // list index: list[123] // dict member: dict[key] *************** *** 3773,3794 **** } *arg = *arg + 1; typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; ! if ((*typep)->tt_type == 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)); --- 3774,3811 ---- } *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; ! 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 { emsg(_(e_listdictblobreq)); *************** *** 5386,5392 **** } stacktype = stack->ga_len == 0 ? &t_void ! : ((type_T **)stack->ga_data)[stack->ga_len - 1]; if (lvar != NULL && (is_decl || !has_type)) { if (new_local && !has_type) --- 5403,5409 ---- } stacktype = stack->ga_len == 0 ? &t_void ! : ((type_T **)stack->ga_data)[stack->ga_len - 1]; if (lvar != NULL && (is_decl || !has_type)) { if (new_local && !has_type) *** ../vim-8.2.1243/src/testdir/test_vim9_expr.vim 2020-07-17 20:35:00.857574357 +0200 --- src/testdir/test_vim9_expr.vim 2020-07-19 15:44:56.568834946 +0200 *************** *** 1115,1121 **** call CheckDefExecFailure(["let x = g:anint[3]"], 'E714:') call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') call CheckDefFailure(["let x = [1,2,3]"], 'E1069:') ! call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E39:') call CheckDefFailure(["let x = g:list_mixed["], 'E1097:') call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:') call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') --- 1115,1121 ---- call CheckDefExecFailure(["let x = g:anint[3]"], 'E714:') call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') call CheckDefFailure(["let x = [1,2,3]"], 'E1069:') ! call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E1029:') call CheckDefFailure(["let x = g:list_mixed["], 'E1097:') call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:') call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') *************** *** 1173,1178 **** --- 1173,1181 ---- }) assert_equal([111, 222, 111], ll) + let dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] }) + assert_equal([{'key': 22}], dl) + call CheckDefFailure(["filter([1, 2], {k,v -> 1})"], 'E1069:') enddef *** ../vim-8.2.1243/src/version.c 2020-07-19 14:41:54.625623029 +0200 --- src/version.c 2020-07-19 15:15:49.832777645 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1244, /**/ -- The 50-50-90 rule: Anytime you have a 50-50 chance of getting something right, there's a 90% probability you'll get it wrong. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///