To: vim_dev@googlegroups.com Subject: Patch 8.2.2753 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.2753 Problem: Vim9: cannot ignore an item in assignment unpack. Solution: Allow using an underscore. Files: runtime/doc/vim9.txt, src/vim.h, src/evalvars.c, src/eval.c, src/vim9compile.c, src/testdir/test_vim9_assign.vim *** ../vim-8.2.2752/runtime/doc/vim9.txt 2021-04-10 17:17:33.431942839 +0200 --- runtime/doc/vim9.txt 2021-04-10 22:32:53.636146498 +0200 *************** *** 335,340 **** --- 335,352 ---- Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be used to repeat a `:substitute` command. + For an unpack assignment the underscore can be used to ignore a list item, + similar to how a function argument can be ignored: > + [a, _, c] = theList + [a, b; _] = longList + + < *E1092* + Declaring more than one variable at a time, using the unpack notation, is + currently not supported: > + var [v1, v2] = GetValues() # Error! + That is because the type needs to be inferred from the list item type, which + isn't that easy. + Constants ~ *vim9-const* *vim9-final* *************** *** 368,380 **** NAMES[1] = ["Emma"] # Error! NAMES[1][0] = "Emma" # OK, now females[0] == "Emma" - < *E1092* - Declaring more than one variable at a time, using the unpack notation, is - currently not supported: > - var [v1, v2] = GetValues() # Error! - That is because the type needs to be inferred from the list item type, which - isn't that easy. - Omitting :call and :eval ~ --- 380,385 ---- *** ../vim-8.2.2752/src/vim.h 2021-03-13 20:57:15.855515074 +0100 --- src/vim.h 2021-04-10 21:57:17.581546078 +0200 *************** *** 2152,2161 **** } estack_arg_T; // Flags for assignment functions. ! #define ASSIGN_FINAL 1 // ":final" ! #define ASSIGN_CONST 2 // ":const" ! #define ASSIGN_NO_DECL 4 // "name = expr" without ":let"/":const"/":final" ! #define ASSIGN_DECL 8 // may declare variable if it does not exist #include "ex_cmds.h" // Ex command defines #include "spell.h" // spell checking stuff --- 2152,2162 ---- } estack_arg_T; // Flags for assignment functions. ! #define ASSIGN_FINAL 0x01 // ":final" ! #define ASSIGN_CONST 0x02 // ":const" ! #define ASSIGN_NO_DECL 0x04 // "name = expr" without ":let"/":const"/":final" ! #define ASSIGN_DECL 0x08 // may declare variable if it does not exist ! #define ASSIGN_UNPACK 0x10 // using [a, b] = list #include "ex_cmds.h" // Ex command defines #include "spell.h" // spell checking stuff *** ../vim-8.2.2752/src/evalvars.c 2021-04-10 17:17:33.431942839 +0200 --- src/evalvars.c 2021-04-10 22:01:13.096977214 +0200 *************** *** 970,977 **** { arg = skipwhite(arg + 1); ++var_idx; ! arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", ! op, var_idx); item = item->li_next; if (arg == NULL) return FAIL; --- 970,977 ---- { arg = skipwhite(arg + 1); ++var_idx; ! arg = ex_let_one(arg, &item->li_tv, TRUE, ! flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx); item = item->li_next; if (arg == NULL) return FAIL; *************** *** 996,1003 **** l->lv_refcount = 1; ++var_idx; ! arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, flags, ! (char_u *)"]", op, var_idx); clear_tv(<v); if (arg == NULL) return FAIL; --- 996,1003 ---- l->lv_refcount = 1; ++var_idx; ! arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, ! flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx); clear_tv(<v); if (arg == NULL) return FAIL; *************** *** 3190,3196 **** var_in_vim9script = is_script_local && current_script_is_vim9(); if (var_in_vim9script && name[0] == '_' && name[1] == NUL) { ! emsg(_(e_cannot_use_underscore_here)); goto failed; } --- 3190,3198 ---- var_in_vim9script = is_script_local && current_script_is_vim9(); if (var_in_vim9script && name[0] == '_' && name[1] == NUL) { ! // For "[a, _] = list" the underscore is ignored. ! if ((flags & ASSIGN_UNPACK) == 0) ! emsg(_(e_cannot_use_underscore_here)); goto failed; } *** ../vim-8.2.2752/src/eval.c 2021-04-10 17:17:33.431942839 +0200 --- src/eval.c 2021-04-10 22:08:02.595934369 +0200 *************** *** 3514,3520 **** { int flags = evalarg == NULL ? 0 : evalarg->eval_flags; ! if (in_vim9script() && len == 1 && *s == '_') { emsg(_(e_cannot_use_underscore_here)); ret = FAIL; --- 3514,3520 ---- { int flags = evalarg == NULL ? 0 : evalarg->eval_flags; ! if (evaluate && in_vim9script() && len == 1 && *s == '_') { emsg(_(e_cannot_use_underscore_here)); ret = FAIL; *** ../vim-8.2.2752/src/vim9compile.c 2021-04-10 21:01:35.017507340 +0200 --- src/vim9compile.c 2021-04-10 22:27:21.069954119 +0200 *************** *** 6369,6374 **** --- 6369,6385 ---- { int instr_count = -1; + if (var_start[0] == '_' && !eval_isnamec(var_start[1])) + { + // Ignore underscore in "[a, _, b] = list". + if (var_count > 0) + { + var_start = skipwhite(var_start + 2); + continue; + } + emsg(_(e_cannot_use_underscore_here)); + goto theend; + } vim_free(lhs.lhs_name); /* *************** *** 6388,6398 **** semsg(_(e_cannot_assign_to_constant), lhs.lhs_name); goto theend; } - if (is_decl && lhs.lhs_name[0] == '_' && lhs.lhs_name[1] == NUL) - { - emsg(_(e_cannot_use_underscore_here)); - goto theend; - } if (!heredoc) { --- 6399,6404 ---- *** ../vim-8.2.2752/src/testdir/test_vim9_assign.vim 2021-04-06 21:17:23.930140057 +0200 --- src/testdir/test_vim9_assign.vim 2021-04-10 22:28:52.725414718 +0200 *************** *** 256,261 **** --- 256,269 ---- [v1, v2] = [1, 2] assert_equal(1, v1) assert_equal(2, v2) + + [v1, _, v2, _] = [1, 99, 2, 77] + assert_equal(1, v1) + assert_equal(2, v2) + + [v1, v2; _] = [1, 2, 3, 4, 5] + assert_equal(1, v1) + assert_equal(2, v2) END CheckDefAndScriptSuccess(lines) *** ../vim-8.2.2752/src/version.c 2021-04-10 21:45:42.938892687 +0200 --- src/version.c 2021-04-10 22:33:17.932028110 +0200 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 2753, /**/ -- From "know your smileys": <<<:-{ Worf (Never smiles anyways, so he's a bad smiley) /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///