aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/eval/typval.c6
-rw-r--r--test/old/testdir/test_listdict.vim19
2 files changed, 22 insertions, 3 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 8028688ac8..0b2be3074f 100644
--- 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
index 0ff3582da9..11dade18f3 100644
--- 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