Problem: Vim9: cannot assign to [var; var].
Solution: Assign rest of items to a list.
if (range)
{
list_T *l;
- listitem_T *item;
if (n2 < 0)
n2 = len + n2;
n2 = len - 1;
if (!empty2 && (n2 < 0 || n2 + 1 < n1))
n2 = -1;
- l = list_alloc();
+ l = list_slice(rettv->vval.v_list, n1, n2);
if (l == NULL)
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);
rettv_list_set(rettv, l);
}
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.
* The refcount of the new list is set to 1.
void f_flatten(typval_T *argvars, typval_T *rettv);
int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
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);
void vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2);
char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
def Test_assignment_var_list()
let v1: string
let v2: string
+ let vrem: list<string>
+ [v1] = ['aaa']
+ assert_equal('aaa', v1)
+
[v1, v2] = ['one', 'two']
assert_equal('one', v1)
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
def Mess(): string
call CheckDefFailure(['let true = 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 &option'], 'E1052:')
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 987,
/**/
986,
/**/
// expression operations
ISN_CONCAT,
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_MEMBER, // dict[member]
ISN_STRINGMEMBER, // dict.member using isn_arg.string
ISN_CHECKNR, // check value can be used as a number
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
} isntype_T;
int fr_var_idx; // variable to store partial
} funcref_T;
+// arguments to ISN_CHECKLEN
+typedef struct {
+ int cl_min_len; // minimum length
+ int cl_more_OK; // longer is allowed
+} checklen_T;
+
/*
* Instruction
*/
script_T script;
unlet_T unlet;
funcref_T funcref;
+ checklen_T checklen;
} isn_arg;
};
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.
*/
}
if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
goto theend;
- // TODO: check length of list to be var_count (or more if
- // "semicolon" set)
+ generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
+ semicolon);
}
}
if (r == FAIL)
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
{
// For "[var, var] = expr" get the "var_idx" item from the
}
// for "[var, var] = expr" drop the "expr" value
- if (var_count > 0 && generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
- goto theend;
+ if (var_count > 0 && !semicolon)
+ {
+ if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
+ goto theend;
+ }
ret = end;
case ISN_CATCH:
case ISN_CHECKNR:
case ISN_CHECKTYPE:
+ case ISN_CHECKLEN:
case ISN_COMPAREANY:
case ISN_COMPAREBLOB:
case ISN_COMPAREBOOL:
case ISN_FOR:
case ISN_INDEX:
case ISN_GETITEM:
+ case ISN_SLICE:
case ISN_MEMBER:
case ISN_JUMP:
case ISN_LOAD:
}
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:
{
listitem_T *li;
}
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:
{
int n;
// expression operations
case ISN_CONCAT: smsg("%4d CONCAT", 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",
current, iptr->isn_arg.number); break;
case ISN_MEMBER: smsg("%4d MEMBER", current); break;
vartype_name(iptr->isn_arg.type.ct_type),
iptr->isn_arg.type.ct_off);
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)
smsg("%4d INVERT (!val)", current);
else