0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.2082: Vim9: can still use the depricated #{} dict syntax

Problem:    Vim9: can still use the depricated #{} dict syntax.
Solution:   Remove support for #{} in Vim9 script. (closes #7406, closes #7405)
This commit is contained in:
Bram Moolenaar 2020-12-02 17:36:54 +01:00
parent 7f76494aac
commit e0de171ecd
14 changed files with 259 additions and 271 deletions

View File

@ -782,6 +782,20 @@ dict2string(typval_T *tv, int copyID, int restore_copyID)
return (char_u *)ga.ga_data; return (char_u *)ga.ga_data;
} }
/*
* Advance over a literal key, including "-". If the first character is not a
* literal key character then "key" is returned.
*/
char_u *
skip_literal_key(char_u *key)
{
char_u *p;
for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
;
return p;
}
/* /*
* Get the key for #{key: val} into "tv" and advance "arg". * Get the key for #{key: val} into "tv" and advance "arg".
* Return FAIL when there is no valid key. * Return FAIL when there is no valid key.
@ -789,13 +803,10 @@ dict2string(typval_T *tv, int copyID, int restore_copyID)
static int static int
get_literal_key(char_u **arg, typval_T *tv) get_literal_key(char_u **arg, typval_T *tv)
{ {
char_u *p; char_u *p = skip_literal_key(*arg);
if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-') if (p == *arg)
return FAIL; return FAIL;
for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
;
tv->v_type = VAR_STRING; tv->v_type = VAR_STRING;
tv->vval.v_string = vim_strnsave(*arg, p - *arg); tv->vval.v_string = vim_strnsave(*arg, p - *arg);
@ -851,17 +862,15 @@ eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
*arg = skipwhite_and_linebreak(*arg + 1, evalarg); *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
while (**arg != '}' && **arg != NUL) while (**arg != '}' && **arg != NUL)
{ {
char_u *p = to_name_end(*arg, FALSE); int has_bracket = vim9script && **arg == '[';
if (literal || (vim9script && *p == ':')) if (literal || (vim9script && !has_bracket))
{ {
if (get_literal_key(arg, &tvkey) == FAIL) if (get_literal_key(arg, &tvkey) == FAIL)
goto failret; goto failret;
} }
else else
{ {
int has_bracket = vim9script && **arg == '[';
if (has_bracket) if (has_bracket)
*arg = skipwhite(*arg + 1); *arg = skipwhite(*arg + 1);
if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive! if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!

View File

@ -3254,7 +3254,7 @@ eval7(
/* /*
* Dictionary: #{key: val, key: val} * Dictionary: #{key: val, key: val}
*/ */
case '#': if ((*arg)[1] == '{') case '#': if (!in_vim9script() && (*arg)[1] == '{')
{ {
++*arg; ++*arg;
ret = eval_dict(arg, rettv, evalarg, TRUE); ret = eval_dict(arg, rettv, evalarg, TRUE);

View File

@ -33,6 +33,7 @@ varnumber_T dict_get_number_def(dict_T *d, char_u *key, int def);
varnumber_T dict_get_number_check(dict_T *d, char_u *key); varnumber_T dict_get_number_check(dict_T *d, char_u *key);
varnumber_T dict_get_bool(dict_T *d, char_u *key, int def); varnumber_T dict_get_bool(dict_T *d, char_u *key, int def);
char_u *dict2string(typval_T *tv, int copyID, int restore_copyID); char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
char_u *skip_literal_key(char_u *key);
int eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal); int eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal);
void dict_extend(dict_T *d1, dict_T *d2, char_u *action); void dict_extend(dict_T *d1, dict_T *d2, char_u *action);
dictitem_T *dict_lookup(hashitem_T *hi); dictitem_T *dict_lookup(hashitem_T *hi);

View File

@ -2172,7 +2172,7 @@ func Test_popup_scrollbar()
endfunc endfunc
def CreatePopup(text: list<string>) def CreatePopup(text: list<string>)
popup_create(text, #{ popup_create(text, {
\ minwidth: 30, \ minwidth: 30,
\ maxwidth: 30, \ maxwidth: 30,
\ minheight: 4, \ minheight: 4,
@ -2717,7 +2717,7 @@ def Popupwin_close_prevwin()
split split
wincmd b wincmd b
assert_equal(2, winnr()) assert_equal(2, winnr())
var buf = term_start(&shell, #{hidden: 1}) var buf = term_start(&shell, {hidden: 1})
popup_create(buf, {}) popup_create(buf, {})
TermWait(buf, 100) TermWait(buf, 100)
popup_clear(true) popup_clear(true)

View File

@ -219,15 +219,15 @@ def Test_prop_find2()
# Multiple props per line, start on the first, should find the second. # Multiple props per line, start on the first, should find the second.
new new
['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1) ['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1)
prop_type_add('misspell', #{highlight: 'ErrorMsg'}) prop_type_add('misspell', {highlight: 'ErrorMsg'})
for lnum in [1, 2] for lnum in [1, 2]
for col in [8, 14, 24, 38] for col in [8, 14, 24, 38]
prop_add(lnum, col, #{type: 'misspell', length: 2}) prop_add(lnum, col, {type: 'misspell', length: 2})
endfor endfor
endfor endfor
cursor(1, 8) cursor(1, 8)
var expected = {'lnum': 1, 'id': 0, 'col': 14, 'end': 1, 'type': 'misspell', 'length': 2, 'start': 1} var expected = {lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', length: 2, start: 1}
var result = prop_find(#{type: 'misspell', skipstart: true}, 'f') var result = prop_find({type: 'misspell', skipstart: true}, 'f')
assert_equal(expected, result) assert_equal(expected, result)
prop_type_delete('misspell') prop_type_delete('misspell')
@ -322,7 +322,7 @@ func Test_prop_remove()
endfunc endfunc
def Test_prop_add_vim9() def Test_prop_add_vim9()
prop_type_add('comment', #{ prop_type_add('comment', {
highlight: 'Directory', highlight: 'Directory',
priority: 123, priority: 123,
start_incl: true, start_incl: true,
@ -336,7 +336,7 @@ def Test_prop_remove_vim9()
new new
AddPropTypes() AddPropTypes()
SetupPropsInFirstLine() SetupPropsInFirstLine()
assert_equal(1, prop_remove({'type': 'three', 'id': 13, 'both': true, 'all': true})) assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
DeletePropTypes() DeletePropTypes()
bwipe! bwipe!
enddef enddef

View File

@ -360,26 +360,26 @@ def Test_extend_dict()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var d: dict<number> var d: dict<number>
extend(d, #{a: 1}) extend(d, {a: 1})
assert_equal(#{a: 1}, d) assert_equal({a: 1}, d)
var d2: dict<number> var d2: dict<number>
d2['one'] = 1 d2['one'] = 1
assert_equal(#{one: 1}, d2) assert_equal({one: 1}, d2)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d: dict<string> = test_null_dict() var d: dict<string> = test_null_dict()
extend(d, #{a: 'x'}) extend(d, {a: 'x'})
assert_equal(#{a: 'x'}, d) assert_equal({a: 'x'}, d)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
lines =<< trim END lines =<< trim END
vim9script vim9script
extend(test_null_dict(), #{a: 'x'}) extend(test_null_dict(), {a: 'x'})
END END
CheckScriptFailure(lines, 'E1133:', 2) CheckScriptFailure(lines, 'E1133:', 2)
enddef enddef
@ -487,20 +487,20 @@ def Test_assignment_list_vim9script()
enddef enddef
def Test_assignment_dict() def Test_assignment_dict()
var dict1: dict<bool> = #{one: false, two: true} var dict1: dict<bool> = {one: false, two: true}
var dict2: dict<number> = #{one: 1, two: 2} var dict2: dict<number> = {one: 1, two: 2}
var dict3: dict<string> = #{key: 'value'} var dict3: dict<string> = {key: 'value'}
var dict4: dict<any> = #{one: 1, two: '2'} var dict4: dict<any> = {one: 1, two: '2'}
var dict5: dict<blob> = #{one: 0z01, two: 0z02} var dict5: dict<blob> = {one: 0z01, two: 0z02}
# overwrite # overwrite
dict3['key'] = 'another' dict3['key'] = 'another'
assert_equal(dict3, #{key: 'another'}) assert_equal(dict3, {key: 'another'})
dict3.key = 'yet another' dict3.key = 'yet another'
assert_equal(dict3, #{key: 'yet another'}) assert_equal(dict3, {key: 'yet another'})
var lines =<< trim END var lines =<< trim END
var dd = #{one: 1} var dd = {one: 1}
dd.one) = 2 dd.one) = 2
END END
CheckDefFailure(lines, 'E15:', 2) CheckDefFailure(lines, 'E15:', 2)
@ -508,10 +508,10 @@ def Test_assignment_dict()
# empty key can be used # empty key can be used
var dd = {} var dd = {}
dd[""] = 6 dd[""] = 6
assert_equal({'': 6}, dd) assert_equal({['']: 6}, dd)
# type becomes dict<any> # type becomes dict<any>
var somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'} var somedict = rand() > 0 ? {a: 1, b: 2} : {a: 'a', b: 'b'}
# assignment to script-local dict # assignment to script-local dict
lines =<< trim END lines =<< trim END
@ -521,7 +521,7 @@ def Test_assignment_dict()
test['a'] = 43 test['a'] = 43
return test return test
enddef enddef
assert_equal(#{a: 43}, FillDict()) assert_equal({a: 43}, FillDict())
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
@ -544,7 +544,7 @@ def Test_assignment_dict()
g:test['a'] = 43 g:test['a'] = 43
return g:test return g:test
enddef enddef
assert_equal(#{a: 43}, FillDict()) assert_equal({a: 43}, FillDict())
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
@ -556,7 +556,7 @@ def Test_assignment_dict()
b:test['a'] = 43 b:test['a'] = 43
return b:test return b:test
enddef enddef
assert_equal(#{a: 43}, FillDict()) assert_equal({a: 43}, FillDict())
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
enddef enddef
@ -636,7 +636,7 @@ def Test_assignment_default()
if has('unix') && executable('cat') if has('unix') && executable('cat')
# check with non-null job and channel, types must match # check with non-null job and channel, types must match
thejob = job_start("cat ", #{}) thejob = job_start("cat ", {})
thechannel = job_getchannel(thejob) thechannel = job_getchannel(thejob)
job_stop(thejob, 'kill') job_stop(thejob, 'kill')
endif endif
@ -827,8 +827,8 @@ def Test_assignment_failure()
CheckDefFailure(['var name: list<string> = [123]'], 'expected list<string> but got list<number>') CheckDefFailure(['var name: list<string> = [123]'], 'expected list<string> but got list<number>')
CheckDefFailure(['var name: list<number> = ["xx"]'], 'expected list<number> but got list<string>') CheckDefFailure(['var name: list<number> = ["xx"]'], 'expected list<number> but got list<string>')
CheckDefFailure(['var name: dict<string> = #{key: 123}'], 'expected dict<string> but got dict<number>') CheckDefFailure(['var name: dict<string> = {key: 123}'], 'expected dict<string> but got dict<number>')
CheckDefFailure(['var name: dict<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>') CheckDefFailure(['var name: dict<number> = {key: "xx"}'], 'expected dict<number> but got dict<string>')
CheckDefFailure(['var name = feedkeys("0")'], 'E1031:') CheckDefFailure(['var name = feedkeys("0")'], 'E1031:')
CheckDefFailure(['var name: number = feedkeys("0")'], 'expected number but got void') CheckDefFailure(['var name: number = feedkeys("0")'], 'expected number but got void')
@ -883,17 +883,17 @@ def Test_assign_dict()
for i in range(3) for i in range(3)
nrd[i] = i nrd[i] = i
endfor endfor
assert_equal({'0': 0, '1': 1, '2': 2}, nrd) assert_equal({0: 0, 1: 1, 2: 2}, nrd)
CheckDefFailure(["var d: dict<number> = #{a: '', b: true}"], 'E1012: Type mismatch; expected dict<number> but got dict<any>', 1) CheckDefFailure(["var d: dict<number> = {a: '', b: true}"], 'E1012: Type mismatch; expected dict<number> but got dict<any>', 1)
CheckDefFailure(["var d: dict<dict<number>> = #{x: #{a: '', b: true}}"], 'E1012: Type mismatch; expected dict<dict<number>> but got dict<dict<any>>', 1) CheckDefFailure(["var d: dict<dict<number>> = {x: {a: '', b: true}}"], 'E1012: Type mismatch; expected dict<dict<number>> but got dict<dict<any>>', 1)
enddef enddef
def Test_assign_dict_unknown_type() def Test_assign_dict_unknown_type()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var mylist = [] var mylist = []
mylist += [#{one: 'one'}] mylist += [{one: 'one'}]
def Func() def Func()
var dd = mylist[0] var dd = mylist[0]
assert_equal('one', dd.one) assert_equal('one', dd.one)
@ -905,7 +905,7 @@ def Test_assign_dict_unknown_type()
lines =<< trim END lines =<< trim END
vim9script vim9script
var mylist = [[]] var mylist = [[]]
mylist[0] += [#{one: 'one'}] mylist[0] += [{one: 'one'}]
def Func() def Func()
var dd = mylist[0][0] var dd = mylist[0][0]
assert_equal('one', dd.one) assert_equal('one', dd.one)
@ -1010,7 +1010,7 @@ def Test_let_declaration()
g:other_var = other g:other_var = other
# type is inferred # type is inferred
s:dict = {'a': 222} s:dict = {['a']: 222}
def GetDictVal(key: any) def GetDictVal(key: any)
g:dict_val = s:dict[key] g:dict_val = s:dict[key]
enddef enddef

View File

@ -32,7 +32,7 @@ func Test_InternalFuncRetType()
enddef enddef
def RetListAny(): list<any> def RetListAny(): list<any>
return items({'k': 'v'}) return items({k: 'v'})
enddef enddef
def RetListString(): list<string> def RetListString(): list<string>
@ -197,10 +197,10 @@ def Test_extend_arg_types()
assert_equal([1, 3, 2], extend([1, 2], [3], 1)) assert_equal([1, 3, 2], extend([1, 2], [3], 1))
assert_equal([1, 3, 2], extend([1, 2], [3], s:number_one)) assert_equal([1, 3, 2], extend([1, 2], [3], s:number_one))
assert_equal(#{a: 1, b: 2, c: 3}, extend(#{a: 1, b: 2}, #{c: 3})) assert_equal({a: 1, b: 2, c: 3}, extend({a: 1, b: 2}, {c: 3}))
assert_equal(#{a: 1, b: 4}, extend(#{a: 1, b: 2}, #{b: 4})) assert_equal({a: 1, b: 4}, extend({a: 1, b: 2}, {b: 4}))
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, 'keep')) assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, 'keep'))
assert_equal(#{a: 1, b: 2}, extend(#{a: 1, b: 2}, #{b: 4}, s:string_keep)) assert_equal({a: 1, b: 2}, extend({a: 1, b: 2}, {b: 4}, s:string_keep))
var res: list<dict<any>> var res: list<dict<any>>
extend(res, map([1, 2], {_, v -> {}})) extend(res, map([1, 2], {_, v -> {}}))
@ -210,9 +210,9 @@ def Test_extend_arg_types()
CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>') CheckDefFailure(['extend([1, 2], ["x"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string') CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
CheckDefFailure(['extend(#{a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number') CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
CheckDefFailure(['extend(#{a: 1}, #{b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>') CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>')
CheckDefFailure(['extend(#{a: 1}, #{b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number') CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
enddef enddef
def Test_extend_return_type() def Test_extend_return_type()
@ -254,7 +254,7 @@ def Test_getbufinfo()
edit Xtestfile1 edit Xtestfile1
hide edit Xtestfile2 hide edit Xtestfile2
hide enew hide enew
getbufinfo(#{bufloaded: true, buflisted: true, bufmodified: false}) getbufinfo({bufloaded: true, buflisted: true, bufmodified: false})
->len()->assert_equal(3) ->len()->assert_equal(3)
bwipe Xtestfile1 Xtestfile2 bwipe Xtestfile1 Xtestfile2
enddef enddef
@ -297,16 +297,16 @@ def Test_getloclist_return_type()
var l = getloclist(1) var l = getloclist(1)
l->assert_equal([]) l->assert_equal([])
var d = getloclist(1, #{items: 0}) var d = getloclist(1, {items: 0})
d->assert_equal(#{items: []}) d->assert_equal({items: []})
enddef enddef
def Test_getqflist_return_type() def Test_getqflist_return_type()
var l = getqflist() var l = getqflist()
l->assert_equal([]) l->assert_equal([])
var d = getqflist(#{items: 0}) var d = getqflist({items: 0})
d->assert_equal(#{items: []}) d->assert_equal({items: []})
enddef enddef
def Test_getreg() def Test_getreg()
@ -368,7 +368,7 @@ def Test_insert()
enddef enddef
def Test_keys_return_type() def Test_keys_return_type()
const var: list<string> = #{a: 1, b: 2}->keys() const var: list<string> = {a: 1, b: 2}->keys()
var->assert_equal(['a', 'b']) var->assert_equal(['a', 'b'])
enddef enddef
@ -388,7 +388,7 @@ enddef
def Test_maparg() def Test_maparg()
var lnum = str2nr(expand('<sflnum>')) var lnum = str2nr(expand('<sflnum>'))
map foo bar map foo bar
maparg('foo', '', false, true)->assert_equal(#{ maparg('foo', '', false, true)->assert_equal({
lnum: lnum + 1, lnum: lnum + 1,
script: 0, script: 0,
mode: ' ', mode: ' ',
@ -428,7 +428,7 @@ def Test_readdir()
enddef enddef
def Test_remove_return_type() def Test_remove_return_type()
var l = remove(#{one: [1, 2], two: [3, 4]}, 'one') var l = remove({one: [1, 2], two: [3, 4]}, 'one')
var res = 0 var res = 0
for n in l for n in l
res += n res += n
@ -466,8 +466,8 @@ def Test_searchcount()
new new
setline(1, "foo bar") setline(1, "foo bar")
:/foo :/foo
searchcount(#{recompute: true}) searchcount({recompute: true})
->assert_equal(#{ ->assert_equal({
exact_match: 1, exact_match: 1,
current: 1, current: 1,
total: 1, total: 1,
@ -496,8 +496,8 @@ def Test_setbufvar()
enddef enddef
def Test_setloclist() def Test_setloclist()
var items = [#{filename: '/tmp/file', lnum: 1, valid: true}] var items = [{filename: '/tmp/file', lnum: 1, valid: true}]
var what = #{items: items} var what = {items: items}
setqflist([], ' ', what) setqflist([], ' ', what)
setloclist(0, [], ' ', what) setloclist(0, [], ' ', what)
enddef enddef
@ -570,7 +570,7 @@ def Test_term_start()
else else
botright new botright new
var winnr = winnr() var winnr = winnr()
term_start(&shell, #{curwin: true}) term_start(&shell, {curwin: true})
winnr()->assert_equal(winnr) winnr()->assert_equal(winnr)
bwipe! bwipe!
endif endif
@ -586,7 +586,7 @@ enddef
def Test_win_splitmove() def Test_win_splitmove()
split split
win_splitmove(1, 2, #{vertical: true, rightbelow: true}) win_splitmove(1, 2, {vertical: true, rightbelow: true})
close close
enddef enddef

View File

@ -251,18 +251,18 @@ def Test_skipped_expr_linebreak()
enddef enddef
def Test_dict_member() def Test_dict_member()
var test: dict<list<number>> = {'data': [3, 1, 2]} var test: dict<list<number>> = {data: [3, 1, 2]}
test.data->sort() test.data->sort()
assert_equal(#{data: [1, 2, 3]}, test) assert_equal({data: [1, 2, 3]}, test)
test.data test.data
->reverse() ->reverse()
assert_equal(#{data: [3, 2, 1]}, test) assert_equal({data: [3, 2, 1]}, test)
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var test: dict<list<number>> = {'data': [3, 1, 2]} var test: dict<list<number>> = {data: [3, 1, 2]}
test.data->sort() test.data->sort()
assert_equal(#{data: [1, 2, 3]}, test) assert_equal({data: [1, 2, 3]}, test)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
enddef enddef
@ -308,9 +308,9 @@ def Test_bar_after_command()
enddef enddef
def Test_filter_is_not_modifier() def Test_filter_is_not_modifier()
var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}] var tags = [{a: 1, b: 2}, {x: 3, y: 4}]
filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 }) filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
assert_equal([#{x: 3, y: 4}], tags) assert_equal([{x: 3, y: 4}], tags)
enddef enddef
def Test_command_modifier_filter() def Test_command_modifier_filter()

View File

@ -390,7 +390,7 @@ enddef
def s:ScriptFuncNew() def s:ScriptFuncNew()
var ll = [1, "two", 333] var ll = [1, "two", 333]
var dd = #{one: 1, two: "val"} var dd = {one: 1, two: "val"}
enddef enddef
def Test_disassemble_new() def Test_disassemble_new()
@ -402,7 +402,7 @@ def Test_disassemble_new()
'\d PUSHNR 333\_s*' .. '\d PUSHNR 333\_s*' ..
'\d NEWLIST size 3\_s*' .. '\d NEWLIST size 3\_s*' ..
'\d STORE $0\_s*' .. '\d STORE $0\_s*' ..
'var dd = #{one: 1, two: "val"}\_s*' .. 'var dd = {one: 1, two: "val"}\_s*' ..
'\d PUSHS "one"\_s*' .. '\d PUSHS "one"\_s*' ..
'\d PUSHNR 1\_s*' .. '\d PUSHNR 1\_s*' ..
'\d PUSHS "two"\_s*' .. '\d PUSHS "two"\_s*' ..
@ -1292,7 +1292,7 @@ def Test_disassemble_list_slice()
enddef enddef
def DictMember(): number def DictMember(): number
var d = #{item: 1} var d = {item: 1}
var res = d.item var res = d.item
res = d["item"] res = d["item"]
return res return res
@ -1301,7 +1301,7 @@ enddef
def Test_disassemble_dict_member() def Test_disassemble_dict_member()
var instr = execute('disassemble DictMember') var instr = execute('disassemble DictMember')
assert_match('DictMember\_s*' .. assert_match('DictMember\_s*' ..
'var d = #{item: 1}\_s*' .. 'var d = {item: 1}\_s*' ..
'\d PUSHS "item"\_s*' .. '\d PUSHS "item"\_s*' ..
'\d PUSHNR 1\_s*' .. '\d PUSHNR 1\_s*' ..
'\d NEWDICT size 1\_s*' .. '\d NEWDICT size 1\_s*' ..
@ -1473,10 +1473,10 @@ def Test_disassemble_compare()
['[1, 2] is aList', 'COMPARELIST is'], ['[1, 2] is aList', 'COMPARELIST is'],
['[1, 2] isnot aList', 'COMPARELIST isnot'], ['[1, 2] isnot aList', 'COMPARELIST isnot'],
['#{a: 1} == aDict', 'COMPAREDICT =='], ['{a: 1} == aDict', 'COMPAREDICT =='],
['#{a: 1} != aDict', 'COMPAREDICT !='], ['{a: 1} != aDict', 'COMPAREDICT !='],
['#{a: 1} is aDict', 'COMPAREDICT is'], ['{a: 1} is aDict', 'COMPAREDICT is'],
['#{a: 1} isnot aDict', 'COMPAREDICT isnot'], ['{a: 1} isnot aDict', 'COMPAREDICT isnot'],
['{->33} == {->44}', 'COMPAREFUNC =='], ['{->33} == {->44}', 'COMPAREFUNC =='],
['{->33} != {->44}', 'COMPAREFUNC !='], ['{->33} != {->44}', 'COMPAREFUNC !='],
@ -1519,7 +1519,7 @@ def Test_disassemble_compare()
' var aString = "yy"', ' var aString = "yy"',
' var aBlob = 0z22', ' var aBlob = 0z22',
' var aList = [3, 4]', ' var aList = [3, 4]',
' var aDict = #{x: 2}', ' var aDict = {x: 2}',
floatDecl, floatDecl,
' if ' .. case[0], ' if ' .. case[0],
' echo 42' ' echo 42'

View File

@ -27,7 +27,7 @@ def Test_expr1_trinary()
: 'two') : 'two')
assert_equal('one', !!0z1234 ? 'one' : 'two') assert_equal('one', !!0z1234 ? 'one' : 'two')
assert_equal('one', !![0] ? 'one' : 'two') assert_equal('one', !![0] ? 'one' : 'two')
assert_equal('one', !!#{x: 0} ? 'one' : 'two') assert_equal('one', !!{x: 0} ? 'one' : 'two')
var name = 1 var name = 1
assert_equal('one', name ? 'one' : 'two') assert_equal('one', name ? 'one' : 'two')
@ -206,7 +206,7 @@ def Test_expr1_falsy()
assert_equal(123, 123 ?? 456) assert_equal(123, 123 ?? 456)
assert_equal('yes', 'yes' ?? 456) assert_equal('yes', 'yes' ?? 456)
assert_equal([1], [1] ?? 456) assert_equal([1], [1] ?? 456)
assert_equal(#{one: 1}, #{one: 1} ?? 456) assert_equal({one: 1}, {one: 1} ?? 456)
if has('float') if has('float')
assert_equal(0.1, 0.1 ?? 456) assert_equal(0.1, 0.1 ?? 456)
endif endif
@ -553,12 +553,12 @@ def Test_expr4_equal()
assert_equal(false, [1, 2, 3] == []) assert_equal(false, [1, 2, 3] == [])
assert_equal(false, [1, 2, 3] == ['1', '2', '3']) assert_equal(false, [1, 2, 3] == ['1', '2', '3'])
assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) assert_equal(true, {one: 1, two: 2} == {one: 1, two: 2})
assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) assert_equal(false, {one: 1, two: 2} == {one: 2, two: 2})
assert_equal(false, #{one: 1, two: 2} == #{two: 2}) assert_equal(false, {one: 1, two: 2} == {two: 2})
assert_equal(false, #{one: 1, two: 2} == #{}) assert_equal(false, {one: 1, two: 2} == {})
assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) assert_equal(true, g:adict == {bbb: 8, aaa: 2})
assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) assert_equal(false, {ccc: 9, aaa: 2} == g:adict)
assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal'))
assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is'))
@ -650,12 +650,12 @@ def Test_expr4_notequal()
assert_equal(true, [1, 2, 3] != []) assert_equal(true, [1, 2, 3] != [])
assert_equal(true, [1, 2, 3] != ['1', '2', '3']) assert_equal(true, [1, 2, 3] != ['1', '2', '3'])
assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) assert_equal(false, {one: 1, two: 2} != {one: 1, two: 2})
assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) assert_equal(true, {one: 1, two: 2} != {one: 2, two: 2})
assert_equal(true, #{one: 1, two: 2} != #{two: 2}) assert_equal(true, {one: 1, two: 2} != {two: 2})
assert_equal(true, #{one: 1, two: 2} != #{}) assert_equal(true, {one: 1, two: 2} != {})
assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) assert_equal(false, g:adict != {bbb: 8, aaa: 2})
assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) assert_equal(true, {ccc: 9, aaa: 2} != g:adict)
assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal'))
assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is'))
@ -1197,7 +1197,7 @@ def Test_expr5_vim9script()
CheckScriptFailure(lines, 'E730:', 2) CheckScriptFailure(lines, 'E730:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
echo 'a' .. #{a: 1} echo 'a' .. {a: 1}
END END
CheckScriptFailure(lines, 'E731:', 2) CheckScriptFailure(lines, 'E731:', 2)
lines =<< trim END lines =<< trim END
@ -1287,7 +1287,7 @@ func Test_expr5_fails()
call CheckDefFailure(["var x = 6 + xxx"], 'E1001:', 1) call CheckDefFailure(["var x = 6 + xxx"], 'E1001:', 1)
call CheckDefFailure(["var x = 'a' .. [1]"], 'E1105:', 1) call CheckDefFailure(["var x = 'a' .. [1]"], 'E1105:', 1)
call CheckDefFailure(["var x = 'a' .. #{a: 1}"], 'E1105:', 1) call CheckDefFailure(["var x = 'a' .. {a: 1}"], 'E1105:', 1)
call CheckDefFailure(["var x = 'a' .. test_void()"], 'E1105:', 1) call CheckDefFailure(["var x = 'a' .. test_void()"], 'E1105:', 1)
call CheckDefFailure(["var x = 'a' .. 0z32"], 'E1105:', 1) call CheckDefFailure(["var x = 'a' .. 0z32"], 'E1105:', 1)
call CheckDefFailure(["var x = 'a' .. function('len')"], 'E1105:', 1) call CheckDefFailure(["var x = 'a' .. function('len')"], 'E1105:', 1)
@ -1469,9 +1469,9 @@ func Test_expr6_fails()
call CheckDefFailure(["var x = [1] / [2]"], 'E1036:', 1) call CheckDefFailure(["var x = [1] / [2]"], 'E1036:', 1)
call CheckDefFailure(["var x = [1] % [2]"], 'E1035:', 1) call CheckDefFailure(["var x = [1] % [2]"], 'E1035:', 1)
call CheckDefFailure(["var x = #{one: 1} * #{two: 2}"], 'E1036:', 1) call CheckDefFailure(["var x = {one: 1} * {two: 2}"], 'E1036:', 1)
call CheckDefFailure(["var x = #{one: 1} / #{two: 2}"], 'E1036:', 1) call CheckDefFailure(["var x = {one: 1} / {two: 2}"], 'E1036:', 1)
call CheckDefFailure(["var x = #{one: 1} % #{two: 2}"], 'E1035:', 1) call CheckDefFailure(["var x = {one: 1} % {two: 2}"], 'E1035:', 1)
call CheckDefFailure(["var x = 0xff[1]"], 'E1107:', 1) call CheckDefFailure(["var x = 0xff[1]"], 'E1107:', 1)
if has('float') if has('float')
@ -1796,9 +1796,9 @@ def Test_expr7_lambda()
# line continuation inside lambda with "cond ? expr : expr" works # line continuation inside lambda with "cond ? expr : expr" works
var ll = range(3) var ll = range(3)
map(ll, {k, v -> v % 2 ? { map(ll, {k, v -> v % 2 ? {
'111': 111 } : {} ['111']: 111 } : {}
}) })
assert_equal([{}, {'111': 111}, {}], ll) assert_equal([{}, {111: 111}, {}], ll)
ll = range(3) ll = range(3)
map(ll, {k, v -> v == 8 || v map(ll, {k, v -> v == 8 || v
@ -1814,11 +1814,11 @@ def Test_expr7_lambda()
}) })
assert_equal([111, 222, 111], ll) assert_equal([111, 222, 111], ll)
var dl = [{'key': 0}, {'key': 22}]->filter({ _, v -> v['key'] }) var dl = [{key: 0}, {key: 22}]->filter({ _, v -> v['key'] })
assert_equal([{'key': 22}], dl) assert_equal([{key: 22}], dl)
dl = [{'key': 12}, {'foo': 34}] dl = [{key: 12}, {['foo']: 34}]
assert_equal([{'key': 12}], filter(dl, assert_equal([{key: 12}], filter(dl,
{_, v -> has_key(v, 'key') ? v['key'] == 12 : 0})) {_, v -> has_key(v, 'key') ? v['key'] == 12 : 0}))
assert_equal(false, LambdaWithComments()(0)) assert_equal(false, LambdaWithComments()(0))
@ -1846,9 +1846,9 @@ def Test_expr7_lambda()
'E1106: 2 arguments too many') 'E1106: 2 arguments too many')
CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1) CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1)
CheckDefSuccess(['var Fx = {a -> #{k1: 0,', ' k2: 1}}']) CheckDefSuccess(['var Fx = {a -> {k1: 0,', ' k2: 1}}'])
CheckDefFailure(['var Fx = {a -> #{k1: 0', ' k2: 1}}'], 'E722:', 2) CheckDefFailure(['var Fx = {a -> {k1: 0', ' k2: 1}}'], 'E722:', 2)
CheckDefFailure(['var Fx = {a -> #{k1: 0,', ' k2 1}}'], 'E720:', 2) CheckDefFailure(['var Fx = {a -> {k1: 0,', ' k2 1}}'], 'E720:', 2)
CheckDefSuccess(['var Fx = {a -> [0,', ' 1]}']) CheckDefSuccess(['var Fx = {a -> [0,', ' 1]}'])
CheckDefFailure(['var Fx = {a -> [0', ' 1]}'], 'E696:', 2) CheckDefFailure(['var Fx = {a -> [0', ' 1]}'], 'E696:', 2)
@ -1894,59 +1894,68 @@ def Test_expr7_dict()
var lines =<< trim END var lines =<< trim END
assert_equal(g:dict_empty, {}) assert_equal(g:dict_empty, {})
assert_equal(g:dict_empty, { }) assert_equal(g:dict_empty, { })
assert_equal(g:dict_one, {'one': 1}) assert_equal(g:dict_one, {['one']: 1})
var key = 'one' var key = 'one'
var val = 1 var val = 1
assert_equal(g:dict_one, {[key]: val}) assert_equal(g:dict_one, {[key]: val})
var numbers: dict<number> = {a: 1, b: 2, c: 3} var numbers: dict<number> = {a: 1, b: 2, c: 3}
numbers = #{a: 1} numbers = {a: 1}
numbers = #{} numbers = {}
var strings: dict<string> = {a: 'a', b: 'b', c: 'c'} var strings: dict<string> = {a: 'a', b: 'b', c: 'c'}
strings = #{a: 'x'} strings = {a: 'x'}
strings = #{} strings = {}
var dash = {xx-x: 8}
assert_equal({['xx-x']: 8}, dash)
var dnr = {8: 8}
assert_equal({['8']: 8}, dnr)
var mixed: dict<any> = {a: 'a', b: 42} var mixed: dict<any> = {a: 'a', b: 42}
mixed = #{a: 'x'} mixed = {a: 'x'}
mixed = #{a: 234} mixed = {a: 234}
mixed = #{} mixed = {}
var dictlist: dict<list<string>> = #{absent: [], present: ['hi']} var dictlist: dict<list<string>> = {absent: [], present: ['hi']}
dictlist = #{absent: ['hi'], present: []} dictlist = {absent: ['hi'], present: []}
dictlist = #{absent: [], present: []} dictlist = {absent: [], present: []}
var dictdict: dict<dict<string>> = #{one: #{a: 'text'}, two: #{}} var dictdict: dict<dict<string>> = {one: {a: 'text'}, two: {}}
dictdict = #{one: #{}, two: #{a: 'text'}} dictdict = {one: {}, two: {a: 'text'}}
dictdict = #{one: #{}, two: #{}} dictdict = {one: {}, two: {}}
assert_equal({'': 0}, {matchstr('string', 'wont match'): 0}) assert_equal({['']: 0}, {[matchstr('string', 'wont match')]: 0})
assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'}) assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'})
assert_equal(g:test_hash_dict, {one: 1, two: 2}) assert_equal(g:test_hash_dict, {one: 1, two: 2})
END END
CheckDefAndScriptSuccess(lines) CheckDefAndScriptSuccess(lines)
CheckDefFailure(["var x = #{a:8}"], 'E1069:', 1) # legacy syntax doesn't work
CheckDefFailure(["var x = #{a : 8}"], 'E1068:', 1) CheckDefFailure(["var x = #{key: 8}"], 'E1097:', 2)
CheckDefFailure(["var x = #{a :8}"], 'E1068:', 1) CheckDefFailure(["var x = {'key': 8}"], 'E1014:', 1)
CheckDefFailure(["var x = #{a: 8 , b: 9}"], 'E1068:', 1) CheckDefFailure(["var x = 'a' .. #{a: 1}"], 'E1097:', 2)
CheckDefFailure(["var x = #{a: 1,b: 2}"], 'E1069:', 1)
CheckDefFailure(["var x = #{8: 8}"], 'E1014:', 1) CheckDefFailure(["var x = {a:8}"], 'E1069:', 1)
CheckDefFailure(["var x = #{xxx}"], 'E720:', 1) CheckDefFailure(["var x = {a : 8}"], 'E1059:', 1)
CheckDefFailure(["var x = #{xxx: 1", "var y = 2"], 'E722:', 2) CheckDefFailure(["var x = {a :8}"], 'E1059:', 1)
CheckDefFailure(["var x = #{xxx: 1,"], 'E723:', 2) CheckDefFailure(["var x = {a: 8 , b: 9}"], 'E1068:', 1)
CheckDefFailure(["var x = {'a': xxx}"], 'E1001:', 1) CheckDefFailure(["var x = {a: 1,b: 2}"], 'E1069:', 1)
CheckDefFailure(["var x = {xx-x: 8}"], 'E1001:', 1)
CheckDefFailure(["var x = #{a: 1, a: 2}"], 'E721:', 1) CheckDefFailure(["var x = {xxx}"], 'E720:', 1)
CheckDefFailure(["var x = {xxx: 1", "var y = 2"], 'E722:', 2)
CheckDefFailure(["var x = {xxx: 1,"], 'E723:', 2)
CheckDefFailure(["var x = {['a']: xxx}"], 'E1001:', 1)
CheckDefFailure(["var x = {a: 1, a: 2}"], 'E721:', 1)
CheckDefExecFailure(["var x = g:anint.member"], 'E715:', 1) CheckDefExecFailure(["var x = g:anint.member"], 'E715:', 1)
CheckDefExecFailure(["var x = g:dict_empty.member"], 'E716:', 1) CheckDefExecFailure(["var x = g:dict_empty.member"], 'E716:', 1)
CheckDefExecFailure(['var x: dict<number> = #{a: 234, b: "1"}'], 'E1012:', 1) CheckDefExecFailure(['var x: dict<number> = {a: 234, b: "1"}'], 'E1012:', 1)
CheckDefExecFailure(['var x: dict<number> = #{a: "x", b: 134}'], 'E1012:', 1) CheckDefExecFailure(['var x: dict<number> = {a: "x", b: 134}'], 'E1012:', 1)
CheckDefExecFailure(['var x: dict<string> = #{a: 234, b: "1"}'], 'E1012:', 1) CheckDefExecFailure(['var x: dict<string> = {a: 234, b: "1"}'], 'E1012:', 1)
CheckDefExecFailure(['var x: dict<string> = #{a: "x", b: 134}'], 'E1012:', 1) CheckDefExecFailure(['var x: dict<string> = {a: "x", b: 134}'], 'E1012:', 1)
CheckDefFailure(['var x = ({'], 'E723:', 2) CheckDefFailure(['var x = ({'], 'E723:', 2)
CheckDefExecFailure(['{}[getftype("")]'], 'E716: Key not present in Dictionary: ""', 1) CheckDefExecFailure(['{}[getftype("")]'], 'E716: Key not present in Dictionary: ""', 1)
@ -1956,89 +1965,89 @@ def Test_expr7_dict_vim9script()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var d = { var d = {
'one': ['one']:
1, 1,
'two': 2, ['two']: 2,
} }
assert_equal({'one': 1, 'two': 2}, d) assert_equal({one: 1, two: 2}, d)
d = { # comment d = { # comment
'one': ['one']:
# comment # comment
1, 1,
# comment # comment
# comment # comment
'two': 2, ['two']: 2,
} }
assert_equal({'one': 1, 'two': 2}, d) assert_equal({one: 1, two: 2}, d)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = { "one": "one", "two": "two", } var d = { ["one"]: "one", ["two"]: "two", }
assert_equal({'one': 'one', 'two': 'two'}, d) assert_equal({one: 'one', two: 'two'}, d)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one: 1, var d = {one: 1,
two: 2, two: 2,
} }
assert_equal({'one': 1, 'two': 2}, d) assert_equal({one: 1, two: 2}, d)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one:1, two: 2} var d = {one:1, two: 2}
END END
CheckScriptFailure(lines, 'E1069:', 2) CheckScriptFailure(lines, 'E1069:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one: 1,two: 2} var d = {one: 1,two: 2}
END END
CheckScriptFailure(lines, 'E1069:', 2) CheckScriptFailure(lines, 'E1069:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one : 1} var d = {one : 1}
END END
CheckScriptFailure(lines, 'E1068:', 2) CheckScriptFailure(lines, 'E1059:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one:1} var d = {one:1}
END END
CheckScriptFailure(lines, 'E1069:', 2) CheckScriptFailure(lines, 'E1069:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var d = #{one: 1 , two: 2} var d = {one: 1 , two: 2}
END END
CheckScriptFailure(lines, 'E1068:', 2) CheckScriptFailure(lines, 'E1068:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var l: dict<number> = #{a: 234, b: 'x'} var l: dict<number> = {a: 234, b: 'x'}
END END
CheckScriptFailure(lines, 'E1012:', 2) CheckScriptFailure(lines, 'E1012:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var l: dict<number> = #{a: 'x', b: 234} var l: dict<number> = {a: 'x', b: 234}
END END
CheckScriptFailure(lines, 'E1012:', 2) CheckScriptFailure(lines, 'E1012:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var l: dict<string> = #{a: 'x', b: 234} var l: dict<string> = {a: 'x', b: 234}
END END
CheckScriptFailure(lines, 'E1012:', 2) CheckScriptFailure(lines, 'E1012:', 2)
lines =<< trim END lines =<< trim END
vim9script vim9script
var l: dict<string> = #{a: 234, b: 'x'} var l: dict<string> = {a: 234, b: 'x'}
END END
CheckScriptFailure(lines, 'E1012:', 2) CheckScriptFailure(lines, 'E1012:', 2)
@ -2047,7 +2056,7 @@ def Test_expr7_dict_vim9script()
def Failing() def Failing()
job_stop() job_stop()
enddef enddef
var dict = #{name: Failing} var dict = {name: Failing}
END END
if has('channel') if has('channel')
CheckScriptFailure(lines, 'E119:', 1) CheckScriptFailure(lines, 'E119:', 1)
@ -2067,15 +2076,15 @@ def Test_expr_member()
]) ])
assert_equal(1, d assert_equal(1, d
.one) .one)
d = {'1': 1, '_': 2} d = {1: 1, _: 2}
assert_equal(1, d assert_equal(1, d
.1) .1)
assert_equal(2, d assert_equal(2, d
._) ._)
# getting the one member should clear the dict after getting the item # getting the one member should clear the dict after getting the item
assert_equal('one', #{one: 'one'}.one) assert_equal('one', {one: 'one'}.one)
assert_equal('one', #{one: 'one'}[g:oneString]) assert_equal('one', {one: 'one'}[g:oneString])
CheckDefFailure(["var x = g:dict_one.#$!"], 'E1002:', 1) CheckDefFailure(["var x = g:dict_one.#$!"], 'E1002:', 1)
CheckDefExecFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2) CheckDefExecFailure(["var d: dict<any>", "echo d['a']"], 'E716:', 2)
@ -2141,7 +2150,7 @@ def Test_expr7_any_index_slice()
assert_equal([], g:testlist[1:-4]) assert_equal([], g:testlist[1:-4])
assert_equal([], g:testlist[1:-9]) assert_equal([], g:testlist[1:-9])
g:testdict = #{a: 1, b: 2} g:testdict = {a: 1, b: 2}
assert_equal(1, g:testdict['a']) assert_equal(1, g:testdict['a'])
assert_equal(2, g:testdict['b']) assert_equal(2, g:testdict['b'])
END END
@ -2172,7 +2181,7 @@ enddef
def Test_expr_member_vim9script() def Test_expr_member_vim9script()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var d = #{one: var d = {one:
'one', 'one',
two: 'two', two: 'two',
1: 1, 1: 1,
@ -2406,7 +2415,7 @@ def Test_expr7_not()
assert_equal(true, !test_null_dict()) assert_equal(true, !test_null_dict())
assert_equal(true, !{}) assert_equal(true, !{})
assert_equal(false, !{'yes': 'no'}) assert_equal(false, !{yes: 'no'})
if has('channel') if has('channel')
assert_equal(true, !test_null_job()) assert_equal(true, !test_null_job())
@ -2433,7 +2442,7 @@ func Test_expr7_fails()
call CheckDefFailure(["var x = +'xx'"], "E1030:", 1) call CheckDefFailure(["var x = +'xx'"], "E1030:", 1)
call CheckDefFailure(["var x = -0z12"], "E974:", 1) call CheckDefFailure(["var x = -0z12"], "E974:", 1)
call CheckDefExecFailure(["var x = -[8]"], "E39:", 1) call CheckDefExecFailure(["var x = -[8]"], "E39:", 1)
call CheckDefExecFailure(["var x = -{'a': 1}"], "E39:", 1) call CheckDefExecFailure(["var x = -{a: 1}"], "E39:", 1)
call CheckDefFailure(["var x = @"], "E1002:", 1) call CheckDefFailure(["var x = @"], "E1002:", 1)
call CheckDefFailure(["var x = @<"], "E354:", 1) call CheckDefFailure(["var x = @<"], "E354:", 1)
@ -2464,8 +2473,8 @@ func Test_expr7_fails()
call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1) call CheckDefFailure(["'yes'->", "Echo()"], 'E488: Trailing characters: ->', 1)
call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2) call CheckDefExecFailure(["[1, 2->len()"], 'E697:', 2)
call CheckDefExecFailure(["#{a: 1->len()"], 'E722:', 1) call CheckDefExecFailure(["{a: 1->len()"], 'E451:', 1)
call CheckDefExecFailure(["{'a': 1->len()"], 'E723:', 2) call CheckDefExecFailure(["{['a']: 1->len()"], 'E723:', 2)
endfunc endfunc
let g:Funcrefs = [function('add')] let g:Funcrefs = [function('add')]
@ -2507,7 +2516,7 @@ def Test_expr7_trailing()
assert_equal([2, 5, 8], l) assert_equal([2, 5, 8], l)
# dict member # dict member
var d = #{key: 123} var d = {key: 123}
assert_equal(123, d.key) assert_equal(123, d.key)
enddef enddef
@ -2594,7 +2603,7 @@ enddef
def Test_expr7_dict_subscript() def Test_expr7_dict_subscript()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
var l = [#{lnum: 2}, #{lnum: 1}] var l = [{lnum: 2}, {lnum: 1}]
var res = l[0].lnum > l[1].lnum var res = l[0].lnum > l[1].lnum
assert_true(res) assert_true(res)
END END
@ -2629,7 +2638,7 @@ def Test_expr7_subscript_linebreak()
assert_equal('1', l[ assert_equal('1', l[
1]) 1])
var d = #{one: 33} var d = {one: 33}
assert_equal(33, d. assert_equal(33, d.
one) one)
enddef enddef
@ -2643,9 +2652,9 @@ def Test_expr7_method_call()
bwipe! bwipe!
var bufnr = bufnr() var bufnr = bufnr()
var loclist = [#{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}] var loclist = [{bufnr: bufnr, lnum: 42, col: 17, text: 'wrong'}]
loclist->setloclist(0) loclist->setloclist(0)
assert_equal([#{bufnr: bufnr, assert_equal([{bufnr: bufnr,
lnum: 42, lnum: 42,
col: 17, col: 17,
text: 'wrong', text: 'wrong',
@ -2657,7 +2666,7 @@ def Test_expr7_method_call()
module: ''} module: ''}
], getloclist(0)) ], getloclist(0))
var result: bool = get(#{n: 0}, 'n', 0) var result: bool = get({n: 0}, 'n', 0)
assert_equal(false, result) assert_equal(false, result)
enddef enddef

View File

@ -30,7 +30,7 @@ def TestCompilingError()
END END
call writefile(lines, 'XTest_compile_error') call writefile(lines, 'XTest_compile_error')
var buf = RunVimInTerminal('-S XTest_compile_error', var buf = RunVimInTerminal('-S XTest_compile_error',
#{rows: 10, wait_for_ruler: 0}) {rows: 10, wait_for_ruler: 0})
var text = '' var text = ''
for loop in range(100) for loop in range(100)
text = '' text = ''
@ -838,16 +838,16 @@ def Test_vim9script_call()
def DictFunc(arg: dict<number>) def DictFunc(arg: dict<number>)
dictvar = arg dictvar = arg
enddef enddef
{'a': 1, 'b': 2}->DictFunc() {a: 1, b: 2}->DictFunc()
dictvar->assert_equal(#{a: 1, b: 2}) dictvar->assert_equal({a: 1, b: 2})
def CompiledDict() def CompiledDict()
{'a': 3, 'b': 4}->DictFunc() {a: 3, b: 4}->DictFunc()
enddef enddef
CompiledDict() CompiledDict()
dictvar->assert_equal(#{a: 3, b: 4}) dictvar->assert_equal({a: 3, b: 4})
#{a: 3, b: 4}->DictFunc() {a: 3, b: 4}->DictFunc()
dictvar->assert_equal(#{a: 3, b: 4}) dictvar->assert_equal({a: 3, b: 4})
('text')->MyFunc() ('text')->MyFunc()
name->assert_equal('text') name->assert_equal('text')
@ -1272,7 +1272,7 @@ def Test_error_reporting()
lines =<< trim END lines =<< trim END
vim9script vim9script
def Func() def Func()
var db = #{foo: 1, bar: 2} var db = {foo: 1, bar: 2}
# comment # comment
var x = db.asdf var x = db.asdf
enddef enddef
@ -1607,7 +1607,7 @@ def Test_ignore_silent_error_in_filter()
return popup_filter_menu(winid, key) return popup_filter_menu(winid, key)
enddef enddef
popup_create('popup', #{filter: Filter}) popup_create('popup', {filter: Filter})
feedkeys("o\r", 'xnt') feedkeys("o\r", 'xnt')
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
@ -1639,7 +1639,7 @@ def Test_closure_in_map()
writefile(['222'], 'XclosureDir/file2') writefile(['222'], 'XclosureDir/file2')
writefile(['333'], 'XclosureDir/tdir/file3') writefile(['333'], 'XclosureDir/tdir/file3')
TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {'tdir': ['file3']}]) TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
delete('XclosureDir', 'rf') delete('XclosureDir', 'rf')
enddef enddef
@ -1672,20 +1672,20 @@ enddef
def Test_partial_call() def Test_partial_call()
var Xsetlist = function('setloclist', [0]) var Xsetlist = function('setloclist', [0])
Xsetlist([], ' ', {'title': 'test'}) Xsetlist([], ' ', {title: 'test'})
getloclist(0, {'title': 1})->assert_equal({'title': 'test'}) getloclist(0, {title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setloclist', [0, [], ' ']) Xsetlist = function('setloclist', [0, [], ' '])
Xsetlist({'title': 'test'}) Xsetlist({title: 'test'})
getloclist(0, {'title': 1})->assert_equal({'title': 'test'}) getloclist(0, {title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setqflist') Xsetlist = function('setqflist')
Xsetlist([], ' ', {'title': 'test'}) Xsetlist([], ' ', {title: 'test'})
getqflist({'title': 1})->assert_equal({'title': 'test'}) getqflist({title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setqflist', [[], ' ']) Xsetlist = function('setqflist', [[], ' '])
Xsetlist({'title': 'test'}) Xsetlist({title: 'test'})
getqflist({'title': 1})->assert_equal({'title': 'test'}) getqflist({title: 1})->assert_equal({title: 'test'})
var Len: func: number = function('len', ['word']) var Len: func: number = function('len', ['word'])
assert_equal(4, Len()) assert_equal(4, Len())

View File

@ -177,14 +177,14 @@ def Test_const()
cl[1] = 88 cl[1] = 88
constlist->assert_equal([1, [77, 88], 3]) constlist->assert_equal([1, [77, 88], 3])
var vardict = #{five: 5, six: 6} var vardict = {five: 5, six: 6}
const constdict = #{one: 1, two: vardict, three: 3} const constdict = {one: 1, two: vardict, three: 3}
vardict['five'] = 55 vardict['five'] = 55
# TODO: does not work yet # TODO: does not work yet
# constdict['two']['six'] = 66 # constdict['two']['six'] = 66
var cd = constdict['two'] var cd = constdict['two']
cd['six'] = 66 cd['six'] = 66
constdict->assert_equal(#{one: 1, two: #{five: 55, six: 66}, three: 3}) constdict->assert_equal({one: 1, two: {five: 55, six: 66}, three: 3})
END END
CheckDefAndScriptSuccess(lines) CheckDefAndScriptSuccess(lines)
enddef enddef
@ -212,14 +212,14 @@ def Test_const_bang()
CheckScriptFailure(['vim9script'] + lines, 'E684:', 3) CheckScriptFailure(['vim9script'] + lines, 'E684:', 3)
lines =<< trim END lines =<< trim END
const dd = #{one: 1, two: 2} const dd = {one: 1, two: 2}
dd["one"] = 99 dd["one"] = 99
END END
CheckDefExecFailure(lines, 'E1121:', 2) CheckDefExecFailure(lines, 'E1121:', 2)
CheckScriptFailure(['vim9script'] + lines, 'E741:', 3) CheckScriptFailure(['vim9script'] + lines, 'E741:', 3)
lines =<< trim END lines =<< trim END
const dd = #{one: 1, two: 2} const dd = {one: 1, two: 2}
dd["three"] = 99 dd["three"] = 99
END END
CheckDefExecFailure(lines, 'E1120:') CheckDefExecFailure(lines, 'E1120:')
@ -385,7 +385,7 @@ def Test_try_catch()
endtry endtry
assert_equal(121, n) assert_equal(121, n)
var d = #{one: 1} var d = {one: 1}
try try
n = d[g:astring] n = d[g:astring]
catch /E716:/ catch /E716:/
@ -775,7 +775,7 @@ def Test_vim9_import_export()
g:imported_func = Exported() g:imported_func = Exported()
def GetExported(): string def GetExported(): string
var local_dict = #{ref: Exported} var local_dict = {ref: Exported}
return local_dict.ref() return local_dict.ref()
enddef enddef
g:funcref_result = GetExported() g:funcref_result = GetExported()
@ -1133,7 +1133,7 @@ def Run_Test_import_fails_on_command_line()
END END
writefile(export, 'XexportCmd.vim') writefile(export, 'XexportCmd.vim')
var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', #{ var buf = RunVimInTerminal('-c "import Foo from ''./XexportCmd.vim''"', {
rows: 6, wait_for_ruler: 0}) rows: 6, wait_for_ruler: 0})
WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))}) WaitForAssert({-> assert_match('^E1094:', term_getline(buf, 5))})
@ -1733,7 +1733,7 @@ def Test_execute_cmd()
execute 'echomsg' (n ? '"true"' : '"no"') execute 'echomsg' (n ? '"true"' : '"no"')
assert_match('^true$', Screenline(&lines)) assert_match('^true$', Screenline(&lines))
echomsg [1, 2, 3] #{a: 1, b: 2} echomsg [1, 2, 3] {a: 1, b: 2}
assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines)) assert_match('^\[1, 2, 3\] {''a'': 1, ''b'': 2}$', Screenline(&lines))
CheckDefFailure(['execute xxx'], 'E1001:', 1) CheckDefFailure(['execute xxx'], 'E1001:', 1)
@ -2024,26 +2024,26 @@ def Test_automatic_line_continuation()
assert_equal(['one', 'two', 'three'], mylist) assert_equal(['one', 'two', 'three'], mylist)
var mydict = { var mydict = {
'one': 1, ['one']: 1,
'two': 2, ['two']: 2,
'three': ['three']:
3, 3,
} # comment } # comment
assert_equal({'one': 1, 'two': 2, 'three': 3}, mydict) assert_equal({one: 1, two: 2, three: 3}, mydict)
mydict = #{ mydict = {
one: 1, # comment one: 1, # comment
two: # comment two: # comment
2, # comment 2, # comment
three: 3 # comment three: 3 # comment
} }
assert_equal(#{one: 1, two: 2, three: 3}, mydict) assert_equal({one: 1, two: 2, three: 3}, mydict)
mydict = #{ mydict = {
one: 1, one: 1,
two: two:
2, 2,
three: 3 three: 3
} }
assert_equal(#{one: 1, two: 2, three: 3}, mydict) assert_equal({one: 1, two: 2, three: 3}, mydict)
assert_equal( assert_equal(
['one', 'two', 'three'], ['one', 'two', 'three'],
@ -2864,7 +2864,7 @@ def Run_Test_define_func_at_command_line()
END END
writefile([''], 'Xdidcmd') writefile([''], 'Xdidcmd')
writefile(lines, 'XcallFunc') writefile(lines, 'XcallFunc')
var buf = RunVimInTerminal('-S XcallFunc', #{rows: 6}) var buf = RunVimInTerminal('-S XcallFunc', {rows: 6})
# define Afunc() on the command line # define Afunc() on the command line
term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>") term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
term_sendkeys(buf, ":call CheckAndQuit()\<CR>") term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
@ -2959,7 +2959,7 @@ def Test_catch_exception_in_callback()
g:caught = 'yes' g:caught = 'yes'
endtry endtry
enddef enddef
popup_menu('popup', #{callback: Callback}) popup_menu('popup', {callback: Callback})
feedkeys("\r", 'xt') feedkeys("\r", 'xt')
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
@ -2981,7 +2981,7 @@ def Test_no_unknown_error_after_error()
sleep 1m sleep 1m
source += l source += l
enddef enddef
var myjob = job_start('echo burp', #{out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'}) var myjob = job_start('echo burp', {out_cb: Out_cb, exit_cb: Exit_cb, mode: 'raw'})
sleep 100m sleep 100m
END END
writefile(lines, 'Xdef') writefile(lines, 'Xdef')

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2082,
/**/ /**/
2081, 2081,
/**/ /**/

View File

@ -2127,12 +2127,12 @@ free_imported(cctx_T *cctx)
} }
/* /*
* Return TRUE if "p" points at a "#" but not at "#{". * Return TRUE if "p" points at a "#". Does not check for white space.
*/ */
int int
vim9_comment_start(char_u *p) vim9_comment_start(char_u *p)
{ {
return p[0] == '#' && p[1] != '{'; return p[0] == '#';
} }
/* /*
@ -2840,14 +2840,6 @@ to_name_const_end(char_u *arg)
if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
p = arg; p = arg;
} }
else if (p == arg && *arg == '#' && arg[1] == '{')
{
// Can be "#{a: 1}->Func()".
++p;
if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
p = arg;
}
return p; return p;
} }
@ -2999,12 +2991,12 @@ compile_lambda_call(char_u **arg, cctx_T *cctx)
} }
/* /*
* parse a dict: {'key': val} or #{key: val} * parse a dict: {key: val, [key]: val}
* "*arg" points to the '{'. * "*arg" points to the '{'.
* ppconst->pp_is_const is set if all item values are a constant. * ppconst->pp_is_const is set if all item values are a constant.
*/ */
static int static int
compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst) compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{ {
garray_T *instr = &cctx->ctx_instr; garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack; garray_T *stack = &cctx->ctx_type_stack;
@ -3022,7 +3014,6 @@ compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
for (;;) for (;;)
{ {
char_u *key = NULL; char_u *key = NULL;
char_u *end;
if (may_get_next_line(whitep, arg, cctx) == FAIL) if (may_get_next_line(whitep, arg, cctx) == FAIL)
{ {
@ -3033,14 +3024,12 @@ compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
if (**arg == '}') if (**arg == '}')
break; break;
// Eventually {name: value} will use "name" as a literal key and // {name: value} uses "name" as a literal key and
// {[expr]: value} for an evaluated key. // {[expr]: value} uses an evaluated key.
// Temporarily: if "name" is indeed a valid key, or "[expr]" is if (**arg != '[')
// used, use the new method, like JavaScript. Otherwise fall back
// to the old method.
end = to_name_end(*arg, FALSE);
if (literal || *end == ':')
{ {
char_u *end = skip_literal_key(*arg);
if (end == *arg) if (end == *arg)
{ {
semsg(_(e_invalid_key_str), *arg); semsg(_(e_invalid_key_str), *arg);
@ -3054,10 +3043,8 @@ compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
else else
{ {
isn_T *isn; isn_T *isn;
int has_bracket = **arg == '[';
if (has_bracket) *arg = skipwhite(*arg + 1);
*arg = skipwhite(*arg + 1);
if (compile_expr0(arg, cctx) == FAIL) if (compile_expr0(arg, cctx) == FAIL)
return FAIL; return FAIL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
@ -3071,16 +3058,13 @@ compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst)
FALSE, FALSE) == FAIL) FALSE, FALSE) == FAIL)
return FAIL; return FAIL;
} }
if (has_bracket) *arg = skipwhite(*arg);
if (**arg != ']')
{ {
*arg = skipwhite(*arg); emsg(_(e_missing_matching_bracket_after_dict_key));
if (**arg != ']') return FAIL;
{
emsg(_(e_missing_matching_bracket_after_dict_key));
return FAIL;
}
++*arg;
} }
++*arg;
} }
// Check for duplicate keys, if using string keys. // Check for duplicate keys, if using string keys.
@ -3766,8 +3750,7 @@ compile_subscript(
* $VAR environment variable * $VAR environment variable
* (expression) nested expression * (expression) nested expression
* [expr, expr] List * [expr, expr] List
* {key: val, key: val} Dictionary * {key: val, [key]: val} Dictionary
* #{key: val, key: val} Dictionary with literal keys
* *
* Also handle: * Also handle:
* ! in front logical NOT * ! in front logical NOT
@ -3883,18 +3866,6 @@ compile_expr7(
case '[': ret = compile_list(arg, cctx, ppconst); case '[': ret = compile_list(arg, cctx, ppconst);
break; break;
/*
* Dictionary: #{key: val, key: val}
*/
case '#': if ((*arg)[1] == '{')
{
++*arg;
ret = compile_dict(arg, cctx, TRUE, ppconst);
}
else
ret = NOTDONE;
break;
/* /*
* Lambda: {arg, arg -> expr} * Lambda: {arg, arg -> expr}
* Dictionary: {'key': val, 'key': val} * Dictionary: {'key': val, 'key': val}
@ -3910,7 +3881,7 @@ compile_expr7(
if (ret != FAIL && *start == '>') if (ret != FAIL && *start == '>')
ret = compile_lambda(arg, cctx); ret = compile_lambda(arg, cctx);
else else
ret = compile_dict(arg, cctx, FALSE, ppconst); ret = compile_dict(arg, cctx, ppconst);
} }
break; break;
@ -4200,7 +4171,7 @@ compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
} }
*arg = skipwhite(op + oplen); *arg = skipwhite(op + oplen);
if (may_get_next_line(op + oplen, arg, cctx) == FAIL) if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
return FAIL; return FAIL;
// get the second expression // get the second expression
@ -7484,13 +7455,9 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
switch (*ea.cmd) switch (*ea.cmd)
{ {
case '#': case '#':
// "#" starts a comment, but "#{" does not. // "#" starts a comment
if (ea.cmd[1] != '{') line = (char_u *)"";
{ continue;
line = (char_u *)"";
continue;
}
break;
case '}': case '}':
{ {