• R/O
  • SSH

vim: Commit

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


Commit MetaInfo

Revision7b5b9558500a62a53992edccff3a4e229b9d3c65 (tree)
Zeit2020-08-02 05:30:04
AutorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Log Message

patch 8.2.1349: Vim9: can define a function with the name of an import

Commit: https://github.com/vim/vim/commit/eef2102e20d24f5fbd1c9f53c7a35df61585c5ab
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Aug 1 22:16:43 2020 +0200

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 https://github.com/vim/vim/issues/6585)

Ändern Zusammenfassung

Diff

diff -r b26b1529cb9b -r 7b5b9558500a src/globals.h
--- a/src/globals.h Sat Aug 01 21:15:04 2020 +0200
+++ b/src/globals.h Sat Aug 01 22:30:04 2020 +0200
@@ -1746,6 +1746,7 @@
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"));
diff -r b26b1529cb9b -r 7b5b9558500a src/testdir/test_vim9_script.vim
--- a/src/testdir/test_vim9_script.vim Sat Aug 01 21:15:04 2020 +0200
+++ b/src/testdir/test_vim9_script.vim Sat Aug 01 22:30:04 2020 +0200
@@ -1618,6 +1618,39 @@
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]
diff -r b26b1529cb9b -r 7b5b9558500a src/userfunc.c
--- a/src/userfunc.c Sat Aug 01 21:15:04 2020 +0200
+++ b/src/userfunc.c Sat Aug 01 22:30:04 2020 +0200
@@ -2652,6 +2652,7 @@
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 @@
32353236 }
32363237
32373238 fp = find_func_even_dead(name, is_global, NULL);
3238- if (fp != NULL)
3239+ if (vim9script)
32393240 {
3240- int dead = fp->uf_flags & FC_DEAD;
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)
3247+ {
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)
diff -r b26b1529cb9b -r 7b5b9558500a src/version.c
--- a/src/version.c Sat Aug 01 21:15:04 2020 +0200
+++ b/src/version.c Sat Aug 01 22:30:04 2020 +0200
@@ -755,6 +755,8 @@
755755 static int included_patches[] =
756756 { /* Add new patch number below this line */
757757 /**/
758+ 1349,
759+/**/
758760 1348,
759761 /**/
760762 1347,
diff -r b26b1529cb9b -r 7b5b9558500a src/vim9compile.c
--- a/src/vim9compile.c Sat Aug 01 21:15:04 2020 +0200
+++ b/src/vim9compile.c Sat Aug 01 22:30:04 2020 +0200
@@ -294,9 +294,10 @@
294294 if (lookup_script(p, len) == OK
295295 || (cctx != NULL
296296 && (lookup_local(p, len, cctx) != NULL
297- || find_imported(p, len, cctx) != NULL)))
298- {
299- semsg("E1073: imported name already defined: %s", p);
297+ || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
298+ || find_imported(p, len, cctx) != NULL)
299+ {
300+ semsg(_(e_already_defined), p);
300301 return FAIL;
301302 }
302303 return OK;
@@ -4899,17 +4900,21 @@
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 @@
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);
Show on old repository browser