Patch 8.2.3128
Problem:    Vim9: uninitialzed list does not get type checked.
Solution:   Set the type when initializing the variable. (closes #8529)
Files:      src/eval.c, src/evalvars.c, src/vim9script.c,
            src/userfunc.c, src/proto/vim9script.pro,
            src/testdir/test_vim9_builtin.vim


*** ../vim-8.2.3127/src/eval.c  2021-07-05 22:22:57.001685238 +0200
--- src/eval.c  2021-07-08 20:50:56.754874541 +0200
***************
*** 959,965 ****
                && lp->ll_tv == &v->di_tv
                && ht != NULL && ht == get_script_local_ht())
        {
!           svar_T  *sv = find_typval_in_script(lp->ll_tv);
  
            // Vim9 script local variable: get the type
            if (sv != NULL)
--- 959,965 ----
                && lp->ll_tv == &v->di_tv
                && ht != NULL && ht == get_script_local_ht())
        {
!           svar_T  *sv = find_typval_in_script(lp->ll_tv, TRUE);
  
            // Vim9 script local variable: get the type
            if (sv != NULL)
*** ../vim-8.2.3127/src/evalvars.c      2021-06-27 22:03:28.641707728 +0200
--- src/evalvars.c      2021-07-08 20:53:01.162720825 +0200
***************
*** 2565,2570 ****
--- 2565,2571 ----
      typval_T  *tv = NULL;
      int               found = FALSE;
      dictitem_T        *v;
+     hashtab_T *ht = NULL;
      int               cc;
  
      // truncate the name, so that we can use strcmp()
***************
*** 2575,2581 ****
      if ((tv = lookup_debug_var(name)) == NULL)
      {
        // Check for user-defined variables.
!       v = find_var(name, NULL, flags & EVAL_VAR_NOAUTOLOAD);
        if (v != NULL)
        {
            tv = &v->di_tv;
--- 2576,2582 ----
      if ((tv = lookup_debug_var(name)) == NULL)
      {
        // Check for user-defined variables.
!       v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
        if (v != NULL)
        {
            tv = &v->di_tv;
***************
*** 2655,2672 ****
--- 2656,2690 ----
        }
        else if (rettv != NULL)
        {
+           type_T      *type = NULL;
+ 
+           if (ht != NULL && ht == get_script_local_ht())
+           {
+               svar_T *sv = find_typval_in_script(tv, FALSE);
+ 
+               // TODO: check imported variable
+               if (sv != NULL)
+                   type = sv->sv_type;
+           }
+ 
            // If a list or dict variable wasn't initialized, do it now.
            if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
            {
                tv->vval.v_dict = dict_alloc();
                if (tv->vval.v_dict != NULL)
+               {
                    ++tv->vval.v_dict->dv_refcount;
+                   tv->vval.v_dict->dv_type = alloc_type(type);
+               }
            }
            else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
            {
                tv->vval.v_list = list_alloc();
                if (tv->vval.v_list != NULL)
+               {
                    ++tv->vval.v_list->lv_refcount;
+                   tv->vval.v_list->lv_type = alloc_type(type);
+               }
            }
            else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
            {
*** ../vim-8.2.3127/src/vim9script.c    2021-06-25 21:31:03.375070663 +0200
--- src/vim9script.c    2021-07-08 20:51:50.850808133 +0200
***************
*** 806,812 ****
      }
      else
      {
!       sv = find_typval_in_script(&di->di_tv);
      }
      if (sv != NULL)
      {
--- 806,812 ----
      }
      else
      {
!       sv = find_typval_in_script(&di->di_tv, TRUE);
      }
      if (sv != NULL)
      {
***************
*** 922,931 ****
  
  /*
   * Find the script-local variable that links to "dest".
!  * Returns NULL if not found.
   */
      svar_T *
! find_typval_in_script(typval_T *dest)
  {
      scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
      int                   idx;
--- 922,932 ----
  
  /*
   * Find the script-local variable that links to "dest".
!  * Returns NULL if not found and when "give_error" is TRUE this is considered
!  * an internal error.
   */
      svar_T *
! find_typval_in_script(typval_T *dest, int give_error)
  {
      scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
      int                   idx;
***************
*** 944,950 ****
        if (sv->sv_name != NULL && sv->sv_tv == dest)
            return sv;
      }
!     iemsg("find_typval_in_script(): not found");
      return NULL;
  }
  
--- 945,952 ----
        if (sv->sv_name != NULL && sv->sv_tv == dest)
            return sv;
      }
!     if (give_error)
!       iemsg("find_typval_in_script(): not found");
      return NULL;
  }
  
***************
*** 959,965 ****
        char_u      *name,
        where_T     where)
  {
!     svar_T  *sv = find_typval_in_script(dest);
      int           ret;
  
      if (sv != NULL)
--- 961,967 ----
        char_u      *name,
        where_T     where)
  {
!     svar_T  *sv = find_typval_in_script(dest, TRUE);
      int           ret;
  
      if (sv != NULL)
*** ../vim-8.2.3127/src/userfunc.c      2021-07-04 23:29:26.821602887 +0200
--- src/userfunc.c      2021-07-08 20:51:19.922846196 +0200
***************
*** 1512,1518 ****
        {
            if (type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(&v->di_tv);
  
                if (sv != NULL)
                    *type = sv->sv_type;
--- 1512,1518 ----
        {
            if (type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(&v->di_tv, TRUE);
  
                if (sv != NULL)
                    *type = sv->sv_type;
*** ../vim-8.2.3127/src/proto/vim9script.pro    2021-05-28 21:06:04.624687355 
+0200
--- src/proto/vim9script.pro    2021-07-08 20:50:42.246892229 +0200
***************
*** 16,22 ****
  void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T 
*tv, type_T **type, int do_member);
  void hide_script_var(scriptitem_T *si, int idx, int func_defined);
  void free_all_script_vars(scriptitem_T *si);
! svar_T *find_typval_in_script(typval_T *dest);
  int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, 
where_T where);
  int check_reserved_name(char_u *name);
  /* vim: set ft=c : */
--- 16,22 ----
  void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T 
*tv, type_T **type, int do_member);
  void hide_script_var(scriptitem_T *si, int idx, int func_defined);
  void free_all_script_vars(scriptitem_T *si);
! svar_T *find_typval_in_script(typval_T *dest, int give_error);
  int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, 
where_T where);
  int check_reserved_name(char_u *name);
  /* vim: set ft=c : */
*** ../vim-8.2.3127/src/testdir/test_vim9_builtin.vim   2021-07-08 
20:53:36.866676082 +0200
--- src/testdir/test_vim9_builtin.vim   2021-07-08 20:54:40.918595171 +0200
***************
*** 111,116 ****
--- 111,123 ----
        l->add(123)
    END
    CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got 
number', 3)
+ 
+   lines =<< trim END
+       vim9script
+       var l: list<string>
+       l->add(123)
+   END
+   CheckScriptFailure(lines, 'E1012: Type mismatch; expected string but got 
number', 3)
  enddef
  
  def Test_add_blob()
*** ../vim-8.2.3127/src/version.c       2021-07-08 20:53:36.870676076 +0200
--- src/version.c       2021-07-08 20:55:30.202532429 +0200
***************
*** 757,758 ****
--- 757,760 ----
  {   /* Add new patch number below this line */
+ /**/
+     3128,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
96. On Super Bowl Sunday, you followed the score by going to the
    Yahoo main page instead of turning on the TV.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            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/202107081857.168Ivu3U089219%40masaka.moolenaar.net.

Raspunde prin e-mail lui