]> granicus.if.org Git - vim/commitdiff
patch 8.2.1416: Vim9: boolean evaluation does not work as intended v8.2.1416
authorBram Moolenaar <Bram@vim.org>
Mon, 10 Aug 2020 19:57:54 +0000 (21:57 +0200)
committerBram Moolenaar <Bram@vim.org>
Mon, 10 Aug 2020 19:57:54 +0000 (21:57 +0200)
Problem:    Vim9: boolean evaluation does not work as intended.
Solution:   Use tv2bool() in Vim9 script. (closes #6681)

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

index abdf076d2649880945d7a8b9841ca1300cf327a1..99554e7e263e2268aefd7992e49d1f278b47e451 100644 (file)
@@ -192,7 +192,10 @@ eval_to_bool(
        *error = FALSE;
        if (!skip)
        {
-           retval = (tv_get_number_chk(&tv, error) != 0);
+           if (in_vim9script())
+               retval = tv2bool(&tv);
+           else
+               retval = (tv_get_number_chk(&tv, error) != 0);
            clear_tv(&tv);
        }
     }
@@ -3098,7 +3101,8 @@ eval7(
 
                // Apply prefixed "-" and "+" now.  Matters especially when
                // "->" follows.
-               if (ret == OK && evaluate && end_leader > start_leader)
+               if (ret == OK && evaluate && end_leader > start_leader
+                                                 && rettv->v_type != VAR_BLOB)
                    ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
                break;
 
@@ -3281,7 +3285,10 @@ eval7_leader(
        f = rettv->vval.v_float;
     else
 #endif
-       val = tv_get_number_chk(rettv, &error);
+       if (in_vim9script() && end_leader[-1] == '!')
+           val = tv2bool(rettv);
+       else
+           val = tv_get_number_chk(rettv, &error);
     if (error)
     {
        clear_tv(rettv);
index aa599731893bcd8879a00384ebbf3f0069a9028e..d2d2e0b1ebfa4b04f2c035a6de567a3fcaf15efd 100644 (file)
@@ -1750,31 +1750,35 @@ enddef
 
 
 def Test_expr7_not()
-  assert_equal(true, !'')
-  assert_equal(true, ![])
-  assert_equal(false, !'asdf')
-  assert_equal(false, ![2])
-  assert_equal(true, !!'asdf')
-  assert_equal(true, !![2])
-
-  assert_equal(true, !test_null_partial())
-  assert_equal(false, !{-> 'yes'})
-
-  assert_equal(true, !test_null_dict())
-  assert_equal(true, !{})
-  assert_equal(false, !{'yes': 'no'})
-
-  if has('channel')
-    assert_equal(true, !test_null_job())
-    assert_equal(true, !test_null_channel())
-  endif
-
-  assert_equal(true, !test_null_blob())
-  assert_equal(true, !0z)
-  assert_equal(false, !0z01)
-
-  assert_equal(true, !test_void())
-  assert_equal(true, !test_unknown())
+  let lines =<< trim END
+      assert_equal(true, !'')
+      assert_equal(true, ![])
+      assert_equal(false, !'asdf')
+      assert_equal(false, ![2])
+      assert_equal(true, !!'asdf')
+      assert_equal(true, !![2])
+
+      assert_equal(true, !test_null_partial())
+      assert_equal(false, !{-> 'yes'})
+
+      assert_equal(true, !test_null_dict())
+      assert_equal(true, !{})
+      assert_equal(false, !{'yes': 'no'})
+
+      if has('channel')
+       assert_equal(true, !test_null_job())
+       assert_equal(true, !test_null_channel())
+      endif
+
+      assert_equal(true, !test_null_blob())
+      assert_equal(true, !0z)
+      assert_equal(false, !0z01)
+
+      assert_equal(true, !test_void())
+      assert_equal(true, !test_unknown())
+  END
+  CheckDefSuccess(lines)
+  CheckScriptSuccess(['vim9script'] + lines)
 enddef
 
 func Test_expr7_fails()
index fdad9f7c3ad12af36ae1e1d2cabd8f07e811dea5..448aa3db947231d294126de75121a0040ce200c9 100644 (file)
@@ -1,5 +1,13 @@
 " Utility functions for testing vim9 script
 
+" Check that "lines" inside ":def" has no error.
+func CheckDefSuccess(lines)
+  call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], 'Xdef')
+  so Xdef
+  call Func()
+  call delete('Xdef')
+endfunc
+
 " Check that "lines" inside ":def" results in an "error" message.
 func CheckDefFailure(lines, error)
   call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], 'Xdef')
index e7a10397147bf1b6d48360894094c8aa3d2640f5..627df9923f5cc7d7f958cb09fdeb07053ef3604e 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1416,
 /**/
     1415,
 /**/