patch 7.4.1836
Problem: When using a partial on a dictionary it always gets bound to that
dictionary.
Solution: Make a difference between binding a function to a dictionary
explicitly or automatically.
diff --git a/src/eval.c b/src/eval.c
index 005485d..ce83143 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -9069,14 +9069,12 @@
if (partial != NULL)
{
- if (partial->pt_dict != NULL)
- {
- /* When the function has a partial with a dict and there is a dict
- * argument, use the dict argument. That is backwards compatible.
- */
- if (selfdict_in == NULL)
- selfdict = partial->pt_dict;
- }
+ /* When the function has a partial with a dict and there is a dict
+ * argument, use the dict argument. That is backwards compatible.
+ * When the dict was bound explicitly use the one from the partial. */
+ if (partial->pt_dict != NULL
+ && (selfdict_in == NULL || !partial->pt_auto))
+ selfdict = partial->pt_dict;
if (error == ERROR_NONE && partial->pt_argc > 0)
{
for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
@@ -12330,12 +12328,16 @@
* use "dict". That is backwards compatible. */
if (dict_idx > 0)
{
+ /* The dict is bound explicitly, pt_auto is FALSE. */
pt->pt_dict = argvars[dict_idx].vval.v_dict;
++pt->pt_dict->dv_refcount;
}
else if (arg_pt != NULL)
{
+ /* If the dict was bound automatically the result is also
+ * bound automatically. */
pt->pt_dict = arg_pt->pt_dict;
+ pt->pt_auto = arg_pt->pt_auto;
if (pt->pt_dict != NULL)
++pt->pt_dict->dv_refcount;
}
@@ -22279,8 +22281,14 @@
}
}
- if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
- && selfdict != NULL)
+ /* Turn "dict.Func" into a partial for "Func" bound to "dict".
+ * Don't do this when "Func" is already a partial that was bound
+ * explicitly (pt_auto is FALSE). */
+ if (selfdict != NULL
+ && (rettv->v_type == VAR_FUNC
+ || (rettv->v_type == VAR_PARTIAL
+ && (rettv->vval.v_partial->pt_auto
+ || rettv->vval.v_partial->pt_dict == NULL))))
{
char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
: rettv->vval.v_partial->pt_name;
@@ -22294,7 +22302,6 @@
fp = find_func(fname);
vim_free(tofree);
- /* Turn "dict.Func" into a partial for "Func" with "dict". */
if (fp != NULL && (fp->uf_flags & FC_DICT))
{
partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
@@ -22303,6 +22310,7 @@
{
pt->pt_refcount = 1;
pt->pt_dict = selfdict;
+ pt->pt_auto = TRUE;
selfdict = NULL;
if (rettv->v_type == VAR_FUNC)
{
diff --git a/src/structs.h b/src/structs.h
index 24d819b..12a8a43 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1261,6 +1261,8 @@
{
int pt_refcount; /* reference count */
char_u *pt_name; /* function name */
+ int pt_auto; /* when TRUE the partial was created for using
+ dict.member in handle_subscript() */
int pt_argc; /* number of arguments */
typval_T *pt_argv; /* arguments in allocated array */
dict_T *pt_dict; /* dict for "self" */
diff --git a/src/testdir/test_partial.vim b/src/testdir/test_partial.vim
index 5f4a48f..8b11200 100644
--- a/src/testdir/test_partial.vim
+++ b/src/testdir/test_partial.vim
@@ -257,3 +257,25 @@
call job_setoptions(g:ref_job, {'exit_cb': function('string', [], d)})
endif
endfunc
+
+func Test_auto_partial_rebind()
+ let dict1 = {'name': 'dict1'}
+ func! dict1.f1()
+ return self.name
+ endfunc
+ let dict1.f2 = function(dict1.f1, dict1)
+
+ call assert_equal('dict1', dict1.f1())
+ call assert_equal('dict1', dict1['f1']())
+ call assert_equal('dict1', dict1.f2())
+ call assert_equal('dict1', dict1['f2']())
+
+ let dict2 = {'name': 'dict2'}
+ let dict2.f1 = dict1.f1
+ let dict2.f2 = dict1.f2
+
+ call assert_equal('dict2', dict2.f1())
+ call assert_equal('dict2', dict2['f1']())
+ call assert_equal('dict1', dict2.f2())
+ call assert_equal('dict1', dict2['f2']())
+endfunc
diff --git a/src/version.c b/src/version.c
index 166a9de..8b92714 100644
--- a/src/version.c
+++ b/src/version.c
@@ -754,6 +754,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1836,
+/**/
1835,
/**/
1834,