Patch 8.2.2378
Problem:    Vim9: no error message for dividing by zero.
Solution:   Give an error message. (issue #7704)
Files:      src/errors.h, src/eval.c, src/vim9execute.c,
            src/testdir/test_vim9_expr.vim


*** ../vim-8.2.2377/src/errors.h        2021-01-17 13:21:14.962687183 +0100
--- src/errors.h        2021-01-20 21:14:22.699199419 +0100
***************
*** 343,345 ****
--- 343,347 ----
        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"));
*** ../vim-8.2.2377/src/eval.c  2021-01-15 18:04:40.102419940 +0100
--- src/eval.c  2021-01-20 21:16:37.614693631 +0100
***************
*** 63,70 ****
  {
      varnumber_T       result;
  
!     if (n2 == 0)      // give an error message?
      {
        if (n1 == 0)
            result = VARNUM_MIN; // similar to NaN
        else if (n1 < 0)
--- 63,72 ----
  {
      varnumber_T       result;
  
!     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,90 ****
        varnumber_T
  num_modulus(varnumber_T n1, varnumber_T n2)
  {
!     // Give an error when n2 is 0?
      return (n2 == 0) ? 0 : (n1 % n2);
  }
  
--- 86,93 ----
        varnumber_T
  num_modulus(varnumber_T n1, varnumber_T n2)
  {
!     if (n2 == 0 && in_vim9script())
!       emsg(_(e_divide_by_zero));
      return (n2 == 0) ? 0 : (n1 % n2);
  }
  
*** ../vim-8.2.2377/src/vim9execute.c   2021-01-19 22:48:06.267990184 +0100
--- src/vim9execute.c   2021-01-20 21:18:20.274317996 +0100
***************
*** 2954,2963 ****
                        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_SUB:  n1 = n1 - n2; break;
                            case EXPR_ADD:  n1 = n1 + n2; break;
!                           default:        n1 = num_modulus(n1, n2); break;
                        }
                        clear_tv(tv1);
                        clear_tv(tv2);
--- 2954,2969 ----
                        switch (iptr->isn_arg.op.op_type)
                        {
                            case EXPR_MULT: n1 = 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);
!                                           if (n2 == 0)
!                                               goto on_error;
!                                           break;
                        }
                        clear_tv(tv1);
                        clear_tv(tv2);
*** ../vim-8.2.2377/src/testdir/test_vim9_expr.vim      2021-01-19 
22:16:37.680519786 +0100
--- src/testdir/test_vim9_expr.vim      2021-01-20 21:22:39.005397731 +0100
***************
*** 1376,1382 ****
        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)
--- 1376,1381 ----
***************
*** 1384,1390 ****
                              g:anint)
        assert_equal(2, g:anint
                              % g:thefour)
-       assert_equal(0, 1 % 0)
  
        assert_equal(4, 6 * 4 / 6)
  
--- 1383,1388 ----
***************
*** 1405,1410 ****
--- 1403,1411 ----
  
    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()
*** ../vim-8.2.2377/src/version.c       2021-01-19 22:48:06.267990184 +0100
--- src/version.c       2021-01-20 21:16:05.010814510 +0100
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2378,
  /**/

-- 
What is the difference between a professional and an amateur?
The ark was built by an amateur; professionals gave us the Titanic.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            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/202101202024.10KKO8QQ1676961%40masaka.moolenaar.net.

Raspunde prin e-mail lui