commit 47132823ab1c4f458c945df89d12e897d77db8a8
parent 7ac63906eae92b5d31fc7e380ef05b8bce73ad8b
Author: zeertzjq <zeertzjq@outlook.com>
Date: Thu, 4 May 2023 16:12:52 +0800
vim-patch:8.2.3336: behavior of negative index in list change changed
Problem: Behavior of negative index in list change changed. (Naruhiko
Nishino)
Solution: Only change it for Vim9 script. (closes vim/vim#8749)
https://github.com/vim/vim/commit/92f05f21afdb8a43581554a252cb2fc050f9e03b
Co-authored-by: Bram Moolenaar <Bram@vim.org>
Diffstat:
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
@@ -787,15 +787,15 @@ int tv_list_slice_or_index(list_T *list, bool range, int n1_arg, int n2_arg, typ
n1 = len + n1;
}
if (n1 < 0 || n1 >= len) {
- // For a range we allow invalid values and return an empty
- // list. A list index out of range is an error.
+ // For a range we allow invalid values and return an empty list.
+ // A list index out of range is an error.
if (!range) {
if (verbose) {
semsg(_(e_listidx), (int64_t)n1);
}
return FAIL;
}
- n1 = n1 < 0 ? 0 : len;
+ n1 = len;
}
if (range) {
if (n2 < 0) {
diff --git a/test/old/testdir/test_listdict.vim b/test/old/testdir/test_listdict.vim
@@ -1,5 +1,7 @@
" Tests for the List and Dict types
+source vim9.vim
+
func TearDown()
" Run garbage collection after every test
call test_garbagecollect_now()
@@ -37,6 +39,23 @@ func Test_list_slice()
let l[:1] += [1, 2]
let l[2:] -= [1]
call assert_equal([2, 4, 2], l)
+
+ let lines =<< trim END
+ VAR l = [1, 2]
+ call assert_equal([1, 2], l[:])
+ call assert_equal([2], l[-1 : -1])
+ call assert_equal([1, 2], l[-2 : -1])
+ END
+ call CheckLegacyAndVim9Success(lines)
+
+ let l = [1, 2]
+ call assert_equal([], l[-3 : -1])
+
+ let lines =<< trim END
+ var l = [1, 2]
+ assert_equal([1, 2], l[-3 : -1])
+ END
+ call CheckDefAndScriptSuccess(lines)
endfunc
" List identity