To: vim_dev@googlegroups.com Subject: Patch 8.2.2680 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.2680 Problem: Vim9: problem defining a script variable from legacy function. Solution: Check if the script is Vim9, not the current syntax. (closes #8032) Files: src/vim9script.c, src/proto/vim9script.pro, src/evalvars.c, src/testdir/test_vim9_script.vim *** ../vim-8.2.2679/src/vim9script.c 2021-03-20 14:59:58.508414399 +0100 --- src/vim9script.c 2021-03-31 20:51:53.198905691 +0200 *************** *** 17,33 **** # include "vim9.h" #endif int in_vim9script(void) { ! // Do not go up the stack, a ":function" inside vim9script uses legacy ! // syntax. "sc_version" is also set when compiling a ":def" function in ! // legacy script. return current_sctx.sc_version == SCRIPT_VERSION_VIM9 || (cmdmod.cmod_flags & CMOD_VIM9CMD); } /* * ":vim9script". */ void --- 17,49 ---- # include "vim9.h" #endif + /* + * Return TRUE when currently using Vim9 script syntax. + * Does not go up the stack, a ":function" inside vim9script uses legacy + * syntax. + */ int in_vim9script(void) { ! // "sc_version" is also set when compiling a ":def" function in legacy ! // script. return current_sctx.sc_version == SCRIPT_VERSION_VIM9 || (cmdmod.cmod_flags & CMOD_VIM9CMD); } /* + * Return TRUE if the current script is Vim9 script. + * This also returns TRUE in a legacy function in a Vim9 script. + */ + int + current_script_is_vim9(void) + { + return SCRIPT_ID_VALID(current_sctx.sc_sid) + && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version + == SCRIPT_VERSION_VIM9; + } + + /* * ":vim9script". */ void *** ../vim-8.2.2679/src/proto/vim9script.pro 2021-03-18 21:37:52.192105213 +0100 --- src/proto/vim9script.pro 2021-03-31 20:55:50.946287204 +0200 *************** *** 1,5 **** --- 1,6 ---- /* vim9script.c */ int in_vim9script(void); + int current_script_is_vim9(void); void ex_vim9script(exarg_T *eap); int not_in_vim9(exarg_T *eap); int vim9_bad_comment(char_u *p); *** ../vim-8.2.2679/src/evalvars.c 2021-03-28 21:14:02.258994195 +0200 --- src/evalvars.c 2021-03-31 20:55:51.866284806 +0200 *************** *** 3168,3173 **** --- 3168,3174 ---- hashtab_T *ht; int is_script_local; int vim9script = in_vim9script(); + int var_in_vim9script; ht = find_var_ht(name, &varname); if (ht == NULL || *varname == NUL) *************** *** 3186,3191 **** --- 3187,3193 ---- vim9_declare_error(name); goto failed; } + var_in_vim9script = is_script_local && current_script_is_vim9(); di = find_var_in_ht(ht, 0, varname, TRUE); *************** *** 3217,3223 **** goto failed; } ! if (is_script_local && vim9script) { where_T where; --- 3219,3225 ---- goto failed; } ! if (var_in_vim9script) { where_T where; *************** *** 3244,3250 **** // A Vim9 script-local variable is also present in sn_all_vars and // sn_var_vals. It may set "type" from "tv". ! if (is_script_local && vim9script) update_vim9_script_var(FALSE, di, flags, tv, &type); } --- 3246,3252 ---- // A Vim9 script-local variable is also present in sn_all_vars and // sn_var_vals. It may set "type" from "tv". ! if (var_in_vim9script) update_vim9_script_var(FALSE, di, flags, tv, &type); } *************** *** 3308,3314 **** } // add a new variable ! if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL)) { semsg(_(e_unknown_variable_str), name); goto failed; --- 3310,3316 ---- } // add a new variable ! if (var_in_vim9script && (flags & ASSIGN_NO_DECL)) { semsg(_(e_unknown_variable_str), name); goto failed; *************** *** 3342,3348 **** // A Vim9 script-local variable is also added to sn_all_vars and // sn_var_vals. It may set "type" from "tv". ! if (is_script_local && vim9script) update_vim9_script_var(TRUE, di, flags, tv, &type); } --- 3344,3350 ---- // A Vim9 script-local variable is also added to sn_all_vars and // sn_var_vals. It may set "type" from "tv". ! if (var_in_vim9script) update_vim9_script_var(TRUE, di, flags, tv, &type); } *** ../vim-8.2.2679/src/testdir/test_vim9_script.vim 2021-03-26 20:41:24.773620612 +0100 --- src/testdir/test_vim9_script.vim 2021-03-31 21:02:12.149293412 +0200 *************** *** 3220,3225 **** --- 3220,3254 ---- delete('Xvim9_script.vim') enddef + def Test_declare_script_in_func() + var lines =<< trim END + vim9script + func Declare() + let s:local = 123 + endfunc + Declare() + assert_equal(123, local) + + var error: string + try + local = 'asdf' + catch + error = v:exception + endtry + assert_match('E1012: Type mismatch; expected number but got string', error) + + lockvar local + try + local = 999 + catch + error = v:exception + endtry + assert_match('E741: Value is locked: local', error) + END + CheckScriptSuccess(lines) + enddef + + func Test_vim9script_not_global() " check that items defined in Vim9 script are script-local, not global let vim9lines =<< trim END *** ../vim-8.2.2679/src/version.c 2021-03-30 22:11:53.085681525 +0200 --- src/version.c 2021-03-31 21:03:44.749051776 +0200 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 2680, /**/ -- Change is inevitable, except from a vending machine. /// 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 ///