Patch 8.2.4682
Problem:    Vim9: can use :unlockvar for const variable. (Ernie Rael)
Solution:   Check whether the variable is a const.
Files:      src/ex_docmd.c, src/evalvars.c, src/vim9script.c,
            src/proto/vim9script.pro, src/eval.c, src/userfunc.c,
            src/testdir/test_vim9_cmd.vim


*** ../vim-8.2.4681/src/ex_docmd.c      2022-03-31 11:37:54.259367941 +0100
--- src/ex_docmd.c      2022-04-04 13:51:14.849756816 +0100
***************
*** 303,309 ****
  # define ex_throw             ex_ni
  # define ex_try                       ex_ni
  # define ex_unlet             ex_ni
- # define ex_unlockvar         ex_ni
  # define ex_while             ex_ni
  # define ex_import            ex_ni
  # define ex_export            ex_ni
--- 303,308 ----
*** ../vim-8.2.4681/src/evalvars.c      2022-04-01 15:26:54.984558728 +0100
--- src/evalvars.c      2022-04-04 14:27:57.000696536 +0100
***************
*** 1951,1973 ****
                                                                  lp->ll_name);
                ret = FAIL;
            }
-           else if ((di->di_flags & DI_FLAGS_FIX)
-                           && di->di_tv.v_type != VAR_DICT
-                           && di->di_tv.v_type != VAR_LIST)
-           {
-               // For historic reasons this error is not given for a list or
-               // dict.  E.g., the b: dict could be locked/unlocked.
-               semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
-               ret = FAIL;
-           }
            else
            {
!               if (lock)
!                   di->di_flags |= DI_FLAGS_LOCK;
                else
!                   di->di_flags &= ~DI_FLAGS_LOCK;
!               if (deep != 0)
!                   item_lock(&di->di_tv, deep, lock, FALSE);
            }
        }
        *name_end = cc;
--- 1951,1992 ----
                                                                  lp->ll_name);
                ret = FAIL;
            }
            else
            {
!               if ((di->di_flags & DI_FLAGS_FIX)
!                           && di->di_tv.v_type != VAR_DICT
!                           && di->di_tv.v_type != VAR_LIST)
!               {
!                   // For historic reasons this error is not given for a list
!                   // or dict.  E.g., the b: dict could be locked/unlocked.
!                   semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
!                   ret = FAIL;
!               }
                else
!               {
!                   if (in_vim9script())
!                   {
!                       svar_T  *sv = find_typval_in_script(&di->di_tv,
!                                                                    0, FALSE);
! 
!                       if (sv != NULL && sv->sv_const != 0)
!                       {
!                           semsg(_(e_cannot_change_readonly_variable_str),
!                                                                 lp->ll_name);
!                           ret = FAIL;
!                       }
!                   }
! 
!                   if (ret == OK)
!                   {
!                       if (lock)
!                           di->di_flags |= DI_FLAGS_LOCK;
!                       else
!                           di->di_flags &= ~DI_FLAGS_LOCK;
!                       if (deep != 0)
!                           item_lock(&di->di_tv, deep, lock, FALSE);
!                   }
!               }
            }
        }
        *name_end = cc;
***************
*** 2812,2818 ****
            if (ht != NULL && ht == get_script_local_ht()
                    && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
            {
!               svar_T *sv = find_typval_in_script(tv, 0);
  
                if (sv != NULL)
                    type = sv->sv_type;
--- 2831,2837 ----
            if (ht != NULL && ht == get_script_local_ht()
                    && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
            {
!               svar_T *sv = find_typval_in_script(tv, 0, TRUE);
  
                if (sv != NULL)
                    type = sv->sv_type;
***************
*** 3557,3563 ****
            if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
            {
                where_T where = WHERE_INIT;
!               svar_T  *sv = find_typval_in_script(&di->di_tv, sid);
  
                if (sv != NULL)
                {
--- 3576,3582 ----
            if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
            {
                where_T where = WHERE_INIT;
!               svar_T  *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
  
                if (sv != NULL)
                {
*** ../vim-8.2.4681/src/vim9script.c    2022-03-31 16:18:19.916278625 +0100
--- src/vim9script.c    2022-04-04 14:28:35.032682564 +0100
***************
*** 956,962 ****
      }
      else
      {
!       sv = find_typval_in_script(&di->di_tv, 0);
      }
      if (sv != NULL)
      {
--- 956,962 ----
      }
      else
      {
!       sv = find_typval_in_script(&di->di_tv, 0, TRUE);
      }
      if (sv != NULL)
      {
***************
*** 1053,1062 ****
  /*
   * Find the script-local variable that links to "dest".
   * If "sid" is zero use the current script.
   * Returns NULL if not found and give an internal error.
   */
      svar_T *
! find_typval_in_script(typval_T *dest, scid_T sid)
  {
      scriptitem_T    *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
      int                   idx;
--- 1053,1063 ----
  /*
   * Find the script-local variable that links to "dest".
   * If "sid" is zero use the current script.
+  * if "must_find" is TRUE and "dest" cannot be found report an internal error.
   * Returns NULL if not found and give an internal error.
   */
      svar_T *
! find_typval_in_script(typval_T *dest, scid_T sid, int must_find)
  {
      scriptitem_T    *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
      int                   idx;
***************
*** 1076,1082 ****
        if (sv->sv_name != NULL && sv->sv_tv == dest)
            return sv;
      }
!     iemsg("find_typval_in_script(): not found");
      return NULL;
  }
  
--- 1077,1084 ----
        if (sv->sv_name != NULL && sv->sv_tv == dest)
            return sv;
      }
!     if (must_find)
!       iemsg("find_typval_in_script(): not found");
      return NULL;
  }
  
*** ../vim-8.2.4681/src/proto/vim9script.pro    2022-03-22 12:13:49.151376708 
+0000
--- src/proto/vim9script.pro    2022-04-04 14:26:56.076719543 +0100
***************
*** 16,22 ****
  char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
  void update_vim9_script_var(int create, dictitem_T *di, char_u *name, int 
flags, typval_T *tv, type_T **type, int do_member);
  void hide_script_var(scriptitem_T *si, int idx, int func_defined);
! svar_T *find_typval_in_script(typval_T *dest, scid_T sid);
  int check_script_var_type(svar_T *sv, typval_T *value, char_u *name, where_T 
where);
  int check_reserved_name(char_u *name);
  /* vim: set ft=c : */
--- 16,22 ----
  char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
  void update_vim9_script_var(int create, dictitem_T *di, char_u *name, int 
flags, typval_T *tv, type_T **type, int do_member);
  void hide_script_var(scriptitem_T *si, int idx, int func_defined);
! svar_T *find_typval_in_script(typval_T *dest, scid_T sid, int must_find);
  int check_script_var_type(svar_T *sv, typval_T *value, char_u *name, where_T 
where);
  int check_reserved_name(char_u *name);
  /* vim: set ft=c : */
*** ../vim-8.2.4681/src/eval.c  2022-04-01 15:26:54.984558728 +0100
--- src/eval.c  2022-04-04 14:27:14.076712659 +0100
***************
*** 1065,1071 ****
                && lp->ll_tv == &v->di_tv
                && ht != NULL && ht == get_script_local_ht())
        {
!           svar_T  *sv = find_typval_in_script(lp->ll_tv, 0);
  
            // Vim9 script local variable: get the type
            if (sv != NULL)
--- 1065,1071 ----
                && lp->ll_tv == &v->di_tv
                && ht != NULL && ht == get_script_local_ht())
        {
!           svar_T  *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
  
            // Vim9 script local variable: get the type
            if (sv != NULL)
*** ../vim-8.2.4681/src/userfunc.c      2022-03-31 21:40:28.889186407 +0100
--- src/userfunc.c      2022-04-04 14:28:03.432694153 +0100
***************
*** 1697,1703 ****
        {
            if (!did_type && type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(tv, 0);
  
                if (sv != NULL)
                    *type = sv->sv_type;
--- 1697,1703 ----
        {
            if (!did_type && type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(tv, 0, TRUE);
  
                if (sv != NULL)
                    *type = sv->sv_type;
*** ../vim-8.2.4681/src/testdir/test_vim9_cmd.vim       2022-04-03 
21:11:31.031579240 +0100
--- src/testdir/test_vim9_cmd.vim       2022-04-04 14:29:42.220658476 +0100
***************
*** 1650,1655 ****
--- 1650,1672 ----
        LockIt()
    END
    v9.CheckScriptFailure(lines, 'E1246', 1)
+ 
+   lines =<< trim END
+       vim9script
+       const name = 'john'
+       unlockvar name
+   END
+   v9.CheckScriptFailure(lines, 'E46', 3)
+ 
+   lines =<< trim END
+       vim9script
+       const name = 'john'
+       def UnLockIt()
+         unlockvar name
+       enddef
+       UnLockIt()
+   END
+   v9.CheckScriptFailure(lines, 'E46', 1)
  enddef
  
  def Test_substitute_expr()
*** ../vim-8.2.4681/src/version.c       2022-04-03 21:57:22.517883893 +0100
--- src/version.c       2022-04-04 13:57:43.533910489 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4682,
  /**/

-- 
It is illegal for a driver to be blindfolded while operating a vehicle.
                [real standing law in Alabama, United States of America]

 /// 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/20220404135836.5D34C1C0561%40moolenaar.net.

Raspunde prin e-mail lui