• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revision5470c36ed7e6bf4e7255c8898fce8d8c0319f5d5 (tree)
Time2020-08-02 02:00:03
AuthorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Log Message

patch 8.2.1343: Vim9: cannot find global function when using g:

Commit: https://github.com/vim/vim/commit/333894b195479c9304a19f4e6ec1a9c09ecf07e4
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Aug 1 18:53:07 2020 +0200

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:.

Change Summary

Incremental Difference

diff -r 31cfab8f9ced -r 5470c36ed7e6 src/testdir/test_vim9_func.vim
--- a/src/testdir/test_vim9_func.vim Sat Aug 01 17:15:04 2020 +0200
+++ b/src/testdir/test_vim9_func.vim Sat Aug 01 19:00:03 2020 +0200
@@ -161,6 +161,21 @@
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
diff -r 31cfab8f9ced -r 5470c36ed7e6 src/userfunc.c
--- a/src/userfunc.c Sat Aug 01 17:15:04 2020 +0200
+++ b/src/userfunc.c Sat Aug 01 19:00:03 2020 +0200
@@ -789,9 +789,10 @@
789789
790790 if (!is_global)
791791 {
792- char_u *after_script = NULL;
793-
794- if (in_vim9script())
792+ int vim9script = in_vim9script();
793+ char_u *after_script = NULL;
794+
795+ if (vim9script)
795796 {
796797 // Find script-local function before global one.
797798 func = find_func_with_sid(name, current_sctx.sc_sid);
@@ -799,7 +800,7 @@
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 @@
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 @@
20862087 if (error == FCERR_NONE && funcexe->evaluate)
20872088 {
20882089 char_u *rfname = fname;
2089-
2090- // Ignore "g:" before a function name.
2090+ int is_global = FALSE;
2091+
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 @@
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 @@
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 @@
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 @@
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
diff -r 31cfab8f9ced -r 5470c36ed7e6 src/version.c
--- a/src/version.c Sat Aug 01 17:15:04 2020 +0200
+++ b/src/version.c Sat Aug 01 19:00:03 2020 +0200
@@ -755,6 +755,8 @@
755755 static int included_patches[] =
756756 { /* Add new patch number below this line */
757757 /**/
758+ 1343,
759+/**/
758760 1342,
759761 /**/
760762 1341,
Show on old repository browser