Skip to content

Commit

Permalink
patch 8.2.3054: Vim9: unpack assignment using "_" after semicolon fails
Browse files Browse the repository at this point in the history
Problem:    Vim9: unpack assignment using "_" after semicolon fails.
Solution:   Drop the expression result. (closes #8453)
  • Loading branch information
brammool committed Jun 26, 2021
1 parent 13e45d1 commit 4d5dfe2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/errors.h
Expand Up @@ -318,7 +318,7 @@ EXTERN char e_using_bool_as_number[]
EXTERN char e_missing_matching_bracket_after_dict_key[]
INIT(= N_("E1139: Missing matching bracket after dict key"));
EXTERN char e_for_argument_must_be_sequence_of_lists[]
INIT(= N_("E1140: For argument must be a sequence of lists"));
INIT(= N_("E1140: :for argument must be a sequence of lists"));
EXTERN char e_indexable_type_required[]
INIT(= N_("E1141: Indexable type required"));
EXTERN char e_non_empty_string_required[]
Expand Down
10 changes: 10 additions & 0 deletions src/testdir/test_vim9_assign.vim
Expand Up @@ -289,6 +289,16 @@ def Test_assign_unpack()
assert_equal(1, v1)
assert_equal(2, v2)

var reslist = []
for text in ['aaa {bbb} ccc', 'ddd {eee} fff']
var before: string
var middle: string
var after: string
[_, before, middle, after; _] = text->matchlist('\(.\{-\}\){\(.\{-\}\)}\(.*\)')
reslist->add(before)->add(middle)->add(after)
endfor
assert_equal(['aaa ', 'bbb', ' ccc', 'ddd ', 'eee', ' fff'], reslist)

var a = 1
var b = 3
[a, b] += [2, 4]
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -755,6 +755,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
3054,
/**/
3053,
/**/
Expand Down
7 changes: 5 additions & 2 deletions src/vim9compile.c
Expand Up @@ -6598,6 +6598,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
int var_count = 0;
int var_idx;
int semicolon = 0;
int did_generate_slice = FALSE;
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
char_u *op;
Expand Down Expand Up @@ -6801,6 +6802,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
else if (semicolon && var_idx == var_count - 1)
{
// For "[var; var] = expr" get the rest of the list
did_generate_slice = TRUE;
if (generate_SLICE(cctx, var_count - 1) == FAIL)
goto theend;
}
Expand Down Expand Up @@ -7010,8 +7012,9 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
var_start = skipwhite(lhs.lhs_dest_end + 1);
}

// for "[var, var] = expr" drop the "expr" value
if (var_count > 0 && !semicolon)
// For "[var, var] = expr" drop the "expr" value.
// Also for "[var, var; _] = expr".
if (var_count > 0 && (!semicolon || !did_generate_slice))
{
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
goto theend;
Expand Down

0 comments on commit 4d5dfe2

Please sign in to comment.