• R/O
  • SSH

vim: Commit

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


Commit MetaInfo

Révision04af0c68dba8542d6325fb7dea3220f596b01615 (tree)
l'heure2022-01-17 04:45:02
AuteurBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Message de Log

patch 8.2.4115: cannot use a method with a complex expression

Commit: https://github.com/vim/vim/commit/c665dabdf4c49a0fbf1dc566253c75c2abe2effa
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jan 16 19:38:07 2022 +0000

patch 8.2.4115: cannot use a method with a complex expression
Problem: Cannot use a method with a complex expression.
Solution: Evaluate the expression after "->" and use the result.

Change Summary

Modification

diff -r e3eab17e6c74 -r 04af0c68dba8 src/errors.h
--- a/src/errors.h Sun Jan 16 19:15:04 2022 +0100
+++ b/src/errors.h Sun Jan 16 20:45:02 2022 +0100
@@ -3212,4 +3212,6 @@
32123212 INIT(= N_("E1263: Using autoload in a script not under an autoload directory"));
32133213 EXTERN char e_autoload_import_cannot_use_absolute_or_relative_path[]
32143214 INIT(= N_("E1264: Autoload import cannot use absolute or relative path: %s"));
3215+EXTERN char e_cannot_use_partial_here[]
3216+ INIT(= N_("E1265: Cannot use a partial here"));
32153217 #endif
diff -r e3eab17e6c74 -r 04af0c68dba8 src/eval.c
--- a/src/eval.c Sun Jan 16 19:15:04 2022 +0100
+++ b/src/eval.c Sun Jan 16 20:45:02 2022 +0100
@@ -3948,6 +3948,7 @@
39483948 char_u *name;
39493949 long len;
39503950 char_u *alias;
3951+ char_u *tofree = NULL;
39513952 typval_T base = *rettv;
39523953 int ret = OK;
39533954 int evaluate = evalarg != NULL
@@ -3968,67 +3969,68 @@
39683969 }
39693970 else
39703971 {
3971- if (**arg == '.')
3972+ char_u *paren;
3973+
3974+ // If there is no "(" immediately following, but there is further on,
3975+ // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
3976+ // Does not handle anything where "(" is part of the expression.
3977+ *arg = skipwhite(*arg);
3978+
3979+ if (**arg != '(' && alias == NULL
3980+ && (paren = vim_strchr(*arg, '(')) != NULL)
39723981 {
3973- int len2;
3974- char_u *fname;
3975- int idx;
3976- imported_T *import = find_imported(name, len, TRUE,
3977- evalarg == NULL ? NULL : evalarg->eval_cctx);
3978- type_T *type;
3979-
3980- // value->import.func()
3981- if (import != NULL)
3982+ typval_T ref;
3983+
3984+ *arg = name;
3985+ *paren = NUL;
3986+ ref.v_type = VAR_UNKNOWN;
3987+ if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
39823988 {
3983- name = NULL;
3984- ++*arg;
3985- fname = *arg;
3986- len2 = get_name_len(arg, &alias, evaluate, TRUE);
3987- if (len2 <= 0)
3989+ *arg = name + len;
3990+ ret = FAIL;
3991+ }
3992+ else if (*skipwhite(*arg) != NUL)
3993+ {
3994+ if (verbose)
3995+ semsg(_(e_trailing_characters_str), *arg);
3996+ ret = FAIL;
3997+ }
3998+ else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
3999+ {
4000+ name = ref.vval.v_string;
4001+ ref.vval.v_string = NULL;
4002+ tofree = name;
4003+ len = STRLEN(name);
4004+ }
4005+ else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
4006+ {
4007+ if (ref.vval.v_partial->pt_argc > 0
4008+ || ref.vval.v_partial->pt_dict != NULL)
39884009 {
3989- if (verbose)
3990- emsg(_(e_missing_name_after_dot));
4010+ emsg(_(e_cannot_use_partial_here));
39914011 ret = FAIL;
39924012 }
3993- else if (evaluate)
4013+ else
39944014 {
3995- int cc = fname[len2];
3996- ufunc_T *ufunc;
3997-
3998- fname[len2] = NUL;
3999- idx = find_exported(import->imp_sid, fname, &ufunc, &type,
4000- evalarg->eval_cctx, verbose);
4001- fname[len2] = cc;
4002-
4003- if (idx >= 0)
4015+ name = vim_strsave(partial_name(ref.vval.v_partial));
4016+ tofree = name;
4017+ if (name == NULL)
40044018 {
4005- scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4006- svar_T *sv =
4007- ((svar_T *)si->sn_var_vals.ga_data) + idx;
4008-
4009- if (sv->sv_tv->v_type == VAR_FUNC
4010- && sv->sv_tv->vval.v_string != NULL)
4011- {
4012- name = sv->sv_tv->vval.v_string;
4013- len = STRLEN(name);
4014- }
4015- else
4016- {
4017- // TODO: how about a partial?
4018- if (verbose)
4019- semsg(_(e_not_callable_type_str), fname);
4020- ret = FAIL;
4021- }
4022- }
4023- else if (ufunc != NULL)
4024- {
4025- name = ufunc->uf_name;
4026- len = STRLEN(name);
4019+ ret = FAIL;
4020+ name = *arg;
40274021 }
40284022 else
4029- ret = FAIL;
4023+ len = STRLEN(name);
40304024 }
40314025 }
4026+ else
4027+ {
4028+ if (verbose)
4029+ semsg(_(e_not_callable_type_str), name);
4030+ ret = FAIL;
4031+ }
4032+ clear_tv(&ref);
4033+ *paren = '(';
40324034 }
40334035
40344036 if (ret == OK)
@@ -4057,6 +4059,7 @@
40574059 // evaluating the arguments is possible (see test55).
40584060 if (evaluate)
40594061 clear_tv(&base);
4062+ vim_free(tofree);
40604063
40614064 return ret;
40624065 }
diff -r e3eab17e6c74 -r 04af0c68dba8 src/testdir/test_vim9_expr.vim
--- a/src/testdir/test_vim9_expr.vim Sun Jan 16 19:15:04 2022 +0100
+++ b/src/testdir/test_vim9_expr.vim Sun Jan 16 20:45:02 2022 +0100
@@ -3136,16 +3136,37 @@
31363136 var sorted = [3, 1, 2]
31373137 -> sort()
31383138 assert_equal([1, 2, 3], sorted)
3139-
3139+ END
3140+ CheckDefAndScriptSuccess(lines)
3141+
3142+ lines =<< trim END
3143+ vim9script
31403144 def SetNumber(n: number)
31413145 g:number = n
31423146 enddef
31433147 const Setit = SetNumber
31443148 len('text')->Setit()
31453149 assert_equal(4, g:number)
3150+
3151+ const SetFuncref = funcref(SetNumber)
3152+ len('longer')->SetFuncref()
3153+ assert_equal(6, g:number)
3154+
3155+ const SetList = [SetNumber, SetFuncref]
3156+ len('xx')->SetList[0]()
3157+ assert_equal(2, g:number)
3158+ len('xxx')->SetList[1]()
3159+ assert_equal(3, g:number)
3160+
3161+ const SetDict = {key: SetNumber}
3162+ len('xxxx')->SetDict['key']()
3163+ assert_equal(4, g:number)
3164+ len('xxxxx')->SetDict.key()
3165+ assert_equal(5, g:number)
3166+
31463167 unlet g:number
31473168 END
3148- CheckDefAndScriptSuccess(lines)
3169+ CheckScriptSuccess(lines) # TODO: CheckDefAndScriptSuccess()
31493170
31503171 lines =<< trim END
31513172 def RetVoid()
diff -r e3eab17e6c74 -r 04af0c68dba8 src/version.c
--- a/src/version.c Sun Jan 16 19:15:04 2022 +0100
+++ b/src/version.c Sun Jan 16 20:45:02 2022 +0100
@@ -751,6 +751,8 @@
751751 static int included_patches[] =
752752 { /* Add new patch number below this line */
753753 /**/
754+ 4115,
755+/**/
754756 4114,
755757 /**/
756758 4113,
Afficher sur ancien navigateur de dépôt.