mirror of
https://github.com/vim/vim.git
synced 2025-09-25 03:54:15 -04:00
patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Problem: Vim9: problem defining a script variable from legacy function. Solution: Check if the script is Vim9, not the current syntax. (closes #8032)
This commit is contained in:
@@ -3168,6 +3168,7 @@ set_var_const(
|
|||||||
hashtab_T *ht;
|
hashtab_T *ht;
|
||||||
int is_script_local;
|
int is_script_local;
|
||||||
int vim9script = in_vim9script();
|
int vim9script = in_vim9script();
|
||||||
|
int var_in_vim9script;
|
||||||
|
|
||||||
ht = find_var_ht(name, &varname);
|
ht = find_var_ht(name, &varname);
|
||||||
if (ht == NULL || *varname == NUL)
|
if (ht == NULL || *varname == NUL)
|
||||||
@@ -3186,6 +3187,7 @@ set_var_const(
|
|||||||
vim9_declare_error(name);
|
vim9_declare_error(name);
|
||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
var_in_vim9script = is_script_local && current_script_is_vim9();
|
||||||
|
|
||||||
di = find_var_in_ht(ht, 0, varname, TRUE);
|
di = find_var_in_ht(ht, 0, varname, TRUE);
|
||||||
|
|
||||||
@@ -3217,7 +3219,7 @@ set_var_const(
|
|||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_script_local && vim9script)
|
if (var_in_vim9script)
|
||||||
{
|
{
|
||||||
where_T where;
|
where_T where;
|
||||||
|
|
||||||
@@ -3244,7 +3246,7 @@ set_var_const(
|
|||||||
|
|
||||||
// A Vim9 script-local variable is also present in sn_all_vars and
|
// A Vim9 script-local variable is also present in sn_all_vars and
|
||||||
// sn_var_vals. It may set "type" from "tv".
|
// sn_var_vals. It may set "type" from "tv".
|
||||||
if (is_script_local && vim9script)
|
if (var_in_vim9script)
|
||||||
update_vim9_script_var(FALSE, di, flags, tv, &type);
|
update_vim9_script_var(FALSE, di, flags, tv, &type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3308,7 +3310,7 @@ set_var_const(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add a new variable
|
// add a new variable
|
||||||
if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
|
if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
|
||||||
{
|
{
|
||||||
semsg(_(e_unknown_variable_str), name);
|
semsg(_(e_unknown_variable_str), name);
|
||||||
goto failed;
|
goto failed;
|
||||||
@@ -3342,7 +3344,7 @@ set_var_const(
|
|||||||
|
|
||||||
// A Vim9 script-local variable is also added to sn_all_vars and
|
// A Vim9 script-local variable is also added to sn_all_vars and
|
||||||
// sn_var_vals. It may set "type" from "tv".
|
// sn_var_vals. It may set "type" from "tv".
|
||||||
if (is_script_local && vim9script)
|
if (var_in_vim9script)
|
||||||
update_vim9_script_var(TRUE, di, flags, tv, &type);
|
update_vim9_script_var(TRUE, di, flags, tv, &type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
/* vim9script.c */
|
/* vim9script.c */
|
||||||
int in_vim9script(void);
|
int in_vim9script(void);
|
||||||
|
int current_script_is_vim9(void);
|
||||||
void ex_vim9script(exarg_T *eap);
|
void ex_vim9script(exarg_T *eap);
|
||||||
int not_in_vim9(exarg_T *eap);
|
int not_in_vim9(exarg_T *eap);
|
||||||
int vim9_bad_comment(char_u *p);
|
int vim9_bad_comment(char_u *p);
|
||||||
|
@@ -3220,6 +3220,35 @@ def Test_source_vim9_from_legacy()
|
|||||||
delete('Xvim9_script.vim')
|
delete('Xvim9_script.vim')
|
||||||
enddef
|
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()
|
func Test_vim9script_not_global()
|
||||||
" check that items defined in Vim9 script are script-local, not global
|
" check that items defined in Vim9 script are script-local, not global
|
||||||
let vim9lines =<< trim END
|
let vim9lines =<< trim END
|
||||||
|
@@ -750,6 +750,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
2680,
|
||||||
/**/
|
/**/
|
||||||
2679,
|
2679,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -17,16 +17,32 @@
|
|||||||
# include "vim9.h"
|
# include "vim9.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return TRUE when currently using Vim9 script syntax.
|
||||||
|
* Does not go up the stack, a ":function" inside vim9script uses legacy
|
||||||
|
* syntax.
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
in_vim9script(void)
|
in_vim9script(void)
|
||||||
{
|
{
|
||||||
// Do not go up the stack, a ":function" inside vim9script uses legacy
|
// "sc_version" is also set when compiling a ":def" function in legacy
|
||||||
// syntax. "sc_version" is also set when compiling a ":def" function in
|
// script.
|
||||||
// legacy script.
|
|
||||||
return current_sctx.sc_version == SCRIPT_VERSION_VIM9
|
return current_sctx.sc_version == SCRIPT_VERSION_VIM9
|
||||||
|| (cmdmod.cmod_flags & CMOD_VIM9CMD);
|
|| (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".
|
* ":vim9script".
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user