comparison src/evalvars.c @ 23297:40f1d3f0c53e v8.2.2194

patch 8.2.2194: Vim9: cannot use :const or :final at the script level Commit: https://github.com/vim/vim/commit/89b474dd4f0de878b4c48eeb9e223f0c22ee1442 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Dec 22 21:19:39 2020 +0100 patch 8.2.2194: Vim9: cannot use :const or :final at the script level Problem: Vim9: cannot use :const or :final at the script level. Solution: Support using :const and :final. (closes https://github.com/vim/vim/issues/7526)
author Bram Moolenaar <Bram@vim.org>
date Tue, 22 Dec 2020 21:30:04 +0100
parents d9ae7dd3a0f2
children d5919c5fd3dc
comparison
equal deleted inserted replaced
23296:c9123e2447bc 23297:40f1d3f0c53e
736 char_u op[4]; 736 char_u op[4];
737 char_u *argend; 737 char_u *argend;
738 int first = TRUE; 738 int first = TRUE;
739 int concat; 739 int concat;
740 int has_assign; 740 int has_assign;
741 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0; 741 int flags = 0;
742 int vim9script = in_vim9script(); 742 int vim9script = in_vim9script();
743 743
744 if (eap->cmdidx == CMD_final && !vim9script) 744 if (eap->cmdidx == CMD_final && !vim9script)
745 { 745 {
746 // In legacy Vim script ":final" is short for ":finally". 746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap); 747 ex_finally(eap);
748 return; 748 return;
749 } 749 }
750 if (eap->cmdidx == CMD_let && vim9script) 750 if (eap->cmdidx == CMD_let && vim9script)
751 { 751 {
752 emsg(_(e_cannot_use_let_in_vim9_script)); 752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return; 753 return;
754 } 754 }
755 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit) 755 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
756 // In legacy Vim script ":const" works like ":final". 756 // In legacy Vim script ":const" works like ":final".
757 eap->cmdidx = CMD_final; 757 eap->cmdidx = CMD_final;
758 758
759 // detect Vim9 assignment without ":let" or ":const" 759 if (eap->cmdidx == CMD_const)
760 flags |= ASSIGN_CONST;
761 else if (eap->cmdidx == CMD_final)
762 flags |= ASSIGN_FINAL;
763
764 // Vim9 assignment without ":let", ":const" or ":final"
760 if (eap->arg == eap->cmd) 765 if (eap->arg == eap->cmd)
761 flags |= ASSIGN_NO_DECL; 766 flags |= ASSIGN_NO_DECL;
762 767
763 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE); 768 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
764 if (argend == NULL) 769 if (argend == NULL)
907 char_u *arg_start, 912 char_u *arg_start,
908 typval_T *tv, 913 typval_T *tv,
909 int copy, // copy values from "tv", don't move 914 int copy, // copy values from "tv", don't move
910 int semicolon, // from skip_var_list() 915 int semicolon, // from skip_var_list()
911 int var_count, // from skip_var_list() 916 int var_count, // from skip_var_list()
912 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL 917 int flags, // ASSIGN_FINAL, ASSIGN_CONST, ASSIGN_NO_DECL
913 char_u *op) 918 char_u *op)
914 { 919 {
915 char_u *arg = arg_start; 920 char_u *arg = arg_start;
916 list_T *l; 921 list_T *l;
917 int i; 922 int i;
1262 static char_u * 1267 static char_u *
1263 ex_let_one( 1268 ex_let_one(
1264 char_u *arg, // points to variable name 1269 char_u *arg, // points to variable name
1265 typval_T *tv, // value to assign to variable 1270 typval_T *tv, // value to assign to variable
1266 int copy, // copy value from "tv" 1271 int copy, // copy value from "tv"
1267 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL 1272 int flags, // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
1268 char_u *endchars, // valid chars after variable name or NULL 1273 char_u *endchars, // valid chars after variable name or NULL
1269 char_u *op) // "+", "-", "." or NULL 1274 char_u *op) // "+", "-", "." or NULL
1270 { 1275 {
1271 int c1; 1276 int c1;
1272 char_u *name; 1277 char_u *name;
1275 int len; 1280 int len;
1276 int opt_flags; 1281 int opt_flags;
1277 char_u *tofree = NULL; 1282 char_u *tofree = NULL;
1278 1283
1279 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0 1284 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
1285 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
1280 && vim_strchr((char_u *)"$@&", *arg) != NULL) 1286 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1281 { 1287 {
1282 vim9_declare_error(arg); 1288 vim9_declare_error(arg);
1283 return NULL; 1289 return NULL;
1284 } 1290 }
1285 1291
1286 // ":let $VAR = expr": Set environment variable. 1292 // ":let $VAR = expr": Set environment variable.
1287 if (*arg == '$') 1293 if (*arg == '$')
1288 { 1294 {
1289 if (flags & ASSIGN_CONST) 1295 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1290 { 1296 {
1291 emsg(_("E996: Cannot lock an environment variable")); 1297 emsg(_("E996: Cannot lock an environment variable"));
1292 return NULL; 1298 return NULL;
1293 } 1299 }
1294 1300
1336 // ":let &option = expr": Set option value. 1342 // ":let &option = expr": Set option value.
1337 // ":let &l:option = expr": Set local option value. 1343 // ":let &l:option = expr": Set local option value.
1338 // ":let &g:option = expr": Set global option value. 1344 // ":let &g:option = expr": Set global option value.
1339 else if (*arg == '&') 1345 else if (*arg == '&')
1340 { 1346 {
1341 if (flags & ASSIGN_CONST) 1347 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1342 { 1348 {
1343 emsg(_(e_const_option)); 1349 emsg(_(e_const_option));
1344 return NULL; 1350 return NULL;
1345 } 1351 }
1346 // Find the end of the name. 1352 // Find the end of the name.
1420 } 1426 }
1421 1427
1422 // ":let @r = expr": Set register contents. 1428 // ":let @r = expr": Set register contents.
1423 else if (*arg == '@') 1429 else if (*arg == '@')
1424 { 1430 {
1425 if (flags & ASSIGN_CONST) 1431 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1426 { 1432 {
1427 emsg(_("E996: Cannot lock a register")); 1433 emsg(_("E996: Cannot lock a register"));
1428 return NULL; 1434 return NULL;
1429 } 1435 }
1430 ++arg; 1436 ++arg;
3054 set_var_const( 3060 set_var_const(
3055 char_u *name, 3061 char_u *name,
3056 type_T *type, 3062 type_T *type,
3057 typval_T *tv_arg, 3063 typval_T *tv_arg,
3058 int copy, // make copy of value in "tv" 3064 int copy, // make copy of value in "tv"
3059 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL 3065 int flags) // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
3060 { 3066 {
3061 typval_T *tv = tv_arg; 3067 typval_T *tv = tv_arg;
3062 typval_T bool_tv; 3068 typval_T bool_tv;
3063 dictitem_T *di; 3069 dictitem_T *di;
3064 char_u *varname; 3070 char_u *varname;
3075 is_script_local = ht == get_script_local_ht(); 3081 is_script_local = ht == get_script_local_ht();
3076 3082
3077 if (vim9script 3083 if (vim9script
3078 && !is_script_local 3084 && !is_script_local
3079 && (flags & ASSIGN_NO_DECL) == 0 3085 && (flags & ASSIGN_NO_DECL) == 0
3086 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
3080 && name[1] == ':') 3087 && name[1] == ':')
3081 { 3088 {
3082 vim9_declare_error(name); 3089 vim9_declare_error(name);
3083 goto failed; 3090 goto failed;
3084 } 3091 }
3104 3111
3105 if (di != NULL) 3112 if (di != NULL)
3106 { 3113 {
3107 if ((di->di_flags & DI_FLAGS_RELOAD) == 0) 3114 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3108 { 3115 {
3109 if (flags & ASSIGN_CONST) 3116 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3110 { 3117 {
3111 emsg(_(e_cannot_mod)); 3118 emsg(_(e_cannot_mod));
3112 goto failed; 3119 goto failed;
3113 } 3120 }
3114 3121
3204 { 3211 {
3205 vim_free(di); 3212 vim_free(di);
3206 goto failed; 3213 goto failed;
3207 } 3214 }
3208 di->di_flags = DI_FLAGS_ALLOC; 3215 di->di_flags = DI_FLAGS_ALLOC;
3209 if (flags & ASSIGN_CONST) 3216 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3210 di->di_flags |= DI_FLAGS_LOCK; 3217 di->di_flags |= DI_FLAGS_LOCK;
3211 3218
3212 // A Vim9 script-local variable is also added to sn_all_vars and 3219 // A Vim9 script-local variable is also added to sn_all_vars and
3213 // sn_var_vals. 3220 // sn_var_vals.
3214 if (is_script_local && vim9script) 3221 if (is_script_local && vim9script)