To: vim_dev@googlegroups.com Subject: Patch 8.2.1349 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1349 Problem: Vim9: can define a function with the name of an import. Solution: Disallow using an existing name. (closes #6585) Files: src/userfunc.c, src/vim9compile.c, src/globals.h, src/testdir/test_vim9_script.vim *** ../vim-8.2.1348/src/userfunc.c 2020-08-01 18:53:04.934467105 +0200 --- src/userfunc.c 2020-08-01 21:58:25.193299761 +0200 *************** *** 2652,2657 **** --- 2652,2658 ---- char_u *skip_until = NULL; char_u *heredoc_trimmed = NULL; int vim9script = in_vim9script(); + imported_T *import = NULL; /* * ":function" without argument: list functions. *************** *** 3235,3251 **** } fp = find_func_even_dead(name, is_global, NULL); ! if (fp != NULL) { ! int dead = fp->uf_flags & FC_DEAD; // Function can be replaced with "function!" and when sourcing the // same script again, but only once. ! if (!dead && !eap->forceit && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid ! || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)) { ! emsg_funcname(e_funcexts, name); goto erret; } if (fp->uf_calls > 0) --- 3236,3264 ---- } fp = find_func_even_dead(name, is_global, NULL); ! if (vim9script) { ! char_u *uname = untrans_function_name(name); ! ! import = find_imported(uname == NULL ? name : uname, 0, NULL); ! } ! ! if (fp != NULL || import != NULL) ! { ! int dead = fp != NULL && (fp->uf_flags & FC_DEAD); // Function can be replaced with "function!" and when sourcing the // same script again, but only once. ! // A name that is used by an import can not be overruled. ! if (import != NULL ! || (!dead && !eap->forceit && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid ! || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))) { ! if (vim9script) ! emsg_funcname(e_already_defined, name); ! else ! emsg_funcname(e_funcexts, name); goto erret; } if (fp->uf_calls > 0) *** ../vim-8.2.1348/src/vim9compile.c 2020-08-01 17:00:00.173379358 +0200 --- src/vim9compile.c 2020-08-01 21:57:08.289610807 +0200 *************** *** 294,302 **** if (lookup_script(p, len) == OK || (cctx != NULL && (lookup_local(p, len, cctx) != NULL ! || find_imported(p, len, cctx) != NULL))) { ! semsg("E1073: imported name already defined: %s", p); return FAIL; } return OK; --- 294,303 ---- if (lookup_script(p, len) == OK || (cctx != NULL && (lookup_local(p, len, cctx) != NULL ! || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK)) ! || find_imported(p, len, cctx) != NULL) { ! semsg(_(e_already_defined), p); return FAIL; } return OK; *************** *** 4899,4915 **** int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; char_u *name_start = eap->arg; char_u *name_end = to_name_end(eap->arg, is_global); ! char_u *name = get_lambda_name(); lvar_T *lvar; ufunc_T *ufunc; int r; eap->arg = name_end; eap->getline = exarg_getline; eap->cookie = cctx; eap->skip = cctx->ctx_skip == SKIP_YES; eap->forceit = FALSE; ! ufunc = def_function(eap, name); if (ufunc == NULL) return NULL; --- 4900,4920 ---- int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; char_u *name_start = eap->arg; char_u *name_end = to_name_end(eap->arg, is_global); ! char_u *lambda_name; lvar_T *lvar; ufunc_T *ufunc; int r; + if (check_defined(name_start, name_end - name_start, cctx) == FAIL) + return NULL; + eap->arg = name_end; eap->getline = exarg_getline; eap->cookie = cctx; eap->skip = cctx->ctx_skip == SKIP_YES; eap->forceit = FALSE; ! lambda_name = get_lambda_name(); ! ufunc = def_function(eap, lambda_name); if (ufunc == NULL) return NULL; *************** *** 4925,4937 **** if (func_name == NULL) r = FAIL; else ! r = generate_NEWFUNC(cctx, name, func_name); } else { // Define a local variable for the function reference. lvar = reserve_local(cctx, name_start, name_end - name_start, TRUE, ufunc->uf_func_type); if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL) return NULL; r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); --- 4930,4944 ---- if (func_name == NULL) r = FAIL; else ! r = generate_NEWFUNC(cctx, lambda_name, func_name); } else { // Define a local variable for the function reference. lvar = reserve_local(cctx, name_start, name_end - name_start, TRUE, ufunc->uf_func_type); + if (lvar == NULL) + return NULL; if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL) return NULL; r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); *** ../vim-8.2.1348/src/globals.h 2020-08-01 16:35:04.365552564 +0200 --- src/globals.h 2020-08-01 21:58:37.829248193 +0200 *************** *** 1746,1751 **** --- 1746,1752 ---- EXTERN char e_duplicate_key[] INIT(= N_("E721: Duplicate key in Dictionary: \"%s\"")); EXTERN char e_missing_dict_comma[] INIT(= N_("E722: Missing comma in Dictionary: %s")); EXTERN char e_missing_dict_end[] INIT(= N_("E723: Missing end of Dictionary '}': %s")); + EXTERN char e_already_defined[] INIT(= N_("E1073: name already defined: %s")); #endif #ifdef FEAT_CLIENTSERVER EXTERN char e_invexprmsg[] INIT(= N_("E449: Invalid expression received")); *** ../vim-8.2.1348/src/testdir/test_vim9_script.vim 2020-08-01 17:00:00.173379358 +0200 --- src/testdir/test_vim9_script.vim 2020-08-01 22:04:50.771686424 +0200 *************** *** 1618,1623 **** --- 1618,1656 ---- delete('Ximport.vim') enddef + def Test_func_overrules_import_fails() + let export_lines =<< trim END + vim9script + export def Func() + echo 'imported' + enddef + END + writefile(export_lines, 'XexportedFunc.vim') + + let lines =<< trim END + vim9script + import Func from './XexportedFunc.vim' + def Func() + echo 'local to function' + enddef + END + CheckScriptFailure(lines, 'E1073:') + + lines =<< trim END + vim9script + import Func from './XexportedFunc.vim' + def Outer() + def Func() + echo 'local to function' + enddef + enddef + defcompile + END + CheckScriptFailure(lines, 'E1073:') + + delete('XexportedFunc.vim') + enddef + def Test_fixed_size_list() # will be allocated as one piece of memory, check that changes work let l = [1, 2, 3, 4] *** ../vim-8.2.1348/src/version.c 2020-08-01 21:11:35.028952048 +0200 --- src/version.c 2020-08-01 21:30:56.255837527 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1349, /**/ -- hundred-and-one symptoms of being an internet addict: 103. When you find yourself in the "Computer" section of Barnes & Noble enjoying yourself. /// 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 ///