]> granicus.if.org Git - vim/commitdiff
patch 8.2.2759: Vim9: for loop infers type of loop variable v8.2.2759
authorBram Moolenaar <Bram@vim.org>
Tue, 13 Apr 2021 18:53:13 +0000 (20:53 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 13 Apr 2021 18:53:13 +0000 (20:53 +0200)
Problem:    Vim9: for loop infers type of loop variable.
Solution:   Do not get the member type. (closes #8102)

src/list.c
src/proto/vim9script.pro
src/proto/vim9type.pro
src/testdir/test_vim9_script.vim
src/version.c
src/vim.h
src/vim9script.c
src/vim9type.c

index 76327abc87c60ffaafe2d1ed77a64df745666b17..56b2188ff4d64b36f3037bb9eab9ed2fd67f1b9e 100644 (file)
@@ -2059,7 +2059,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
     {
        // Check that map() does not change the type of the dict.
        ga_init2(&type_list, sizeof(type_T *), 10);
-       type = typval2type(argvars, get_copyID(), &type_list);
+       type = typval2type(argvars, get_copyID(), &type_list, TRUE);
     }
 
     if (argvars[0].v_type == VAR_BLOB)
@@ -2565,7 +2565,7 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
     {
        // Check that map() does not change the type of the dict.
        ga_init2(&type_list, sizeof(type_T *), 10);
-       type = typval2type(argvars, get_copyID(), &type_list);
+       type = typval2type(argvars, get_copyID(), &type_list, TRUE);
     }
 
     if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
index c43120c4527a59822bc5b5bff80e2d7fffa97edd..cb5a30d2e0fcd806cb28d8c71b141fdeeb7eba14 100644 (file)
@@ -12,7 +12,7 @@ void ex_import(exarg_T *eap);
 int find_exported(int sid, char_u *name, ufunc_T **ufunc, type_T **type, cctx_T *cctx, int verbose);
 char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, evalarg_T *evalarg, void *cctx);
 char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
-void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type);
+void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type, int do_member);
 void hide_script_var(scriptitem_T *si, int idx, int func_defined);
 void free_all_script_vars(scriptitem_T *si);
 svar_T *find_typval_in_script(typval_T *dest);
index b5aa6ee921261f81904a3f8242666ef7bed0acbd..a512fa4d557bd688725426388d9f306a96466e2f 100644 (file)
@@ -9,7 +9,7 @@ type_T *alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap);
 type_T *get_func_type(type_T *ret_type, int argcount, garray_T *type_gap);
 int func_type_add_arg_types(type_T *functype, int argcount, garray_T *type_gap);
 int need_convert_to_bool(type_T *type, typval_T *tv);
-type_T *typval2type(typval_T *tv, int copyID, garray_T *type_gap);
+type_T *typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member);
 type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
 int check_typval_arg_type(type_T *expected, typval_T *actual_tv, int arg_idx);
 int check_typval_type(type_T *expected, typval_T *actual_tv, where_T where);
index e57021009148299f94207fc902b4e490a1ddb771..db1707ace60f011931f04acdf798a397daa18577 100644 (file)
@@ -2295,70 +2295,82 @@ def Test_for_outside_of_function()
 enddef
 
 def Test_for_loop()
-  var result = ''
-  for cnt in range(7)
-    if cnt == 4
-      break
-    endif
-    if cnt == 2
-      continue
-    endif
-    result ..= cnt .. '_'
-  endfor
-  assert_equal('0_1_3_', result)
+  var lines =<< trim END
+      var result = ''
+      for cnt in range(7)
+        if cnt == 4
+          break
+        endif
+        if cnt == 2
+          continue
+        endif
+        result ..= cnt .. '_'
+      endfor
+      assert_equal('0_1_3_', result)
 
-  var concat = ''
-  for str in eval('["one", "two"]')
-    concat ..= str
-  endfor
-  assert_equal('onetwo', concat)
+      var concat = ''
+      for str in eval('["one", "two"]')
+        concat ..= str
+      endfor
+      assert_equal('onetwo', concat)
 
-  var total = 0
-  for nr in
-      [1, 2, 3]
-    total += nr
-  endfor
-  assert_equal(6, total)
+      var total = 0
+      for nr in
+          [1, 2, 3]
+        total += nr
+      endfor
+      assert_equal(6, total)
 
-  total = 0
-  for nr
-    in [1, 2, 3]
-    total += nr
-  endfor
-  assert_equal(6, total)
+      total = 0
+      for nr
+        in [1, 2, 3]
+        total += nr
+      endfor
+      assert_equal(6, total)
 
-  total = 0
-  for nr
-    in
-    [1, 2, 3]
-    total += nr
-  endfor
-  assert_equal(6, total)
+      total = 0
+      for nr
+        in
+        [1, 2, 3]
+        total += nr
+      endfor
+      assert_equal(6, total)
+
+      # loop over string
+      var res = ''
+      for c in 'aéc̀d'
+        res ..= c .. '-'
+      endfor
+      assert_equal('a-é-c̀-d-', res)
+
+      res = ''
+      for c in ''
+        res ..= c .. '-'
+      endfor
+      assert_equal('', res)
+
+      res = ''
+      for c in test_null_string()
+        res ..= c .. '-'
+      endfor
+      assert_equal('', res)
 
+      var foo: list<dict<any>> = [
+              {a: 'Cat'}
+            ]
+      for dd in foo
+        dd.counter = 12
+      endfor
+      assert_equal([{a: 'Cat', counter: 12}], foo)
+  END
+  CheckDefAndScriptSuccess(lines)
+
+  # TODO: should also work at script level
   var res = ""
   for [n: number, s: string] in [[1, 'a'], [2, 'b']]
     res ..= n .. s
   endfor
   assert_equal('1a2b', res)
-
-  # loop over string
-  res = ''
-  for c in 'aéc̀d'
-    res ..= c .. '-'
-  endfor
-  assert_equal('a-é-c̀-d-', res)
-
-  res = ''
-  for c in ''
-    res ..= c .. '-'
-  endfor
-  assert_equal('', res)
-
-  res = ''
-  for c in test_null_string()
-    res ..= c .. '-'
-  endfor
-  assert_equal('', res)
 enddef
 
 def Test_for_loop_fails()
@@ -2471,20 +2483,23 @@ def Test_for_loop_unpack()
 enddef
 
 def Test_for_loop_with_try_continue()
-  var looped = 0
-  var cleanup = 0
-  for i in range(3)
-    looped += 1
-    try
-      eval [][0]
-    catch
-      continue
-    finally
-      cleanup += 1
-    endtry
-  endfor
-  assert_equal(3, looped)
-  assert_equal(3, cleanup)
+  var lines =<< trim END
+      var looped = 0
+      var cleanup = 0
+      for i in range(3)
+        looped += 1
+        try
+          eval [][0]
+        catch
+          continue
+        finally
+          cleanup += 1
+        endtry
+      endfor
+      assert_equal(3, looped)
+      assert_equal(3, cleanup)
+  END
+  CheckDefAndScriptSuccess(lines)
 enddef
 
 def Test_while_loop()
index 4e61210536102b6342a3295bc35569e9618e4dda..fdb8d0a9b5c82d9981ab93cf2dd591304b5a17fc 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2759,
 /**/
     2758,
 /**/
index 31e181a13a299ac81011cde2256e991c577376b9..baa3526d0feddedb32b63b2b3eab005eedb7343a 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -2157,6 +2157,7 @@ typedef enum {
 #define ASSIGN_NO_DECL 0x04  // "name = expr" without ":let"/":const"/":final"
 #define ASSIGN_DECL    0x08  // may declare variable if it does not exist
 #define ASSIGN_UNPACK  0x10  // using [a, b] = list
+#define ASSIGN_NO_MEMBER_TYPE 0x20 // use "any" for list and dict member type
 
 #include "ex_cmds.h"       // Ex command defines
 #include "spell.h"         // spell checking stuff
index 2f2f2dce67b97f94f8e882bf2f2d292e3e13f78b..796b7d13cbdf6ac993300ea0d8edfee53ff9cffc 100644 (file)
@@ -713,7 +713,8 @@ vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
  * When "create" is TRUE this is a new variable, otherwise find and update an
  * existing variable.
  * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
- * When "*type" is NULL use "tv" for the type and update "*type".
+ * When "*type" is NULL use "tv" for the type and update "*type".  If
+ * "do_member" is TRUE also use the member type, otherwise use "any".
  */
     void
 update_vim9_script_var(
@@ -721,7 +722,8 @@ update_vim9_script_var(
        dictitem_T  *di,
        int         flags,
        typval_T    *tv,
-       type_T      **type)
+       type_T      **type,
+       int         do_member)
 {
     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
     hashitem_T     *hi;
@@ -774,7 +776,8 @@ update_vim9_script_var(
     if (sv != NULL)
     {
        if (*type == NULL)
-           *type = typval2type(tv, get_copyID(), &si->sn_type_list);
+           *type = typval2type(tv, get_copyID(), &si->sn_type_list,
+                                                                   do_member);
        sv->sv_type = *type;
     }
 
index 2485efb41e4f08aca3b01f7d451ef7e89fad2915..8b0f2f15e2b46c2fe57bc4197b5a622f641408fc 100644 (file)
@@ -252,9 +252,10 @@ func_type_add_arg_types(
 /*
  * Get a type_T for a typval_T.
  * "type_gap" is used to temporarily create types in.
+ * When "do_member" is TRUE also get the member type, otherwise use "any".
  */
     static type_T *
-typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
+typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 {
     type_T  *type;
     type_T  *member_type = &t_any;
@@ -274,6 +275,8 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
 
        if (l == NULL || l->lv_first == NULL)
            return &t_list_empty;
+       if (!do_member)
+           return &t_list_any;
        if (l->lv_first == &range_list_item)
            return &t_list_number;
        if (l->lv_copyID == copyID)
@@ -282,9 +285,9 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
        l->lv_copyID = copyID;
 
        // Use the common type of all members.
-       member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap);
+       member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap, TRUE);
        for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
-           common_type(typval2type(&li->li_tv, copyID, type_gap),
+           common_type(typval2type(&li->li_tv, copyID, type_gap, TRUE),
                                          member_type, &member_type, type_gap);
        return get_list_type(member_type, type_gap);
     }
@@ -297,6 +300,8 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
 
        if (d == NULL || d->dv_hashtab.ht_used == 0)
            return &t_dict_empty;
+       if (!do_member)
+           return &t_dict_any;
        if (d->dv_copyID == copyID)
            // avoid recursion
            return &t_dict_any;
@@ -305,9 +310,9 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
        // Use the common type of all values.
        dict_iterate_start(tv, &iter);
        dict_iterate_next(&iter, &value);
-       member_type = typval2type(value, copyID, type_gap);
+       member_type = typval2type(value, copyID, type_gap, TRUE);
        while (dict_iterate_next(&iter, &value) != NULL)
-           common_type(typval2type(value, copyID, type_gap),
+           common_type(typval2type(value, copyID, type_gap, TRUE),
                                          member_type, &member_type, type_gap);
        return get_dict_type(member_type, type_gap);
     }
@@ -378,11 +383,12 @@ need_convert_to_bool(type_T *type, typval_T *tv)
 /*
  * Get a type_T for a typval_T.
  * "type_list" is used to temporarily create types in.
+ * When "do_member" is TRUE also get the member type, otherwise use "any".
  */
     type_T *
-typval2type(typval_T *tv, int copyID, garray_T *type_gap)
+typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 {
-    type_T *type = typval2type_int(tv, copyID, type_gap);
+    type_T *type = typval2type_int(tv, copyID, type_gap, do_member);
 
     if (type != NULL && type != &t_bool
            && (tv->v_type == VAR_NUMBER
@@ -404,7 +410,7 @@ typval2type_vimvar(typval_T *tv, garray_T *type_gap)
        return &t_list_string;
     if (tv->v_type == VAR_DICT)  // e.g. for v:completed_item
        return &t_dict_any;
-    return typval2type(tv, get_copyID(), type_gap);
+    return typval2type(tv, get_copyID(), type_gap, TRUE);
 }
 
     int
@@ -429,7 +435,7 @@ check_typval_type(type_T *expected, typval_T *actual_tv, where_T where)
     int                res = FAIL;
 
     ga_init2(&type_list, sizeof(type_T *), 10);
-    actual_type = typval2type(actual_tv, get_copyID(), &type_list);
+    actual_type = typval2type(actual_tv, get_copyID(), &type_list, TRUE);
     if (actual_type != NULL)
        res = check_type(expected, actual_type, TRUE, where);
     clear_type_list(&type_list);
@@ -1210,7 +1216,7 @@ f_typename(typval_T *argvars, typval_T *rettv)
 
     rettv->v_type = VAR_STRING;
     ga_init2(&type_list, sizeof(type_T *), 10);
-    type = typval2type(argvars, get_copyID(), &type_list);
+    type = typval2type(argvars, get_copyID(), &type_list, TRUE);
     name = type_name(type, &tofree);
     if (tofree != NULL)
        rettv->vval.v_string = (char_u *)tofree;