]> granicus.if.org Git - vim/commitdiff
patch 8.2.1644: Vim9: cannot assign 1 and 0 to bool at script level v8.2.1644
authorBram Moolenaar <Bram@vim.org>
Wed, 9 Sep 2020 16:54:42 +0000 (18:54 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 9 Sep 2020 16:54:42 +0000 (18:54 +0200)
Problem:    Vim9: cannot assign 1 and 0 to bool at script level.
Solution:   Add the TTFLAG_BOOL_OK flag to the type. Fix name of test
            function.

src/testdir/test_vim9_expr.vim
src/testdir/test_vim9_script.vim
src/testdir/vim9.vim
src/version.c
src/vim9type.c

index 3c16fa48ea56c2b23769eba6257613c697f0debc..8cf5bc0d60b1415251d50214faec09e0b5eefd55 100644 (file)
@@ -2382,7 +2382,7 @@ func Test_expr_fails()
 
   call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1)
   call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1)
-  call CheckScriptAndDefFailure(["CallMe2('yes','no')"], 'E1069:', 1)
+  call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
   call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1)
 
   call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1)
index 5712e384234fa68e58607b7cf554a7e39932eedc..36e39eb05a6d89b85c9e8863b70eae25c015568f 100644 (file)
@@ -39,7 +39,7 @@ let g:alist = [7]
 let g:astring = 'text'
 let g:anumber = 123
 
-def Test_assignment()
+def Test_assignment_bool()
   let bool1: bool = true
   assert_equal(v:true, bool1)
   let bool2: bool = false
@@ -50,6 +50,25 @@ def Test_assignment()
   let bool4: bool = 1
   assert_equal(1, bool4)
 
+  let lines =<< trim END
+    vim9script
+    def GetFlag(): bool
+      let flag: bool = 1
+      return flag
+    enddef
+    let flag: bool = GetFlag()
+    flag = 0
+    flag = 1
+  END
+  CheckScriptSuccess(lines)
+  CheckDefAndScriptFailure(['let x: bool = 2'], 'E1012:')
+  CheckDefAndScriptFailure(['let x: bool = -1'], 'E1012:')
+  CheckDefAndScriptFailure(['let x: bool = [1]'], 'E1012:')
+  CheckDefAndScriptFailure(['let x: bool = {}'], 'E1012:')
+  CheckDefAndScriptFailure(['let x: bool = "x"'], 'E1012:')
+enddef
+
+def Test_assignment()
   CheckDefFailure(['let x:string'], 'E1069:')
   CheckDefFailure(['let x:string = "x"'], 'E1069:')
   CheckDefFailure(['let a:string = "x"'], 'E1069:')
@@ -164,8 +183,7 @@ def Test_assignment()
     assert_equal('xxx', &t_TI)
     &t_TI = save_TI
   END
-  CheckDefSuccess(lines)
-  CheckScriptSuccess(['vim9script'] + lines)
+  CheckDefAndScriptSuccess(lines)
 
   CheckDefFailure(['&t_TI = 123'], 'E1012:')
   CheckScriptFailure(['vim9script', '&t_TI = 123'], 'E928:')
index 7fbe4a5ed33421e772e7e238448242d39cf6409c..2e4b03f495d6b3a091ea1fd803ef2ca4c949507e 100644 (file)
@@ -48,7 +48,7 @@ enddef
 
 " Check that a command fails both when used in a :def function and when used
 " in Vim9 script.
-def CheckScriptAndDefFailure(lines: list<string>, error: string, lnum = -3)
+def CheckDefAndScriptFailure(lines: list<string>, error: string, lnum = -3)
   CheckDefFailure(lines, error, lnum)
   CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
 enddef
index 18d2b3ed8e965de3dcd1a15f651d7cf3ecf6dad5..1d1e00e61fe5c6314141b41ef206775e8a170af4 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1644,
 /**/
     1643,
 /**/
index 4f4876da0441fe4a0d6665b21054a0a3953d0459..43710fc0f63f266c1f4de7b8d978becc49f45f1c 100644 (file)
@@ -202,11 +202,23 @@ func_type_add_arg_types(
     type_T *
 typval2type(typval_T *tv, garray_T *type_gap)
 {
-    type_T  *actual;
+    type_T  *type;
     type_T  *member_type;
 
     if (tv->v_type == VAR_NUMBER)
+    {
+       if (tv->vval.v_number == 0 || tv->vval.v_number == 1)
+       {
+           // number 0 and 1 can also be used for bool
+           type = alloc_type(type_gap);
+           if (type == NULL)
+               return NULL;
+           type->tt_type = VAR_NUMBER;
+           type->tt_flags = TTFLAG_BOOL_OK;
+           return type;
+       }
        return &t_number;
+    }
     if (tv->v_type == VAR_BOOL)
        return &t_bool;  // not used
     if (tv->v_type == VAR_STRING)
@@ -276,13 +288,13 @@ typval2type(typval_T *tv, garray_T *type_gap)
        }
     }
 
-    actual = alloc_type(type_gap);
-    if (actual == NULL)
+    type = alloc_type(type_gap);
+    if (type == NULL)
        return NULL;
-    actual->tt_type = tv->v_type;
-    actual->tt_member = &t_any;
+    type->tt_type = tv->v_type;
+    type->tt_member = &t_any;
 
-    return actual;
+    return type;
 }
 
 /*