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)

92f05f21af

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2023-05-04 16:12:52 +08:00
parent 7ac63906ea
commit 47132823ab
2 changed files with 22 additions and 3 deletions

View File

@@ -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; n1 = len + n1;
} }
if (n1 < 0 || n1 >= len) { if (n1 < 0 || n1 >= len) {
// For a range we allow invalid values and return an empty // For a range we allow invalid values and return an empty list.
// list. A list index out of range is an error. // A list index out of range is an error.
if (!range) { if (!range) {
if (verbose) { if (verbose) {
semsg(_(e_listidx), (int64_t)n1); semsg(_(e_listidx), (int64_t)n1);
} }
return FAIL; return FAIL;
} }
n1 = n1 < 0 ? 0 : len; n1 = len;
} }
if (range) { if (range) {
if (n2 < 0) { if (n2 < 0) {

View File

@@ -1,5 +1,7 @@
" Tests for the List and Dict types " Tests for the List and Dict types
source vim9.vim
func TearDown() func TearDown()
" Run garbage collection after every test " Run garbage collection after every test
call test_garbagecollect_now() call test_garbagecollect_now()
@@ -37,6 +39,23 @@ func Test_list_slice()
let l[:1] += [1, 2] let l[:1] += [1, 2]
let l[2:] -= [1] let l[2:] -= [1]
call assert_equal([2, 4, 2], l) 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 endfunc
" List identity " List identity