Patch 8.2.2365
Problem:    Vim9: no check for map() changing item type at script level.
Solution:   Check the new value type.
Files:      src/list.c, src/testdir/test_vim9_builtin.vim,
            src/testdir/test_vim9_assign.vim


*** ../vim-8.2.2364/src/list.c  2021-01-13 21:46:53.832589880 +0100
--- src/list.c  2021-01-16 18:49:35.506868399 +0100
***************
*** 1985,1994 ****
--- 1985,2002 ----
                                                    : N_("filter() argument"));
      int               save_did_emsg;
      int               idx = 0;
+     type_T    *type = NULL;
+     garray_T  type_list;
  
      // map() and filter() return the first argument, also on failure.
      if (filtermap != FILTERMAP_MAPNEW)
        copy_tv(&argvars[0], rettv);
+     if (filtermap == FILTERMAP_MAP && in_vim9script())
+     {
+       // Check that map() does not change the type of the dict.
+       ga_init2(&type_list, sizeof(type_T *), 10);
+       type = typval2type(argvars, &type_list);
+     }
  
      if (argvars[0].v_type == VAR_BLOB)
      {
***************
*** 1998,2004 ****
            rettv->vval.v_blob = NULL;
        }
        if ((b = argvars[0].vval.v_blob) == NULL)
!           return;
      }
      else if (argvars[0].v_type == VAR_LIST)
      {
--- 2006,2012 ----
            rettv->vval.v_blob = NULL;
        }
        if ((b = argvars[0].vval.v_blob) == NULL)
!           goto theend;
      }
      else if (argvars[0].v_type == VAR_LIST)
      {
***************
*** 2010,2016 ****
        if ((l = argvars[0].vval.v_list) == NULL
              || (filtermap == FILTERMAP_FILTER
                            && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
!           return;
      }
      else if (argvars[0].v_type == VAR_DICT)
      {
--- 2018,2024 ----
        if ((l = argvars[0].vval.v_list) == NULL
              || (filtermap == FILTERMAP_FILTER
                            && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
!           goto theend;
      }
      else if (argvars[0].v_type == VAR_DICT)
      {
***************
*** 2022,2033 ****
        if ((d = argvars[0].vval.v_dict) == NULL
              || (filtermap == FILTERMAP_FILTER
                            && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
!           return;
      }
      else
      {
        semsg(_(e_listdictblobarg), ermsg);
!       return;
      }
  
      expr = &argvars[1];
--- 2030,2041 ----
        if ((d = argvars[0].vval.v_dict) == NULL
              || (filtermap == FILTERMAP_FILTER
                            && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
!           goto theend;
      }
      else
      {
        semsg(_(e_listdictblobarg), ermsg);
!       goto theend;
      }
  
      expr = &argvars[1];
***************
*** 2055,2061 ****
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (rettv_dict_alloc(rettv) == FAIL)
!                   return;
                d_ret = rettv->vval.v_dict;
            }
  
--- 2063,2069 ----
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (rettv_dict_alloc(rettv) == FAIL)
!                   goto theend;
                d_ret = rettv->vval.v_dict;
            }
  
***************
*** 2090,2095 ****
--- 2098,2109 ----
                    }
                    if (filtermap == FILTERMAP_MAP)
                    {
+                       if (type != NULL && check_typval_type(type->tt_member,
+                                                           &newtv, 0) == FAIL)
+                       {
+                           clear_tv(&newtv);
+                           break;
+                       }
                        // map(): replace the dict item value
                        clear_tv(&di->di_tv);
                        newtv.v_lock = 0;
***************
*** 2126,2132 ****
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (blob_copy(b, rettv) == FAIL)
!                   return;
                b_ret = rettv->vval.v_blob;
            }
  
--- 2140,2146 ----
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (blob_copy(b, rettv) == FAIL)
!                   goto theend;
                b_ret = rettv->vval.v_blob;
            }
  
***************
*** 2175,2181 ****
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (rettv_list_alloc(rettv) == FAIL)
!                   return;
                l_ret = rettv->vval.v_list;
            }
            // set_vim_var_nr() doesn't set the type
--- 2189,2195 ----
            if (filtermap == FILTERMAP_MAPNEW)
            {
                if (rettv_list_alloc(rettv) == FAIL)
!                   goto theend;
                l_ret = rettv->vval.v_list;
            }
            // set_vim_var_nr() doesn't set the type
***************
*** 2218,2223 ****
--- 2232,2244 ----
                    }
                    if (filtermap != FILTERMAP_FILTER)
                    {
+                       if (filtermap == FILTERMAP_MAP && type != NULL
+                                         && check_typval_type(type->tt_member,
+                                                           &newtv, 0) == FAIL)
+                       {
+                           clear_tv(&newtv);
+                           break;
+                       }
                        // map(), mapnew(): always append the new value to the
                        // list
                        if (list_append_tv_move(filtermap == FILTERMAP_MAP
***************
*** 2256,2261 ****
--- 2277,2288 ----
                    }
                    if (filtermap == FILTERMAP_MAP)
                    {
+                       if (type != NULL && check_typval_type(type->tt_member,
+                                                           &newtv, 0) == FAIL)
+                       {
+                           clear_tv(&newtv);
+                           break;
+                       }
                        // map(): replace the list item value
                        clear_tv(&li->li_tv);
                        newtv.v_lock = 0;
***************
*** 2281,2286 ****
--- 2308,2317 ----
  
        did_emsg |= save_did_emsg;
      }
+ 
+ theend:
+     if (type != NULL)
+       clear_type_list(&type_list);
  }
  
  /*
*** ../vim-8.2.2364/src/testdir/test_vim9_builtin.vim   2021-01-16 
16:06:58.126713782 +0100
--- src/testdir/test_vim9_builtin.vim   2021-01-16 18:54:59.226039860 +0100
***************
*** 331,357 ****
    return filter(items, (_, val) => get({[val]: 1}, 'x'))
  enddef
  
- def Test_map_function_arg()
-   var lines =<< trim END
-       def MapOne(i: number, v: string): string
-         return i .. ':' .. v
-       enddef
-       var l = ['a', 'b', 'c']
-       map(l, MapOne)
-       assert_equal(['0:a', '1:b', '2:c'], l)
-   END
-   CheckDefAndScriptSuccess(lines)
- enddef
- 
- def Test_map_item_type()
-   var lines =<< trim END
-       var l = ['a', 'b', 'c']
-       map(l, (k, v) => k .. '/' .. v )
-       assert_equal(['0/a', '1/b', '2/c'], l)
-   END
-   CheckDefAndScriptSuccess(lines)
- enddef
- 
  def Test_filereadable()
    assert_false(filereadable(""))
    assert_false(filereadable(test_null_string()))
--- 331,336 ----
***************
*** 584,589 ****
--- 563,607 ----
            ->str2nr()
  enddef
  
+ def Test_map_function_arg()
+   var lines =<< trim END
+       def MapOne(i: number, v: string): string
+         return i .. ':' .. v
+       enddef
+       var l = ['a', 'b', 'c']
+       map(l, MapOne)
+       assert_equal(['0:a', '1:b', '2:c'], l)
+   END
+   CheckDefAndScriptSuccess(lines)
+ enddef
+ 
+ def Test_map_item_type()
+   var lines =<< trim END
+       var l = ['a', 'b', 'c']
+       map(l, (k, v) => k .. '/' .. v )
+       assert_equal(['0/a', '1/b', '2/c'], l)
+   END
+   CheckDefAndScriptSuccess(lines)
+ 
+   lines =<< trim END
+     var l: list<number> = [0]
+     echo map(l, (_, v) => [])
+   END
+   CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number 
but got list<unknown>', 2)
+ 
+   lines =<< trim END
+     var l: list<number> = range(2)
+     echo map(l, (_, v) => [])
+   END
+   CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number 
but got list<unknown>', 2)
+ 
+   lines =<< trim END
+     var d: dict<number> = {key: 0}
+     echo map(d, (_, v) => [])
+   END
+   CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number 
but got list<unknown>', 2)
+ enddef
+ 
  def Test_maparg()
    var lnum = str2nr(expand('<sflnum>'))
    map foo bar
*** ../vim-8.2.2364/src/testdir/test_vim9_assign.vim    2021-01-11 
21:20:05.669652000 +0100
--- src/testdir/test_vim9_assign.vim    2021-01-16 18:57:37.085638745 +0100
***************
*** 1351,1357 ****
        var ll: list<number>
        ll = [1, 2, 3]->map('"one"')
    END
!   CheckDefExecFailure(lines, 'E1012: Type mismatch; expected list<number> but 
got list<string>')
  enddef
  
  def Test_cannot_use_let()
--- 1351,1357 ----
        var ll: list<number>
        ll = [1, 2, 3]->map('"one"')
    END
!   CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got 
string')
  enddef
  
  def Test_cannot_use_let()
*** ../vim-8.2.2364/src/version.c       2021-01-16 18:09:48.017277750 +0100
--- src/version.c       2021-01-16 18:44:17.299695949 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2365,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
167. You have more than 200 websites bookmarked.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202101161802.10GI2Ssj350964%40masaka.moolenaar.net.

Raspunde prin e-mail lui