@@ -1354,13 +1354,11 @@ def Test_expr5_list_add()
13541354 endfor
13551355
13561356 # concatenating two lists with different member types results in " any"
1357- var lines = << trim END
1358- var d = {}
1359- for i in [' a' ] + [0 ]
1360- d = {[i ]: 0 }
1361- endfor
1362- END
1363- CheckDefExecFailure (lines , ' E1012:' )
1357+ var dany = {}
1358+ for i in [' a' ] + [12 ]
1359+ dany[i ] = i
1360+ endfor
1361+ assert_equal ({a: ' a' , 12 : 12 }, dany)
13641362enddef
13651363
13661364" test multiply, divide, modulo
@@ -2116,6 +2114,25 @@ def Test_expr7_dict()
21162114 var cd = { # comment
21172115 key : ' val' # comment
21182116 }
2117+
2118+ # different types used for the key
2119+ var dkeys = {[' key' ]: ' string' ,
2120+ [12 ]: ' numberexpr' ,
2121+ 34 : ' number' ,
2122+ [true]: ' bool' }
2123+ assert_equal (' string' , dkeys[' key' ])
2124+ assert_equal (' numberexpr' , dkeys[12 ])
2125+ assert_equal (' number' , dkeys[34 ])
2126+ assert_equal (' bool' , dkeys[true])
2127+ if has (' float' )
2128+ dkeys = {[1.2 ]: ' floatexpr' , [3.4 ]: ' float' }
2129+ assert_equal (' floatexpr' , dkeys[1.2 ])
2130+ assert_equal (' float' , dkeys[3.4 ])
2131+ endif
2132+
2133+ # automatic conversion from number to string
2134+ var n = 123
2135+ var dictnr = {[n ]: 1 }
21192136 END
21202137 CheckDefAndScriptSuccess (lines )
21212138
@@ -2142,16 +2159,11 @@ def Test_expr7_dict()
21422159 CheckDefExecFailure ([' var x: dict<string> = {a: 234, b: "1"}' ], ' E1012:' , 1 )
21432160 CheckDefExecFailure ([' var x: dict<string> = {a: "x", b: 134}' ], ' E1012:' , 1 )
21442161
2162+ # invalid types for the key
2163+ CheckDefFailure ([" var x = {[[1, 2]]: 0}" ], ' E1105:' , 1 )
2164+
21452165 CheckDefFailure ([' var x = ({' ], ' E723:' , 2 )
21462166 CheckDefExecFailure ([' {}[getftype("file")]' ], ' E716: Key not present in Dictionary: ""' , 1 )
2147-
2148- # no automatic conversion from number to string
2149- lines = << trim END
2150- var n = 123
2151- var d = {[n ]: 1 }
2152- END
2153- CheckDefFailure (lines , ' E1012:' , 2 )
2154- CheckScriptFailure ([' vim9script' ] + lines , ' E928:' , 3 )
21552167enddef
21562168
21572169def Test_expr7_dict_vim9script ()
0 commit comments