To: vim_dev@googlegroups.com Subject: Patch 8.2.3137 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3137 Problem: Vim9: no error when a line only has a variable name. Solution: Give an error when an expression is evaluated without an effect. (closes #8538) Files: src/ex_eval.c, src/errors.h, src/vim9compile.c, src/testdir/test_vim9_script.vim, src/testdir/test_vim9_expr.vim, src/testdir/test_vim9_func.vim *** ../vim-8.2.3136/src/ex_eval.c 2021-07-08 19:22:09.238611461 +0200 --- src/ex_eval.c 2021-07-10 19:13:20.105983373 +0200 *************** *** 208,214 **** * not skipped. Errors in those commands may affect what of the subsequent * commands are regarded part of catch and finally clauses. Catching the * exception would then cause execution of commands not intended by the ! * user, who wouldn't even get aware of the problem. Therefor, discard the * exception currently being thrown to prevent it from being caught. Just * execute finally clauses and terminate. */ --- 208,214 ---- * not skipped. Errors in those commands may affect what of the subsequent * commands are regarded part of catch and finally clauses. Catching the * exception would then cause execution of commands not intended by the ! * user, who wouldn't even get aware of the problem. Therefore, discard the * exception currently being thrown to prevent it from being caught. Just * execute finally clauses and terminate. */ *************** *** 896,906 **** --- 896,923 ---- { typval_T tv; evalarg_T evalarg; + int name_only = FALSE; + char_u *p; + long lnum = SOURCING_LNUM; + + if (in_vim9script()) + { + char_u *alias; + + p = eap->arg; + get_name_len(&p, &alias, FALSE, FALSE); + name_only = ends_excmd2(eap->arg, skipwhite(p)); + vim_free(alias); + } fill_evalarg_from_eap(&evalarg, eap, eap->skip); if (eval0(eap->arg, &tv, eap, &evalarg) == OK) + { clear_tv(&tv); + if (in_vim9script() && name_only && lnum == SOURCING_LNUM) + semsg(_(e_expression_without_effect_str), eap->arg); + } clear_evalarg(&evalarg, eap); } *************** *** 1287,1293 **** { // Try to find the matching ":while". This might stop at a try // conditional not in its finally clause (which is then to be executed ! // next). Therefor, inactivate all conditionals except the ":while" // itself (if reached). idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE); if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR))) --- 1304,1310 ---- { // Try to find the matching ":while". This might stop at a try // conditional not in its finally clause (which is then to be executed ! // next). Therefore, inactivate all conditionals except the ":while" // itself (if reached). idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE); if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR))) *** ../vim-8.2.3136/src/errors.h 2021-07-10 13:15:35.291053015 +0200 --- src/errors.h 2021-07-10 19:12:43.182031229 +0200 *************** *** 494,496 **** --- 494,498 ---- INIT(= N_("E1205: No white space allowed between option and")); EXTERN char e_dict_required_for_argument_nr[] INIT(= N_("E1206: Dictionary required for argument %d")); + EXTERN char e_expression_without_effect_str[] + INIT(= N_("E1207: Expression without an effect: %s")); *** ../vim-8.2.3136/src/vim9compile.c 2021-07-07 21:55:20.850427019 +0200 --- src/vim9compile.c 2021-07-10 19:33:09.564915463 +0200 *************** *** 8563,8568 **** --- 8563,8599 ---- return p; } + static char_u * + compile_eval(char_u *arg, cctx_T *cctx) + { + char_u *p = arg; + int name_only; + char_u *alias; + long lnum = SOURCING_LNUM; + + // find_ex_command() will consider a variable name an expression, assuming + // that something follows on the next line. Check that something actually + // follows, otherwise it's probably a misplaced command. + get_name_len(&p, &alias, FALSE, FALSE); + name_only = ends_excmd2(arg, skipwhite(p)); + vim_free(alias); + + p = arg; + if (compile_expr0(&p, cctx) == FAIL) + return NULL; + + if (name_only && lnum == SOURCING_LNUM) + { + semsg(_(e_expression_without_effect_str), arg); + return NULL; + } + + // drop the result + generate_instr_drop(cctx, ISN_DROP, 1); + + return skipwhite(p); + } + /* * compile "echo expr" * compile "echomsg expr" *************** *** 9630,9642 **** break; case CMD_eval: ! if (compile_expr0(&p, &cctx) == FAIL) ! goto erret; ! ! // drop the result ! generate_instr_drop(&cctx, ISN_DROP, 1); ! ! line = skipwhite(p); break; case CMD_echo: --- 9661,9667 ---- break; case CMD_eval: ! line = compile_eval(p, &cctx); break; case CMD_echo: *** ../vim-8.2.3136/src/testdir/test_vim9_script.vim 2021-07-08 22:02:07.504975025 +0200 --- src/testdir/test_vim9_script.vim 2021-07-10 19:34:09.024855400 +0200 *************** *** 691,697 **** def Test_cnext_works_in_catch() var lines =<< trim END vim9script ! au BufEnter * eval 0 writefile(['text'], 'Xfile1') writefile(['text'], 'Xfile2') var items = [ --- 691,697 ---- def Test_cnext_works_in_catch() var lines =<< trim END vim9script ! au BufEnter * eval 1 + 2 writefile(['text'], 'Xfile1') writefile(['text'], 'Xfile2') var items = [ *************** *** 1754,1759 **** --- 1754,1774 ---- CheckScriptFailure(lines, 'E1041:', 5) enddef + def Test_script_var_shadows_command() + var lines =<< trim END + var undo = 1 + undo = 2 + assert_equal(2, undo) + END + CheckDefAndScriptSuccess(lines) + + lines =<< trim END + var undo = 1 + undo + END + CheckDefAndScriptFailure(lines, 'E1207:', 2) + enddef + def s:RetSome(): string return 'some' enddef *************** *** 2270,2276 **** assert_equal(false, res) # with constant "false" expression may be invalid so long as the syntax is OK ! if false | eval 0 | endif if false | eval burp + 234 | endif if false | echo burp 234 'asd' | endif if false --- 2285,2291 ---- assert_equal(false, res) # with constant "false" expression may be invalid so long as the syntax is OK ! if false | eval 1 + 2 | endif if false | eval burp + 234 | endif if false | echo burp 234 'asd' | endif if false *** ../vim-8.2.3136/src/testdir/test_vim9_expr.vim 2021-07-04 15:54:04.939754549 +0200 --- src/testdir/test_vim9_expr.vim 2021-07-10 19:19:35.785489731 +0200 *************** *** 647,653 **** CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2) ! CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4) CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2) CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2) --- 647,653 ---- CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2) ! CheckDefExecAndScriptFailure2(['var items: any', 'eval 1 + 1', 'eval 2 + 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4) CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2) CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2) *** ../vim-8.2.3136/src/testdir/test_vim9_func.vim 2021-07-04 15:54:04.939754549 +0200 --- src/testdir/test_vim9_func.vim 2021-07-10 19:40:01.056476090 +0200 *************** *** 2538,2544 **** set eventignore= autocmd QuickFixCmdPost * copen def AutocmdsDisabled() ! eval 0 enddef func Func() noautocmd call s:AutocmdsDisabled() --- 2538,2544 ---- set eventignore= autocmd QuickFixCmdPost * copen def AutocmdsDisabled() ! eval 1 + 2 enddef func Func() noautocmd call s:AutocmdsDisabled() *************** *** 2551,2558 **** enddef def StackTop() ! eval 1 ! eval 2 # call not on fourth line StackBot() enddef --- 2551,2558 ---- enddef def StackTop() ! eval 1 + 2 ! eval 2 + 3 # call not on fourth line StackBot() enddef *** ../vim-8.2.3136/src/version.c 2021-07-10 17:59:44.679330992 +0200 --- src/version.c 2021-07-10 19:12:09.258075044 +0200 *************** *** 757,758 **** --- 757,760 ---- { /* Add new patch number below this line */ + /**/ + 3137, /**/ -- hundred-and-one symptoms of being an internet addict: 109. You actually read -- and enjoy -- lists like this. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///