diff src/evalvars.c @ 28313:b418e073b42f v8.2.4682

patch 8.2.4682: Vim9: can use :unlockvar for const variable Commit: https://github.com/vim/vim/commit/7a411a306f90339d8686e42ac16e1ae4fc7533c5 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Apr 4 14:58:06 2022 +0100 patch 8.2.4682: Vim9: can use :unlockvar for const variable Problem: Vim9: can use :unlockvar for const variable. (Ernie Rael) Solution: Check whether the variable is a const.
author Bram Moolenaar <Bram@vim.org>
date Mon, 04 Apr 2022 16:00:03 +0200
parents fff70771d4bb
children fabe722b24e9
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -1951,23 +1951,42 @@ do_lock_var(
 								  lp->ll_name);
 		ret = FAIL;
 	    }
-	    else if ((di->di_flags & DI_FLAGS_FIX)
+	    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;
+		{
+		    // 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
-		    di->di_flags &= ~DI_FLAGS_LOCK;
-		if (deep != 0)
-		    item_lock(&di->di_tv, deep, lock, FALSE);
+		{
+		    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,7 +2831,7 @@ eval_variable(
 	    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);
+		svar_T *sv = find_typval_in_script(tv, 0, TRUE);
 
 		if (sv != NULL)
 		    type = sv->sv_type;
@@ -3557,7 +3576,7 @@ set_var_const(
 	    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);
+		svar_T  *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
 
 		if (sv != NULL)
 		{