]> granicus.if.org Git - vim/commitdiff
patch 8.2.3310: Vim9: unpack assignment does not mention source of type error v8.2.3310
authorBram Moolenaar <Bram@vim.org>
Sat, 7 Aug 2021 14:30:42 +0000 (16:30 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 7 Aug 2021 14:30:42 +0000 (16:30 +0200)
Problem:    Vim9: unpack assignment does not mention source of type error.
Solution:   Mention the argument number. (closes #8719)

src/testdir/test_vim9_assign.vim
src/testdir/test_vim9_disassemble.vim
src/version.c
src/vim9compile.c

index 8491a4d879ae9c926bd38e08a91213e97b262008..a96e97cadadf327bb06d3f267781b71884275ecf 100644 (file)
@@ -414,6 +414,22 @@ def Test_assign_unpack()
     [x, y] = g:values
   END
   CheckDefExecAndScriptFailure(lines, 'E1163: Variable 2: type mismatch, expected string but got number')
+
+  lines =<< trim END
+    var x: number
+    var y: number
+    var z: string
+    [x, y, z] = [1, 2, 3]
+  END
+  CheckDefAndScriptFailure(lines, 'E1163: Variable 3: type mismatch, expected string but got number')
+
+  lines =<< trim END
+    var x: number
+    var y: string
+    var z: string
+    [x, y, z] = [1, '2', 3]
+  END
+  CheckDefExecAndScriptFailure(lines, 'E1163: Variable 3: type mismatch, expected string but got number')
 enddef
 
 def Test_assign_linebreak()
index 253fe1b06e62ea1dea5ed34ebb9fa7f2e4e4ddc6..9eaa8647f3638bf954ad8d844e535f2ce550db1e 100644 (file)
@@ -441,10 +441,10 @@ def Test_disassemble_list_assign()
         '\d CHECKTYPE list<any> stack\[-1\]\_s*' ..
         '\d CHECKLEN >= 2\_s*' ..
         '\d\+ ITEM 0\_s*' ..
-        '\d\+ CHECKTYPE string stack\[-1\]\_s*' ..
+        '\d\+ CHECKTYPE string stack\[-1\] arg 1\_s*' ..
         '\d\+ STORE $0\_s*' ..
         '\d\+ ITEM 1\_s*' ..
-        '\d\+ CHECKTYPE string stack\[-1\]\_s*' ..
+        '\d\+ CHECKTYPE string stack\[-1\] arg 2\_s*' ..
         '\d\+ STORE $1\_s*' ..
         '\d\+ SLICE 2\_s*' ..
         '\d\+ STORE $2\_s*' ..
index 97823dfd8fd35b3ae4916e8e822a61bbf0b981a8..9c374b1647d5cf24a47654036ddf2e7dce483cd3 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3310,
 /**/
     3309,
 /**/
index 5d625e0c75b9fd17f86ca2257e6d01c6f7f6ac2f..125135adaae5ac9fec95307d0c25a4c92952d2b5 100644 (file)
@@ -1039,18 +1039,16 @@ use_typecheck(type_T *actual, type_T *expected)
  * If "actual_is_const" is TRUE then the type won't change at runtime, do not
  * generate a TYPECHECK.
  */
-    int
-need_type(
+    static int
+need_type_where(
        type_T  *actual,
        type_T  *expected,
        int     offset,
-       int     arg_idx,
+       where_T where,
        cctx_T  *cctx,
        int     silent,
        int     actual_is_const)
 {
-    where_T where = WHERE_INIT;
-
     if (expected == &t_bool && actual != &t_bool
                                        && (actual->tt_flags & TTFLAG_BOOL_OK))
     {
@@ -1060,7 +1058,6 @@ need_type(
        return OK;
     }
 
-    where.wt_index = arg_idx;
     if (check_type(expected, actual, FALSE, where) == OK)
        return OK;
 
@@ -1069,15 +1066,32 @@ need_type(
     if ((!actual_is_const || actual == &t_any)
                                            && use_typecheck(actual, expected))
     {
-       generate_TYPECHECK(cctx, expected, offset, arg_idx);
+       generate_TYPECHECK(cctx, expected, offset, where.wt_index);
        return OK;
     }
 
     if (!silent)
-       arg_type_mismatch(expected, actual, arg_idx);
+       type_mismatch_where(expected, actual, where);
     return FAIL;
 }
 
+    int
+need_type(
+       type_T  *actual,
+       type_T  *expected,
+       int     offset,
+       int     arg_idx,
+       cctx_T  *cctx,
+       int     silent,
+       int     actual_is_const)
+{
+    where_T where = WHERE_INIT;
+
+    where.wt_index = arg_idx;
+    return need_type_where(actual, expected, offset, where,
+                                               cctx, silent, actual_is_const);
+}
+
 /*
  * Check that the top of the type stack has a type that can be used as a
  * condition.  Give an error and return FAIL if not.
@@ -7004,14 +7018,17 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
                    else if (*op == '=')
                    {
                        type_T *use_type = lhs.lhs_lvar->lv_type;
+                       where_T where = WHERE_INIT;
 
                        // Without operator check type here, otherwise below.
                        // Use the line number of the assignment.
                        SOURCING_LNUM = start_lnum;
+                       where.wt_index = var_count > 0 ? var_idx + 1 : 0;
+                       where.wt_variable = var_count > 0;
                        if (lhs.lhs_has_index)
                            use_type = lhs.lhs_member_type;
-                       if (need_type(rhs_type, use_type, -1, 0, cctx,
-                                                     FALSE, is_const) == FAIL)
+                       if (need_type_where(rhs_type, use_type, -1, where,
+                                   cctx, FALSE, is_const) == FAIL)
                            goto theend;
                    }
                }