changeset 21610:586241ee8096 v8.2.1355

patch 8.2.1355: Vim9: no error using :let for options and registers Commit: https://github.com/vim/vim/commit/c2ee44cc382d4b097f51ea3251f00fb35493ea4f Author: Bram Moolenaar <Bram@vim.org> Date: Sun Aug 2 16:59:00 2020 +0200 patch 8.2.1355: Vim9: no error using :let for options and registers Problem: Vim9: no error using :let for options and registers. Solution: Give an error. (closes https://github.com/vim/vim/issues/6568)
author Bram Moolenaar <Bram@vim.org>
date Sun, 02 Aug 2020 17:00:06 +0200
parents 5f68185d8148
children 4bc6d27d7c73
files src/evalvars.c src/testdir/test_vim9_script.vim src/version.c src/vim9compile.c
diffstat 4 files changed, 36 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -1219,6 +1219,13 @@ ex_let_one(
     int		opt_flags;
     char_u	*tofree = NULL;
 
+    if (in_vim9script() && (flags & LET_NO_COMMAND) == 0
+				  && vim_strchr((char_u *)"$@&", *arg) != NULL)
+    {
+	vim9_declare_error(arg);
+	return NULL;
+    }
+
     // ":let $VAR = expr": Set environment variable.
     if (*arg == '$')
     {
@@ -1227,11 +1234,6 @@ ex_let_one(
 	    emsg(_("E996: Cannot lock an environment variable"));
 	    return NULL;
 	}
-	if (in_vim9script() && (flags & LET_NO_COMMAND) == 0)
-	{
-	    vim9_declare_error(arg);
-	    return NULL;
-	}
 
 	// Find the end of the name.
 	++arg;
@@ -2427,7 +2429,7 @@ eval_variable(
 	    else
 	    {
 		scriptitem_T    *si = SCRIPT_ITEM(import->imp_sid);
-		svar_T	    *sv = ((svar_T *)si->sn_var_vals.ga_data)
+		svar_T		*sv = ((svar_T *)si->sn_var_vals.ga_data)
 						    + import->imp_var_vals_idx;
 		tv = sv->sv_tv;
 	    }
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -463,13 +463,16 @@ def Test_assignment_failure()
                             '[x, y; z] = [1]'], 'E1093:')
 
   call CheckDefFailure(['let somevar'], "E1022:")
-  call CheckDefFailure(['let &option'], 'E1052:')
+  call CheckDefFailure(['let &tabstop = 4'], 'E1052:')
   call CheckDefFailure(['&g:option = 5'], 'E113:')
+  call CheckScriptFailure(['vim9script', 'let &tabstop = 4'], 'E1052:')
 
   call CheckDefFailure(['let $VAR = 5'], 'E1016: Cannot declare an environment variable:')
+  call CheckScriptFailure(['vim9script', 'let $ENV = "xxx"'], 'E1016:')
 
   call CheckDefFailure(['let @~ = 5'], 'E354:')
   call CheckDefFailure(['let @a = 5'], 'E1066:')
+  call CheckScriptFailure(['vim9script', 'let @a = "abc"'], 'E1066:')
 
   call CheckDefFailure(['let g:var = 5'], 'E1016: Cannot declare a global variable:')
   call CheckDefFailure(['let w:var = 5'], 'E1016: Cannot declare a window variable:')
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1355,
+/**/
     1354,
 /**/
     1353,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5067,7 +5067,12 @@ vim9_declare_error(char_u *name)
 	case 'w': scope = _("window"); break;
 	case 't': scope = _("tab"); break;
 	case 'v': scope = "v:"; break;
-	case '$': semsg(_(e_declare_env_var), name); return;
+	case '$': semsg(_(e_declare_env_var), name);
+		  return;
+	case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
+		  return;
+	case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
+		  return;
 	default: return;
     }
     semsg(_(e_declare_var), scope, name);
@@ -5229,6 +5234,8 @@ compile_assignment(char_u *arg, exarg_T 
 
 	if (cctx->ctx_skip != SKIP_YES)
 	{
+	    int	    declare_error = FALSE;
+
 	    if (*var_start == '&')
 	    {
 		int	    cc;
@@ -5240,11 +5247,7 @@ compile_assignment(char_u *arg, exarg_T 
 		    emsg(_(e_const_option));
 		    goto theend;
 		}
-		if (is_decl)
-		{
-		    semsg(_("E1052: Cannot declare an option: %s"), var_start);
-		    goto theend;
-		}
+		declare_error = is_decl;
 		p = var_start;
 		p = find_option_end(&p, &opt_flags);
 		if (p == NULL)
@@ -5272,11 +5275,7 @@ compile_assignment(char_u *arg, exarg_T 
 	    {
 		dest = dest_env;
 		type = &t_string;
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (*var_start == '@')
 	    {
@@ -5287,47 +5286,27 @@ compile_assignment(char_u *arg, exarg_T 
 		}
 		dest = dest_reg;
 		type = &t_string;
-		if (is_decl)
-		{
-		    semsg(_("E1066: Cannot declare a register: %s"), name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
 	    {
 		dest = dest_global;
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
 	    {
 		dest = dest_buffer;
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
 	    {
 		dest = dest_window;
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
 	    {
 		dest = dest_tab;
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
 	    {
@@ -5346,11 +5325,7 @@ compile_assignment(char_u *arg, exarg_T 
 		dest = dest_vimvar;
 		vtv = get_vim_var_tv(vimvaridx);
 		type = typval2type_vimvar(vtv, cctx->ctx_type_list);
-		if (is_decl)
-		{
-		    vim9_declare_error(name);
-		    goto theend;
-		}
+		declare_error = is_decl;
 	    }
 	    else
 	    {
@@ -5439,6 +5414,12 @@ compile_assignment(char_u *arg, exarg_T 
 		    goto theend;
 		}
 	    }
+
+	    if (declare_error)
+	    {
+		vim9_declare_error(name);
+		goto theend;
+	    }
 	}
 
 	// handle "a:name" as a name, not index "name" on "a"