Patch 8.2.4285
Problem:    Vim9: type of item in for loop not checked properly.
Solution:   Adjust the type checking. (closes #9683)
Files:      src/vim9compile.c, src/proto/vim9compile.pro, src/vim9cmds.c,
            src/testdir/test_vim9_script.vim


*** ../vim-8.2.4284/src/vim9compile.c   2022-01-24 13:54:42.298380706 +0000
--- src/vim9compile.c   2022-02-02 15:51:17.953296740 +0000
***************
*** 386,392 ****
   * If "actual_is_const" is TRUE then the type won't change at runtime, do not
   * generate a TYPECHECK.
   */
!     static int
  need_type_where(
        type_T  *actual,
        type_T  *expected,
--- 386,392 ----
   * If "actual_is_const" is TRUE then the type won't change at runtime, do not
   * generate a TYPECHECK.
   */
!     int
  need_type_where(
        type_T  *actual,
        type_T  *expected,
*** ../vim-8.2.4284/src/proto/vim9compile.pro   2022-01-09 21:32:57.713739111 
+0000
--- src/proto/vim9compile.pro   2022-02-02 15:52:19.345186804 +0000
***************
*** 4,9 ****
--- 4,10 ----
  int script_is_vim9(void);
  int script_var_exists(char_u *name, size_t len, cctx_T *cctx);
  int check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg);
+ int need_type_where(type_T *actual, type_T *expected, int offset, where_T 
where, cctx_T *cctx, int silent, int actual_is_const);
  int need_type(type_T *actual, type_T *expected, int offset, int arg_idx, 
cctx_T *cctx, int silent, int actual_is_const);
  lvar_T *reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, 
type_T *type);
  int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T 
*cctx);
*** ../vim-8.2.4284/src/vim9cmds.c      2022-01-26 21:32:55.687520550 +0000
--- src/vim9cmds.c      2022-02-02 16:04:53.619860649 +0000
***************
*** 990,1000 ****
                if (lhs_type == &t_any)
                    lhs_type = item_type;
                else if (item_type != &t_unknown
!                           && (item_type == &t_any
!                             ? need_type(item_type, lhs_type,
!                                                    -1, 0, cctx, FALSE, FALSE)
!                             : check_type(lhs_type, item_type, TRUE, where))
!                           == FAIL)
                    goto failed;
                var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
                if (var_lvar == NULL)
--- 990,997 ----
                if (lhs_type == &t_any)
                    lhs_type = item_type;
                else if (item_type != &t_unknown
!                       && need_type_where(item_type, lhs_type, -1,
!                                           where, cctx, FALSE, FALSE) == FAIL)
                    goto failed;
                var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
                if (var_lvar == NULL)
***************
*** 1003,1010 ****
  
                if (semicolon && idx == var_count - 1)
                    var_lvar->lv_type = vartype;
-               else
-                   var_lvar->lv_type = item_type;
                generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
            }
  
--- 1000,1005 ----
*** ../vim-8.2.4284/src/testdir/test_vim9_script.vim    2022-01-30 
18:40:40.539255759 +0000
--- src/testdir/test_vim9_script.vim    2022-02-02 16:08:38.551513797 +0000
***************
*** 2011,2017 ****
          echo k v
        endfor
    END
!   v9.CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected job 
but got string', 2)
  
    lines =<< trim END
        var i = 0
--- 2011,2017 ----
          echo k v
        endfor
    END
!   v9.CheckDefExecAndScriptFailure(lines, ['E1013: Argument 1: type mismatch, 
expected job but got string', 'E1012: Type mismatch; expected job but got 
string'], 2)
  
    lines =<< trim END
        var i = 0
***************
*** 2036,2041 ****
--- 2036,2057 ----
        endfor
    END
    v9.CheckDefExecAndScriptFailure(lines, ['E461:', 'E1017:'])
+ 
+   lines =<< trim END
+       var l: list<dict<any>> = [{a: 1, b: 'x'}]
+       for item: dict<number> in l
+         echo item
+       endfor
+   END
+   v9.CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected 
dict<number> but got dict<any>')
+ 
+   lines =<< trim END
+       var l: list<dict<any>> = [{n: 1}]
+       for item: dict<number> in l
+         item->extend({s: ''})
+       endfor
+   END
+   v9.CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, 
expected dict<number> but got dict<string>')
  enddef
  
  def Test_for_loop_script_var()
*** ../vim-8.2.4284/src/version.c       2022-02-02 15:19:08.656845882 +0000
--- src/version.c       2022-02-02 16:09:03.171485912 +0000
***************
*** 748,749 ****
--- 748,751 ----
  {   /* Add new patch number below this line */
+ /**/
+     4285,
  /**/

-- 
If the Universe is constantly expanding, why can't I ever find a parking space?

 /// Bram Moolenaar -- b...@moolenaar.net -- 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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220202162100.1E2CE1C044D%40moolenaar.net.

Raspunde prin e-mail lui