Skip to content

Commit eef2102

Browse files
committed
patch 8.2.1349: Vim9: can define a function with the name of an import
Problem: Vim9: can define a function with the name of an import. Solution: Disallow using an existing name. (closes #6585)
1 parent e4218b9 commit eef2102

5 files changed

Lines changed: 66 additions & 10 deletions

File tree

src/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,7 @@ EXTERN char e_missing_dict_colon[] INIT(= N_("E720: Missing colon in Dictionary:
17461746
EXTERN char e_duplicate_key[] INIT(= N_("E721: Duplicate key in Dictionary: \"%s\""));
17471747
EXTERN char e_missing_dict_comma[] INIT(= N_("E722: Missing comma in Dictionary: %s"));
17481748
EXTERN char e_missing_dict_end[] INIT(= N_("E723: Missing end of Dictionary '}': %s"));
1749+
EXTERN char e_already_defined[] INIT(= N_("E1073: name already defined: %s"));
17491750
#endif
17501751
#ifdef FEAT_CLIENTSERVER
17511752
EXTERN char e_invexprmsg[] INIT(= N_("E449: Invalid expression received"));

src/testdir/test_vim9_script.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,39 @@ def Test_import_compile_error()
16181618
delete('Ximport.vim')
16191619
enddef
16201620

1621+
def Test_func_overrules_import_fails()
1622+
let export_lines =<< trim END
1623+
vim9script
1624+
export def Func()
1625+
echo 'imported'
1626+
enddef
1627+
END
1628+
writefile(export_lines, 'XexportedFunc.vim')
1629+
1630+
let lines =<< trim END
1631+
vim9script
1632+
import Func from './XexportedFunc.vim'
1633+
def Func()
1634+
echo 'local to function'
1635+
enddef
1636+
END
1637+
CheckScriptFailure(lines, 'E1073:')
1638+
1639+
lines =<< trim END
1640+
vim9script
1641+
import Func from './XexportedFunc.vim'
1642+
def Outer()
1643+
def Func()
1644+
echo 'local to function'
1645+
enddef
1646+
enddef
1647+
defcompile
1648+
END
1649+
CheckScriptFailure(lines, 'E1073:')
1650+
1651+
delete('XexportedFunc.vim')
1652+
enddef
1653+
16211654
def Test_fixed_size_list()
16221655
# will be allocated as one piece of memory, check that changes work
16231656
let l = [1, 2, 3, 4]

src/userfunc.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,7 @@ def_function(exarg_T *eap, char_u *name_arg)
26522652
char_u *skip_until = NULL;
26532653
char_u *heredoc_trimmed = NULL;
26542654
int vim9script = in_vim9script();
2655+
imported_T *import = NULL;
26552656

26562657
/*
26572658
* ":function" without argument: list functions.
@@ -3235,17 +3236,29 @@ def_function(exarg_T *eap, char_u *name_arg)
32353236
}
32363237

32373238
fp = find_func_even_dead(name, is_global, NULL);
3238-
if (fp != NULL)
3239+
if (vim9script)
3240+
{
3241+
char_u *uname = untrans_function_name(name);
3242+
3243+
import = find_imported(uname == NULL ? name : uname, 0, NULL);
3244+
}
3245+
3246+
if (fp != NULL || import != NULL)
32393247
{
3240-
int dead = fp->uf_flags & FC_DEAD;
3248+
int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
32413249

32423250
// Function can be replaced with "function!" and when sourcing the
32433251
// same script again, but only once.
3244-
if (!dead && !eap->forceit
3252+
// A name that is used by an import can not be overruled.
3253+
if (import != NULL
3254+
|| (!dead && !eap->forceit
32453255
&& (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
3246-
|| fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
3256+
|| fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
32473257
{
3248-
emsg_funcname(e_funcexts, name);
3258+
if (vim9script)
3259+
emsg_funcname(e_already_defined, name);
3260+
else
3261+
emsg_funcname(e_funcexts, name);
32493262
goto erret;
32503263
}
32513264
if (fp->uf_calls > 0)

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+
1349,
757759
/**/
758760
1348,
759761
/**/

src/vim9compile.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,10 @@ check_defined(char_u *p, size_t len, cctx_T *cctx)
294294
if (lookup_script(p, len) == OK
295295
|| (cctx != NULL
296296
&& (lookup_local(p, len, cctx) != NULL
297-
|| find_imported(p, len, cctx) != NULL)))
297+
|| lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
298+
|| find_imported(p, len, cctx) != NULL)
298299
{
299-
semsg("E1073: imported name already defined: %s", p);
300+
semsg(_(e_already_defined), p);
300301
return FAIL;
301302
}
302303
return OK;
@@ -4899,17 +4900,21 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
48994900
int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
49004901
char_u *name_start = eap->arg;
49014902
char_u *name_end = to_name_end(eap->arg, is_global);
4902-
char_u *name = get_lambda_name();
4903+
char_u *lambda_name;
49034904
lvar_T *lvar;
49044905
ufunc_T *ufunc;
49054906
int r;
49064907

4908+
if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4909+
return NULL;
4910+
49074911
eap->arg = name_end;
49084912
eap->getline = exarg_getline;
49094913
eap->cookie = cctx;
49104914
eap->skip = cctx->ctx_skip == SKIP_YES;
49114915
eap->forceit = FALSE;
4912-
ufunc = def_function(eap, name);
4916+
lambda_name = get_lambda_name();
4917+
ufunc = def_function(eap, lambda_name);
49134918

49144919
if (ufunc == NULL)
49154920
return NULL;
@@ -4925,13 +4930,15 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
49254930
if (func_name == NULL)
49264931
r = FAIL;
49274932
else
4928-
r = generate_NEWFUNC(cctx, name, func_name);
4933+
r = generate_NEWFUNC(cctx, lambda_name, func_name);
49294934
}
49304935
else
49314936
{
49324937
// Define a local variable for the function reference.
49334938
lvar = reserve_local(cctx, name_start, name_end - name_start,
49344939
TRUE, ufunc->uf_func_type);
4940+
if (lvar == NULL)
4941+
return NULL;
49354942
if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
49364943
return NULL;
49374944
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);

0 commit comments

Comments
 (0)