Patch 8.2.4616
Problem:    Vim9: Declarations in a {} block of a user command do not use Vim9
            rules if defined in a legacy script. (Yegappan Lakshmanan)
Solution:   Pretend the script is Vim9 script.
Files:      src/usercmd.c, src/testdir/test_usercommands.vim


*** ../vim-8.2.4615/src/usercmd.c       2022-03-14 19:24:41.867926390 +0000
--- src/usercmd.c       2022-03-23 21:33:43.721848829 +0000
***************
*** 22,27 ****
--- 22,28 ----
      int               uc_compl;       // completion type
      cmd_addr_T        uc_addr_type;   // The command's address type
      sctx_T    uc_script_ctx;  // SCTX where the command was defined
+     int               uc_flags;       // some UC_ flags
  # ifdef FEAT_EVAL
      char_u    *uc_compl_arg;  // completion argument if any
  # endif
***************
*** 1038,1043 ****
--- 1039,1045 ----
      cmd->uc_script_ctx = current_sctx;
      if (flags & UC_VIM9)
        cmd->uc_script_ctx.sc_version = SCRIPT_VERSION_VIM9;
+     cmd->uc_flags = flags & UC_VIM9;
  #ifdef FEAT_EVAL
      cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM;
      cmd->uc_compl_arg = compl_arg;
***************
*** 1725,1730 ****
--- 1727,1735 ----
      ucmd_T    *cmd;
      sctx_T    save_current_sctx;
      int               restore_current_sctx = FALSE;
+ #ifdef FEAT_EVAL
+     int               restore_script_version = 0;
+ #endif
  
      if (eap->cmdidx == CMD_USER)
        cmd = USER_CMD(eap->useridx);
***************
*** 1830,1835 ****
--- 1835,1848 ----
        current_sctx.sc_version = cmd->uc_script_ctx.sc_version;
  #ifdef FEAT_EVAL
        current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
+       if (cmd->uc_flags & UC_VIM9)
+       {
+           // In a {} block variables use Vim9 script rules, even in a legacy
+           // script.
+           restore_script_version =
+                                 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version;
+           SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = SCRIPT_VERSION_VIM9;
+       }
  #endif
      }
  
***************
*** 1839,1845 ****
--- 1852,1865 ----
      // Careful: Do not use "cmd" here, it may have become invalid if a user
      // command was added.
      if (restore_current_sctx)
+     {
+ #ifdef FEAT_EVAL
+       if (restore_script_version != 0)
+           SCRIPT_ITEM(current_sctx.sc_sid)->sn_version =
+                                                       restore_script_version;
+ #endif
        current_sctx = save_current_sctx;
+     }
      vim_free(buf);
      vim_free(split_buf);
  }
*** ../vim-8.2.4615/src/testdir/test_usercommands.vim   2022-02-17 
11:26:38.717059014 +0000
--- src/testdir/test_usercommands.vim   2022-03-23 21:34:08.841791470 +0000
***************
*** 798,801 ****
--- 798,837 ----
    delcommand SubJapanesePeriodToDot
  endfunc
  
+ " Declaring a variable in a {} uses Vim9 script rules, even when defined in a
+ " legacy script.
+ func Test_block_declaration_legacy_script()
+   let lines =<< trim END
+       command -range Rename {
+                      var save = @a
+                      @a = 'something'
+                      g:someExpr = @a
+                      @a = save
+                 }
+   END
+   call writefile(lines, 'Xlegacy')
+   source Xlegacy
+ 
+   let lines =<< trim END
+       let @a = 'saved'
+       Rename
+       call assert_equal('something', g:someExpr)
+       call assert_equal('saved', @a)
+ 
+       let g:someExpr = 'xxx'
+       let @a = 'also'
+       Rename
+       call assert_equal('something', g:someExpr)
+       call assert_equal('also', @a)
+   END
+   call writefile(lines, 'Xother')
+   source Xother
+ 
+   unlet g:someExpr
+   call delete('Xlegacy')
+   call delete('Xother')
+   delcommand Rename
+ endfunc
+ 
+ 
  " vim: shiftwidth=2 sts=2 expandtab
*** ../vim-8.2.4615/src/version.c       2022-03-23 19:44:56.102161393 +0000
--- src/version.c       2022-03-23 21:21:23.739703205 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4616,
  /**/

-- 
Nothing is impossible for the man who doesn't have to do it.

 /// 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/20220323213658.DF7A51C0497%40moolenaar.net.

Raspunde prin e-mail lui