]> granicus.if.org Git - vim/commitdiff
patch 8.2.1407: Vim9: type of list and dict only depends on first item v8.2.1407
authorBram Moolenaar <Bram@vim.org>
Sun, 9 Aug 2020 15:22:04 +0000 (17:22 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 9 Aug 2020 15:22:04 +0000 (17:22 +0200)
Problem:    Vim9: type of list and dict only depends on first item.
Solution:   Use all items to decide about the type.

runtime/doc/vim9.txt
src/proto/vim9type.pro
src/testdir/test_vim9_expr.vim
src/version.c
src/vim9compile.c
src/vim9type.c

index 7f7b15bb238396a1de8748f68b8d9f59f2bfd6ba..8ff70595f464839e98aa88eb1ae5c25b3701734a 100644 (file)
@@ -619,6 +619,8 @@ called in the same way the declaration is the same.
 
 Custom types can be defined with `:type`: >
        :type MyList list<string>
+Custom types must start with a capital letter, to avoid name clashes with
+builtin types added later, similarly to user functions.
 {not implemented yet}
 
 And classes and interfaces can be used as types: >
@@ -645,6 +647,12 @@ declaring a variable and giving it a value: >
        let var = 0             " infers number type
        let var = 'hello'       " infers string type
 
+The type of a list and dictionary comes from the common type of the values.
+If the values all have the same type, that type is used for the list or
+dictionary.  If there is a mix of types, the "any" type is used. >
+       [1, 2, 3]       list<number>
+       ['a', 'b', 'c'] list<string>
+       [1, 'x', 3]     list<any>
 
 ==============================================================================
 
index 5c7ba03ce66d5c4b4c0e0b57f30adbc6cd329425..5ad87e8e0eb9e361e4725debbd1a66552ac09f21 100644 (file)
@@ -14,6 +14,7 @@ int check_type(type_T *expected, type_T *actual, int give_msg);
 char_u *skip_type(char_u *start, int optional);
 type_T *parse_type(char_u **arg, garray_T *type_gap);
 void common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap);
+type_T *get_member_type_from_stack(type_T **stack_top, int count, int skip, garray_T *type_gap);
 char *vartype_name(vartype_T type);
 char *type_name(type_T *type, char **tofree);
 /* vim: set ft=c : */
index 7e1e16acdd9d40b3dad16df7fe06e5d2f750f421..7891127e5cd87442fbd401830220ccc3057b03ff 100644 (file)
@@ -1334,7 +1334,17 @@ def Test_expr7_list()
   # list
   assert_equal(g:list_empty, [])
   assert_equal(g:list_empty, [  ])
-  assert_equal(g:list_mixed, [1, 'b', false,])
+
+  let numbers: list<number> = [1, 2, 3]
+  numbers = [1]
+  numbers = []
+
+  let strings: list<string> = ['a', 'b', 'c']
+  strings = ['x']
+  strings = []
+
+  let mixed: list<any> = [1, 'b', false,]
+  assert_equal(g:list_mixed, mixed)
   assert_equal('b', g:list_mixed[1])
 
   echo [1,
@@ -1348,6 +1358,10 @@ def Test_expr7_list()
   call CheckDefFailure(["let x = g:list_mixed["], 'E1097:')
   call CheckDefFailure(["let x = g:list_mixed[0"], 'E1097:')
   call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:')
+  call CheckDefFailure(["let l: list<number> = [234, 'x']"], 'E1013:')
+  call CheckDefFailure(["let l: list<number> = ['x', 234]"], 'E1013:')
+  call CheckDefFailure(["let l: list<string> = [234, 'x']"], 'E1013:')
+  call CheckDefFailure(["let l: list<string> = ['x', 123]"], 'E1013:')
 enddef
 
 def Test_expr7_list_vim9script()
@@ -1437,6 +1451,19 @@ def Test_expr7_dict()
   let val = 1
   assert_equal(g:dict_one, {key: val})
 
+  let numbers: dict<number> = #{a: 1, b: 2, c: 3}
+  numbers = #{a: 1}
+  numbers = #{}
+
+  let strings: dict<string> = #{a: 'a', b: 'b', c: 'c'}
+  strings = #{a: 'x'}
+  strings = #{}
+
+  let mixed: dict<any> = #{a: 'a', b: 42}
+  mixed = #{a: 'x'}
+  mixed = #{a: 234}
+  mixed = #{}
+
   call CheckDefFailure(["let x = #{8: 8}"], 'E1014:')
   call CheckDefFailure(["let x = #{xxx}"], 'E720:')
   call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:')
@@ -1449,6 +1476,11 @@ def Test_expr7_dict()
   call CheckDefFailure(["let x = x + 1"], 'E1001:')
   call CheckDefExecFailure(["let x = g:anint.member"], 'E715:')
   call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:')
+
+  call CheckDefFailure(['let x: dict<number> = #{a: 234, b: "1"}'], 'E1013:')
+  call CheckDefFailure(['let x: dict<number> = #{a: "x", b: 134}'], 'E1013:')
+  call CheckDefFailure(['let x: dict<string> = #{a: 234, b: "1"}'], 'E1013:')
+  call CheckDefFailure(['let x: dict<string> = #{a: "x", b: 134}'], 'E1013:')
 enddef
 
 def Test_expr7_dict_vim9script()
index 77613464a5110449a339fc4a4144081f23431c3e..e7dd7dd0f1121cf544ff7767607ded250ab62a0c 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1407,
 /**/
     1406,
 /**/
index e23dd7a31b0244dbe731817e5d0f3fd0c90145b2..19760fb695f63e590201f3bf726ef8efff13024b 100644 (file)
@@ -1110,17 +1110,15 @@ generate_NEWLIST(cctx_T *cctx, int count)
        return FAIL;
     isn->isn_arg.number = count;
 
+    // get the member type from all the items on the stack.
+    member = get_member_type_from_stack(
+           ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
+                                                         cctx->ctx_type_list);
+    type = get_list_type(member, cctx->ctx_type_list);
+
     // drop the value types
     stack->ga_len -= count;
 
-    // Use the first value type for the list member type.  Use "any" for an
-    // empty list.
-    if (count > 0)
-       member = ((type_T **)stack->ga_data)[stack->ga_len];
-    else
-       member = &t_void;
-    type = get_list_type(member, cctx->ctx_type_list);
-
     // add the list type to the type stack
     if (ga_grow(stack, 1) == FAIL)
        return FAIL;
@@ -1146,17 +1144,14 @@ generate_NEWDICT(cctx_T *cctx, int count)
        return FAIL;
     isn->isn_arg.number = count;
 
+    member = get_member_type_from_stack(
+           ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
+                                                         cctx->ctx_type_list);
+    type = get_dict_type(member, cctx->ctx_type_list);
+
     // drop the key and value types
     stack->ga_len -= 2 * count;
 
-    // Use the first value type for the list member type.  Use "void" for an
-    // empty dict.
-    if (count > 0)
-       member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
-    else
-       member = &t_void;
-    type = get_dict_type(member, cctx->ctx_type_list);
-
     // add the dict type to the type stack
     if (ga_grow(stack, 1) == FAIL)
        return FAIL;
index 5cfdc665ee73bf5711653198e2caa54093283e36..ecac69414acf4cf9fd8456421e202c0ae532a423 100644 (file)
@@ -789,6 +789,42 @@ common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
     *dest = &t_any;
 }
 
+/*
+ * Get the member type of a dict or list from the items on the stack.
+ * "stack_top" points just after the last type on the type stack.
+ * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped.
+ * Returns &t_void for an empty list or dict.
+ * Otherwise finds the common type of all items.
+ */
+    type_T *
+get_member_type_from_stack(
+       type_T      **stack_top,
+       int         count,
+       int         skip,
+       garray_T    *type_gap)
+{
+    int            i;
+    type_T  *result;
+    type_T  *type;
+
+    // Use "any" for an empty list or dict.
+    if (count == 0)
+       return &t_void;
+
+    // Use the first value type for the list member type, then find the common
+    // type from following items.
+    result = *(stack_top -(count * skip) + skip - 1);
+    for (i = 1; i < count; ++i)
+    {
+       if (result == &t_any)
+           break;  // won't get more common
+       type = *(stack_top -((count - i) * skip) + skip - 1);
+       common_type(type, result, &result, type_gap);
+    }
+
+    return result;
+}
+
     char *
 vartype_name(vartype_T type)
 {