To: vim_dev@googlegroups.com Subject: Patch 8.2.3196 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3196 Problem: Vim9: bool expression with numbers only fails at runtime. Solution: Check constant to be bool at compile time. (closes #8603) Files: src/vim9compile.c, src/testdir/test_vim9_expr.vim *** ../vim-8.2.3195/src/vim9compile.c 2021-07-21 20:38:43.283635192 +0200 --- src/vim9compile.c 2021-07-21 21:27:28.013228572 +0200 *************** *** 2796,2801 **** --- 2796,2819 ---- } /* + * Check that the last item of "ppconst" is a bool. + */ + static int + check_ppconst_bool(ppconst_T *ppconst) + { + if (ppconst->pp_used > 0) + { + typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; + where_T where; + + where.wt_index = 0; + where.wt_variable = FALSE; + return check_typval_type(&t_bool, tv, where); + } + return OK; + } + + /* * Clear ppconst constants. Used when failing. */ static void *************** *** 5138,5143 **** --- 5156,5162 ---- long save_sourcing_lnum; int start_ctx_lnum = cctx->ctx_lnum; int save_lnum; + int status; if (next != NULL) { *************** *** 5152,5179 **** return FAIL; } - // TODO: use ppconst if the value is a constant and check - // evaluating to bool - generate_ppconst(cctx, ppconst); - - // Every part must evaluate to a bool. save_sourcing_lnum = SOURCING_LNUM; SOURCING_LNUM = start_lnum; save_lnum = cctx->ctx_lnum; cctx->ctx_lnum = start_ctx_lnum; ! if (bool_on_stack(cctx) == FAIL) { ! cctx->ctx_lnum = save_lnum; ! ga_clear(&end_ga); ! return FAIL; } cctx->ctx_lnum = save_lnum; ! ! if (ga_grow(&end_ga, 1) == FAIL) { ga_clear(&end_ga); return FAIL; } *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; ++end_ga.ga_len; generate_JUMP(cctx, opchar == '|' --- 5171,5199 ---- return FAIL; } save_sourcing_lnum = SOURCING_LNUM; SOURCING_LNUM = start_lnum; save_lnum = cctx->ctx_lnum; cctx->ctx_lnum = start_ctx_lnum; ! ! status = check_ppconst_bool(ppconst); ! if (status == OK) { ! // TODO: use ppconst if the value is a constant ! generate_ppconst(cctx, ppconst); ! ! // Every part must evaluate to a bool. ! status = (bool_on_stack(cctx)); ! if (status == OK) ! status = ga_grow(&end_ga, 1); } cctx->ctx_lnum = save_lnum; ! if (status == FAIL) { ga_clear(&end_ga); return FAIL; } + *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; ++end_ga.ga_len; generate_JUMP(cctx, opchar == '|' *************** *** 5196,5201 **** --- 5216,5227 ---- p = may_peek_next_line(cctx, *arg, &next); } + + if (check_ppconst_bool(ppconst) == FAIL) + { + ga_clear(&end_ga); + return FAIL; + } generate_ppconst(cctx, ppconst); // Every part must evaluate to a bool. *** ../vim-8.2.3195/src/testdir/test_vim9_expr.vim 2021-07-19 21:04:20.179847743 +0200 --- src/testdir/test_vim9_expr.vim 2021-07-21 21:34:02.172472923 +0200 *************** *** 372,380 **** def Test_expr2_fails() var msg = "White space required before and after '||'" ! call CheckDefAndScriptFailure(["var x = 1||2"], msg, 1) ! call CheckDefAndScriptFailure(["var x = 1 ||2"], msg, 1) ! call CheckDefAndScriptFailure(["var x = 1|| 2"], msg, 1) call CheckDefFailure(["var x = false || "], 'E1097:', 3) call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2) --- 372,380 ---- def Test_expr2_fails() var msg = "White space required before and after '||'" ! call CheckDefAndScriptFailure(["var x = 1||0"], msg, 1) ! call CheckDefAndScriptFailure(["var x = 1 ||0"], msg, 1) ! call CheckDefAndScriptFailure(["var x = 1|| 0"], msg, 1) call CheckDefFailure(["var x = false || "], 'E1097:', 3) call CheckScriptFailure(['vim9script', "var x = false || "], 'E15:', 2) *************** *** 386,393 **** call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) ! # TODO: should fail at compile time ! call CheckDefExecAndScriptFailure(["var x = 3 || 7"], 'E1023:', 1) call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1) call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2) --- 386,393 ---- call CheckDefAndScriptFailure2(["if 'yes' || 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) ! call CheckDefAndScriptFailure2(["var x = 3 || false"], 'E1012:', 'E1023:', 1) ! call CheckDefAndScriptFailure2(["var x = false || 3"], 'E1012:', 'E1023:', 1) call CheckDefAndScriptFailure(["if 3"], 'E1023:', 1) call CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2) *************** *** 505,519 **** def Test_expr3_fails() var msg = "White space required before and after '&&'" ! CheckDefAndScriptFailure(["var x = 1&&2"], msg, 1) ! CheckDefAndScriptFailure(["var x = 1 &&2"], msg, 1) ! CheckDefAndScriptFailure(["var x = 1&& 2"], msg, 1) var lines =<< trim END var x = 1 ! &&2 # comment END ! CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&2"', 2) g:vals = [] CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) --- 505,519 ---- def Test_expr3_fails() var msg = "White space required before and after '&&'" ! CheckDefAndScriptFailure(["var x = 1&&0"], msg, 1) ! CheckDefAndScriptFailure(["var x = 1 &&0"], msg, 1) ! CheckDefAndScriptFailure(["var x = 1&& 0"], msg, 1) var lines =<< trim END var x = 1 ! &&0 # comment END ! CheckDefAndScriptFailure(lines, 'E1004: White space required before and after ''&&'' at "&&0"', 2) g:vals = [] CheckDefAndScriptFailure2(["if 'yes' && 0", 'echo 0', 'endif'], 'E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool', 1) *************** *** 525,531 **** && true endif END ! CheckDefExecAndScriptFailure(lines, 'E1023:', 1) lines =<< trim END if 'yes' --- 525,538 ---- && true endif END ! CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 1) ! ! lines =<< trim END ! if true ! && 3 ! endif ! END ! CheckDefAndScriptFailure2(lines, 'E1012:', 'E1023:', 2) lines =<< trim END if 'yes' *** ../vim-8.2.3195/src/version.c 2021-07-21 20:38:43.283635192 +0200 --- src/version.c 2021-07-21 21:30:36.196873146 +0200 *************** *** 757,758 **** --- 757,760 ---- { /* Add new patch number below this line */ + /**/ + 3196, /**/ -- hundred-and-one symptoms of being an internet addict: 197. Your desk collapses under the weight of your computer peripherals. /// 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 ///