Skip to content

Commit e535db8

Browse files
committed
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)
1 parent dad4473 commit e535db8

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

src/evalvars.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,7 @@ set_var_const(
31683168
hashtab_T *ht;
31693169
int is_script_local;
31703170
int vim9script = in_vim9script();
3171+
int var_in_vim9script;
31713172

31723173
ht = find_var_ht(name, &varname);
31733174
if (ht == NULL || *varname == NUL)
@@ -3186,6 +3187,7 @@ set_var_const(
31863187
vim9_declare_error(name);
31873188
goto failed;
31883189
}
3190+
var_in_vim9script = is_script_local && current_script_is_vim9();
31893191

31903192
di = find_var_in_ht(ht, 0, varname, TRUE);
31913193

@@ -3217,7 +3219,7 @@ set_var_const(
32173219
goto failed;
32183220
}
32193221

3220-
if (is_script_local && vim9script)
3222+
if (var_in_vim9script)
32213223
{
32223224
where_T where;
32233225

@@ -3244,7 +3246,7 @@ set_var_const(
32443246

32453247
// A Vim9 script-local variable is also present in sn_all_vars and
32463248
// sn_var_vals. It may set "type" from "tv".
3247-
if (is_script_local && vim9script)
3249+
if (var_in_vim9script)
32483250
update_vim9_script_var(FALSE, di, flags, tv, &type);
32493251
}
32503252

@@ -3308,7 +3310,7 @@ set_var_const(
33083310
}
33093311

33103312
// add a new variable
3311-
if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3313+
if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
33123314
{
33133315
semsg(_(e_unknown_variable_str), name);
33143316
goto failed;
@@ -3342,7 +3344,7 @@ set_var_const(
33423344

33433345
// A Vim9 script-local variable is also added to sn_all_vars and
33443346
// sn_var_vals. It may set "type" from "tv".
3345-
if (is_script_local && vim9script)
3347+
if (var_in_vim9script)
33463348
update_vim9_script_var(TRUE, di, flags, tv, &type);
33473349
}
33483350

src/proto/vim9script.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* vim9script.c */
22
int in_vim9script(void);
3+
int current_script_is_vim9(void);
34
void ex_vim9script(exarg_T *eap);
45
int not_in_vim9(exarg_T *eap);
56
int vim9_bad_comment(char_u *p);

src/testdir/test_vim9_script.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,35 @@ def Test_source_vim9_from_legacy()
32203220
delete('Xvim9_script.vim')
32213221
enddef
32223222

3223+
def Test_declare_script_in_func()
3224+
var lines =<< trim END
3225+
vim9script
3226+
func Declare()
3227+
let s:local = 123
3228+
endfunc
3229+
Declare()
3230+
assert_equal(123, local)
3231+
3232+
var error: string
3233+
try
3234+
local = 'asdf'
3235+
catch
3236+
error = v:exception
3237+
endtry
3238+
assert_match('E1012: Type mismatch; expected number but got string', error)
3239+
3240+
lockvar local
3241+
try
3242+
local = 999
3243+
catch
3244+
error = v:exception
3245+
endtry
3246+
assert_match('E741: Value is locked: local', error)
3247+
END
3248+
CheckScriptSuccess(lines)
3249+
enddef
3250+
3251+
32233252
func Test_vim9script_not_global()
32243253
" check that items defined in Vim9 script are script-local, not global
32253254
let vim9lines =<< trim END

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2680,
753755
/**/
754756
2679,
755757
/**/

src/vim9script.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,32 @@
1717
# include "vim9.h"
1818
#endif
1919

20+
/*
21+
* Return TRUE when currently using Vim9 script syntax.
22+
* Does not go up the stack, a ":function" inside vim9script uses legacy
23+
* syntax.
24+
*/
2025
int
2126
in_vim9script(void)
2227
{
23-
// Do not go up the stack, a ":function" inside vim9script uses legacy
24-
// syntax. "sc_version" is also set when compiling a ":def" function in
25-
// legacy script.
28+
// "sc_version" is also set when compiling a ":def" function in legacy
29+
// script.
2630
return current_sctx.sc_version == SCRIPT_VERSION_VIM9
2731
|| (cmdmod.cmod_flags & CMOD_VIM9CMD);
2832
}
2933

34+
/*
35+
* Return TRUE if the current script is Vim9 script.
36+
* This also returns TRUE in a legacy function in a Vim9 script.
37+
*/
38+
int
39+
current_script_is_vim9(void)
40+
{
41+
return SCRIPT_ID_VALID(current_sctx.sc_sid)
42+
&& SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
43+
== SCRIPT_VERSION_VIM9;
44+
}
45+
3046
/*
3147
* ":vim9script".
3248
*/

0 commit comments

Comments
 (0)