summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-08-14 21:27:37 +0200
committerBram Moolenaar <Bram@vim.org>2020-08-14 21:27:37 +0200
commit41fab3eac80893fd203663fc6a7ded09b04b633f (patch)
tree38c127f5920ba148355c5b9ebe8e35e49765ecac
parent7d6997015d533604f18756e59b4a2a1266a66a97 (diff)
patch 8.2.1451: Vim9: list type at script level only uses first itemv8.2.1451
Problem: Vim9: list type at script level only uses first item. Solution: Use all members, like in a compiled function. (closes #6712) Also for dictionary.
-rw-r--r--src/testdir/test_vim9_expr.vim42
-rw-r--r--src/version.c2
-rw-r--r--src/vim9type.c13
3 files changed, 55 insertions, 2 deletions
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 81d3cb9f93..c6734539e0 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -1498,6 +1498,27 @@ def Test_expr7_list_vim9script()
let l = [11 , 22]
END
CheckScriptFailure(lines, 'E1068:')
+
+ lines =<< trim END
+ vim9script
+ let l: list<number> = [234, 'x']
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: list<number> = ['x', 234]
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: list<string> = ['x', 234]
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: list<string> = [234, 'x']
+ END
+ CheckScriptFailure(lines, 'E1013:')
enddef
def LambdaWithComments(): func
@@ -1679,6 +1700,27 @@ def Test_expr7_dict_vim9script()
let d = #{one: 1 , two: 2}
END
CheckScriptFailure(lines, 'E1068:')
+
+ lines =<< trim END
+ vim9script
+ let l: dict<number> = #{a: 234, b: 'x'}
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: dict<number> = #{a: 'x', b: 234}
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: dict<string> = #{a: 'x', b: 234}
+ END
+ CheckScriptFailure(lines, 'E1013:')
+ lines =<< trim END
+ vim9script
+ let l: dict<string> = #{a: 234, b: 'x'}
+ END
+ CheckScriptFailure(lines, 'E1013:')
enddef
let g:oneString = 'one'
diff --git a/src/version.c b/src/version.c
index ce5bfb01a9..132af7cae1 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1451,
+/**/
1450,
/**/
1449,
diff --git a/src/vim9type.c b/src/vim9type.c
index 95785974ad..318739bde8 100644
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -214,11 +214,17 @@ typval2type(typval_T *tv, garray_T *type_gap)
if (tv->v_type == VAR_LIST)
{
+ listitem_T *li;
+
if (tv->vval.v_list == NULL || tv->vval.v_list->lv_first == NULL)
return &t_list_empty;
- // Use the type of the first member, it is the most specific.
+ // Use the common type of all members.
member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
+ for (li = tv->vval.v_list->lv_first->li_next; li != NULL;
+ li = li->li_next)
+ common_type(typval2type(&li->li_tv, type_gap),
+ member_type, &member_type, type_gap);
return get_list_type(member_type, type_gap);
}
@@ -231,10 +237,13 @@ typval2type(typval_T *tv, garray_T *type_gap)
|| tv->vval.v_dict->dv_hashtab.ht_used == 0)
return &t_dict_empty;
- // Use the type of the first value, it is the most specific.
+ // Use the common type of all values.
dict_iterate_start(tv, &iter);
dict_iterate_next(&iter, &value);
member_type = typval2type(value, type_gap);
+ while (dict_iterate_next(&iter, &value) != NULL)
+ common_type(typval2type(value, type_gap),
+ member_type, &member_type, type_gap);
return get_dict_type(member_type, type_gap);
}