Patch 8.2.4666
Problem:    Vim9: assignment not recognized in skipped block.
Solution:   When skipping assume identifier exists. (closes #10059)
Files:      src/vim9compile.c, src/proto/vim9compile.pro, src/vim9cmds.c,
            src/testdir/test_vim9_cmd.vim, src/testdir/test_vim9_script.vim


*** ../vim-8.2.4665/src/vim9compile.c   2022-03-27 16:29:49.876153380 +0100
--- src/vim9compile.c   2022-04-02 19:30:39.675696958 +0100
***************
*** 279,285 ****
  
  /*
   * Return TRUE if "name" is a local variable, argument, script variable,
!  * imported or function.
   */
      static int
  item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
--- 279,286 ----
  
  /*
   * Return TRUE if "name" is a local variable, argument, script variable,
!  * imported or function.  Or commands are being skipped, a declaration may 
have
!  * been skipped then.
   */
      static int
  item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
***************
*** 1109,1115 ****
  get_var_dest(
        char_u          *name,
        assign_dest_T   *dest,
!       int             cmdidx,
        int             *option_scope,
        int             *vimvaridx,
        type_T          **type,
--- 1110,1116 ----
  get_var_dest(
        char_u          *name,
        assign_dest_T   *dest,
!       cmdidx_T        cmdidx,
        int             *option_scope,
        int             *vimvaridx,
        type_T          **type,
***************
*** 1225,1231 ****
  }
  
      static int
! is_decl_command(int cmdidx)
  {
      return cmdidx == CMD_let || cmdidx == CMD_var
                                 || cmdidx == CMD_final || cmdidx == CMD_const;
--- 1226,1232 ----
  }
  
      static int
! is_decl_command(cmdidx_T cmdidx)
  {
      return cmdidx == CMD_let || cmdidx == CMD_var
                                 || cmdidx == CMD_final || cmdidx == CMD_const;
***************
*** 1238,1249 ****
   */
      int
  compile_lhs(
!       char_u  *var_start,
!       lhs_T   *lhs,
!       int     cmdidx,
!       int     heredoc,
!       int     oplen,
!       cctx_T  *cctx)
  {
      char_u    *var_end;
      int               is_decl = is_decl_command(cmdidx);
--- 1239,1251 ----
   */
      int
  compile_lhs(
!       char_u      *var_start,
!       lhs_T       *lhs,
!       cmdidx_T    cmdidx,
!       int         heredoc,
!       int         has_cmd,        // "var" before "var_start"
!       int         oplen,
!       cctx_T      *cctx)
  {
      char_u    *var_end;
      int               is_decl = is_decl_command(cmdidx);
***************
*** 1493,1499 ****
            semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
            return FAIL;
        }
!       if (!is_decl)
        {
            semsg(_(e_unknown_variable_str), lhs->lhs_name);
            return FAIL;
--- 1495,1502 ----
            semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
            return FAIL;
        }
!       if (!is_decl || (lhs->lhs_has_index && !has_cmd
!                                               && cctx->ctx_skip != SKIP_YES))
        {
            semsg(_(e_unknown_variable_str), lhs->lhs_name);
            return FAIL;
***************
*** 1520,1528 ****
        char_u  *p;
  
        // Something follows after the variable: "var[idx]" or "var.key".
!       if (is_decl)
        {
!           emsg(_(e_cannot_use_index_when_declaring_variable));
            return FAIL;
        }
  
--- 1523,1534 ----
        char_u  *p;
  
        // Something follows after the variable: "var[idx]" or "var.key".
!       if (is_decl && cctx->ctx_skip != SKIP_YES)
        {
!           if (has_cmd)
!               emsg(_(e_cannot_use_index_when_declaring_variable));
!           else
!               semsg(_(e_unknown_variable_str), lhs->lhs_name);
            return FAIL;
        }
  
***************
*** 1562,1576 ****
   */
      int
  compile_assign_lhs(
!       char_u  *var_start,
!       lhs_T   *lhs,
!       int     cmdidx,
!       int     is_decl,
!       int     heredoc,
!       int     oplen,
!       cctx_T  *cctx)
  {
!     if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
        return FAIL;
  
      if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
--- 1568,1584 ----
   */
      int
  compile_assign_lhs(
!       char_u      *var_start,
!       lhs_T       *lhs,
!       cmdidx_T    cmdidx,
!       int         is_decl,
!       int         heredoc,
!       int         has_cmd,        // "var" before "var_start"
!       int         oplen,
!       cctx_T      *cctx)
  {
!     if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
!                                                                      == FAIL)
        return FAIL;
  
      if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
***************
*** 2049,2055 ****
         * Figure out the LHS type and other properties.
         */
        if (compile_assign_lhs(var_start, &lhs, cmdidx,
!                                       is_decl, heredoc, oplen, cctx) == FAIL)
            goto theend;
        if (heredoc)
        {
--- 2057,2064 ----
         * Figure out the LHS type and other properties.
         */
        if (compile_assign_lhs(var_start, &lhs, cmdidx,
!                               is_decl, heredoc, var_start > eap->cmd,
!                               oplen, cctx) == FAIL)
            goto theend;
        if (heredoc)
        {
***************
*** 2769,2774 ****
--- 2778,2784 ----
        CLEAR_FIELD(ea);
        ea.cmdlinep = &line;
        ea.cmd = skipwhite(line);
+       ea.skip = cctx.ctx_skip == SKIP_YES;
  
        if (*ea.cmd == '#')
        {
***************
*** 2957,2971 ****
  
        if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
        {
!           if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
            {
                line += STRLEN(line);
                goto nextline;
            }
!           else if (ea.cmdidx != CMD_eval)
            {
-               // CMD_var cannot happen, compile_assignment() above would be
-               // used.  Most likely an assignment to a non-existing variable.
                semsg(_(e_command_not_recognized_str), ea.cmd);
                goto erret;
            }
--- 2967,2983 ----
  
        if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
        {
!           // "eval" is used for "val->func()" and "var" for "var = val", then
!           // "p" is equal to "ea.cmd" for a valid command.
!           if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
!               ;
!           else if (cctx.ctx_skip == SKIP_YES)
            {
                line += STRLEN(line);
                goto nextline;
            }
!           else
            {
                semsg(_(e_command_not_recognized_str), ea.cmd);
                goto erret;
            }
*** ../vim-8.2.4665/src/proto/vim9compile.pro   2022-03-15 19:29:26.542954696 
+0000
--- src/proto/vim9compile.pro   2022-04-02 19:08:27.235773230 +0100
***************
*** 18,26 ****
  int func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type);
  int assignment_len(char_u *p, int *heredoc);
  void vim9_declare_error(char_u *name);
! int get_var_dest(char_u *name, assign_dest_T *dest, int cmdidx, int 
*option_scope, int *vimvaridx, type_T **type, cctx_T *cctx);
! int compile_lhs(char_u *var_start, lhs_T *lhs, int cmdidx, int heredoc, int 
oplen, cctx_T *cctx);
! int compile_assign_lhs(char_u *var_start, lhs_T *lhs, int cmdidx, int 
is_decl, int heredoc, int oplen, cctx_T *cctx);
  int compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx);
  int compile_assign_unlet(char_u *var_start, lhs_T *lhs, int is_assign, type_T 
*rhs_type, cctx_T *cctx);
  compiletype_T get_compile_type(ufunc_T *ufunc);
--- 18,26 ----
  int func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type);
  int assignment_len(char_u *p, int *heredoc);
  void vim9_declare_error(char_u *name);
! int get_var_dest(char_u *name, assign_dest_T *dest, cmdidx_T cmdidx, int 
*option_scope, int *vimvaridx, type_T **type, cctx_T *cctx);
! int compile_lhs(char_u *var_start, lhs_T *lhs, cmdidx_T cmdidx, int heredoc, 
int has_cmd, int oplen, cctx_T *cctx);
! int compile_assign_lhs(char_u *var_start, lhs_T *lhs, cmdidx_T cmdidx, int 
is_decl, int heredoc, int has_cmd, int oplen, cctx_T *cctx);
  int compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx);
  int compile_assign_unlet(char_u *var_start, lhs_T *lhs, int is_assign, type_T 
*rhs_type, cctx_T *cctx);
  compiletype_T get_compile_type(ufunc_T *ufunc);
*** ../vim-8.2.4665/src/vim9cmds.c      2022-03-23 19:44:56.098161437 +0000
--- src/vim9cmds.c      2022-04-02 19:07:58.495762572 +0100
***************
*** 139,145 ****
        //
        // Figure out the LHS type and other properties.
        //
!       ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
  
        // Use the info in "lhs" to unlet the item at the index in the
        // list or dict.
--- 139,145 ----
        //
        // Figure out the LHS type and other properties.
        //
!       ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx);
  
        // Use the info in "lhs" to unlet the item at the index in the
        // list or dict.
***************
*** 2160,2166 ****
        arg = skipwhite(arg);
  
        if (compile_assign_lhs(arg, lhs, CMD_redir,
!                                               FALSE, FALSE, 1, cctx) == FAIL)
            return NULL;
        if (need_type(&t_string, lhs->lhs_member_type,
                                            -1, 0, cctx, FALSE, FALSE) == FAIL)
--- 2160,2166 ----
        arg = skipwhite(arg);
  
        if (compile_assign_lhs(arg, lhs, CMD_redir,
!                                        FALSE, FALSE, FALSE, 1, cctx) == FAIL)
            return NULL;
        if (need_type(&t_string, lhs->lhs_member_type,
                                            -1, 0, cctx, FALSE, FALSE) == FAIL)
*** ../vim-8.2.4665/src/testdir/test_vim9_cmd.vim       2022-03-23 
19:44:56.098161437 +0000
--- src/testdir/test_vim9_cmd.vim       2022-04-02 19:31:29.479685117 +0100
***************
*** 1363,1369 ****
    var lines =<< trim END
      d.key = 'asdf'
    END
!   v9.CheckDefFailure(lines, 'E1146:', 1)
  
    lines =<< trim END
      if 0
--- 1363,1374 ----
    var lines =<< trim END
      d.key = 'asdf'
    END
!   v9.CheckDefFailure(lines, 'E1089: Unknown variable: d', 1)
! 
!   lines =<< trim END
!     d['key'] = 'asdf'
!   END
!   v9.CheckDefFailure(lines, 'E1089: Unknown variable: d', 1)
  
    lines =<< trim END
      if 0
***************
*** 1371,1381 ****
      endif
    END
    v9.CheckDefSuccess(lines)
- 
-   lines =<< trim END
-     d['key'] = 'asdf'
-   END
-   v9.CheckDefFailure(lines, 'E1146:', 1)
  enddef
  
  def Test_magic_not_used()
--- 1376,1381 ----
*** ../vim-8.2.4665/src/testdir/test_vim9_script.vim    2022-03-25 
11:16:24.932035331 +0000
--- src/testdir/test_vim9_script.vim    2022-04-02 19:33:49.171650920 +0100
***************
*** 2003,2008 ****
--- 2003,2021 ----
        assert_equal([3, 4], result)
      enddef
      DefFalse()
+ 
+     def BuildDiagrams()
+       var diagrams: list<any>
+       if false
+         var max = 0
+         for v in diagrams
+           var l = 3
+           if max < l | max = l | endif
+           v->add(l)
+         endfor
+       endif
+     enddef
+     BuildDiagrams()
    END
    v9.CheckDefAndScriptSuccess(lines)
  enddef
*** ../vim-8.2.4665/src/version.c       2022-04-02 15:31:48.301003446 +0100
--- src/version.c       2022-04-02 17:40:06.865962886 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4666,
  /**/

-- 
John:   When I'm playing tennis with friends I always get carried away
George: You hurt your foot each time?

 /// 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/20220402184426.E63921C0561%40moolenaar.net.

Raspunde prin e-mail lui