]> granicus.if.org Git - vim/commitdiff
patch 8.2.0987: Vim9: cannot assign to [var; var] v8.2.0987
authorBram Moolenaar <Bram@vim.org>
Tue, 16 Jun 2020 09:34:42 +0000 (11:34 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 16 Jun 2020 09:34:42 +0000 (11:34 +0200)
Problem:    Vim9: cannot assign to [var; var].
Solution:   Assign rest of items to a list.

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

index dfd82429f08dfe22da69c1a131f8dcd4160a5cd9..b4e2c2fece75e212764bdf7089c6fdd70fe4ec84 100644 (file)
@@ -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);
                }
index 675cf4283b3aabc3f32305e9051f2128e3055ade..130ab25251cc8daf9c45dee7183de6fbae3056e2 100644 (file)
@@ -868,6 +868,26 @@ list_concat(list_T *l1, list_T *l2, typval_T *tv)
     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.
index bdbf7eb8410e741c74fec696740f57c4c50ee30f..07dd4e2b1d78236552fc429a1007c7601f7e9669 100644 (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);
 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);
index a030686dc679d18a6122ad6ec5f3582104b06606..9dd950523ec100666d55865e6ec266479c90faa4 100644 (file)
@@ -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:')
index c7806bcdad38ad78b2fe2e6f79b6cf07eff623db..979ce701fe8de95f42cf290dacbaba5ee0de868a 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    987,
 /**/
     986,
 /**/
index 66b0882905e4dd9a3b2344f49f4992c291de1258..259bdb91104dd6de50089c310370add1baff6259 100644 (file)
@@ -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;
 };
 
index 12b125a6868d3c8c014c8ec26bb312770bfca9ed..7beecd9a308d1ab31d347744443a255618b875d7 100644 (file)
@@ -1085,6 +1085,39 @@ generate_GETITEM(cctx_T *cctx, int index)
     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.
  */
@@ -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)
                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 *eap, cmdidx_T cmdidx, cctx_T *cctx)
                    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 *eap, cmdidx_T cmdidx, cctx_T *cctx)
     }
 
     // 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:
index 7462b68c98a60d9e343a0088cb0bdc81c421d78e..e92af22cc237ecf87cae7abe30db22c94b09f18a 100644 (file)
@@ -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