]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0080: unexpected error for modifying final list using += v9.1.0080
authorYegappan Lakshmanan <yegappan@yahoo.com>
Tue, 6 Feb 2024 10:03:36 +0000 (11:03 +0100)
committerChristian Brabandt <cb@256bit.org>
Tue, 6 Feb 2024 10:03:36 +0000 (11:03 +0100)
Problem:  unexpected error for modifying final list using += operator
          (Ernie Rael)
Solution: Allow List value modification of a final variable using +=
          operator
          (Yegappan Lakshmanan)

fixes: #13745
fixes: #13959
closes: #13962

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/builtin.txt
runtime/doc/eval.txt
src/eval.c
src/evalvars.c
src/testdir/test_vim9_assign.vim
src/version.c
src/vim.h

index 19b9bc9f65bffaf2c011177b7b3ca4f1aa9c8482..062da08d6ce500e840993f802c1e989a8b2a48b2 100644 (file)
@@ -2093,7 +2093,7 @@ diff({fromlist}, {tolist} [, {options}])          *diff()*
                Returns an empty List or String if {fromlist} and {tolist} are
                identical.
 
-               Examples:
+               Examples: >
                    :echo diff(['abc'], ['xxx'])
                     @@ -1 +1 @@
                     -abc
@@ -2103,7 +2103,7 @@ diff({fromlist}, {tolist} [, {options}])          *diff()*
                     [{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1}]
                    :echo diff(readfile('oldfile'), readfile('newfile'))
                    :echo diff(getbufline(5, 1, '$'), getbufline(6, 1, '$'))
-
+<
                For more examples, refer to |diff-func-examples|
 
                Can also be used as a |method|: >
index 574b9c9dd97fa7157dfc373417674c56f0488cd8..75ea2c7f4260b4b73d1a5deb328a14a033c648ab 100644 (file)
@@ -1,4 +1,4 @@
-*eval.txt*     For Vim version 9.1.  Last change: 2024 Feb 05
+*eval.txt*     For Vim version 9.1.  Last change: 2024 Feb 06
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -425,6 +425,18 @@ To change part of a list you can specify the first and last item to be
 modified.  The value must at least have the number of items in the range: >
        :let list[3:5] = [3, 4, 5]
 
+To add items to a List in-place, you can use the |+=| operator: >
+       :let listA = [1, 2]
+       :let listA += [3, 4]
+<
+When two variables refer to the same List, changing one List in-place will
+cause the referenced List to be changed in-place: >
+       :let listA = [1, 2]
+       :let listB = listA
+       :let listB += [3, 4]
+       :echo listA
+       [1, 2, 3, 4]
+<
 Adding and removing items from a list is done with functions.  Here are a few
 examples: >
        :call insert(list, 'a')         " prepend item 'a'
index bf053dfb69494a057c3d36c4d40bfb7cd65f22a5..fd44db6f7e7e76e044ce87446db358f89fc7f02a 100644 (file)
@@ -1901,7 +1901,7 @@ set_var_lval(
                           && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
                        && tv_op(&tv, rettv, op) == OK)
                    set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
-                                                           ASSIGN_NO_DECL, 0);
+                               ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
                clear_tv(&tv);
            }
        }
index cbcf2a707ece39462f99642f1ef12d6ed20abc72..0a84afdaaf0ff7fc9f72445da65bc7178e8b4895 100644 (file)
@@ -3977,7 +3977,14 @@ set_var_const(
            if (check_typval_is_value(&di->di_tv) == FAIL)
                goto failed;
 
-           if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
+           // List and Blob types can be modified in-place using the "+="
+           // compound operator.  For other types, this is not allowed.
+           int type_inplace_modifiable =
+               (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
+
+           if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
+                   && ((flags & ASSIGN_COMPOUND_OP) == 0
+                       || !type_inplace_modifiable))
            {
                where_T where = WHERE_INIT;
                svar_T  *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
@@ -3998,7 +4005,11 @@ set_var_const(
                }
            }
 
-           if ((flags & ASSIGN_FOR_LOOP) == 0
+           // Modifying a final variable with a List value using the "+="
+           // operator is allowed.  For other types, it is not allowed.
+           if (((flags & ASSIGN_FOR_LOOP) == 0
+                       && ((flags & ASSIGN_COMPOUND_OP) == 0
+                           || !type_inplace_modifiable))
                                 ? var_check_permission(di, name) == FAIL
                                 : var_check_ro(di->di_flags, name, FALSE))
                goto failed;
index aef09c5f6b98d4aa0c90da9f5a3d26094639e2a5..4414f55c067fb1389a7c70b65c8626f2f2c408b1 100644 (file)
@@ -3484,4 +3484,101 @@ def Test_assign_type_to_list_dict()
   v9.CheckScriptFailure(lines, 'E1407: Cannot use a Typealias as a variable or value')
 enddef
 
+" Test for modifying a final variable using a compound operator
+def Test_final_var_modification_with_compound_op()
+  var lines =<< trim END
+    vim9script
+
+    final i: number = 1000
+    assert_fails('i += 2', 'E46: Cannot change read-only variable "i"')
+    assert_fails('i -= 2', 'E46: Cannot change read-only variable "i"')
+    assert_fails('i *= 2', 'E46: Cannot change read-only variable "i"')
+    assert_fails('i /= 2', 'E46: Cannot change read-only variable "i"')
+    assert_fails('i %= 2', 'E46: Cannot change read-only variable "i"')
+    assert_equal(1000, i)
+
+    final f: float = 1000.0
+    assert_fails('f += 2', 'E46: Cannot change read-only variable "f"')
+    assert_fails('f -= 2', 'E46: Cannot change read-only variable "f"')
+    assert_fails('f *= 2', 'E46: Cannot change read-only variable "f"')
+    assert_fails('f /= 2', 'E46: Cannot change read-only variable "f"')
+    assert_equal(1000.0, f)
+
+    final s: string = 'abc'
+    assert_fails('s ..= "y"', 'E46: Cannot change read-only variable "s"')
+    assert_equal('abc', s)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
+" Test for modifying a final variable with a List value
+def Test_final_var_with_list_value()
+  var lines =<< trim END
+    vim9script
+
+    final listA: list<string> = []
+    var listB = listA
+
+    listB->add('a')
+    assert_true(listA is listB)
+    assert_equal(['a'], listA)
+    assert_equal(['a'], listB)
+
+    listB += ['b']
+    assert_true(listA is listB)
+    assert_equal(['a', 'b'], listA)
+    assert_equal(['a', 'b'], listB)
+
+    listA->add('c')
+    assert_true(listA is listB)
+    assert_equal(['a', 'b', 'c'], listA)
+    assert_equal(['a', 'b', 'c'], listB)
+
+    listA += ['d']
+    assert_true(listA is listB)
+    assert_equal(['a', 'b', 'c', 'd'], listA)
+    assert_equal(['a', 'b', 'c', 'd'], listB)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
+" Test for modifying a final variable with a List value using "+=" from a legacy
+" function.
+func Test_final_var_with_list_value_legacy()
+  vim9cmd final g:TestVar = ['a']
+  vim9cmd g:TestVar += ['b']
+  call assert_equal(['a', 'b'], g:TestVar)
+endfunc
+
+" Test for modifying a final variable with a Blob value
+def Test_final_var_with_blob_value()
+  var lines =<< trim END
+    vim9script
+
+    final blobA: blob = 0z10
+    var blobB = blobA
+
+    blobB->add(32)
+    assert_true(blobA is blobB)
+    assert_equal(0z1020, blobA)
+    assert_equal(0z1020, blobB)
+
+    blobB += 0z30
+    assert_true(blobA is blobB)
+    assert_equal(0z102030, blobA)
+    assert_equal(0z102030, blobB)
+
+    blobA->add(64)
+    assert_true(blobA is blobB)
+    assert_equal(0z10203040, blobA)
+    assert_equal(0z10203040, blobB)
+
+    blobA += 0z50
+    assert_true(blobA is blobB)
+    assert_equal(0z1020304050, blobA)
+    assert_equal(0z1020304050, blobB)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 3d99917a75fb2536ecd051360765026949c4e103..36f9dc07ea9498a7a35915933dab9b06668abfc2 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    80,
 /**/
     79,
 /**/
index fe239581af803e40845ba41ab054bb2d4d71dadc..45cf8cad0d56e394fe685af0dab3d73b3e6cc073 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -2379,6 +2379,7 @@ typedef int (*opt_expand_cb_T)(optexpand_T *args, int *numMatches, char_u ***mat
 #define ASSIGN_FOR_LOOP 0x40 // assigning to loop variable
 #define ASSIGN_INIT    0x80 // not assigning a value, just a declaration
 #define ASSIGN_UPDATE_BLOCK_ID 0x100  // update sav_block_id
+#define ASSIGN_COMPOUND_OP 0x200  // compound operator e.g. "+="
 
 #include "ex_cmds.h"       // Ex command defines
 #include "spell.h"         // spell checking stuff