]> granicus.if.org Git - vim/commitdiff
patch 8.2.3991: Vim9: error when extending dict<any> v8.2.3991
authorBram Moolenaar <Bram@vim.org>
Mon, 3 Jan 2022 12:28:03 +0000 (12:28 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 3 Jan 2022 12:28:03 +0000 (12:28 +0000)
Problem:    Vim9: error when extending dict<any> with another type that it was
            initialized with.
Solution:   Also set the type for dict<any> if the initializer has a more
            specific type. (closes #9461)

src/eval.c
src/list.c
src/testdir/test_vim9_assign.vim
src/testdir/test_vim9_builtin.vim
src/testdir/test_vim9_func.vim
src/version.c
src/vim9.h
src/vim9compile.c
src/vim9script.c
src/vim9type.c

index 99bc5a577a176893e7c7e139b344d55fc1428066..0fb79112dc1c260e151785df0deaebf350396f15 100644 (file)
@@ -3328,7 +3328,8 @@ eval7t(
     {
        if (res == OK)
        {
-           type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
+           type_T *actual = typval2type(rettv, get_copyID(), &type_list,
+                                                              TVTT_DO_MEMBER);
 
            if (!equal_type(want_type, actual, 0))
            {
index 743ccf5193c298151e0249812a84562561577b16..6b275789d502134531b6019d14c2a8c1c4505f0f 100644 (file)
@@ -2478,7 +2478,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, TRUE);
+       type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
     }
 
     if (argvars[0].v_type != VAR_BLOB
@@ -2763,9 +2763,9 @@ extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
 
     if (!is_new && in_vim9script())
     {
-       // Check that map() does not change the type of the dict.
+       // Check that extend() does not change the type of the dict.
        ga_init2(&type_list, sizeof(type_T *), 10);
-       type = typval2type(argvars, get_copyID(), &type_list, TRUE);
+       type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
     }
 
     if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
index 683180995ba9df42917f39f6ec6a26f83074a75b..223a6e401dd8273a3c6ba52c1809e3d082d35c95 100644 (file)
@@ -757,6 +757,10 @@ def Test_assignment_list()
   # type becomes list<any>
   var somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c']
 
+  # type is list<any> even though initializer is list<number>
+  var anyList: list<any> = [0]
+  assert_equal([0, 'x'], extend(anyList, ['x']))
+
   var lines =<< trim END
     var d = {dd: test_null_list()}
     d.dd[0] = 0
@@ -955,6 +959,10 @@ def Test_assignment_dict()
   # type becomes dict<any>
   var somedict = rand() > 0 ? {a: 1, b: 2} : {a: 'a', b: 'b'}
 
+  # type is dict<any> even though initializer is dict<number>
+  var anyDict: dict<any> = {a: 0}
+  assert_equal({a: 0, b: 'x'}, extend(anyDict, {b: 'x'}))
+
   # assignment to script-local dict
   lines =<< trim END
     vim9script
index 2ee1a93442ab3ea4bd6079767c70d8b9862d4ef7..df27dba6f2eddc86329d2357e1a36101f381fbf8 100644 (file)
@@ -2143,6 +2143,11 @@ def Test_map()
     CheckDefAndScriptFailure(['map(test_null_channel(), "1")'], ['E1013: Argument 1: type mismatch, expected list<any> but got channel', 'E1251: List, Dictionary, Blob or String required for argument 1'])
   endif
   CheckDefAndScriptFailure(['map(1, "1")'], ['E1013: Argument 1: type mismatch, expected list<any> but got number', 'E1251: List, Dictionary, Blob or String required for argument 1'])
+
+  # type of dict remains dict<any> even when type of values changes
+  var d: dict<any> = {a: 0}
+  d->map((k, v) => true)
+  d->map((k, v) => 'x')
 enddef
 
 def Test_map_failure()
index 1cdf7d480820b2950f3a2c0b0c9db37edf8f8cee..0ec1700dc621e6a65daafb389cd6c1c672dfdb18 100644 (file)
@@ -439,6 +439,8 @@ def Test_return_invalid()
 enddef
 
 def Test_return_list_any()
+  # This used to fail but now the actual list type is checked, and since it has
+  # an item of type string it can be used as list<string>.
   var lines =<< trim END
       vim9script
       def Func(): list<string>
@@ -448,7 +450,8 @@ def Test_return_list_any()
       enddef
       echo Func()
   END
-  CheckScriptFailure(lines, 'E1012:')
+  CheckScriptSuccess(lines)
+
   lines =<< trim END
       vim9script
       def Func(): list<string>
@@ -458,7 +461,7 @@ def Test_return_list_any()
       enddef
       echo Func()
   END
-  CheckScriptFailure(lines, 'E1012:')
+  CheckScriptSuccess(lines)
 enddef
 
 func Increment()
index 6ad10903372b17f86ce49c61cb6b4de5bc27f8ce..96864ef31363d31f5d59cbd89fe0b34b6b631ad0 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3991,
 /**/
     3990,
 /**/
index d2c23be01b1b84b0aec63ebcc018b8a1b3f8a928..2ae3f42088cc875bd1fbb38d568b7e4d1926b917 100644 (file)
@@ -725,3 +725,7 @@ struct cctx_S {
                                    // lhs_name is not NULL
 };
 
+// flags for typval2type()
+#define TVTT_DO_MEMBER     1
+#define TVTT_MORE_SPECIFIC  2  // get most specific type for member
+
index 51cb44d544d409d361a849316ef73504750b617a..026b18f39b2f159c40b900e5cb0246f4e6ab9713 100644 (file)
@@ -2286,11 +2286,6 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
                    && (lhs.lhs_type->tt_type == VAR_DICT
                                          || lhs.lhs_type->tt_type == VAR_LIST)
                    && lhs.lhs_type->tt_member != NULL
-                   && !(lhs.lhs_type->tt_member == &t_any
-                           && oplen > 0
-                           && rhs_type != NULL
-                           && rhs_type->tt_type == lhs.lhs_type->tt_type
-                           && rhs_type->tt_member != &t_unknown)
                    && lhs.lhs_type->tt_member != &t_unknown)
                // Set the type in the list or dict, so that it can be checked,
                // also in legacy script.  Not for "list<any> = val", then the
index ab6557073f00d873cc9fc5254283feed76dd9610..12c266a27bf0bfd2c48d20b2ecf06d6d793915ef 100644 (file)
@@ -907,7 +907,8 @@ update_vim9_script_var(
     if (sv != NULL)
     {
        if (*type == NULL)
-           *type = typval2type(tv, get_copyID(), &si->sn_type_list, do_member);
+           *type = typval2type(tv, get_copyID(), &si->sn_type_list,
+                                              do_member ? TVTT_DO_MEMBER : 0);
        if (sv->sv_type_allocated)
            free_type(sv->sv_type);
        if (*type != NULL && ((*type)->tt_type == VAR_FUNC
index c6e80e6777c92d604d1965b910d59075709c3090..d07c6e6a08f70e541b70fd51b1440754ed7767e1 100644 (file)
@@ -252,10 +252,13 @@ 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".
+ * When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
+ * "any".
+ * When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
+ * is "any".
  */
     static type_T *
-typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
+typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
 {
     type_T  *type;
     type_T  *member_type = NULL;
@@ -278,9 +281,13 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 
        if (l == NULL || (l->lv_first == NULL && l->lv_type == NULL))
            return &t_list_empty;
-       if (!do_member)
+       if ((flags & TVTT_DO_MEMBER) == 0)
            return &t_list_any;
-       if (l->lv_type != NULL)
+       // If the type is list<any> go through the members, it may end up a
+       // more specific type.
+       if (l->lv_type != NULL && (l->lv_first == NULL
+                                          || (flags & TVTT_MORE_SPECIFIC) == 0
+                                          || l->lv_type->tt_member != &t_any))
            return l->lv_type;
        if (l->lv_first == &range_list_item)
            return &t_list_number;
@@ -290,9 +297,11 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
        l->lv_copyID = copyID;
 
        // Use the common type of all members.
-       member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap, TRUE);
+       member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap,
+                                                              TVTT_DO_MEMBER);
        for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
-           common_type(typval2type(&li->li_tv, copyID, type_gap, TRUE),
+           common_type(typval2type(&li->li_tv, copyID, type_gap,
+                                                              TVTT_DO_MEMBER),
                                          member_type, &member_type, type_gap);
        return get_list_type(member_type, type_gap);
     }
@@ -305,9 +314,13 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 
        if (d == NULL || (d->dv_hashtab.ht_used == 0 && d->dv_type == NULL))
            return &t_dict_empty;
-       if (!do_member)
+       if ((flags & TVTT_DO_MEMBER) == 0)
            return &t_dict_any;
-       if (d->dv_type != NULL)
+       // If the type is dict<any> go through the members, it may end up a
+       // more specific type.
+       if (d->dv_type != NULL && (d->dv_hashtab.ht_used == 0
+                                          || (flags & TVTT_MORE_SPECIFIC) == 0
+                                          || d->dv_type->tt_member != &t_any))
            return d->dv_type;
        if (d->dv_copyID == copyID)
            // avoid recursion
@@ -317,9 +330,9 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
        // Use the common type of all values.
        dict_iterate_start(tv, &iter);
        dict_iterate_next(&iter, &value);
-       member_type = typval2type(value, copyID, type_gap, TRUE);
+       member_type = typval2type(value, copyID, type_gap, TVTT_DO_MEMBER);
        while (dict_iterate_next(&iter, &value) != NULL)
-           common_type(typval2type(value, copyID, type_gap, TRUE),
+           common_type(typval2type(value, copyID, type_gap, TVTT_DO_MEMBER),
                                          member_type, &member_type, type_gap);
        return get_dict_type(member_type, type_gap);
     }
@@ -424,12 +437,14 @@ 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".
+ * When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
+ * "any".
+ * When "flags" has TVTT_MORE_SPECIFIC get the most specific member type.
  */
     type_T *
-typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
+typval2type(typval_T *tv, int copyID, garray_T *type_gap, int flags)
 {
-    type_T *type = typval2type_int(tv, copyID, type_gap, do_member);
+    type_T *type = typval2type_int(tv, copyID, type_gap, flags);
 
     if (type != NULL && type != &t_bool
            && (tv->v_type == VAR_NUMBER
@@ -451,7 +466,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, TRUE);
+    return typval2type(tv, get_copyID(), type_gap, TVTT_DO_MEMBER);
 }
 
     int
@@ -493,7 +508,11 @@ check_typval_type(type_T *expected, typval_T *actual_tv, where_T where)
     }
 
     ga_init2(&type_list, sizeof(type_T *), 10);
-    actual_type = typval2type(actual_tv, get_copyID(), &type_list, TRUE);
+
+    // When the actual type is list<any> or dict<any> go through the values to
+    // possibly get a more specific type.
+    actual_type = typval2type(actual_tv, get_copyID(), &type_list,
+                                         TVTT_DO_MEMBER | TVTT_MORE_SPECIFIC);
     if (actual_type != NULL)
     {
        res = check_type_maybe(expected, actual_type, TRUE, where);
@@ -1346,7 +1365,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, TRUE);
+    type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
     name = type_name(type, &tofree);
     if (tofree != NULL)
        rettv->vval.v_string = (char_u *)tofree;