Skip to content

Commit 108cf01

Browse files
committed
patch 8.2.2621: typval2type() cannot handle recursive structures
Problem: typval2type() cannot handle recursive structures. Solution: Use copyID. (closes #7979)
1 parent 4b3e196 commit 108cf01

6 files changed

Lines changed: 33 additions & 16 deletions

File tree

src/list.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
20522052
{
20532053
// Check that map() does not change the type of the dict.
20542054
ga_init2(&type_list, sizeof(type_T *), 10);
2055-
type = typval2type(argvars, &type_list);
2055+
type = typval2type(argvars, get_copyID(), &type_list);
20562056
}
20572057

20582058
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)
25582558
{
25592559
// Check that map() does not change the type of the dict.
25602560
ga_init2(&type_list, sizeof(type_T *), 10);
2561-
type = typval2type(argvars, &type_list);
2561+
type = typval2type(argvars, get_copyID(), &type_list);
25622562
}
25632563

25642564
if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)

src/proto/vim9type.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type_T *alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap);
99
type_T *get_func_type(type_T *ret_type, int argcount, garray_T *type_gap);
1010
int func_type_add_arg_types(type_T *functype, int argcount, garray_T *type_gap);
1111
int need_convert_to_bool(type_T *type, typval_T *tv);
12-
type_T *typval2type(typval_T *tv, garray_T *type_gap);
12+
type_T *typval2type(typval_T *tv, int copyID, garray_T *type_gap);
1313
type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
1414
int check_typval_arg_type(type_T *expected, typval_T *actual_tv, int arg_idx);
1515
int check_typval_type(type_T *expected, typval_T *actual_tv, where_T where);

src/testdir/test_vimscript.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6606,6 +6606,13 @@ func Test_typename()
66066606
call assert_equal('list<number>', typename([123]))
66076607
call assert_equal('dict<number>', typename(#{key: 123}))
66086608
call assert_equal('list<dict<number>>', typename([#{key: 123}]))
6609+
6610+
let l = []
6611+
let d = #{a: 0}
6612+
let l = [d]
6613+
let l[0].e = #{b: l}
6614+
call assert_equal('list<dict<any>>', typename(l))
6615+
call assert_equal('dict<any>', typename(d))
66096616
endfunc
66106617

66116618
"-------------------------------------------------------------------------------

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2621,
753755
/**/
754756
2620,
755757
/**/

src/vim9script.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ update_vim9_script_var(
750750
if (sv != NULL)
751751
{
752752
if (*type == NULL)
753-
*type = typval2type(tv, &si->sn_type_list);
753+
*type = typval2type(tv, get_copyID(), &si->sn_type_list);
754754
sv->sv_type = *type;
755755
}
756756

src/vim9type.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func_type_add_arg_types(
254254
* "type_gap" is used to temporarily create types in.
255255
*/
256256
static type_T *
257-
typval2type_int(typval_T *tv, garray_T *type_gap)
257+
typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
258258
{
259259
type_T *type;
260260
type_T *member_type = &t_any;
@@ -276,11 +276,15 @@ typval2type_int(typval_T *tv, garray_T *type_gap)
276276
return &t_list_empty;
277277
if (l->lv_first == &range_list_item)
278278
return &t_list_number;
279+
if (l->lv_copyID == copyID)
280+
// avoid recursion
281+
return &t_list_any;
282+
l->lv_copyID = copyID;
279283

280284
// Use the common type of all members.
281-
member_type = typval2type(&l->lv_first->li_tv, type_gap);
285+
member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap);
282286
for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
283-
common_type(typval2type(&li->li_tv, type_gap),
287+
common_type(typval2type(&li->li_tv, copyID, type_gap),
284288
member_type, &member_type, type_gap);
285289
return get_list_type(member_type, type_gap);
286290
}
@@ -289,17 +293,21 @@ typval2type_int(typval_T *tv, garray_T *type_gap)
289293
{
290294
dict_iterator_T iter;
291295
typval_T *value;
296+
dict_T *d = tv->vval.v_dict;
292297

293-
if (tv->vval.v_dict == NULL
294-
|| tv->vval.v_dict->dv_hashtab.ht_used == 0)
298+
if (d == NULL || d->dv_hashtab.ht_used == 0)
295299
return &t_dict_empty;
300+
if (d->dv_copyID == copyID)
301+
// avoid recursion
302+
return &t_dict_any;
303+
d->dv_copyID = copyID;
296304

297305
// Use the common type of all values.
298306
dict_iterate_start(tv, &iter);
299307
dict_iterate_next(&iter, &value);
300-
member_type = typval2type(value, type_gap);
308+
member_type = typval2type(value, copyID, type_gap);
301309
while (dict_iterate_next(&iter, &value) != NULL)
302-
common_type(typval2type(value, type_gap),
310+
common_type(typval2type(value, copyID, type_gap),
303311
member_type, &member_type, type_gap);
304312
return get_dict_type(member_type, type_gap);
305313
}
@@ -372,9 +380,9 @@ need_convert_to_bool(type_T *type, typval_T *tv)
372380
* "type_list" is used to temporarily create types in.
373381
*/
374382
type_T *
375-
typval2type(typval_T *tv, garray_T *type_gap)
383+
typval2type(typval_T *tv, int copyID, garray_T *type_gap)
376384
{
377-
type_T *type = typval2type_int(tv, type_gap);
385+
type_T *type = typval2type_int(tv, copyID, type_gap);
378386

379387
if (type != NULL && type != &t_bool
380388
&& (tv->v_type == VAR_NUMBER
@@ -396,7 +404,7 @@ typval2type_vimvar(typval_T *tv, garray_T *type_gap)
396404
return &t_list_string;
397405
if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
398406
return &t_dict_any;
399-
return typval2type(tv, type_gap);
407+
return typval2type(tv, get_copyID(), type_gap);
400408
}
401409

402410
int
@@ -421,7 +429,7 @@ check_typval_type(type_T *expected, typval_T *actual_tv, where_T where)
421429
int res = FAIL;
422430

423431
ga_init2(&type_list, sizeof(type_T *), 10);
424-
actual_type = typval2type(actual_tv, &type_list);
432+
actual_type = typval2type(actual_tv, get_copyID(), &type_list);
425433
if (actual_type != NULL)
426434
res = check_type(expected, actual_type, TRUE, where);
427435
clear_type_list(&type_list);
@@ -1202,7 +1210,7 @@ f_typename(typval_T *argvars, typval_T *rettv)
12021210

12031211
rettv->v_type = VAR_STRING;
12041212
ga_init2(&type_list, sizeof(type_T *), 10);
1205-
type = typval2type(argvars, &type_list);
1213+
type = typval2type(argvars, get_copyID(), &type_list);
12061214
name = type_name(type, &tofree);
12071215
if (tofree != NULL)
12081216
rettv->vval.v_string = (char_u *)tofree;

0 commit comments

Comments
 (0)