Skip to content

Commit cd6b4f3

Browse files
committed
patch 8.2.3353: Vim9: type of argument for negate not checked at compile time
Problem: Vim9: type of argument for negate not checked at compile time. Solution: Add a compile time check.
1 parent 4bba16d commit cd6b4f3

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

src/testdir/test_vim9_disassemble.vim

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,25 +1680,27 @@ def Test_disassemble_any_slice()
16801680
enddef
16811681

16821682
def NegateNumber(): number
1683-
var nr = 9
1684-
var plus = +nr
1685-
var res = -nr
1686-
return res
1683+
g:nr = 9
1684+
var plus = +g:nr
1685+
var minus = -g:nr
1686+
return minus
16871687
enddef
16881688

16891689
def Test_disassemble_negate_number()
16901690
var instr = execute('disassemble NegateNumber')
16911691
assert_match('NegateNumber\_s*' ..
1692-
'var nr = 9\_s*' ..
1693-
'\d STORE 9 in $0\_s*' ..
1694-
'var plus = +nr\_s*' ..
1695-
'\d LOAD $0\_s*' ..
1696-
'\d CHECKNR\_s*' ..
1697-
'\d STORE $1\_s*' ..
1698-
'var res = -nr\_s*' ..
1699-
'\d LOAD $0\_s*' ..
1692+
'g:nr = 9\_s*' ..
1693+
'\d PUSHNR 9\_s*' ..
1694+
'\d STOREG g:nr\_s*' ..
1695+
'var plus = +g:nr\_s*' ..
1696+
'\d LOADG g:nr\_s*' ..
1697+
'\d CHECKTYPE number stack\[-1\]\_s*' ..
1698+
'\d STORE $0\_s*' ..
1699+
'var minus = -g:nr\_s*' ..
1700+
'\d LOADG g:nr\_s*' ..
1701+
'\d CHECKTYPE number stack\[-1\]\_s*' ..
17001702
'\d NEGATENR\_s*' ..
1701-
'\d STORE $2\_s*',
1703+
'\d STORE $1\_s*',
17021704
instr)
17031705
assert_equal(-9, NegateNumber())
17041706
enddef

src/testdir/test_vim9_expr.vim

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,15 +3123,26 @@ def Test_expr7_not()
31233123
CheckDefAndScriptSuccess(lines)
31243124
enddef
31253125

3126+
let g:anumber = 42
3127+
3128+
def Test_expr7_negate()
3129+
var lines =<< trim END
3130+
var nr = 1
3131+
assert_equal(-1, -nr)
3132+
assert_equal(-42, -g:anumber)
3133+
END
3134+
CheckDefAndScriptSuccess(lines)
3135+
enddef
3136+
31263137
func Test_expr7_fails()
31273138
call CheckDefFailure(["var x = (12"], "E1097:", 3)
31283139
call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2)
31293140

31303141
call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1)
31313142
call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1)
31323143
call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1)
3133-
call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1)
3134-
call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1)
3144+
call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E1012:", 'E745:', 1)
3145+
call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E1012:", 'E728:', 1)
31353146

31363147
call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1)
31373148
call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1)
@@ -3154,10 +3165,10 @@ func Test_expr7_fails()
31543165
call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1)
31553166
call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1)
31563167

3157-
call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1)
3158-
call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1)
3159-
call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1)
3160-
call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1)
3168+
call CheckDefExecAndScriptFailure2(["var x = +g:astring"], 'E1012:', 'E1030:', 1)
3169+
call CheckDefExecAndScriptFailure2(["var x = +g:ablob"], 'E1012:', 'E974:', 1)
3170+
call CheckDefExecAndScriptFailure2(["var x = +g:alist"], 'E1012:', 'E745:', 1)
3171+
call CheckDefExecAndScriptFailure2(["var x = +g:adict"], 'E1012:', 'E728:', 1)
31613172

31623173
call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E1229: Expected dictionary for using key "memb", but got string', 'E488:', 2)
31633174

src/testdir/test_vim9_script.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,21 +469,21 @@ def Test_try_catch_throw()
469469

470470
try
471471
n = -g:astring
472-
catch /E39:/
472+
catch /E1012:/
473473
n = 233
474474
endtry
475475
assert_equal(233, n)
476476

477477
try
478478
n = +g:astring
479-
catch /E1030:/
479+
catch /E1012:/
480480
n = 244
481481
endtry
482482
assert_equal(244, n)
483483

484484
try
485485
n = +g:alist
486-
catch /E745:/
486+
catch /E1012:/
487487
n = 255
488488
endtry
489489
assert_equal(255, n)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3353,
758760
/**/
759761
3352,
760762
/**/

src/vim9compile.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,10 +4210,15 @@ compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
42104210
--p;
42114211
if (*p == '-' || *p == '+')
42124212
{
4213-
int negate = *p == '-';
4214-
isn_T *isn;
4213+
int negate = *p == '-';
4214+
isn_T *isn;
4215+
garray_T *stack = &cctx->ctx_type_stack;
4216+
type_T *type;
4217+
4218+
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4219+
if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
4220+
return FAIL;
42154221

4216-
// TODO: check type
42174222
while (p > start && (p[-1] == '-' || p[-1] == '+'))
42184223
{
42194224
--p;
@@ -4222,11 +4227,11 @@ compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
42224227
}
42234228
// only '-' has an effect, for '+' we only check the type
42244229
if (negate)
4230+
{
42254231
isn = generate_instr(cctx, ISN_NEGATENR);
4226-
else
4227-
isn = generate_instr(cctx, ISN_CHECKNR);
4228-
if (isn == NULL)
4229-
return FAIL;
4232+
if (isn == NULL)
4233+
return FAIL;
4234+
}
42304235
}
42314236
else if (numeric_only)
42324237
{
@@ -5809,7 +5814,6 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx)
58095814
goto theend;
58105815
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
58115816
}
5812-
// TODO: warning for trailing text?
58135817

58145818
theend:
58155819
vim_free(lambda_name);
@@ -5852,7 +5856,6 @@ generate_loadvar(
58525856
switch (dest)
58535857
{
58545858
case dest_option:
5855-
// TODO: check the option exists
58565859
generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
58575860
break;
58585861
case dest_global:

0 commit comments

Comments
 (0)