diff src/vim9compile.c @ 22391:a9fb7efa31d6 v8.2.1744

patch 8.2.1744: Vim9: using ":const!" is weird Commit: https://github.com/vim/vim/commit/30fd8204cecb317d842b964d624d492088d6d15f Author: Bram Moolenaar <Bram@vim.org> Date: Sat Sep 26 15:09:30 2020 +0200 patch 8.2.1744: Vim9: using ":const!" is weird Problem: Vim9: using ":const!" is weird. Solution: Use "var" - "final" - "const" like Dart. "let" still works for now.
author Bram Moolenaar <Bram@vim.org>
date Sat, 26 Sep 2020 15:15:05 +0200
parents 6fe9536694ff
children 66d1131a7eff
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -4562,8 +4562,12 @@ vim9_declare_error(char_u *name)
 
 /*
  * Compile declaration and assignment:
- * "let var", "let var = expr", "const var = expr" and "var = expr"
- * "arg" points to "var".
+ * "let name"
+ * "var name = expr"
+ * "final name = expr"
+ * "const name = expr"
+ * "name = expr"
+ * "arg" points to "name".
  * Return NULL for an error.
  * Return "arg" if it does not look like a variable list.
  */
@@ -4588,7 +4592,8 @@ compile_assignment(char_u *arg, exarg_T 
     type_T	*member_type = &t_any;
     char_u	*name = NULL;
     char_u	*sp;
-    int		is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
+    int		is_decl = cmdidx == CMD_let || cmdidx == CMD_var
+				 || cmdidx == CMD_final || cmdidx == CMD_const;
 
     // Skip over the "var" or "[var, var]" to get to any "=".
     p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
@@ -4729,7 +4734,7 @@ compile_assignment(char_u *arg, exarg_T 
 		long	    numval;
 
 		dest = dest_option;
-		if (cmdidx == CMD_const)
+		if (cmdidx == CMD_final || cmdidx == CMD_const)
 		{
 		    emsg(_(e_const_option));
 		    goto theend;
@@ -4968,7 +4973,7 @@ compile_assignment(char_u *arg, exarg_T 
 					    && var_wrong_func_name(name, TRUE))
 		goto theend;
 	    lvar = reserve_local(cctx, var_start, varlen,
-						    cmdidx == CMD_const, type);
+			     cmdidx == CMD_final || cmdidx == CMD_const, type);
 	    if (lvar == NULL)
 		goto theend;
 	    new_local = TRUE;
@@ -5119,6 +5124,11 @@ compile_assignment(char_u *arg, exarg_T 
 							  cctx, FALSE) == FAIL)
 		    goto theend;
 	    }
+	    else if (cmdidx == CMD_final)
+	    {
+		emsg(_(e_final_requires_a_value));
+		goto theend;
+	    }
 	    else if (cmdidx == CMD_const)
 	    {
 		emsg(_(e_const_requires_a_value));
@@ -5283,9 +5293,9 @@ compile_assignment(char_u *arg, exarg_T 
 	}
 	else
 	{
-	    if (is_decl && eap->forceit && cmdidx == CMD_const
-		    && (dest == dest_script || dest == dest_local))
-		// ":const! var": lock the value, but not referenced variables
+	    if (is_decl && cmdidx == CMD_const
+				&& (dest == dest_script || dest == dest_local))
+		// ":const var": lock the value, but not referenced variables
 		generate_LOCKCONST(cctx);
 
 	    switch (dest)
@@ -6915,7 +6925,7 @@ compile_def_function(ufunc_T *ufunc, int
 	    // Expression or function call.
 	    if (ea.cmdidx != CMD_eval)
 	    {
-		// CMD_let cannot happen, compile_assignment() above is used
+		// CMD_var cannot happen, compile_assignment() above is used
 		iemsg("Command from find_ex_command() not handled");
 		goto erret;
 	    }
@@ -6967,6 +6977,8 @@ compile_def_function(ufunc_T *ufunc, int
 		    break;
 
 	    case CMD_let:
+	    case CMD_var:
+	    case CMD_final:
 	    case CMD_const:
 		    line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
 		    if (line == p)