0
0
mirror of https://github.com/vim/vim.git synced 2025-10-04 05:25:06 -04:00

patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent

Problem:    Vim9: key type that can be used for literal dict and indexing is
            inconsistent.
Solution:   Allow using number and bool as key for a literal dict. (#7771)
This commit is contained in:
Bram Moolenaar
2021-02-03 17:41:24 +01:00
parent 91478ae49a
commit 2e5910bfbb
8 changed files with 74 additions and 38 deletions

View File

@@ -1354,13 +1354,11 @@ def Test_expr5_list_add()
endfor
# concatenating two lists with different member types results in "any"
var lines =<< trim END
var d = {}
for i in ['a'] + [0]
d = {[i]: 0}
endfor
END
CheckDefExecFailure(lines, 'E1012:')
var dany = {}
for i in ['a'] + [12]
dany[i] = i
endfor
assert_equal({a: 'a', 12: 12}, dany)
enddef
" test multiply, divide, modulo
@@ -2116,6 +2114,25 @@ def Test_expr7_dict()
var cd = { # comment
key: 'val' # comment
}
# different types used for the key
var dkeys = {['key']: 'string',
[12]: 'numberexpr',
34: 'number',
[true]: 'bool'}
assert_equal('string', dkeys['key'])
assert_equal('numberexpr', dkeys[12])
assert_equal('number', dkeys[34])
assert_equal('bool', dkeys[true])
if has('float')
dkeys = {[1.2]: 'floatexpr', [3.4]: 'float'}
assert_equal('floatexpr', dkeys[1.2])
assert_equal('float', dkeys[3.4])
endif
# automatic conversion from number to string
var n = 123
var dictnr = {[n]: 1}
END
CheckDefAndScriptSuccess(lines)
@@ -2142,16 +2159,11 @@ def Test_expr7_dict()
CheckDefExecFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1)
CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
# invalid types for the key
CheckDefFailure(["var x = {[[1, 2]]: 0}"], 'E1105:', 1)
CheckDefFailure(['var x = ({'], 'E723:', 2)
CheckDefExecFailure(['{}[getftype("file")]'], 'E716: Key not present in Dictionary: ""', 1)
# no automatic conversion from number to string
lines =<< trim END
var n = 123
var d = {[n]: 1}
END
CheckDefFailure(lines, 'E1012:', 2)
CheckScriptFailure(['vim9script'] + lines, 'E928:', 3)
enddef
def Test_expr7_dict_vim9script()