changeset 20871:65d9189d4dca v8.2.0987

patch 8.2.0987: Vim9: cannot assign to [var; var] Commit: https://github.com/vim/vim/commit/9af78769eeae0318e07aa8b6af4d6e2244481ca7 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jun 16 11:34:42 2020 +0200 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.
author Bram Moolenaar <Bram@vim.org>
date Tue, 16 Jun 2020 11:45:04 +0200
parents 6203fd2cfb51
children 4b47824b3f1e
files src/eval.c src/list.c src/proto/list.pro src/testdir/test_vim9_script.vim src/version.c src/vim9.h src/vim9compile.c src/vim9execute.c
diffstat 8 files changed, 161 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -3237,7 +3237,6 @@ eval_index(
 		if (range)
 		{
 		    list_T	*l;
-		    listitem_T	*item;
 
 		    if (n2 < 0)
 			n2 = len + n2;
@@ -3245,19 +3244,9 @@ eval_index(
 			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);
 		}
--- a/src/list.c
+++ b/src/list.c
@@ -868,6 +868,26 @@ list_concat(list_T *l1, list_T *l2, typv
     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.
--- a/src/proto/list.pro
+++ b/src/proto/list.pro
@@ -33,6 +33,7 @@ void list_insert(list_T *l, listitem_T *
 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);
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -226,9 +226,23 @@ enddef
 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
@@ -244,7 +258,18 @@ def Test_assignment_failure()
   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:')
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    987,
+/**/
     986,
 /**/
     985,
--- a/src/vim9.h
+++ b/src/vim9.h
@@ -112,6 +112,7 @@ typedef enum {
     // 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
@@ -121,6 +122,7 @@ typedef enum {
 
     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;
@@ -229,6 +231,12 @@ typedef struct {
     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
  */
@@ -261,6 +269,7 @@ struct isn_S {
 	script_T	    script;
 	unlet_T		    unlet;
 	funcref_T	    funcref;
+	checklen_T	    checklen;
     } isn_arg;
 };
 
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1086,6 +1086,39 @@ generate_GETITEM(cctx_T *cctx, int index
 }
 
 /*
+ * 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.
  */
     static int
@@ -4708,8 +4741,8 @@ compile_assignment(char_u *arg, exarg_T 
 	    }
 	    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);
 	}
     }
 
@@ -5066,6 +5099,12 @@ compile_assignment(char_u *arg, exarg_T 
 		    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
@@ -5373,8 +5412,11 @@ compile_assignment(char_u *arg, exarg_T 
     }
 
     // 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;
 
@@ -7073,6 +7115,7 @@ delete_instr(isn_T *isn)
 	case ISN_CATCH:
 	case ISN_CHECKNR:
 	case ISN_CHECKTYPE:
+	case ISN_CHECKLEN:
 	case ISN_COMPAREANY:
 	case ISN_COMPAREBLOB:
 	case ISN_COMPAREBOOL:
@@ -7095,6 +7138,7 @@ delete_instr(isn_T *isn)
 	case ISN_FOR:
 	case ISN_INDEX:
 	case ISN_GETITEM:
+	case ISN_SLICE:
 	case ISN_MEMBER:
 	case ISN_JUMP:
 	case ISN_LOAD:
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -2114,6 +2114,35 @@ call_def_function(
 		}
 		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;
@@ -2243,6 +2272,25 @@ call_def_function(
 		}
 		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;
@@ -2814,6 +2862,8 @@ ex_disassemble(exarg_T *eap)
 	    // 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;
@@ -2826,6 +2876,10 @@ ex_disassemble(exarg_T *eap)
 				      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