]> granicus.if.org Git - vim/commitdiff
patch 8.2.1668: Vim9: not accepting 0 or 1 as bool when type is any v8.2.1668
authorBram Moolenaar <Bram@vim.org>
Sat, 12 Sep 2020 17:11:23 +0000 (19:11 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 12 Sep 2020 17:11:23 +0000 (19:11 +0200)
Problem:    Vim9: not accepting 0 or 1 as bool when type is any.
Solution:   Convert the type with the CHECKTYPE instruction. (closes #6913)

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

index 8cf5bc0d60b1415251d50214faec09e0b5eefd55..15fc3aeea103245aac2c3c71c57d26dd6b4177c1 100644 (file)
@@ -2369,6 +2369,9 @@ def Test_expr7_method_call()
                type: '',
                module: ''}
                ], getloclist(0))
+
+  let result: bool = get(#{n: 0}, 'n', 0)
+  assert_equal(false, result)
 enddef
 
 func Test_expr7_trailing_fails()
index b225f7406caa801a4d0615e310dbe6d83fa830e6..305f0cb7073200b134ca12f0c93b9d9a86030c90 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1668,
 /**/
     1667,
 /**/
index 18cfb0213b8ec5d5448af45aee26ed8061f63e7b..e93817a62bb1237a4b2067650610c6645d805b53 100644 (file)
@@ -2510,11 +2510,23 @@ call_def_function(
                                || (tv->v_type == VAR_FUNC
                                               && ct->ct_type == VAR_PARTIAL)))
                    {
-                       SOURCING_LNUM = iptr->isn_lnum;
-                       semsg(_(e_expected_str_but_got_str),
-                                   vartype_name(ct->ct_type),
-                                   vartype_name(tv->v_type));
-                       goto on_error;
+                       if (tv->v_type == VAR_NUMBER && ct->ct_type == VAR_BOOL
+                               && (tv->vval.v_number == 0
+                                                   || tv->vval.v_number == 1))
+                       {
+                           // number 0 is FALSE, number 1 is TRUE
+                           tv->v_type = VAR_BOOL;
+                           tv->vval.v_number = tv->vval.v_number
+                                                     ? VVAL_TRUE : VVAL_FALSE;
+                       }
+                       else
+                       {
+                           SOURCING_LNUM = iptr->isn_lnum;
+                           semsg(_(e_expected_str_but_got_str),
+                                       vartype_name(ct->ct_type),
+                                       vartype_name(tv->v_type));
+                           goto on_error;
+                       }
                    }
                }
                break;