]> granicus.if.org Git - vim/commitdiff
patch 8.2.2621: typval2type() cannot handle recursive structures v8.2.2621
authorBram Moolenaar <Bram@vim.org>
Thu, 18 Mar 2021 21:15:04 +0000 (22:15 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 18 Mar 2021 21:15:04 +0000 (22:15 +0100)
Problem:    typval2type() cannot handle recursive structures.
Solution:   Use copyID. (closes #7979)

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

index b1080dee0f578aa1f735bbe6676588250fbb0e57..873f9e63de5db6f7bea0cc9d91fd986ee1a8fcd2 100644 (file)
@@ -2052,7 +2052,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, &type_list);
+       type = typval2type(argvars, get_copyID(), &type_list);
     }
 
     if (argvars[0].v_type == VAR_BLOB)
@@ -2558,7 +2558,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, &type_list);
+       type = typval2type(argvars, get_copyID(), &type_list);
     }
 
     if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
index 35da1fe72e54bfc54bc65548682ee7c94f604235..b5aa6ee921261f81904a3f8242666ef7bed0acbd 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, garray_T *type_gap);
+type_T *typval2type(typval_T *tv, int copyID, garray_T *type_gap);
 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 0ba933b7cbe84e8262c2fd6c1b00aaa1e8d9f948..b57d86d471d67b7afb218cb2208b6ccda366bb26 100644 (file)
@@ -6606,6 +6606,13 @@ func Test_typename()
   call assert_equal('list<number>', typename([123]))
   call assert_equal('dict<number>', typename(#{key: 123}))
   call assert_equal('list<dict<number>>', typename([#{key: 123}]))
+
+  let l = []
+  let d = #{a: 0}
+  let l = [d]
+  let l[0].e = #{b: l}
+  call assert_equal('list<dict<any>>', typename(l))
+  call assert_equal('dict<any>', typename(d))
 endfunc
 
 "-------------------------------------------------------------------------------
index dfc0f033cab130cd57af8a9ddf7a0ba0a2a146f9..842d138f939ace202a063ad53d3b0ef0943de96e 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2621,
 /**/
     2620,
 /**/
index 309d4bdaf6c9672f0ef01ba3ea5ae020726bd90f..9366e7632a8c029d3eff4b060c793d0bb635e478 100644 (file)
@@ -750,7 +750,7 @@ update_vim9_script_var(
     if (sv != NULL)
     {
        if (*type == NULL)
-           *type = typval2type(tv, &si->sn_type_list);
+           *type = typval2type(tv, get_copyID(), &si->sn_type_list);
        sv->sv_type = *type;
     }
 
index 9eda30d6e6260726095db07e7fef0c96cd4f1f52..2485efb41e4f08aca3b01f7d451ef7e89fad2915 100644 (file)
@@ -254,7 +254,7 @@ func_type_add_arg_types(
  * "type_gap" is used to temporarily create types in.
  */
     static type_T *
-typval2type_int(typval_T *tv, garray_T *type_gap)
+typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
 {
     type_T  *type;
     type_T  *member_type = &t_any;
@@ -276,11 +276,15 @@ typval2type_int(typval_T *tv, garray_T *type_gap)
            return &t_list_empty;
        if (l->lv_first == &range_list_item)
            return &t_list_number;
+       if (l->lv_copyID == copyID)
+           // avoid recursion
+           return &t_list_any;
+       l->lv_copyID = copyID;
 
        // Use the common type of all members.
-       member_type = typval2type(&l->lv_first->li_tv, type_gap);
+       member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap);
        for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
-           common_type(typval2type(&li->li_tv, type_gap),
+           common_type(typval2type(&li->li_tv, copyID, type_gap),
                                          member_type, &member_type, type_gap);
        return get_list_type(member_type, type_gap);
     }
@@ -289,17 +293,21 @@ typval2type_int(typval_T *tv, garray_T *type_gap)
     {
        dict_iterator_T iter;
        typval_T        *value;
+       dict_T          *d = tv->vval.v_dict;
 
-       if (tv->vval.v_dict == NULL
-                                  || tv->vval.v_dict->dv_hashtab.ht_used == 0)
+       if (d == NULL || d->dv_hashtab.ht_used == 0)
            return &t_dict_empty;
+       if (d->dv_copyID == copyID)
+           // avoid recursion
+           return &t_dict_any;
+       d->dv_copyID = copyID;
 
        // Use the common type of all values.
        dict_iterate_start(tv, &iter);
        dict_iterate_next(&iter, &value);
-       member_type = typval2type(value, type_gap);
+       member_type = typval2type(value, copyID, type_gap);
        while (dict_iterate_next(&iter, &value) != NULL)
-           common_type(typval2type(value, type_gap),
+           common_type(typval2type(value, copyID, type_gap),
                                          member_type, &member_type, type_gap);
        return get_dict_type(member_type, type_gap);
     }
@@ -372,9 +380,9 @@ need_convert_to_bool(type_T *type, typval_T *tv)
  * "type_list" is used to temporarily create types in.
  */
     type_T *
-typval2type(typval_T *tv, garray_T *type_gap)
+typval2type(typval_T *tv, int copyID, garray_T *type_gap)
 {
-    type_T *type = typval2type_int(tv, type_gap);
+    type_T *type = typval2type_int(tv, copyID, type_gap);
 
     if (type != NULL && type != &t_bool
            && (tv->v_type == VAR_NUMBER
@@ -396,7 +404,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, type_gap);
+    return typval2type(tv, get_copyID(), type_gap);
 }
 
     int
@@ -421,7 +429,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, &type_list);
+    actual_type = typval2type(actual_tv, get_copyID(), &type_list);
     if (actual_type != NULL)
        res = check_type(expected, actual_type, TRUE, where);
     clear_type_list(&type_list);
@@ -1202,7 +1210,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, &type_list);
+    type = typval2type(argvars, get_copyID(), &type_list);
     name = type_name(type, &tofree);
     if (tofree != NULL)
        rettv->vval.v_string = (char_u *)tofree;