Skip to content

Commit 333894b

Browse files
committed
patch 8.2.1343: Vim9: cannot find global function when using g:
Problem: Vim9: cannot find global function when using g: when local function with the same name exists. Solution: Find global function when using g:.
1 parent f5a4801 commit 333894b

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/testdir/test_vim9_func.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ def Test_nested_global_function()
161161
CheckScriptSuccess(lines)
162162
enddef
163163

164+
def Test_global_local_function()
165+
let lines =<< trim END
166+
vim9script
167+
def g:Func(): string
168+
return 'global'
169+
enddef
170+
def Func(): string
171+
return 'local'
172+
enddef
173+
assert_equal('global', g:Func())
174+
assert_equal('local', Func())
175+
END
176+
CheckScriptSuccess(lines)
177+
enddef
178+
164179
func TakesOneArg(arg)
165180
echo a:arg
166181
endfunc

src/userfunc.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,17 +789,18 @@ find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
789789

790790
if (!is_global)
791791
{
792-
char_u *after_script = NULL;
792+
int vim9script = in_vim9script();
793+
char_u *after_script = NULL;
793794

794-
if (in_vim9script())
795+
if (vim9script)
795796
{
796797
// Find script-local function before global one.
797798
func = find_func_with_sid(name, current_sctx.sc_sid);
798799
if (func != NULL)
799800
return func;
800801
}
801802

802-
if (!in_vim9script()
803+
if (!vim9script
803804
&& name[0] == K_SPECIAL
804805
&& name[1] == KS_EXTRA
805806
&& name[2] == KE_SNR)
@@ -815,7 +816,7 @@ find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
815816
else
816817
after_script = NULL;
817818
}
818-
if (in_vim9script() || after_script != NULL)
819+
if (vim9script || after_script != NULL)
819820
{
820821
// Find imported function before global one.
821822
imported = find_imported(
@@ -2086,10 +2087,14 @@ call_func(
20862087
if (error == FCERR_NONE && funcexe->evaluate)
20872088
{
20882089
char_u *rfname = fname;
2090+
int is_global = FALSE;
20892091

2090-
// Ignore "g:" before a function name.
2092+
// Skip "g:" before a function name.
20912093
if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
2094+
{
2095+
is_global = TRUE;
20922096
rfname = fname + 2;
2097+
}
20932098

20942099
rettv->v_type = VAR_NUMBER; // default rettv is number zero
20952100
rettv->vval.v_number = 0;
@@ -2101,7 +2106,7 @@ call_func(
21012106
* User defined function.
21022107
*/
21032108
if (fp == NULL)
2104-
fp = find_func(rfname, FALSE, NULL);
2109+
fp = find_func(rfname, is_global, NULL);
21052110

21062111
// Trigger FuncUndefined event, may load the function.
21072112
if (fp == NULL
@@ -2110,13 +2115,13 @@ call_func(
21102115
&& !aborting())
21112116
{
21122117
// executed an autocommand, search for the function again
2113-
fp = find_func(rfname, FALSE, NULL);
2118+
fp = find_func(rfname, is_global, NULL);
21142119
}
21152120
// Try loading a package.
21162121
if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
21172122
{
21182123
// loaded a package, search for the function again
2119-
fp = find_func(rfname, FALSE, NULL);
2124+
fp = find_func(rfname, is_global, NULL);
21202125
}
21212126
if (fp == NULL)
21222127
{
@@ -2125,7 +2130,7 @@ call_func(
21252130
// If using Vim9 script try not local to the script.
21262131
// TODO: should not do this if the name started with "s:".
21272132
if (p != NULL)
2128-
fp = find_func(p, FALSE, NULL);
2133+
fp = find_func(p, is_global, NULL);
21292134
}
21302135

21312136
if (fp != NULL && (fp->uf_flags & FC_DELETED))
@@ -2175,6 +2180,7 @@ call_func(
21752180
*/
21762181
error = call_internal_func(fname, argcount, argvars, rettv);
21772182
}
2183+
21782184
/*
21792185
* The function call (or "FuncUndefined" autocommand sequence) might
21802186
* have been aborted by an error, an interrupt, or an explicitly thrown

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1343,
757759
/**/
758760
1342,
759761
/**/

0 commit comments

Comments
 (0)