]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1483: += operator does not work on class member v9.0.1483
authorBram Moolenaar <Bram@vim.org>
Mon, 24 Apr 2023 16:15:25 +0000 (17:15 +0100)
committerBram Moolenaar <Bram@vim.org>
Mon, 24 Apr 2023 16:15:25 +0000 (17:15 +0100)
Problem:    += operator does not work on class member.
Solution:   Do not skip as if "this." was used. (Christian Brabandt,
            closes #12263)

src/testdir/test_vim9_class.vim
src/version.c
src/vim9compile.c

index a8f161d8980e31790f8a78fd0a3a739d40ef736c..1aee9e08dc12d42d64122d9f0426834e87d8c9c2 100644 (file)
@@ -401,6 +401,13 @@ def Test_assignment_with_operator()
       var f =  Foo.new(3)
       f.Add(17)
       assert_equal(20, f.x)
+
+      def AddToFoo(obj: Foo)
+        obj.x += 3
+      enddef
+
+      AddToFoo(f)
+      assert_equal(23, f.x)
   END
   v9.CheckScriptSuccess(lines)
 enddef
index aa480c1699686c63458b2acb0fb4879a81b77e05..842b5830e0a0e3470c5412cc957cef0b1f791d73 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1483,
 /**/
     1482,
 /**/
index e689c55cfd6230fe5238493da4455714ba9e4786..f045c74af0055cff4da036afaa5c7fd9e90364a1 100644 (file)
@@ -2065,16 +2065,31 @@ compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
 {
     if (lhs->lhs_type->tt_type == VAR_OBJECT)
     {
-       // "this.value": load "this" object and get the value at index
-       // for an object or class member get the type of the member
+       // "this.value": load "this" object and get the value at index for an
+       // object or class member get the type of the member.
+       // Also for "obj.value".
+       char_u *dot = vim_strchr(var_start, '.');
+       if (dot == NULL)
+           return FAIL;
+
        class_T *cl = lhs->lhs_type->tt_class;
-       type_T *type = class_member_type(cl, var_start + 5,
+       type_T *type = class_member_type(cl, dot + 1,
                                           lhs->lhs_end, &lhs->lhs_member_idx);
        if (lhs->lhs_member_idx < 0)
            return FAIL;
 
-       if (generate_LOAD(cctx, ISN_LOAD, 0, NULL, lhs->lhs_type) == FAIL)
-           return FAIL;
+       if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
+       {
+           // load "this"
+           if (generate_LOAD(cctx, ISN_LOAD, 0, NULL, lhs->lhs_type) == FAIL)
+               return FAIL;
+       }
+       else
+       {
+           // load object variable or argument
+           if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
+               return FAIL;
+       }
        if (cl->class_flags & CLASS_INTERFACE)
            return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
        return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
@@ -2169,7 +2184,7 @@ compile_assign_unlet(
     if (cctx->ctx_skip == SKIP_YES)
        return OK;
 
-    // Load the dict or list.  On the stack we then have:
+    // Load the dict, list or object.  On the stack we then have:
     // - value (for assignment, not for :unlet)
     // - index
     // - for [a : b] second index
@@ -2731,7 +2746,7 @@ compile_assignment(
        if (lhs.lhs_has_index)
        {
            // Use the info in "lhs" to store the value at the index in the
-           // list or dict.
+           // list, dict or object.
            if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
                                                                       == FAIL)
            {