1
0
forked from aniani/vim

patch 8.2.2250: Vim9: sublist is ambiguous

Problem:    Vim9: sublist is ambiguous.
Solution:   Require white space around the colon. (closes #7409)
This commit is contained in:
Bram Moolenaar 2020-12-30 20:39:21 +01:00
parent 2a5c61a019
commit de4f95b041
5 changed files with 111 additions and 72 deletions

View File

@ -3725,6 +3725,7 @@ eval_index(
int range = FALSE; int range = FALSE;
char_u *key = NULL; char_u *key = NULL;
int keylen = -1; int keylen = -1;
int vim9 = in_vim9script();
if (check_can_index(rettv, evaluate, verbose) == FAIL) if (check_can_index(rettv, evaluate, verbose) == FAIL)
return FAIL; return FAIL;
@ -3755,6 +3756,12 @@ eval_index(
empty1 = TRUE; empty1 = TRUE;
else if (eval1(arg, &var1, evalarg) == FAIL) // recursive! else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
return FAIL; return FAIL;
else if (vim9 && **arg == ':')
{
semsg(_(e_white_space_required_before_and_after_str), ":");
clear_tv(&var1);
return FAIL;
}
else if (evaluate && tv_get_string_chk(&var1) == NULL) else if (evaluate && tv_get_string_chk(&var1) == NULL)
{ {
// not a number or string // not a number or string
@ -3769,7 +3776,15 @@ eval_index(
if (**arg == ':') if (**arg == ':')
{ {
range = TRUE; range = TRUE;
*arg = skipwhite_and_linebreak(*arg + 1, evalarg); ++*arg;
if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
{
semsg(_(e_white_space_required_before_and_after_str), ":");
if (!empty1)
clear_tv(&var1);
return FAIL;
}
*arg = skipwhite_and_linebreak(*arg, evalarg);
if (**arg == ']') if (**arg == ']')
empty2 = TRUE; empty2 = TRUE;
else if (eval1(arg, &var2, evalarg) == FAIL) // recursive! else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!

View File

@ -1285,7 +1285,7 @@ enddef
def StringSlice(): string def StringSlice(): string
var s = "abcd" var s = "abcd"
var res = s[1:8] var res = s[1 : 8]
return res return res
enddef enddef
@ -1295,7 +1295,7 @@ def Test_disassemble_string_slice()
'var s = "abcd"\_s*' .. 'var s = "abcd"\_s*' ..
'\d PUSHS "abcd"\_s*' .. '\d PUSHS "abcd"\_s*' ..
'\d STORE $0\_s*' .. '\d STORE $0\_s*' ..
'var res = s\[1:8]\_s*' .. 'var res = s\[1 : 8]\_s*' ..
'\d LOAD $0\_s*' .. '\d LOAD $0\_s*' ..
'\d PUSHNR 1\_s*' .. '\d PUSHNR 1\_s*' ..
'\d PUSHNR 8\_s*' .. '\d PUSHNR 8\_s*' ..
@ -1331,7 +1331,7 @@ enddef
def ListSlice(): list<number> def ListSlice(): list<number>
var l = [1, 2, 3] var l = [1, 2, 3]
var res = l[1:8] var res = l[1 : 8]
return res return res
enddef enddef
@ -1344,7 +1344,7 @@ def Test_disassemble_list_slice()
'\d PUSHNR 3\_s*' .. '\d PUSHNR 3\_s*' ..
'\d NEWLIST size 3\_s*' .. '\d NEWLIST size 3\_s*' ..
'\d STORE $0\_s*' .. '\d STORE $0\_s*' ..
'var res = l\[1:8]\_s*' .. 'var res = l\[1 : 8]\_s*' ..
'\d LOAD $0\_s*' .. '\d LOAD $0\_s*' ..
'\d PUSHNR 1\_s*' .. '\d PUSHNR 1\_s*' ..
'\d PUSHNR 8\_s*' .. '\d PUSHNR 8\_s*' ..
@ -1405,14 +1405,14 @@ def Test_disassemble_any_index()
enddef enddef
def AnySlice(): list<number> def AnySlice(): list<number>
var res = g:somelist[1:3] var res = g:somelist[1 : 3]
return res return res
enddef enddef
def Test_disassemble_any_slice() def Test_disassemble_any_slice()
var instr = execute('disassemble AnySlice') var instr = execute('disassemble AnySlice')
assert_match('AnySlice\_s*' .. assert_match('AnySlice\_s*' ..
'var res = g:somelist\[1:3\]\_s*' .. 'var res = g:somelist\[1 : 3\]\_s*' ..
'\d LOADG g:somelist\_s*' .. '\d LOADG g:somelist\_s*' ..
'\d PUSHNR 1\_s*' .. '\d PUSHNR 1\_s*' ..
'\d PUSHNR 3\_s*' .. '\d PUSHNR 3\_s*' ..

View File

@ -1718,6 +1718,15 @@ def Test_expr7_list()
Main() Main()
END END
CheckScriptFailure(lines, 'E1127:') CheckScriptFailure(lines, 'E1127:')
lines =<< trim END
var numbers = [1, 2, 3, 4]
var a = 1
var b = 2
END
CheckDefAndScriptFailure(lines + ['echo numbers[1:b]'], 'E1004:', 4)
CheckDefAndScriptFailure(lines + ['echo numbers[1: b]'], 'E1004:', 4)
CheckDefAndScriptFailure(lines + ['echo numbers[a :b]'], 'E1004:', 4)
enddef enddef
def Test_expr7_list_vim9script() def Test_expr7_list_vim9script()
@ -2274,13 +2283,13 @@ def Test_expr7_any_index_slice()
assert_equal('', g:teststring[-1]) assert_equal('', g:teststring[-1])
assert_equal('', g:teststring[99]) assert_equal('', g:teststring[99])
assert_equal('b', g:teststring[1:1]) assert_equal('b', g:teststring[1 : 1])
assert_equal('bcdef', g:teststring[1:]) assert_equal('bcdef', g:teststring[1 :])
assert_equal('abcd', g:teststring[:3]) assert_equal('abcd', g:teststring[: 3])
assert_equal('cdef', g:teststring[-4:]) assert_equal('cdef', g:teststring[-4 :])
assert_equal('abcdef', g:teststring[-9:]) assert_equal('abcdef', g:teststring[-9 :])
assert_equal('abcd', g:teststring[:-3]) assert_equal('abcd', g:teststring[: -3])
assert_equal('', g:teststring[:-9]) assert_equal('', g:teststring[: -9])
# blob index cannot be out of range # blob index cannot be out of range
g:testblob = 0z01ab g:testblob = 0z01ab
@ -2290,13 +2299,13 @@ def Test_expr7_any_index_slice()
assert_equal(0x01, g:testblob[-2]) assert_equal(0x01, g:testblob[-2])
# blob slice accepts out of range # blob slice accepts out of range
assert_equal(0z01ab, g:testblob[0:1]) assert_equal(0z01ab, g:testblob[0 : 1])
assert_equal(0z01, g:testblob[0:0]) assert_equal(0z01, g:testblob[0 : 0])
assert_equal(0z01, g:testblob[-2:-2]) assert_equal(0z01, g:testblob[-2 : -2])
assert_equal(0zab, g:testblob[1:1]) assert_equal(0zab, g:testblob[1 : 1])
assert_equal(0zab, g:testblob[-1:-1]) assert_equal(0zab, g:testblob[-1 : -1])
assert_equal(0z, g:testblob[2:2]) assert_equal(0z, g:testblob[2 : 2])
assert_equal(0z, g:testblob[0:-3]) assert_equal(0z, g:testblob[0 : -3])
# list index cannot be out of range # list index cannot be out of range
g:testlist = [0, 1, 2, 3] g:testlist = [0, 1, 2, 3]
@ -2308,19 +2317,19 @@ def Test_expr7_any_index_slice()
assert_equal(1, g:testlist[g:theone]) assert_equal(1, g:testlist[g:theone])
# list slice accepts out of range # list slice accepts out of range
assert_equal([0], g:testlist[0:0]) assert_equal([0], g:testlist[0 : 0])
assert_equal([3], g:testlist[3:3]) assert_equal([3], g:testlist[3 : 3])
assert_equal([0, 1], g:testlist[0:1]) assert_equal([0, 1], g:testlist[0 : 1])
assert_equal([0, 1, 2, 3], g:testlist[0:3]) assert_equal([0, 1, 2, 3], g:testlist[0 : 3])
assert_equal([0, 1, 2, 3], g:testlist[0:9]) assert_equal([0, 1, 2, 3], g:testlist[0 : 9])
assert_equal([], g:testlist[-1:1]) assert_equal([], g:testlist[-1 : 1])
assert_equal([1], g:testlist[-3:1]) assert_equal([1], g:testlist[-3 : 1])
assert_equal([0, 1], g:testlist[-4:1]) assert_equal([0, 1], g:testlist[-4 : 1])
assert_equal([0, 1], g:testlist[-9:1]) assert_equal([0, 1], g:testlist[-9 : 1])
assert_equal([1, 2, 3], g:testlist[1:-1]) assert_equal([1, 2, 3], g:testlist[1 : -1])
assert_equal([1], g:testlist[1:-3]) assert_equal([1], g:testlist[1 : -3])
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'])
@ -2340,8 +2349,8 @@ def Test_expr7_any_index_slice()
CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1) CheckDefExecFailure(['echo g:testlist[-5]'], 'E684:', 1)
CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2) CheckScriptFailure(['vim9script', 'echo g:testlist[-5]'], 'E684:', 2)
CheckDefExecFailure(['echo g:testdict["a":"b"]'], 'E719:', 1) CheckDefExecFailure(['echo g:testdict["a" : "b"]'], 'E719:', 1)
CheckScriptFailure(['vim9script', 'echo g:testdict["a":"b"]'], 'E719:', 2) CheckScriptFailure(['vim9script', 'echo g:testdict["a" : "b"]'], 'E719:', 2)
CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1) CheckDefExecFailure(['echo g:testdict[1]'], 'E716:', 1)
CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2) CheckScriptFailure(['vim9script', 'echo g:testdict[1]'], 'E716:', 2)
@ -2746,45 +2755,45 @@ def Test_expr7_string_subscript()
assert_equal('', text[6]) assert_equal('', text[6])
assert_equal('', text[999]) assert_equal('', text[999])
assert_equal('ábçdëf', text[0:-1]) assert_equal('ábçdëf', text[0 : -1])
assert_equal('ábçdëf', text[0 :-1]) assert_equal('ábçdëf', text[0 : -1])
assert_equal('ábçdëf', text[0: -1]) assert_equal('ábçdëf', text[0 : -1])
assert_equal('ábçdëf', text[0 : -1]) assert_equal('ábçdëf', text[0 : -1])
assert_equal('ábçdëf', text[0 assert_equal('ábçdëf', text[0
:-1]) : -1])
assert_equal('ábçdëf', text[0: assert_equal('ábçdëf', text[0 :
-1]) -1])
assert_equal('ábçdëf', text[0 : -1 assert_equal('ábçdëf', text[0 : -1
]) ])
assert_equal('bçdëf', text[1:-1]) assert_equal('bçdëf', text[1 : -1])
assert_equal('çdëf', text[2:-1]) assert_equal('çdëf', text[2 : -1])
assert_equal('dëf', text[3:-1]) assert_equal('dëf', text[3 : -1])
assert_equal('ëf', text[4:-1]) assert_equal('ëf', text[4 : -1])
assert_equal('f', text[5:-1]) assert_equal('f', text[5 : -1])
assert_equal('', text[6:-1]) assert_equal('', text[6 : -1])
assert_equal('', text[999:-1]) assert_equal('', text[999 : -1])
assert_equal('ábçd', text[:3]) assert_equal('ábçd', text[: 3])
assert_equal('bçdëf', text[1:]) assert_equal('bçdëf', text[1 :])
assert_equal('ábçdëf', text[:]) assert_equal('ábçdëf', text[:])
END END
CheckDefSuccess(lines) CheckDefSuccess(lines)
CheckScriptSuccess(['vim9script'] + lines) CheckScriptSuccess(['vim9script'] + lines)
lines =<< trim END lines =<< trim END
var d = 'asdf'[1: var d = 'asdf'[1 :
END END
CheckDefFailure(lines, 'E1097:', 3) CheckDefFailure(lines, 'E1097:', 3)
lines =<< trim END lines =<< trim END
var d = 'asdf'[1:xxx] var d = 'asdf'[1 : xxx]
END END
CheckDefFailure(lines, 'E1001:', 1) CheckDefFailure(lines, 'E1001:', 1)
lines =<< trim END lines =<< trim END
var d = 'asdf'[1:2 var d = 'asdf'[1 : 2
END END
CheckDefFailure(lines, 'E1097:', 3) CheckDefFailure(lines, 'E1097:', 3)
lines =<< trim END lines =<< trim END
var d = 'asdf'[1:2 var d = 'asdf'[1 : 2
echo d echo d
END END
CheckDefFailure(lines, 'E111:', 2) CheckDefFailure(lines, 'E111:', 2)
@ -2794,12 +2803,12 @@ def Test_expr7_string_subscript()
END END
CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
lines =<< trim END lines =<< trim END
var d = 'asdf'['1':2] var d = 'asdf'['1' : 2]
echo d echo d
END END
CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
lines =<< trim END lines =<< trim END
var d = 'asdf'[1:'2'] var d = 'asdf'[1 : '2']
echo d echo d
END END
CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1) CheckDefFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
@ -2813,22 +2822,22 @@ def Test_expr7_list_subscript()
assert_equal(4, list[-1]) assert_equal(4, list[-1])
assert_equal(0, list[-5]) assert_equal(0, list[-5])
assert_equal([0, 1, 2, 3, 4], list[0:4]) assert_equal([0, 1, 2, 3, 4], list[0 : 4])
assert_equal([0, 1, 2, 3, 4], list[:]) assert_equal([0, 1, 2, 3, 4], list[:])
assert_equal([1, 2, 3, 4], list[1:]) assert_equal([1, 2, 3, 4], list[1 :])
assert_equal([2, 3, 4], list[2:-1]) assert_equal([2, 3, 4], list[2 : -1])
assert_equal([4], list[4:-1]) assert_equal([4], list[4 : -1])
assert_equal([], list[5:-1]) assert_equal([], list[5 : -1])
assert_equal([], list[999:-1]) assert_equal([], list[999 : -1])
assert_equal([1, 2, 3, 4], list[g:theone:g:thefour]) assert_equal([1, 2, 3, 4], list[g:theone : g:thefour])
assert_equal([0, 1, 2, 3], list[0:3]) assert_equal([0, 1, 2, 3], list[0 : 3])
assert_equal([0], list[0:0]) assert_equal([0], list[0 : 0])
assert_equal([0, 1, 2, 3, 4], list[0:-1]) assert_equal([0, 1, 2, 3, 4], list[0 : -1])
assert_equal([0, 1, 2], list[0:-3]) assert_equal([0, 1, 2], list[0 : -3])
assert_equal([0], list[0:-5]) assert_equal([0], list[0 : -5])
assert_equal([], list[0:-6]) assert_equal([], list[0 : -6])
assert_equal([], list[0:-99]) assert_equal([], list[0 : -99])
END END
CheckDefSuccess(lines) CheckDefSuccess(lines)
CheckScriptSuccess(['vim9script'] + lines) CheckScriptSuccess(['vim9script'] + lines)

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 */
/**/
2250,
/**/ /**/
2249, 2249,
/**/ /**/

View File

@ -3695,19 +3695,33 @@ compile_subscript(
if (may_get_next_line_error(p, arg, cctx) == FAIL) if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL; return FAIL;
if (**arg == ':') if (**arg == ':')
{
// missing first index is equal to zero // missing first index is equal to zero
generate_PUSHNR(cctx, 0); generate_PUSHNR(cctx, 0);
}
else else
{ {
if (compile_expr0(arg, cctx) == FAIL) if (compile_expr0(arg, cctx) == FAIL)
return FAIL; return FAIL;
if (**arg == ':')
{
semsg(_(e_white_space_required_before_and_after_str), ":");
return FAIL;
}
if (may_get_next_line_error(p, arg, cctx) == FAIL) if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL; return FAIL;
*arg = skipwhite(*arg); *arg = skipwhite(*arg);
} }
if (**arg == ':') if (**arg == ':')
{ {
*arg = skipwhite(*arg + 1); is_slice = TRUE;
++*arg;
if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
{
semsg(_(e_white_space_required_before_and_after_str), ":");
return FAIL;
}
*arg = skipwhite(*arg);
if (may_get_next_line_error(p, arg, cctx) == FAIL) if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL; return FAIL;
if (**arg == ']') if (**arg == ']')
@ -3721,7 +3735,6 @@ compile_subscript(
return FAIL; return FAIL;
*arg = skipwhite(*arg); *arg = skipwhite(*arg);
} }
is_slice = TRUE;
} }
if (**arg != ']') if (**arg != ']')