Skip to content

Commit 848fadd

Browse files
committed
patch 8.2.4260: Vim9: can still use a global function without g:
Problem: Vim9: can still use a global function without g: at the script level. Solution: Also check for g: at the script level. (issue #9637)
1 parent 06011e1 commit 848fadd

16 files changed

+102
-54
lines changed

src/evalvars.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,17 +2779,20 @@ eval_variable(
27792779
}
27802780
else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
27812781
{
2782+
int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
27822783
ufunc_T *ufunc = find_func(name, FALSE);
27832784

27842785
// In Vim9 script we can get a function reference by using the
2785-
// function name.
2786-
if (ufunc != NULL)
2786+
// function name. For a global non-autoload function "g:" is
2787+
// required.
2788+
if (ufunc != NULL && (has_g_prefix
2789+
|| !func_requires_g_prefix(ufunc)))
27872790
{
27882791
found = TRUE;
27892792
if (rettv != NULL)
27902793
{
27912794
rettv->v_type = VAR_FUNC;
2792-
if (STRNCMP(name, "g:", 2) == 0)
2795+
if (has_g_prefix)
27932796
// Keep the "g:", otherwise script-local may be
27942797
// assumed.
27952798
rettv->vval.v_string = vim_strsave(name);

src/proto/userfunc.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ char_u *fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *e
1111
ufunc_T *find_func_even_dead(char_u *name, int flags);
1212
ufunc_T *find_func(char_u *name, int is_global);
1313
int func_is_global(ufunc_T *ufunc);
14+
int func_requires_g_prefix(ufunc_T *ufunc);
1415
int func_name_refcount(char_u *name);
1516
void func_clear_free(ufunc_T *fp, int force);
1617
int copy_func(char_u *lambda, char_u *global, ectx_T *ectx);

src/testdir/dumps/Test_popupwin_scroll_11.dump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
|7| @20|4+0#0000001#ffd7ff255| @28| +0#0000000#a8a8a8255| +0&#ffffff0@21
88
|8| @73
99
|9| @73
10-
|:|c|a|l@1| |P|o|p|u|p|S|c|r|o|l@1|(|)| @37|1|,|1| @10|T|o|p|
10+
|:|c|a|l@1| |g|:|P|o|p|u|p|S|c|r|o|l@1|(|)| @35|1|,|1| @10|T|o|p|

src/testdir/dumps/Test_popupwin_scroll_12.dump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
|7| @20|l+0#0000001#ffd7ff255|o|n|g| |l|i|n|e| |l|o|n|g| |l|i|n|e| |l|o|n|g| |l|i|n|e| | +0#0000000#a8a8a8255| +0&#ffffff0@21
88
|8| @73
99
|9| @73
10-
|:|c|a|l@1| |P|o|p|u|p|S|c|r|o|l@1|(|)| @37|1|,|1| @10|T|o|p|
10+
|:|c|a|l@1| |g|:|P|o|p|u|p|S|c|r|o|l@1|(|)| @35|1|,|1| @10|T|o|p|

src/testdir/test_ins_complete.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ func Test_completefunc_callback()
14221422
call assert_fails("set completefunc=funcref('abc')", "E700:")
14231423

14241424
#" set 'completefunc' to a non-existing function
1425-
set completefunc=CompleteFunc2
1425+
set completefunc=g:CompleteFunc2
14261426
call setline(1, 'five')
14271427
call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
14281428
call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
@@ -1679,7 +1679,7 @@ func Test_omnifunc_callback()
16791679
call assert_fails("set omnifunc=funcref('abc')", "E700:")
16801680

16811681
#" set 'omnifunc' to a non-existing function
1682-
set omnifunc=OmniFunc2
1682+
set omnifunc=g:OmniFunc2
16831683
call setline(1, 'nine')
16841684
call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
16851685
call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
@@ -1936,7 +1936,7 @@ func Test_thesaurusfunc_callback()
19361936
call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
19371937

19381938
#" set 'thesaurusfunc' to a non-existing function
1939-
set thesaurusfunc=TsrFunc2
1939+
set thesaurusfunc=g:TsrFunc2
19401940
call setline(1, 'ten')
19411941
call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
19421942
call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')

src/testdir/test_popupwin.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ func Test_popup_scrollbar()
22632263
\ wrap: true,
22642264
\ scrollbar: true,
22652265
\ mapping: false,
2266-
\ filter: Popup_filter,
2266+
\ filter: g:Popup_filter,
22672267
\ })
22682268
enddef
22692269

@@ -2328,7 +2328,7 @@ func Test_popup_scrollbar()
23282328
call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
23292329

23302330
" check size with non-wrapping lines
2331-
call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2331+
call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
23322332
call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
23332333

23342334
" check size with wrapping lines

src/testdir/test_vim9_assign.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def Test_assignment()
7575

7676
# lower case name is OK for a list
7777
var lambdaLines =<< trim END
78-
var lambdaList: list<func> = [Test_syntax]
78+
var lambdaList: list<func> = [g:Test_syntax]
7979
lambdaList[0] = () => "lambda"
8080
END
8181
v9.CheckDefAndScriptSuccess(lambdaLines)
@@ -890,7 +890,7 @@ enddef
890890

891891
def Test_assignment_partial()
892892
var lines =<< trim END
893-
var Partial: func(): string = function(PartFuncBool, [true])
893+
var Partial: func(): string = function(g:PartFuncBool, [true])
894894
assert_equal('done', Partial())
895895
END
896896
v9.CheckDefAndScriptSuccess(lines)
@@ -1393,7 +1393,7 @@ def Test_assignment_failure()
13931393
v9.CheckDefFailure(['var name: dict <number>'], 'E1068:')
13941394
v9.CheckDefFailure(['var name: dict<number'], 'E1009:')
13951395

1396-
assert_fails('s/^/\=Mess()/n', 'E794:')
1396+
assert_fails('s/^/\=g:Mess()/n', 'E794:')
13971397
v9.CheckDefFailure(['var name: dict<number'], 'E1009:')
13981398

13991399
v9.CheckDefFailure(['w:foo: number = 10'],

src/testdir/test_vim9_builtin.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ def Test_filter()
13031303
enddef
13041304

13051305
def Test_filter_wrong_dict_key_type()
1306-
assert_fails('Wrong_dict_key_type([1, v:null, 3])', 'E1013:')
1306+
assert_fails('g:Wrong_dict_key_type([1, v:null, 3])', 'E1013:')
13071307
enddef
13081308

13091309
def Test_filter_return_type()

src/testdir/test_vim9_cmd.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def Test_gdefault_not_used()
13241324
bwipe!
13251325
enddef
13261326

1327-
def g:SomeComplFunc(findstart: number, base: string): any
1327+
def s:SomeComplFunc(findstart: number, base: string): any
13281328
if findstart
13291329
return 0
13301330
else

src/testdir/test_vim9_disassemble.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,23 +887,23 @@ def Test_disassemble_call_default()
887887
enddef
888888

889889

890-
def HasEval()
890+
def s:HasEval()
891891
if has("eval")
892892
echo "yes"
893893
else
894894
echo "no"
895895
endif
896896
enddef
897897

898-
def HasNothing()
898+
def s:HasNothing()
899899
if has("nothing")
900900
echo "yes"
901901
else
902902
echo "no"
903903
endif
904904
enddef
905905

906-
def HasSomething()
906+
def s:HasSomething()
907907
if has("nothing")
908908
echo "nothing"
909909
elseif has("something")
@@ -915,7 +915,7 @@ def HasSomething()
915915
endif
916916
enddef
917917

918-
def HasGuiRunning()
918+
def s:HasGuiRunning()
919919
if has("gui_running")
920920
echo "yes"
921921
else
@@ -2487,7 +2487,7 @@ def Test_debug_for()
24872487
res)
24882488
enddef
24892489

2490-
func Legacy() dict
2490+
func s:Legacy() dict
24912491
echo 'legacy'
24922492
endfunc
24932493

@@ -2501,7 +2501,7 @@ def Test_disassemble_dict_stack()
25012501
assert_match('<SNR>\d*_UseMember\_s*' ..
25022502
'var d = {func: Legacy}\_s*' ..
25032503
'\d PUSHS "func"\_s*' ..
2504-
'\d PUSHFUNC "g:Legacy"\_s*' ..
2504+
'\d PUSHFUNC "<80><fd>R\d\+_Legacy"\_s*' ..
25052505
'\d NEWDICT size 1\_s*' ..
25062506
'\d STORE $0\_s*' ..
25072507

0 commit comments

Comments
 (0)