Skip to content

Commit 92f05f2

Browse files
committed
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 #8749)
1 parent ef98257 commit 92f05f2

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/list.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,19 @@ list_slice_or_index(
11461146
n1 = len + n1;
11471147
if (n1 < 0 || n1 >= len)
11481148
{
1149-
// For a range we allow invalid values and return an empty
1150-
// list. A list index out of range is an error.
1149+
// For a range we allow invalid values and for legacy script return an
1150+
// empty list, for Vim9 script start at the first item.
1151+
// A list index out of range is an error.
11511152
if (!range)
11521153
{
11531154
if (verbose)
11541155
semsg(_(e_listidx), (long)n1_arg);
11551156
return FAIL;
11561157
}
1157-
n1 = n1 < 0 ? 0 : len;
1158+
if (in_vim9script())
1159+
n1 = n1 < 0 ? 0 : len;
1160+
else
1161+
n1 = len;
11581162
}
11591163
if (range)
11601164
{

src/testdir/test_listdict.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ func Test_list_slice()
4242
let l[:1] += [1, 2]
4343
let l[2:] -= [1]
4444
call assert_equal([2, 4, 2], l)
45+
46+
let lines =<< trim END
47+
VAR l = [1, 2]
48+
call assert_equal([1, 2], l[:])
49+
call assert_equal([2], l[-1 : -1])
50+
call assert_equal([1, 2], l[-2 : -1])
51+
END
52+
call CheckLegacyAndVim9Success(lines)
53+
54+
let l = [1, 2]
55+
call assert_equal([], l[-3 : -1])
56+
57+
let lines =<< trim END
58+
var l = [1, 2]
59+
assert_equal([1, 2], l[-3 : -1])
60+
END
61+
call CheckDefAndScriptSuccess(lines)
4562
endfunc
4663

4764
" List identity

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3336,
758760
/**/
759761
3335,
760762
/**/

0 commit comments

Comments
 (0)