Patch 8.2.3353
Problem: Vim9: type of argument for negate not checked at compile time.
Solution: Add a compile time check.
Files: src/vim9compile.c, src/testdir/test_vim9_expr.vim,
src/testdir/test_vim9_script.vim,
src/testdir/test_vim9_disassemble.vim
*** ../vim-8.2.3352/src/vim9compile.c 2021-08-13 19:40:47.417028286 +0200
--- src/vim9compile.c 2021-08-15 19:43:30.345191256 +0200
***************
*** 4210,4219 ****
--p;
if (*p == '-' || *p == '+')
{
! int negate = *p == '-';
! isn_T *isn;
- // TODO: check type
while (p > start && (p[-1] == '-' || p[-1] == '+'))
{
--p;
--- 4210,4224 ----
--p;
if (*p == '-' || *p == '+')
{
! int negate = *p == '-';
! isn_T *isn;
! garray_T *stack = &cctx->ctx_type_stack;
! type_T *type;
!
! type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
! if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
! return FAIL;
while (p > start && (p[-1] == '-' || p[-1] == '+'))
{
--p;
***************
*** 4222,4232 ****
}
// only '-' has an effect, for '+' we only check the type
if (negate)
isn = generate_instr(cctx, ISN_NEGATENR);
! else
! isn = generate_instr(cctx, ISN_CHECKNR);
! if (isn == NULL)
! return FAIL;
}
else if (numeric_only)
{
--- 4227,4237 ----
}
// only '-' has an effect, for '+' we only check the type
if (negate)
+ {
isn = generate_instr(cctx, ISN_NEGATENR);
! if (isn == NULL)
! return FAIL;
! }
}
else if (numeric_only)
{
***************
*** 5809,5815 ****
goto theend;
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
}
- // TODO: warning for trailing text?
theend:
vim_free(lambda_name);
--- 5814,5819 ----
***************
*** 5852,5858 ****
switch (dest)
{
case dest_option:
- // TODO: check the option exists
generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
break;
case dest_global:
--- 5856,5861 ----
*** ../vim-8.2.3352/src/testdir/test_vim9_expr.vim 2021-08-07
17:20:07.966856597 +0200
--- src/testdir/test_vim9_expr.vim 2021-08-15 19:51:22.050632751 +0200
***************
*** 3123,3128 ****
--- 3123,3139 ----
CheckDefAndScriptSuccess(lines)
enddef
+ let g:anumber = 42
+
+ def Test_expr7_negate()
+ var lines =<< trim END
+ var nr = 1
+ assert_equal(-1, -nr)
+ assert_equal(-42, -g:anumber)
+ END
+ CheckDefAndScriptSuccess(lines)
+ enddef
+
func Test_expr7_fails()
call CheckDefFailure(["var x = (12"], "E1097:", 3)
call CheckScriptFailure(['vim9script', "var x = (12"], 'E110:', 2)
***************
*** 3130,3137 ****
call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1)
call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1)
call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1)
! call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E39:", 'E745:', 1)
! call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E39:", 'E728:', 1)
call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1)
call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1)
--- 3141,3148 ----
call CheckDefAndScriptFailure(["var x = -'xx'"], "E1030:", 1)
call CheckDefAndScriptFailure(["var x = +'xx'"], "E1030:", 1)
call CheckDefAndScriptFailure(["var x = -0z12"], "E974:", 1)
! call CheckDefExecAndScriptFailure2(["var x = -[8]"], "E1012:", 'E745:', 1)
! call CheckDefExecAndScriptFailure2(["var x = -{a: 1}"], "E1012:", 'E728:',
1)
call CheckDefAndScriptFailure(["var x = @"], "E1002:", 1)
call CheckDefAndScriptFailure(["var x = @<"], "E354:", 1)
***************
*** 3154,3163 ****
call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1)
call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1)
! call CheckDefExecAndScriptFailure(["var x = +g:astring"], 'E1030:', 1)
! call CheckDefExecAndScriptFailure(["var x = +g:ablob"], 'E974:', 1)
! call CheckDefExecAndScriptFailure(["var x = +g:alist"], 'E745:', 1)
! call CheckDefExecAndScriptFailure(["var x = +g:adict"], 'E728:', 1)
call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E1229:
Expected dictionary for using key "memb", but got string', 'E488:', 2)
--- 3165,3174 ----
call CheckDefAndScriptFailure2(["echo l:somevar"], 'E1075:', 'E121:', 1)
call CheckDefAndScriptFailure2(["echo x:somevar"], 'E1075:', 'E121:', 1)
! call CheckDefExecAndScriptFailure2(["var x = +g:astring"], 'E1012:',
'E1030:', 1)
! call CheckDefExecAndScriptFailure2(["var x = +g:ablob"], 'E1012:', 'E974:',
1)
! call CheckDefExecAndScriptFailure2(["var x = +g:alist"], 'E1012:', 'E745:',
1)
! call CheckDefExecAndScriptFailure2(["var x = +g:adict"], 'E1012:', 'E728:',
1)
call CheckDefAndScriptFailure2(["var x = ''", "var y = x.memb"], 'E1229:
Expected dictionary for using key "memb", but got string', 'E488:', 2)
*** ../vim-8.2.3352/src/testdir/test_vim9_script.vim 2021-08-07
18:12:35.495528716 +0200
--- src/testdir/test_vim9_script.vim 2021-08-15 20:06:34.043106304 +0200
***************
*** 469,489 ****
try
n = -g:astring
! catch /E39:/
n = 233
endtry
assert_equal(233, n)
try
n = +g:astring
! catch /E1030:/
n = 244
endtry
assert_equal(244, n)
try
n = +g:alist
! catch /E745:/
n = 255
endtry
assert_equal(255, n)
--- 469,489 ----
try
n = -g:astring
! catch /E1012:/
n = 233
endtry
assert_equal(233, n)
try
n = +g:astring
! catch /E1012:/
n = 244
endtry
assert_equal(244, n)
try
n = +g:alist
! catch /E1012:/
n = 255
endtry
assert_equal(255, n)
*** ../vim-8.2.3352/src/testdir/test_vim9_disassemble.vim 2021-08-13
19:40:47.417028286 +0200
--- src/testdir/test_vim9_disassemble.vim 2021-08-15 20:34:59.847469119
+0200
***************
*** 1680,1704 ****
enddef
def NegateNumber(): number
! var nr = 9
! var plus = +nr
! var res = -nr
! return res
enddef
def Test_disassemble_negate_number()
var instr = execute('disassemble NegateNumber')
assert_match('NegateNumber\_s*' ..
! 'var nr = 9\_s*' ..
! '\d STORE 9 in $0\_s*' ..
! 'var plus = +nr\_s*' ..
! '\d LOAD $0\_s*' ..
! '\d CHECKNR\_s*' ..
! '\d STORE $1\_s*' ..
! 'var res = -nr\_s*' ..
! '\d LOAD $0\_s*' ..
'\d NEGATENR\_s*' ..
! '\d STORE $2\_s*',
instr)
assert_equal(-9, NegateNumber())
enddef
--- 1680,1706 ----
enddef
def NegateNumber(): number
! g:nr = 9
! var plus = +g:nr
! var minus = -g:nr
! return minus
enddef
def Test_disassemble_negate_number()
var instr = execute('disassemble NegateNumber')
assert_match('NegateNumber\_s*' ..
! 'g:nr = 9\_s*' ..
! '\d PUSHNR 9\_s*' ..
! '\d STOREG g:nr\_s*' ..
! 'var plus = +g:nr\_s*' ..
! '\d LOADG g:nr\_s*' ..
! '\d CHECKTYPE number stack\[-1\]\_s*' ..
! '\d STORE $0\_s*' ..
! 'var minus = -g:nr\_s*' ..
! '\d LOADG g:nr\_s*' ..
! '\d CHECKTYPE number stack\[-1\]\_s*' ..
'\d NEGATENR\_s*' ..
! '\d STORE $1\_s*',
instr)
assert_equal(-9, NegateNumber())
enddef
*** ../vim-8.2.3352/src/version.c 2021-08-15 19:28:01.808558478 +0200
--- src/version.c 2021-08-15 19:45:04.356599861 +0200
***************
*** 757,758 ****
--- 757,760 ----
{ /* Add new patch number below this line */
+ /**/
+ 3353,
/**/
--
John: When I'm playing tennis with friends I always get carried away
George: You hurt your foot each time?
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/202108151840.17FIeZBE4069505%40masaka.moolenaar.net.