0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

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)
This commit is contained in:
Bram Moolenaar
2022-01-30 15:28:30 +00:00
parent 06011e1a55
commit 848faddb87
16 changed files with 102 additions and 54 deletions

View File

@@ -1902,7 +1902,7 @@ find_func_with_prefix(char_u *name, int sid)
char_u buffer[200];
scriptitem_T *si;
if (vim_strchr(name, AUTOLOAD_CHAR) != 0)
if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
return NULL; // already has the prefix
if (!SCRIPT_ID_VALID(sid))
return NULL; // not in a script
@@ -2004,6 +2004,17 @@ func_is_global(ufunc_T *ufunc)
return ufunc->uf_name[0] != K_SPECIAL;
}
/*
* Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
*/
int
func_requires_g_prefix(ufunc_T *ufunc)
{
return ufunc->uf_name[0] != K_SPECIAL
&& (ufunc->uf_flags & FC_LAMBDA) == 0
&& vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
}
/*
* Copy the function name of "fp" to buffer "buf".
* "buf" must be able to hold the function name plus three bytes.
@@ -3442,7 +3453,14 @@ call_func(
* User defined function.
*/
if (fp == NULL)
{
fp = find_func(rfname, is_global);
if (fp != NULL && !is_global && in_vim9script()
&& func_requires_g_prefix(fp))
// In Vim9 script g: is required to find a global
// non-autoload function.
fp = NULL;
}
// Trigger FuncUndefined event, may load the function.
if (fp == NULL
@@ -3672,6 +3690,7 @@ trans_function_name(
char_u sid_buf[20];
int len;
int extra = 0;
int prefix_g = FALSE;
lval_T lv;
int vim9script;
@@ -3837,8 +3856,20 @@ trans_function_name(
// skip over "s:" and "g:"
if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
{
if (is_global != NULL && lv.ll_name[0] == 'g')
*is_global = TRUE;
if (lv.ll_name[0] == 'g')
{
if (is_global != NULL)
{
*is_global = TRUE;
}
else
{
// dropping "g:" without setting "is_global" won't work in
// Vim9script, put it back later
prefix_g = TRUE;
extra = 2;
}
}
lv.ll_name += 2;
}
len = (int)(end - lv.ll_name);
@@ -3919,6 +3950,11 @@ trans_function_name(
if (vim9script || lead > 3) // If it's "<SID>"
STRCPY(name + 3, sid_buf);
}
else if (prefix_g)
{
name[0] = 'g';
name[1] = ':';
}
mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
name[lead + extra + len] = NUL;
}