Skip to content

Commit

Permalink
patch 8.2.2082: Vim9: can still use the depricated #{} dict syntax
Browse files Browse the repository at this point in the history
Problem:    Vim9: can still use the depricated #{} dict syntax.
Solution:   Remove support for #{} in Vim9 script. (closes #7406, closes #7405)
  • Loading branch information
brammool committed Dec 2, 2020
1 parent 7f76494 commit e0de171
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 272 deletions.
27 changes: 18 additions & 9 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,20 +782,31 @@ dict2string(typval_T *tv, int copyID, int restore_copyID)
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".
* Return FAIL when there is no valid key.
*/
static int
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;

for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
;
tv->v_type = VAR_STRING;
tv->vval.v_string = vim_strnsave(*arg, p - *arg);

Expand Down Expand Up @@ -851,17 +862,15 @@ eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
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)
goto failret;
}
else
{
int has_bracket = vim9script && **arg == '[';

if (has_bracket)
*arg = skipwhite(*arg + 1);
if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
Expand Down
2 changes: 1 addition & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ eval7(
/*
* Dictionary: #{key: val, key: val}
*/
case '#': if ((*arg)[1] == '{')
case '#': if (!in_vim9script() && (*arg)[1] == '{')
{
++*arg;
ret = eval_dict(arg, rettv, evalarg, TRUE);
Expand Down
1 change: 1 addition & 0 deletions src/proto/dict.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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_bool(dict_T *d, char_u *key, int def);
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);
void dict_extend(dict_T *d1, dict_T *d2, char_u *action);
dictitem_T *dict_lookup(hashitem_T *hi);
Expand Down
4 changes: 2 additions & 2 deletions src/testdir/test_popupwin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ func Test_popup_scrollbar()
endfunc

def CreatePopup(text: list<string>)
popup_create(text, #{
popup_create(text, {
\ minwidth: 30,
\ maxwidth: 30,
\ minheight: 4,
Expand Down Expand Up @@ -2717,7 +2717,7 @@ def Popupwin_close_prevwin()
split
wincmd b
assert_equal(2, winnr())
var buf = term_start(&shell, #{hidden: 1})
var buf = term_start(&shell, {hidden: 1})
popup_create(buf, {})
TermWait(buf, 100)
popup_clear(true)
Expand Down
12 changes: 6 additions & 6 deletions src/testdir/test_textprop.vim
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ def Test_prop_find2()
# Multiple props per line, start on the first, should find the second.
new
['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 col in [8, 14, 24, 38]
prop_add(lnum, col, #{type: 'misspell', length: 2})
prop_add(lnum, col, {type: 'misspell', length: 2})
endfor
endfor
cursor(1, 8)
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 expected = {lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', length: 2, start: 1}
var result = prop_find({type: 'misspell', skipstart: true}, 'f')
assert_equal(expected, result)

prop_type_delete('misspell')
Expand Down Expand Up @@ -322,7 +322,7 @@ func Test_prop_remove()
endfunc

def Test_prop_add_vim9()
prop_type_add('comment', #{
prop_type_add('comment', {
highlight: 'Directory',
priority: 123,
start_incl: true,
Expand All @@ -336,7 +336,7 @@ def Test_prop_remove_vim9()
new
AddPropTypes()
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()
bwipe!
enddef
Expand Down
56 changes: 28 additions & 28 deletions src/testdir/test_vim9_assign.vim
Original file line number Diff line number Diff line change
Expand Up @@ -360,26 +360,26 @@ def Test_extend_dict()
var lines =<< trim END
vim9script
var d: dict<number>
extend(d, #{a: 1})
assert_equal(#{a: 1}, d)
extend(d, {a: 1})
assert_equal({a: 1}, d)

var d2: dict<number>
d2['one'] = 1
assert_equal(#{one: 1}, d2)
assert_equal({one: 1}, d2)
END
CheckScriptSuccess(lines)

lines =<< trim END
vim9script
var d: dict<string> = test_null_dict()
extend(d, #{a: 'x'})
assert_equal(#{a: 'x'}, d)
extend(d, {a: 'x'})
assert_equal({a: 'x'}, d)
END
CheckScriptSuccess(lines)

lines =<< trim END
vim9script
extend(test_null_dict(), #{a: 'x'})
extend(test_null_dict(), {a: 'x'})
END
CheckScriptFailure(lines, 'E1133:', 2)
enddef
Expand Down Expand Up @@ -487,31 +487,31 @@ def Test_assignment_list_vim9script()
enddef

def Test_assignment_dict()
var dict1: dict<bool> = #{one: false, two: true}
var dict2: dict<number> = #{one: 1, two: 2}
var dict3: dict<string> = #{key: 'value'}
var dict4: dict<any> = #{one: 1, two: '2'}
var dict5: dict<blob> = #{one: 0z01, two: 0z02}
var dict1: dict<bool> = {one: false, two: true}
var dict2: dict<number> = {one: 1, two: 2}
var dict3: dict<string> = {key: 'value'}
var dict4: dict<any> = {one: 1, two: '2'}
var dict5: dict<blob> = {one: 0z01, two: 0z02}

# overwrite
dict3['key'] = 'another'
assert_equal(dict3, #{key: 'another'})
assert_equal(dict3, {key: 'another'})
dict3.key = 'yet another'
assert_equal(dict3, #{key: 'yet another'})
assert_equal(dict3, {key: 'yet another'})

var lines =<< trim END
var dd = #{one: 1}
var dd = {one: 1}
dd.one) = 2
END
CheckDefFailure(lines, 'E15:', 2)

# empty key can be used
var dd = {}
dd[""] = 6
assert_equal({'': 6}, dd)
assert_equal({['']: 6}, dd)

# 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
lines =<< trim END
Expand All @@ -521,7 +521,7 @@ def Test_assignment_dict()
test['a'] = 43
return test
enddef
assert_equal(#{a: 43}, FillDict())
assert_equal({a: 43}, FillDict())
END
CheckScriptSuccess(lines)

Expand All @@ -544,7 +544,7 @@ def Test_assignment_dict()
g:test['a'] = 43
return g:test
enddef
assert_equal(#{a: 43}, FillDict())
assert_equal({a: 43}, FillDict())
END
CheckScriptSuccess(lines)

Expand All @@ -556,7 +556,7 @@ def Test_assignment_dict()
b:test['a'] = 43
return b:test
enddef
assert_equal(#{a: 43}, FillDict())
assert_equal({a: 43}, FillDict())
END
CheckScriptSuccess(lines)
enddef
Expand Down Expand Up @@ -636,7 +636,7 @@ def Test_assignment_default()

if has('unix') && executable('cat')
# check with non-null job and channel, types must match
thejob = job_start("cat ", #{})
thejob = job_start("cat ", {})
thechannel = job_getchannel(thejob)
job_stop(thejob, 'kill')
endif
Expand Down Expand Up @@ -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<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<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>')
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 = feedkeys("0")'], 'E1031:')
CheckDefFailure(['var name: number = feedkeys("0")'], 'expected number but got void')
Expand Down Expand Up @@ -883,17 +883,17 @@ def Test_assign_dict()
for i in range(3)
nrd[i] = i
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<dict<number>> = #{x: #{a: '', b: true}}"], 'E1012: Type mismatch; expected dict<dict<number>> but got dict<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)
enddef

def Test_assign_dict_unknown_type()
var lines =<< trim END
vim9script
var mylist = []
mylist += [#{one: 'one'}]
mylist += [{one: 'one'}]
def Func()
var dd = mylist[0]
assert_equal('one', dd.one)
Expand All @@ -905,7 +905,7 @@ def Test_assign_dict_unknown_type()
lines =<< trim END
vim9script
var mylist = [[]]
mylist[0] += [#{one: 'one'}]
mylist[0] += [{one: 'one'}]
def Func()
var dd = mylist[0][0]
assert_equal('one', dd.one)
Expand Down Expand Up @@ -1010,7 +1010,7 @@ def Test_let_declaration()
g:other_var = other

# type is inferred
s:dict = {'a': 222}
s:dict = {['a']: 222}
def GetDictVal(key: any)
g:dict_val = s:dict[key]
enddef
Expand Down

0 comments on commit e0de171

Please sign in to comment.