vim-patch:8.1.1692: using *{} for literal dict is not backwards compatible

Problem:    Using *{} for literal dict is not backwards compatible. (Yasuhiro
            Matsumoto)
Solution:   Use ~{} instead.
b8be54dcc5
This commit is contained in:
Jan Edmund Lazo 2020-10-07 00:45:05 -04:00
parent d109a33144
commit 20fc7ef161
No known key found for this signature in database
GPG Key ID: 64915E6E9F735B15
3 changed files with 12 additions and 9 deletions

View File

@ -40,7 +40,7 @@ Dictionary An associative, unordered array: Each entry has a key and a
value. |Dictionary| value. |Dictionary|
Examples: Examples:
{'blue': "#0000ff", 'red': "#ff0000"} {'blue': "#0000ff", 'red': "#ff0000"}
*{blue: "#0000ff", red: "#ff0000"} ~{blue: "#0000ff", red: "#ff0000"}
The Number and String types are converted automatically, depending on how they The Number and String types are converted automatically, depending on how they
are used. are used.
@ -441,10 +441,10 @@ entry. Note that the String '04' and the Number 04 are different, since the
Number will be converted to the String '4'. The empty string can also be used Number will be converted to the String '4'. The empty string can also be used
as a key. as a key.
*literal-Dict* *literal-Dict*
To avoid having to put quotes around every key the *{} form can be used. This To avoid having to put quotes around every key the ~{} form can be used. This
does require the key to consist only of ASCII letters, digits, '-' and '_'. does require the key to consist only of ASCII letters, digits, '-' and '_'.
Example: > Example: >
let mydict = *{zero: 0, one_key: 1, two-key: 2, 333: 3} let mydict = ~{zero: 0, one_key: 1, two-key: 2, 333: 3}
Note that 333 here is the string "333". Empty keys are not possible here. Note that 333 here is the string "333". Empty keys are not possible here.
A value can be any expression. Using a Dictionary for a value creates a A value can be any expression. Using a Dictionary for a value creates a

View File

@ -3802,7 +3802,7 @@ static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string)
*/ */
for (;; ) { for (;; ) {
op = **arg; op = **arg;
if ((op != '*' || (*arg)[1] == '{') && op != '/' && op != '%') { if (op != '*' && op != '/' && op != '%') {
break; break;
} }
@ -3906,7 +3906,7 @@ static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string)
// (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 // ~{key: val, key: val} Dictionary with literal keys
// //
// Also handle: // Also handle:
// ! in front logical NOT // ! in front logical NOT
@ -4014,8 +4014,8 @@ static int eval7(
case '[': ret = get_list_tv(arg, rettv, evaluate); case '[': ret = get_list_tv(arg, rettv, evaluate);
break; break;
// Dictionary: *{key: val, key: val} // Dictionary: ~{key: val, key: val}
case '*': case '~':
if ((*arg)[1] == '{') { if ((*arg)[1] == '{') {
(*arg)++; (*arg)++;
ret = dict_get_tv(arg, rettv, evaluate, true); ret = dict_get_tv(arg, rettv, evaluate, true);

View File

@ -281,8 +281,11 @@ func Test_dict_func_remove_in_use()
endfunc endfunc
func Test_dict_literal_keys() func Test_dict_literal_keys()
call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, *{one: 1, two2: 2, 3three: 3, 44: 4},) call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, ~{one: 1, two2: 2, 3three: 3, 44: 4},)
call assert_equal('2 3', trim(execute('echo 2 *{blue: 3}.blue')))
" why *{} cannot be used
let blue = 'blue'
call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
endfunc endfunc
" Nasty: deepcopy() dict that refers to itself (fails when noref used) " Nasty: deepcopy() dict that refers to itself (fails when noref used)