To: vim_dev@googlegroups.com Subject: Patch 8.2.1343 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1343 Problem: Vim9: cannot find global function when using g: when local function with the same name exists. Solution: Find global function when using g:. Files: src/userfunc.c, src/testdir/test_vim9_func.vim *** ../vim-8.2.1342/src/userfunc.c 2020-08-01 00:03:02.972887783 +0200 --- src/userfunc.c 2020-08-01 18:51:22.670926109 +0200 *************** *** 789,797 **** if (!is_global) { ! char_u *after_script = NULL; ! if (in_vim9script()) { // Find script-local function before global one. func = find_func_with_sid(name, current_sctx.sc_sid); --- 789,798 ---- if (!is_global) { ! int vim9script = in_vim9script(); ! char_u *after_script = NULL; ! if (vim9script) { // Find script-local function before global one. func = find_func_with_sid(name, current_sctx.sc_sid); *************** *** 799,805 **** return func; } ! if (!in_vim9script() && name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR) --- 800,806 ---- return func; } ! if (!vim9script && name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR) *************** *** 815,821 **** else after_script = NULL; } ! if (in_vim9script() || after_script != NULL) { // Find imported function before global one. imported = find_imported( --- 816,822 ---- else after_script = NULL; } ! if (vim9script || after_script != NULL) { // Find imported function before global one. imported = find_imported( *************** *** 2086,2095 **** if (error == FCERR_NONE && funcexe->evaluate) { char_u *rfname = fname; ! // Ignore "g:" before a function name. if (fp == NULL && fname[0] == 'g' && fname[1] == ':') rfname = fname + 2; rettv->v_type = VAR_NUMBER; // default rettv is number zero rettv->vval.v_number = 0; --- 2087,2100 ---- if (error == FCERR_NONE && funcexe->evaluate) { char_u *rfname = fname; + int is_global = FALSE; ! // Skip "g:" before a function name. if (fp == NULL && fname[0] == 'g' && fname[1] == ':') + { + is_global = TRUE; rfname = fname + 2; + } rettv->v_type = VAR_NUMBER; // default rettv is number zero rettv->vval.v_number = 0; *************** *** 2101,2107 **** * User defined function. */ if (fp == NULL) ! fp = find_func(rfname, FALSE, NULL); // Trigger FuncUndefined event, may load the function. if (fp == NULL --- 2106,2112 ---- * User defined function. */ if (fp == NULL) ! fp = find_func(rfname, is_global, NULL); // Trigger FuncUndefined event, may load the function. if (fp == NULL *************** *** 2110,2122 **** && !aborting()) { // executed an autocommand, search for the function again ! fp = find_func(rfname, FALSE, NULL); } // Try loading a package. if (fp == NULL && script_autoload(rfname, TRUE) && !aborting()) { // loaded a package, search for the function again ! fp = find_func(rfname, FALSE, NULL); } if (fp == NULL) { --- 2115,2127 ---- && !aborting()) { // executed an autocommand, search for the function again ! fp = find_func(rfname, is_global, NULL); } // Try loading a package. if (fp == NULL && script_autoload(rfname, TRUE) && !aborting()) { // loaded a package, search for the function again ! fp = find_func(rfname, is_global, NULL); } if (fp == NULL) { *************** *** 2125,2131 **** // If using Vim9 script try not local to the script. // TODO: should not do this if the name started with "s:". if (p != NULL) ! fp = find_func(p, FALSE, NULL); } if (fp != NULL && (fp->uf_flags & FC_DELETED)) --- 2130,2136 ---- // If using Vim9 script try not local to the script. // TODO: should not do this if the name started with "s:". if (p != NULL) ! fp = find_func(p, is_global, NULL); } if (fp != NULL && (fp->uf_flags & FC_DELETED)) *************** *** 2175,2180 **** --- 2180,2186 ---- */ error = call_internal_func(fname, argcount, argvars, rettv); } + /* * The function call (or "FuncUndefined" autocommand sequence) might * have been aborted by an error, an interrupt, or an explicitly thrown *** ../vim-8.2.1342/src/testdir/test_vim9_func.vim 2020-08-01 00:03:02.972887783 +0200 --- src/testdir/test_vim9_func.vim 2020-08-01 18:52:57.206492566 +0200 *************** *** 161,166 **** --- 161,181 ---- CheckScriptSuccess(lines) enddef + def Test_global_local_function() + let lines =<< trim END + vim9script + def g:Func(): string + return 'global' + enddef + def Func(): string + return 'local' + enddef + assert_equal('global', g:Func()) + assert_equal('local', Func()) + END + CheckScriptSuccess(lines) + enddef + func TakesOneArg(arg) echo a:arg endfunc *** ../vim-8.2.1342/src/version.c 2020-08-01 17:00:00.173379358 +0200 --- src/version.c 2020-08-01 18:41:44.933991531 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1343, /**/ -- hundred-and-one symptoms of being an internet addict: 100. The most exciting sporting events you noticed during summer 1996 was Netscape vs. Microsoft. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///