Patch 8.2.0291
Problem:    Vim9: assigning [] to list<string> doesn't work.
Solution:   Use void for empty list and dict. (Ken Takata, closes #5669)
Files:      src/vim9compile.c, src/globals.h, src/testdir/test_vim9_script.vim


*** ../vim-8.2.0290/src/vim9compile.c   2020-02-20 22:14:27.249201378 +0100
--- src/vim9compile.c   2020-02-20 22:53:42.893678104 +0100
***************
*** 211,216 ****
--- 211,218 ----
      // recognize commonly used types
      if (member_type->tt_type == VAR_UNKNOWN)
        return &t_list_any;
+     if (member_type->tt_type == VAR_VOID)
+       return &t_list_empty;
      if (member_type->tt_type == VAR_NUMBER)
        return &t_list_number;
      if (member_type->tt_type == VAR_STRING)
***************
*** 234,239 ****
--- 236,243 ----
      // recognize commonly used types
      if (member_type->tt_type == VAR_UNKNOWN)
        return &t_dict_any;
+     if (member_type->tt_type == VAR_VOID)
+       return &t_dict_empty;
      if (member_type->tt_type == VAR_NUMBER)
        return &t_dict_number;
      if (member_type->tt_type == VAR_STRING)
***************
*** 813,823 ****
      // drop the value types
      stack->ga_len -= count;
  
!     // use the first value type for the list member type
      if (count > 0)
        member = ((type_T **)stack->ga_data)[stack->ga_len];
      else
!       member = &t_any;
      type = get_list_type(member, type_list);
  
      // add the list type to the type stack
--- 817,828 ----
      // drop the value types
      stack->ga_len -= count;
  
!     // Use the first value type for the list member type.  Use "void" 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, type_list);
  
      // add the list type to the type stack
***************
*** 848,858 ****
      // drop the key and value types
      stack->ga_len -= 2 * count;
  
!     // use the first value type for the list member type
      if (count > 0)
        member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
      else
!       member = &t_any;
      type = get_dict_type(member, type_list);
  
      // add the dict type to the type stack
--- 853,864 ----
      // 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, type_list);
  
      // add the dict type to the type stack
***************
*** 1854,1861 ****
        }
        if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
        {
!           int ret = check_type(expected->tt_member, actual->tt_member,
!                                                                       FALSE);
            if (ret == FAIL && give_msg)
                type_mismatch(expected, actual);
            return ret;
--- 1860,1872 ----
        }
        if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
        {
!           int ret;
! 
!           // void is used for an empty list or dict
!           if (actual->tt_member == &t_void)
!               ret = OK;
!           else
!               ret = check_type(expected->tt_member, actual->tt_member, FALSE);
            if (ret == FAIL && give_msg)
                type_mismatch(expected, actual);
            return ret;
***************
*** 1873,1879 ****
      static int
  need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
  {
!     if (equal_type(actual, expected) || expected->tt_type == VAR_UNKNOWN)
        return OK;
      if (actual->tt_type != VAR_UNKNOWN)
      {
--- 1884,1890 ----
      static int
  need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
  {
!     if (check_type(expected, actual, FALSE))
        return OK;
      if (actual->tt_type != VAR_UNKNOWN)
      {
*** ../vim-8.2.0290/src/globals.h       2020-02-18 21:32:55.854340478 +0100
--- src/globals.h       2020-02-20 22:48:20.634651727 +0100
***************
*** 400,405 ****
--- 400,407 ----
  
  EXTERN type_T t_list_any INIT4(VAR_LIST, 0, &t_any, NULL);
  EXTERN type_T t_dict_any INIT4(VAR_DICT, 0, &t_any, NULL);
+ EXTERN type_T t_list_empty INIT4(VAR_LIST, 0, &t_void, NULL);
+ EXTERN type_T t_dict_empty INIT4(VAR_DICT, 0, &t_void, NULL);
  
  EXTERN type_T t_list_number INIT4(VAR_LIST, 0, &t_number, NULL);
  EXTERN type_T t_list_string INIT4(VAR_LIST, 0, &t_string, NULL);
***************
*** 1496,1502 ****
   * Excluded are errors that are only used once and debugging messages.
   */
  EXTERN char e_abort[]         INIT(= N_("E470: Command aborted"));
! EXTERN char e_argreq[]        INIT(= N_("E471: Argument required"));
  EXTERN char e_backslash[]     INIT(= N_("E10: \\ should be followed by /, ? 
or &"));
  #ifdef FEAT_CMDWIN
  EXTERN char e_cmdwin[]        INIT(= N_("E11: Invalid in command-line window; 
<CR> executes, CTRL-C quits"));
--- 1498,1504 ----
   * Excluded are errors that are only used once and debugging messages.
   */
  EXTERN char e_abort[]         INIT(= N_("E470: Command aborted"));
! EXTERN char e_argreq[]                INIT(= N_("E471: Argument required"));
  EXTERN char e_backslash[]     INIT(= N_("E10: \\ should be followed by /, ? 
or &"));
  #ifdef FEAT_CMDWIN
  EXTERN char e_cmdwin[]        INIT(= N_("E11: Invalid in command-line window; 
<CR> executes, CTRL-C quits"));
***************
*** 1639,1645 ****
  EXTERN char e_toomanyarg[]    INIT(= N_("E118: Too many arguments for 
function: %s"));
  EXTERN char e_toofewarg[]     INIT(= N_("E119: Not enough arguments for 
function: %s"));
  EXTERN char e_func_deleted[]  INIT(= N_("E933: Function was deleted: %s"));
! EXTERN char e_dictkey[]       INIT(= N_("E716: Key not present in Dictionary: 
%s"));
  EXTERN char e_listreq[]               INIT(= N_("E714: List required"));
  EXTERN char e_listblobreq[]   INIT(= N_("E897: List or Blob required"));
  EXTERN char e_listdictarg[]   INIT(= N_("E712: Argument of %s must be a List 
or Dictionary"));
--- 1641,1647 ----
  EXTERN char e_toomanyarg[]    INIT(= N_("E118: Too many arguments for 
function: %s"));
  EXTERN char e_toofewarg[]     INIT(= N_("E119: Not enough arguments for 
function: %s"));
  EXTERN char e_func_deleted[]  INIT(= N_("E933: Function was deleted: %s"));
! EXTERN char e_dictkey[]               INIT(= N_("E716: Key not present in 
Dictionary: %s"));
  EXTERN char e_listreq[]               INIT(= N_("E714: List required"));
  EXTERN char e_listblobreq[]   INIT(= N_("E897: List or Blob required"));
  EXTERN char e_listdictarg[]   INIT(= N_("E712: Argument of %s must be a List 
or Dictionary"));
*** ../vim-8.2.0290/src/testdir/test_vim9_script.vim    2020-02-20 
20:41:02.987044162 +0100
--- src/testdir/test_vim9_script.vim    2020-02-20 22:43:58.711428426 +0100
***************
*** 40,48 ****
    let list1: list<string> = ['sdf', 'asdf']
    let list2: list<number> = [1, 2, 3]
  
!   " TODO: does not work yet
!   " let listS: list<string> = []
!   " let listN: list<number> = []
  
    let dict1: dict<string> = #{key: 'value'}
    let dict2: dict<number> = #{one: 1, two: 2}
--- 40,47 ----
    let list1: list<string> = ['sdf', 'asdf']
    let list2: list<number> = [1, 2, 3]
  
!   let listS: list<string> = []
!   let listN: list<number> = []
  
    let dict1: dict<string> = #{key: 'value'}
    let dict2: dict<number> = #{one: 1, two: 2}
*** ../vim-8.2.0290/src/version.c       2020-02-20 22:34:59.264986542 +0100
--- src/version.c       2020-02-20 22:47:03.146883197 +0100
***************
*** 740,741 ****
--- 740,743 ----
  {   /* Add new patch number below this line */
+ /**/
+     291,
  /**/

-- 
Bare feet magnetize sharp metal objects so they point upward from the
floor -- especially in the dark.

 /// 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/202002202155.01KLtSQX006508%40masaka.moolenaar.net.

Raspunde prin e-mail lui