Patch 8.2.1644
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.
Files:      src/vim9type.c, src/testdir/test_vim9_script.vim,
            src/testdir/vim9.vim, src/testdir/test_vim9_expr.vim


*** ../vim-8.2.1643/src/vim9type.c      2020-09-09 14:55:28.155619452 +0200
--- src/vim9type.c      2020-09-09 18:44:03.660305530 +0200
***************
*** 202,212 ****
      type_T *
  typval2type(typval_T *tv, garray_T *type_gap)
  {
!     type_T  *actual;
      type_T  *member_type;
  
      if (tv->v_type == VAR_NUMBER)
        return &t_number;
      if (tv->v_type == VAR_BOOL)
        return &t_bool;  // not used
      if (tv->v_type == VAR_STRING)
--- 202,224 ----
      type_T *
  typval2type(typval_T *tv, garray_T *type_gap)
  {
!     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,288 ****
        }
      }
  
!     actual = alloc_type(type_gap);
!     if (actual == NULL)
        return NULL;
!     actual->tt_type = tv->v_type;
!     actual->tt_member = &t_any;
  
!     return actual;
  }
  
  /*
--- 288,300 ----
        }
      }
  
!     type = alloc_type(type_gap);
!     if (type == NULL)
        return NULL;
!     type->tt_type = tv->v_type;
!     type->tt_member = &t_any;
  
!     return type;
  }
  
  /*
*** ../vim-8.2.1643/src/testdir/test_vim9_script.vim    2020-09-09 
14:55:28.155619452 +0200
--- src/testdir/test_vim9_script.vim    2020-09-09 18:53:06.826569623 +0200
***************
*** 39,45 ****
  let g:astring = 'text'
  let g:anumber = 123
  
! def Test_assignment()
    let bool1: bool = true
    assert_equal(v:true, bool1)
    let bool2: bool = false
--- 39,45 ----
  let g:astring = 'text'
  let g:anumber = 123
  
! def Test_assignment_bool()
    let bool1: bool = true
    assert_equal(v:true, bool1)
    let bool2: bool = false
***************
*** 50,55 ****
--- 50,74 ----
    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,171 ****
      assert_equal('xxx', &t_TI)
      &t_TI = save_TI
    END
!   CheckDefSuccess(lines)
!   CheckScriptSuccess(['vim9script'] + lines)
  
    CheckDefFailure(['&t_TI = 123'], 'E1012:')
    CheckScriptFailure(['vim9script', '&t_TI = 123'], 'E928:')
--- 183,189 ----
      assert_equal('xxx', &t_TI)
      &t_TI = save_TI
    END
!   CheckDefAndScriptSuccess(lines)
  
    CheckDefFailure(['&t_TI = 123'], 'E1012:')
    CheckScriptFailure(['vim9script', '&t_TI = 123'], 'E928:')
*** ../vim-8.2.1643/src/testdir/vim9.vim        2020-08-23 19:34:44.722827763 
+0200
--- src/testdir/vim9.vim        2020-09-09 18:51:04.966995143 +0200
***************
*** 48,54 ****
  
  " 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)
    CheckDefFailure(lines, error, lnum)
    CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
  enddef
--- 48,54 ----
  
  " Check that a command fails both when used in a :def function and when used
  " in Vim9 script.
! def CheckDefAndScriptFailure(lines: list<string>, error: string, lnum = -3)
    CheckDefFailure(lines, error, lnum)
    CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
  enddef
*** ../vim-8.2.1643/src/testdir/test_vim9_expr.vim      2020-09-06 
15:58:33.383154915 +0200
--- src/testdir/test_vim9_expr.vim      2020-09-09 18:51:36.614883609 +0200
***************
*** 2382,2388 ****
  
    call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1)
    call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1)
!   call CheckScriptAndDefFailure(["CallMe2('yes','no')"], 'E1069:', 1)
    call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1)
  
    call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1)
--- 2382,2388 ----
  
    call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1)
    call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1)
!   call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
    call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1)
  
    call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1)
*** ../vim-8.2.1643/src/version.c       2020-09-09 17:08:47.565323019 +0200
--- src/version.c       2020-09-09 18:45:08.896108327 +0200
***************
*** 756,757 ****
--- 756,759 ----
  {   /* Add new patch number below this line */
+ /**/
+     1644,
  /**/

-- 
Lose weight, NEVER Diet again with
                  The "Invisible Weight Loss Patch"
                                                (spam e-mail)

 /// 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/202009091655.089GtA83385753%40masaka.moolenaar.net.

Raspunde prin e-mail lui