0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.0987: Vim9: cannot assign to [var; var]

Problem:    Vim9: cannot assign to [var; var].
Solution:   Assign rest of items to a list.
This commit is contained in:
Bram Moolenaar 2020-06-16 11:34:42 +02:00
parent c70222d12a
commit 9af78769ee
8 changed files with 161 additions and 17 deletions

View File

@ -3237,7 +3237,6 @@ eval_index(
if (range) if (range)
{ {
list_T *l; list_T *l;
listitem_T *item;
if (n2 < 0) if (n2 < 0)
n2 = len + n2; n2 = len + n2;
@ -3245,19 +3244,9 @@ eval_index(
n2 = len - 1; n2 = len - 1;
if (!empty2 && (n2 < 0 || n2 + 1 < n1)) if (!empty2 && (n2 < 0 || n2 + 1 < n1))
n2 = -1; n2 = -1;
l = list_alloc(); l = list_slice(rettv->vval.v_list, n1, n2);
if (l == NULL) if (l == NULL)
return FAIL; return FAIL;
for (item = list_find(rettv->vval.v_list, n1);
n1 <= n2; ++n1)
{
if (list_append_tv(l, &item->li_tv) == FAIL)
{
list_free(l);
return FAIL;
}
item = item->li_next;
}
clear_tv(rettv); clear_tv(rettv);
rettv_list_set(rettv, l); rettv_list_set(rettv, l);
} }

View File

@ -868,6 +868,26 @@ list_concat(list_T *l1, list_T *l2, typval_T *tv)
return list_extend(l, l2, NULL); return list_extend(l, l2, NULL);
} }
list_T *
list_slice(list_T *ol, long n1, long n2)
{
listitem_T *item;
list_T *l = list_alloc();
if (l == NULL)
return NULL;
for (item = list_find(ol, n1); n1 <= n2; ++n1)
{
if (list_append_tv(l, &item->li_tv) == FAIL)
{
list_free(l);
return NULL;
}
item = item->li_next;
}
return l;
}
/* /*
* Make a copy of list "orig". Shallow if "deep" is FALSE. * Make a copy of list "orig". Shallow if "deep" is FALSE.
* The refcount of the new list is set to 1. * The refcount of the new list is set to 1.

View File

@ -33,6 +33,7 @@ void list_insert(list_T *l, listitem_T *ni, listitem_T *item);
void f_flatten(typval_T *argvars, typval_T *rettv); void f_flatten(typval_T *argvars, typval_T *rettv);
int list_extend(list_T *l1, list_T *l2, listitem_T *bef); int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
int list_concat(list_T *l1, list_T *l2, typval_T *tv); int list_concat(list_T *l1, list_T *l2, typval_T *tv);
list_T *list_slice(list_T *ol, long n1, long n2);
list_T *list_copy(list_T *orig, int deep, int copyID); list_T *list_copy(list_T *orig, int deep, int copyID);
void vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2); void vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2);
char_u *list2string(typval_T *tv, int copyID, int restore_copyID); char_u *list2string(typval_T *tv, int copyID, int restore_copyID);

View File

@ -226,9 +226,23 @@ enddef
def Test_assignment_var_list() def Test_assignment_var_list()
let v1: string let v1: string
let v2: string let v2: string
let vrem: list<string>
[v1] = ['aaa']
assert_equal('aaa', v1)
[v1, v2] = ['one', 'two'] [v1, v2] = ['one', 'two']
assert_equal('one', v1) assert_equal('one', v1)
assert_equal('two', v2) assert_equal('two', v2)
[v1, v2; vrem] = ['one', 'two']
assert_equal('one', v1)
assert_equal('two', v2)
assert_equal([], vrem)
[v1, v2; vrem] = ['one', 'two', 'three']
assert_equal('one', v1)
assert_equal('two', v2)
assert_equal(['three'], vrem)
enddef enddef
def Mess(): string def Mess(): string
@ -244,7 +258,18 @@ def Test_assignment_failure()
call CheckDefFailure(['let true = 1'], 'E1034:') call CheckDefFailure(['let true = 1'], 'E1034:')
call CheckDefFailure(['let false = 1'], 'E1034:') call CheckDefFailure(['let false = 1'], 'E1034:')
call CheckDefFailure(['let [a; b; c] = g:list'], 'E452:') call CheckDefFailure(['[a; b; c] = g:list'], 'E452:')
call CheckDefExecFailure(['let a: number',
'[a] = test_null_list()'], 'E1093:')
call CheckDefExecFailure(['let a: number',
'[a] = []'], 'E1093:')
call CheckDefExecFailure(['let x: number',
'let y: number',
'[x, y] = [1]'], 'E1093:')
call CheckDefExecFailure(['let x: number',
'let y: number',
'let z: list<number>',
'[x, y; z] = [1]'], 'E1093:')
call CheckDefFailure(['let somevar'], "E1022:") call CheckDefFailure(['let somevar'], "E1022:")
call CheckDefFailure(['let &option'], 'E1052:') call CheckDefFailure(['let &option'], 'E1052:')

View File

@ -754,6 +754,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
987,
/**/ /**/
986, 986,
/**/ /**/

View File

@ -112,6 +112,7 @@ typedef enum {
// expression operations // expression operations
ISN_CONCAT, ISN_CONCAT,
ISN_INDEX, // [expr] list index ISN_INDEX, // [expr] list index
ISN_SLICE, // drop isn_arg.number items from start of list
ISN_GETITEM, // push list item, isn_arg.number is the index ISN_GETITEM, // push list item, isn_arg.number is the index
ISN_MEMBER, // dict[member] ISN_MEMBER, // dict[member]
ISN_STRINGMEMBER, // dict.member using isn_arg.string ISN_STRINGMEMBER, // dict.member using isn_arg.string
@ -121,6 +122,7 @@ typedef enum {
ISN_CHECKNR, // check value can be used as a number ISN_CHECKNR, // check value can be used as a number
ISN_CHECKTYPE, // check value type is isn_arg.type.tc_type ISN_CHECKTYPE, // check value type is isn_arg.type.tc_type
ISN_CHECKLEN, // check list length is isn_arg.checklen.cl_min_len
ISN_DROP // pop stack and discard value ISN_DROP // pop stack and discard value
} isntype_T; } isntype_T;
@ -229,6 +231,12 @@ typedef struct {
int fr_var_idx; // variable to store partial int fr_var_idx; // variable to store partial
} funcref_T; } funcref_T;
// arguments to ISN_CHECKLEN
typedef struct {
int cl_min_len; // minimum length
int cl_more_OK; // longer is allowed
} checklen_T;
/* /*
* Instruction * Instruction
*/ */
@ -261,6 +269,7 @@ struct isn_S {
script_T script; script_T script;
unlet_T unlet; unlet_T unlet;
funcref_T funcref; funcref_T funcref;
checklen_T checklen;
} isn_arg; } isn_arg;
}; };

View File

@ -1085,6 +1085,39 @@ generate_GETITEM(cctx_T *cctx, int index)
return OK; return OK;
} }
/*
* Generate an ISN_SLICE instruction with "count".
*/
static int
generate_SLICE(cctx_T *cctx, int count)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Generate an ISN_CHECKLEN instruction with "min_len".
*/
static int
generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
return FAIL;
isn->isn_arg.checklen.cl_min_len = min_len;
isn->isn_arg.checklen.cl_more_OK = more_OK;
return OK;
}
/* /*
* Generate an ISN_STORE instruction. * Generate an ISN_STORE instruction.
*/ */
@ -4708,8 +4741,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
} }
if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL) if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
goto theend; goto theend;
// TODO: check length of list to be var_count (or more if generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
// "semicolon" set) semicolon);
} }
} }
@ -5066,6 +5099,12 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
if (r == FAIL) if (r == FAIL)
goto theend; goto theend;
} }
else if (semicolon && var_idx == var_count - 1)
{
// For "[var; var] = expr" get the rest of the list
if (generate_SLICE(cctx, var_count - 1) == FAIL)
goto theend;
}
else else
{ {
// For "[var, var] = expr" get the "var_idx" item from the // For "[var, var] = expr" get the "var_idx" item from the
@ -5373,8 +5412,11 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
} }
// for "[var, var] = expr" drop the "expr" value // for "[var, var] = expr" drop the "expr" value
if (var_count > 0 && generate_instr_drop(cctx, ISN_DROP, 1) == NULL) if (var_count > 0 && !semicolon)
goto theend; {
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
goto theend;
}
ret = end; ret = end;
@ -7073,6 +7115,7 @@ delete_instr(isn_T *isn)
case ISN_CATCH: case ISN_CATCH:
case ISN_CHECKNR: case ISN_CHECKNR:
case ISN_CHECKTYPE: case ISN_CHECKTYPE:
case ISN_CHECKLEN:
case ISN_COMPAREANY: case ISN_COMPAREANY:
case ISN_COMPAREBLOB: case ISN_COMPAREBLOB:
case ISN_COMPAREBOOL: case ISN_COMPAREBOOL:
@ -7095,6 +7138,7 @@ delete_instr(isn_T *isn)
case ISN_FOR: case ISN_FOR:
case ISN_INDEX: case ISN_INDEX:
case ISN_GETITEM: case ISN_GETITEM:
case ISN_SLICE:
case ISN_MEMBER: case ISN_MEMBER:
case ISN_JUMP: case ISN_JUMP:
case ISN_LOAD: case ISN_LOAD:

View File

@ -2114,6 +2114,35 @@ call_def_function(
} }
break; break;
case ISN_SLICE:
{
list_T *list;
int count = iptr->isn_arg.number;
tv = STACK_TV_BOT(-1);
if (tv->v_type != VAR_LIST)
{
emsg(_(e_listreq));
goto failed;
}
list = tv->vval.v_list;
// no error for short list, expect it to be checked earlier
if (list != NULL && list->lv_len >= count)
{
list_T *newlist = list_slice(list,
count, list->lv_len - 1);
if (newlist != NULL)
{
list_unref(list);
tv->vval.v_list = newlist;
++newlist->lv_refcount;
}
}
}
break;
case ISN_GETITEM: case ISN_GETITEM:
{ {
listitem_T *li; listitem_T *li;
@ -2243,6 +2272,25 @@ call_def_function(
} }
break; break;
case ISN_CHECKLEN:
{
int min_len = iptr->isn_arg.checklen.cl_min_len;
list_T *list = NULL;
tv = STACK_TV_BOT(-1);
if (tv->v_type == VAR_LIST)
list = tv->vval.v_list;
if (list == NULL || list->lv_len < min_len
|| (list->lv_len > min_len
&& !iptr->isn_arg.checklen.cl_more_OK))
{
semsg(_("E1093: Expected %d items but got %d"),
min_len, list == NULL ? 0 : list->lv_len);
goto failed;
}
}
break;
case ISN_2BOOL: case ISN_2BOOL:
{ {
int n; int n;
@ -2814,6 +2862,8 @@ ex_disassemble(exarg_T *eap)
// expression operations // expression operations
case ISN_CONCAT: smsg("%4d CONCAT", current); break; case ISN_CONCAT: smsg("%4d CONCAT", current); break;
case ISN_INDEX: smsg("%4d INDEX", current); break; case ISN_INDEX: smsg("%4d INDEX", current); break;
case ISN_SLICE: smsg("%4d SLICE %lld",
current, iptr->isn_arg.number); break;
case ISN_GETITEM: smsg("%4d ITEM %lld", case ISN_GETITEM: smsg("%4d ITEM %lld",
current, iptr->isn_arg.number); break; current, iptr->isn_arg.number); break;
case ISN_MEMBER: smsg("%4d MEMBER", current); break; case ISN_MEMBER: smsg("%4d MEMBER", current); break;
@ -2826,6 +2876,10 @@ ex_disassemble(exarg_T *eap)
vartype_name(iptr->isn_arg.type.ct_type), vartype_name(iptr->isn_arg.type.ct_type),
iptr->isn_arg.type.ct_off); iptr->isn_arg.type.ct_off);
break; break;
case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
iptr->isn_arg.checklen.cl_min_len);
break;
case ISN_2BOOL: if (iptr->isn_arg.number) case ISN_2BOOL: if (iptr->isn_arg.number)
smsg("%4d INVERT (!val)", current); smsg("%4d INVERT (!val)", current);
else else