]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.1205: Vim9: && and || work different when not compiled v8.2.1205
authorBram Moolenaar <Bram@vim.org>
Mon, 13 Jul 2020 20:29:02 +0000 (22:29 +0200)
committerBram Moolenaar <Bram@vim.org>
Mon, 13 Jul 2020 20:29:02 +0000 (22:29 +0200)
Problem:    Vim9: && and || work different when not compiled.
Solution:   Keep the value.

src/eval.c
src/testdir/test_vim9_expr.vim
src/version.c

index c5241513044c161dc117ea7f3a25aaec28e36952..9a9110efcd17084b1e8e1e89c646807bce484983 100644 (file)
@@ -2196,6 +2196,7 @@ eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
        long        result = FALSE;
        typval_T    var2;
        int         error;
+       int         vim9script = in_vim9script();
 
        if (evalarg == NULL)
        {
@@ -2206,12 +2207,19 @@ eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
        evaluate = orig_flags & EVAL_EVALUATE;
        if (evaluate)
        {
-           error = FALSE;
-           if (tv_get_number_chk(rettv, &error) != 0)
-               result = TRUE;
-           clear_tv(rettv);
-           if (error)
-               return FAIL;
+           if (vim9script)
+           {
+               result = tv2bool(rettv);
+           }
+           else
+           {
+               error = FALSE;
+               if (tv_get_number_chk(rettv, &error) != 0)
+                   result = TRUE;
+               clear_tv(rettv);
+               if (error)
+                   return FAIL;
+           }
        }
 
        /*
@@ -2236,13 +2244,22 @@ eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
             */
            if (evaluate && !result)
            {
-               if (tv_get_number_chk(&var2, &error) != 0)
-                   result = TRUE;
-               clear_tv(&var2);
-               if (error)
-                   return FAIL;
+               if (vim9script)
+               {
+                   clear_tv(rettv);
+                   *rettv = var2;
+                   result = tv2bool(rettv);
+               }
+               else
+               {
+                   if (tv_get_number_chk(&var2, &error) != 0)
+                       result = TRUE;
+                   clear_tv(&var2);
+                   if (error)
+                       return FAIL;
+               }
            }
-           if (evaluate)
+           if (evaluate && !vim9script)
            {
                rettv->v_type = VAR_NUMBER;
                rettv->vval.v_number = result;
@@ -2294,6 +2311,7 @@ eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
        long        result = TRUE;
        typval_T    var2;
        int         error;
+       int         vim9script = in_vim9script();
 
        if (evalarg == NULL)
        {
@@ -2304,12 +2322,19 @@ eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
        evaluate = orig_flags & EVAL_EVALUATE;
        if (evaluate)
        {
-           error = FALSE;
-           if (tv_get_number_chk(rettv, &error) == 0)
-               result = FALSE;
-           clear_tv(rettv);
-           if (error)
-               return FAIL;
+           if (vim9script)
+           {
+               result = tv2bool(rettv);
+           }
+           else
+           {
+               error = FALSE;
+               if (tv_get_number_chk(rettv, &error) == 0)
+                   result = FALSE;
+               clear_tv(rettv);
+               if (error)
+                   return FAIL;
+           }
        }
 
        /*
@@ -2334,13 +2359,22 @@ eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
             */
            if (evaluate && result)
            {
-               if (tv_get_number_chk(&var2, &error) == 0)
-                   result = FALSE;
-               clear_tv(&var2);
-               if (error)
-                   return FAIL;
+               if (vim9script)
+               {
+                   clear_tv(rettv);
+                   *rettv = var2;
+                   result = tv2bool(rettv);
+               }
+               else
+               {
+                   if (tv_get_number_chk(&var2, &error) == 0)
+                       result = FALSE;
+                   clear_tv(&var2);
+                   if (error)
+                       return FAIL;
+               }
            }
-           if (evaluate)
+           if (evaluate && !vim9script)
            {
                rettv->v_type = VAR_NUMBER;
                rettv->vval.v_number = result;
index cba41e395bbcb107b9a2b0c1384845ba2d4f37b8..dc9514480068945fbb7f6a22a8daca83547588fa 100644 (file)
@@ -127,7 +127,7 @@ def Test_expr2()
 enddef
 
 def Test_expr2_vimscript()
-  " only checks line continuation
+  " check line continuation
   let lines =<< trim END
       vim9script
       let var = 0
@@ -141,7 +141,7 @@ def Test_expr2_vimscript()
       let var = v:false
                || v:true
                || v:false
-      assert_equal(1, var)
+      assert_equal(v:true, var)
   END
   CheckScriptSuccess(lines)
 
@@ -150,7 +150,39 @@ def Test_expr2_vimscript()
       let var = v:false ||
                v:true ||
                v:false
-      assert_equal(1, var)
+      assert_equal(v:true, var)
+  END
+  CheckScriptSuccess(lines)
+
+  " check keeping the value
+  lines =<< trim END
+      vim9script
+      assert_equal(2, 2 || 0)
+      assert_equal(7, 0 ||
+                       0 ||
+                       7)
+      assert_equal(0, 0 || 0)
+      assert_equal(0, 0
+                       || 0)
+      assert_equal('', 0 || '')
+
+      g:vals = []
+      assert_equal(3, Record(3) || Record(1))
+      assert_equal([3], g:vals)
+
+      g:vals = []
+      assert_equal(5, Record(0) || Record(5))
+      assert_equal([0, 5], g:vals)
+
+      g:vals = []
+      assert_equal(4, Record(0)
+                         || Record(4)
+                         || Record(0))
+      assert_equal([0, 4], g:vals)
+
+      g:vals = []
+      assert_equal(0, Record([]) || Record('') || Record(0))
+      assert_equal([[], '', 0], g:vals)
   END
   CheckScriptSuccess(lines)
 enddef
@@ -199,7 +231,7 @@ def Test_expr3()
 enddef
 
 def Test_expr3_vimscript()
-  " only checks line continuation
+  " check line continuation
   let lines =<< trim END
       vim9script
       let var = 0
@@ -213,7 +245,7 @@ def Test_expr3_vimscript()
       let var = v:true
                && v:true
                && v:true
-      assert_equal(1, var)
+      assert_equal(v:true, var)
   END
   CheckScriptSuccess(lines)
 
@@ -222,7 +254,43 @@ def Test_expr3_vimscript()
       let var = v:true &&
                v:true &&
                v:true
-      assert_equal(1, var)
+      assert_equal(v:true, var)
+  END
+  CheckScriptSuccess(lines)
+
+  " check keeping the value
+  lines =<< trim END
+      vim9script
+      assert_equal(0, 2 && 0)
+      assert_equal(0, 0 &&
+                   0 &&
+                   7)
+      assert_equal(7, 2
+                       && 3
+                       && 7)
+      assert_equal(0, 0 && 0)
+      assert_equal(0, 0 && '')
+      assert_equal('', 8 && '')
+
+      g:vals = []
+      assert_equal(1, Record(3) && Record(1))
+      assert_equal([3, 1], g:vals)
+
+      g:vals = []
+      assert_equal(0, Record(0) && Record(5))
+      assert_equal([0], g:vals)
+
+      g:vals = []
+      assert_equal(0, Record(0) && Record(4) && Record(0))
+      assert_equal([0], g:vals)
+
+      g:vals = []
+      assert_equal(0, Record(8) && Record(4) && Record(0))
+      assert_equal([8, 4, 0], g:vals)
+
+      g:vals = []
+      assert_equal(0, Record([1]) && Record('z') && Record(0))
+      assert_equal([[1], 'z', 0], g:vals)
   END
   CheckScriptSuccess(lines)
 enddef
index 49896f7ff82706bf95e2f1335920cef0883850d9..668b3b18c45261dd86c4bfa374c4b2ad2c966537 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1205,
 /**/
     1204,
 /**/