diff options
-rw-r--r-- | src/errors.h | 2 | ||||
-rw-r--r-- | src/eval.c | 7 | ||||
-rw-r--r-- | src/testdir/test_vim9_expr.vim | 5 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9execute.c | 10 |
5 files changed, 20 insertions, 6 deletions
diff --git a/src/errors.h b/src/errors.h index eb5ee1de2e..4bd4efea69 100644 --- a/src/errors.h +++ b/src/errors.h @@ -343,3 +343,5 @@ EXTERN char e_mismatched_enddef[] INIT(= N_("E1152: Mismatched enddef")); EXTERN char e_invalid_operation_for_bool[] INIT(= N_("E1153: Invalid operation for bool")); +EXTERN char e_divide_by_zero[] + INIT(= N_("E1154: Divide by zero")); diff --git a/src/eval.c b/src/eval.c index 13ee49aac4..40c6feb706 100644 --- a/src/eval.c +++ b/src/eval.c @@ -63,8 +63,10 @@ num_divide(varnumber_T n1, varnumber_T n2) { varnumber_T result; - if (n2 == 0) // give an error message? + if (n2 == 0) { + if (in_vim9script()) + emsg(_(e_divide_by_zero)); if (n1 == 0) result = VARNUM_MIN; // similar to NaN else if (n1 < 0) @@ -84,7 +86,8 @@ num_divide(varnumber_T n1, varnumber_T n2) varnumber_T num_modulus(varnumber_T n1, varnumber_T n2) { - // Give an error when n2 is 0? + if (n2 == 0 && in_vim9script()) + emsg(_(e_divide_by_zero)); return (n2 == 0) ? 0 : (n1 % n2); } diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim index c81468cb36..2239e76df9 100644 --- a/src/testdir/test_vim9_expr.vim +++ b/src/testdir/test_vim9_expr.vim @@ -1376,7 +1376,6 @@ def Test_expr6() assert_equal(1, g:anint / 6) assert_equal(2, g:anint / g:thefour) - assert_true(1 / 0 > 99999) assert_equal(5, 11 % 6) assert_equal(4, g:anint % 6) @@ -1384,7 +1383,6 @@ def Test_expr6() g:anint) assert_equal(2, g:anint % g:thefour) - assert_equal(0, 1 % 0) assert_equal(4, 6 * 4 / 6) @@ -1405,6 +1403,9 @@ def Test_expr6() CheckDefFailure(["var x = 6 * xxx"], 'E1001:', 1) CheckDefFailure(["var d = 6 * "], 'E1097:', 3) + + CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1) + CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1) enddef def Test_expr6_vim9script() diff --git a/src/version.c b/src/version.c index 57b1a0c474..5668f75546 100644 --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2378, +/**/ 2377, /**/ 2376, diff --git a/src/vim9execute.c b/src/vim9execute.c index 907a52f18f..b1bc282636 100644 --- a/src/vim9execute.c +++ b/src/vim9execute.c @@ -2954,10 +2954,16 @@ call_def_function( switch (iptr->isn_arg.op.op_type) { case EXPR_MULT: n1 = n1 * n2; break; - case EXPR_DIV: n1 = num_divide(n1, n2); break; + case EXPR_DIV: n1 = num_divide(n1, n2); + if (n2 == 0) + goto on_error; + break; case EXPR_SUB: n1 = n1 - n2; break; case EXPR_ADD: n1 = n1 + n2; break; - default: n1 = num_modulus(n1, n2); break; + default: n1 = num_modulus(n1, n2); + if (n2 == 0) + goto on_error; + break; } clear_tv(tv1); clear_tv(tv2); |