From 00eb99528edc9ee1d14241d6914168fc33331ded Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Sat, 5 Feb 2022 19:23:18 +0000 Subject: [PATCH] patch 8.2.4304: Vim9: slice() makes a copy but doesn't change the type Problem: Vim9: slice() makes a copy but doesn't change the type. Solution: Change the declared type like copy(). (closes #9696) --- src/evalfunc.c | 23 ++++++++++++++++++++++- src/testdir/test_vim9_builtin.vim | 3 +++ src/version.c | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/evalfunc.c b/src/evalfunc.c index e1dca9499e..3cc95c9884 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -1169,6 +1169,27 @@ ret_first_arg(int argcount, return &t_void; } static type_T * +ret_slice(int argcount, + type2_T *argtypes, + type_T **decl_type) +{ + if (argcount > 0) + { + if (argtypes[0].type_decl != NULL) + { + switch (argtypes[0].type_decl->tt_type) + { + case VAR_STRING: *decl_type = &t_string; break; + case VAR_BLOB: *decl_type = &t_blob; break; + case VAR_LIST: *decl_type = &t_list_any; break; + default: break; + } + } + return argtypes[0].type_curr; + } + return &t_void; +} + static type_T * ret_copy(int argcount, type2_T *argtypes, type_T **decl_type) @@ -2292,7 +2313,7 @@ static funcentry_T global_functions[] = {"sinh", 1, 1, FEARG_1, arg1_float_or_nr, ret_float, FLOAT_FUNC(f_sinh)}, {"slice", 2, 3, FEARG_1, arg23_slice, - ret_first_arg, f_slice}, + ret_slice, f_slice}, {"sort", 1, 3, FEARG_1, arg13_sortuniq, ret_first_arg, f_sort}, {"sound_clear", 0, 0, 0, NULL, diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim index 743cb50234..557ddf2190 100644 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -3619,6 +3619,9 @@ def Test_slice() assert_equal([], slice(range(6), 1, -5)) assert_equal([], slice(range(6), 1, -6)) + var lds: list> = [{key: 'value'}] + assert_equal(['val'], lds->slice(0, 1)->map((_, v) => 'val')) + assert_equal(0z1122334455, slice(0z001122334455, 1)) assert_equal(0z112233, slice(0z001122334455, 1, 4)) assert_equal(0z11223344, slice(0z001122334455, 1, -1)) diff --git a/src/version.c b/src/version.c index f86aa8926f..3df4cfca94 100644 --- a/src/version.c +++ b/src/version.c @@ -746,6 +746,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 4304, /**/ 4303, /**/