Skip to content

Commit 6c3843c

Browse files
committed
patch 8.2.2567: Vim9: no error if variable is defined for existing function
Problem: Vim9: no error if variable is defined for existing function. Solution: Check if name isn't already in use. (closes #7897)
1 parent 2e2d758 commit 6c3843c

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/evalvars.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ ex_let(exarg_T *eap)
909909
}
910910

911911
/*
912-
* Assign the typevalue "tv" to the variable or variables at "arg_start".
912+
* Assign the typeval "tv" to the variable or variables at "arg_start".
913913
* Handles both "var" with any type and "[var, var; var]" with a list type.
914914
* When "op" is not NULL it points to a string with characters that
915915
* must appear after the variable(s). Use "+", "-" or "." for add, subtract
@@ -3179,6 +3179,7 @@ set_var_const(
31793179

31803180
if (di != NULL)
31813181
{
3182+
// Item already exists. Allowed to replace when reloading.
31823183
if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
31833184
{
31843185
if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
@@ -3269,6 +3270,14 @@ set_var_const(
32693270
}
32703271
else
32713272
{
3273+
// Item not found, check if a function already exists.
3274+
if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3275+
&& lookup_scriptitem(name, STRLEN(name), NULL) == OK)
3276+
{
3277+
semsg(_(e_redefining_script_item_str), name);
3278+
goto failed;
3279+
}
3280+
32723281
// add a new variable
32733282
if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
32743283
{

src/testdir/test_vim9_script.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,17 @@ def Test_script_reload_change_type()
15151515
delete('Xreload.vim')
15161516
enddef
15171517

1518+
def Test_script_var_shadows_function()
1519+
var lines =<< trim END
1520+
vim9script
1521+
def Func(): number
1522+
return 123
1523+
enddef
1524+
var Func = 1
1525+
END
1526+
CheckScriptFailure(lines, 'E1041:', 5)
1527+
enddef
1528+
15181529
def s:RetSome(): string
15191530
return 'some'
15201531
enddef

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+
2567,
753755
/**/
754756
2566,
755757
/**/

0 commit comments

Comments
 (0)