To: vim_dev@googlegroups.com Subject: Patch 9.0.0444 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0444 Problem: Trying to declare g:variable gives confusing error. Solution: Give a better error message. (closes #11108) Files: src/ex_docmd.c, src/errors.h, src/eval.c, src/evalvars.c, src/proto/evalvars.pro, src/testdir/test_vim9_assign.vim, src/testdir/test_vim9_cmd.vim, src/testdir/test_vim9_script.vim *** ../vim-9.0.0443/src/ex_docmd.c 2022-09-09 13:27:40.403539790 +0100 --- src/ex_docmd.c 2022-09-11 14:08:13.150329350 +0100 *************** *** 3761,3771 **** } } ! // Recognize using a type for a w:, b:, t: or g: variable: // "w:varname: number = 123". if (eap->cmd[1] == ':' && *p == ':') { ! eap->cmdidx = CMD_eval; return eap->cmd; } } --- 3761,3771 ---- } } ! // Recognize trying to use a type for a w:, b:, t: or g: variable: // "w:varname: number = 123". if (eap->cmd[1] == ':' && *p == ':') { ! eap->cmdidx = CMD_var; return eap->cmd; } } *** ../vim-9.0.0443/src/errors.h 2022-09-10 13:51:18.117956356 +0100 --- src/errors.h 2022-09-11 14:22:05.381052496 +0100 *************** *** 3335,3338 **** --- 3335,3340 ---- INIT(= N_("E1302: Script variable was deleted")); EXTERN char e_custom_list_completion_function_does_not_return_list_but_str[] INIT(= N_("E1303: Custom list completion function does not return a List but a %s")); + EXTERN char e_cannot_use_type_with_this_variable_str[] + INIT(= N_("E1304: Cannot use type with this variable: %s")); #endif *** ../vim-9.0.0443/src/eval.c 2022-09-10 13:51:18.117956356 +0100 --- src/eval.c 2022-09-11 14:27:33.964486080 +0100 *************** *** 1031,1042 **** { char_u *tp = skipwhite(p + 1); if (tp == p + 1 && !quiet) { semsg(_(e_white_space_required_after_str_str), ":", p); return NULL; } - if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) { semsg(_(e_using_type_not_in_script_context_str), p); --- 1031,1046 ---- { char_u *tp = skipwhite(p + 1); + if (is_scoped_variable(name)) + { + semsg(_(e_cannot_use_type_with_this_variable_str), name); + return NULL; + } if (tp == p + 1 && !quiet) { semsg(_(e_white_space_required_after_str_str), ":", p); return NULL; } if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) { semsg(_(e_using_type_not_in_script_context_str), p); *** ../vim-9.0.0443/src/evalvars.c 2022-09-06 18:57:04.927749453 +0100 --- src/evalvars.c 2022-09-11 14:40:01.407012433 +0100 *************** *** 603,608 **** --- 603,620 ---- } /* + * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:". + * But only when an identifier character follows. + */ + int + is_scoped_variable(char_u *name) + { + return vim_strchr((char_u *)"gwbt", name[0]) != NULL + && name[1] == ':' + && eval_isnamec(name[2]); + } + + /* * Evaluate one Vim expression {expr} in string "p" and append the * resulting string to "gap". "p" points to the opening "{". * When "evaluate" is FALSE only skip over the expression. *************** *** 3679,3686 **** vim9_declare_error(name); goto failed; } ! if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':' ! && vim_strchr((char_u *)"gwbt", name[0]) != NULL) // Do not make g:var, w:var, b:var or t:var final. flags &= ~ASSIGN_FINAL; --- 3691,3697 ---- vim9_declare_error(name); goto failed; } ! if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name)) // Do not make g:var, w:var, b:var or t:var final. flags &= ~ASSIGN_FINAL; *** ../vim-9.0.0443/src/proto/evalvars.pro 2022-06-29 12:54:48.064572066 +0100 --- src/proto/evalvars.pro 2022-09-11 14:29:01.972331996 +0100 *************** *** 13,18 **** --- 13,19 ---- int get_spellword(list_T *list, char_u **pp); void prepare_vimvar(int idx, typval_T *save_tv); void restore_vimvar(int idx, typval_T *save_tv); + int is_scoped_variable(char_u *name); char_u *eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate); list_T *heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile); void ex_var(exarg_T *eap); *** ../vim-9.0.0443/src/testdir/test_vim9_assign.vim 2022-09-02 21:55:45.507049444 +0100 --- src/testdir/test_vim9_assign.vim 2022-09-11 14:57:22.892926615 +0100 *************** *** 1597,1609 **** v9.CheckDefFailure(['var name: dict") g:VerifyScreenDump(buf, 'Test_misplaced_type', {}) g:StopVimInTerminal(buf) - delete('XTest_misplaced_type') enddef " Ensure echo doesn't crash when stringifying empty variables. --- 4241,4252 ---- endfunc def Run_Test_misplaced_type() ! writefile(['let g:somevar = "asdf"'], 'XTest_misplaced_type', 'D') var buf = g:RunVimInTerminal('-S XTest_misplaced_type', {'rows': 6}) ! term_sendkeys(buf, ":vim9cmd echo islocked('somevar: string')\") g:VerifyScreenDump(buf, 'Test_misplaced_type', {}) g:StopVimInTerminal(buf) enddef " Ensure echo doesn't crash when stringifying empty variables. *** ../vim-9.0.0443/src/version.c 2022-09-11 13:37:31.797170405 +0100 --- src/version.c 2022-09-11 15:11:25.475316178 +0100 *************** *** 705,706 **** --- 705,708 ---- { /* Add new patch number below this line */ + /**/ + 444, /**/ -- Everybody lies, but it doesn't matter since nobody listens. -- Lieberman's Law /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///