Skip to content

Commit 752fc69

Browse files
committed
patch 8.2.2301: Vim9: cannot unlet a dict or list item
Problem: Vim9: cannot unlet a dict or list item. Solution: Add ISN_UNLETINDEX. Refactor assignment code to use for unlet.
1 parent d62d87d commit 752fc69

5 files changed

Lines changed: 685 additions & 433 deletions

File tree

src/testdir/test_vim9_assign.vim

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,14 +1349,47 @@ def Test_unlet()
13491349
assert_false(exists('s:somevar'))
13501350
unlet! s:somevar
13511351

1352+
# dict unlet
1353+
var dd = {a: 1, b: 2, c: 3}
1354+
unlet dd['a']
1355+
unlet dd.c
1356+
assert_equal({b: 2}, dd)
1357+
1358+
# list unlet
1359+
var ll = [1, 2, 3, 4]
1360+
unlet ll[1]
1361+
unlet ll[-1]
1362+
assert_equal([1, 3], ll)
1363+
1364+
# list of dict unlet
1365+
var dl = [{a: 1, b: 2}, {c: 3}]
1366+
unlet dl[0]['b']
1367+
assert_equal([{a: 1}, {c: 3}], dl)
1368+
1369+
CheckDefExecFailure([
1370+
'var ll = test_null_list()',
1371+
'unlet ll[0]',
1372+
], 'E684:')
1373+
CheckDefExecFailure([
1374+
'var ll = [1]',
1375+
'unlet ll[2]',
1376+
], 'E684:')
1377+
CheckDefExecFailure([
1378+
'var dd = test_null_dict()',
1379+
'unlet dd["a"]',
1380+
], 'E716:')
1381+
CheckDefExecFailure([
1382+
'var dd = {a: 1}',
1383+
'unlet dd["b"]',
1384+
], 'E716:')
1385+
13521386
# can compile unlet before variable exists
1353-
# This doesn't work yet
1354-
#g:someDict = {key: 'val'}
1355-
#var k = 'key'
1356-
#unlet g:someDict[k]
1357-
#assert_equal({}, g:someDict)
1358-
#unlet g:someDict
1359-
#assert_false(exists('g:someDict'))
1387+
g:someDict = {key: 'val'}
1388+
var k = 'key'
1389+
unlet g:someDict[k]
1390+
assert_equal({}, g:someDict)
1391+
unlet g:someDict
1392+
assert_false(exists('g:someDict'))
13601393

13611394
CheckScriptFailure([
13621395
'vim9script',

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2301,
753755
/**/
754756
2300,
755757
/**/

src/vim9.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef enum {
6060

6161
ISN_UNLET, // unlet variable isn_arg.unlet.ul_name
6262
ISN_UNLETENV, // unlet environment variable isn_arg.unlet.ul_name
63+
ISN_UNLETINDEX, // unlet item of list or dict
6364

6465
ISN_LOCKCONST, // lock constant value
6566

0 commit comments

Comments
 (0)