diff options
author | Bram Moolenaar <Bram@vim.org> | 2021-08-12 21:12:56 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2021-08-12 21:12:56 +0200 |
commit | 92f05f21afdb8a43581554a252cb2fc050f9e03b (patch) | |
tree | bce736e711441e1d3ee9365ad6e4780b2d0dbcdb | |
parent | ef98257593a0abf1300d0f70358dc45a70a62580 (diff) |
patch 8.2.3336: behavior of negative index in list change changedv8.2.3336
Problem: Behavior of negative index in list change changed. (Naruhiko
Nishino)
Solution: Only change it for Vim9 script. (closes #8749)
-rw-r--r-- | src/list.c | 10 | ||||
-rw-r--r-- | src/testdir/test_listdict.vim | 17 | ||||
-rw-r--r-- | src/version.c | 2 |
3 files changed, 26 insertions, 3 deletions
diff --git a/src/list.c b/src/list.c index 9d07b7e233..04ddbfca15 100644 --- a/src/list.c +++ b/src/list.c @@ -1146,15 +1146,19 @@ list_slice_or_index( 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 for legacy script return an + // empty list, for Vim9 script start at the first item. + // A list index out of range is an error. if (!range) { if (verbose) semsg(_(e_listidx), (long)n1_arg); return FAIL; } - n1 = n1 < 0 ? 0 : len; + if (in_vim9script()) + n1 = n1 < 0 ? 0 : len; + else + n1 = len; } if (range) { diff --git a/src/testdir/test_listdict.vim b/src/testdir/test_listdict.vim index bef4581d3f..8f8e34ceed 100644 --- a/src/testdir/test_listdict.vim +++ b/src/testdir/test_listdict.vim @@ -42,6 +42,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 diff --git a/src/version.c b/src/version.c index 7fd4d80d75..a31b4941a5 100644 --- a/src/version.c +++ b/src/version.c @@ -756,6 +756,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 3336, +/**/ 3335, /**/ 3334, |