Patch 8.2.1647
Problem: Vim9: result of expression with && and || cannot be assigned to a
bool variable.
Solution: Add the TTFLAG_BOOL_OK flag and convert the value if needed.
Files: src/vim9compile.c, src/testdir/test_vim9_script.vim,
src/testdir/test_vim9_disassemble.vim
*** ../vim-8.2.1646/src/vim9compile.c 2020-09-09 14:55:28.155619452 +0200
--- src/vim9compile.c 2020-09-09 19:41:06.017524247 +0200
***************
*** 729,734 ****
--- 729,743 ----
cctx_T *cctx,
int silent)
{
+ if (expected == &t_bool && actual != &t_bool
+ && (actual->tt_flags & TTFLAG_BOOL_OK))
+ {
+ // Using "0", "1" or the result of an expression with "&&" or "||" as a
+ // boolean is OK but requires a conversion.
+ generate_2BOOL(cctx, FALSE);
+ return OK;
+ }
+
if (check_type(expected, actual, FALSE, 0) == OK)
return OK;
if (actual->tt_type != VAR_ANY
***************
*** 3926,3931 ****
--- 3935,3942 ----
{
garray_T *instr = &cctx->ctx_instr;
garray_T end_ga;
+ garray_T *stack = &cctx->ctx_type_stack;
+ type_T **typep;
/*
* Repeat until there is no following "||" or "&&"
***************
*** 3985,3990 ****
--- 3996,4015 ----
isn->isn_arg.jump.jump_where = instr->ga_len;
}
ga_clear(&end_ga);
+
+ // The resulting type can be used as a bool.
+ typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
+ if (*typep != &t_bool)
+ {
+ type_T *type = alloc_type(cctx->ctx_type_list);
+
+ if (type != NULL)
+ {
+ *type = **typep;
+ type->tt_flags |= TTFLAG_BOOL_OK;
+ *typep = type;
+ }
+ }
}
return OK;
*** ../vim-8.2.1646/src/testdir/test_vim9_script.vim 2020-09-09
18:54:39.166253632 +0200
--- src/testdir/test_vim9_script.vim 2020-09-09 19:59:22.957983610 +0200
***************
*** 46,54 ****
assert_equal(v:false, bool2)
let bool3: bool = 0
! assert_equal(0, bool3)
let bool4: bool = 1
! assert_equal(1, bool4)
let lines =<< trim END
vim9script
--- 46,61 ----
assert_equal(v:false, bool2)
let bool3: bool = 0
! assert_equal(false, bool3)
let bool4: bool = 1
! assert_equal(true, bool4)
!
! let bool5: bool = 'yes' && 'no'
! assert_equal(true, bool5)
! let bool6: bool = [] && 99
! assert_equal(false, bool6)
! let bool7: bool = [] || #{a: 1} && 99
! assert_equal(true, bool7)
let lines =<< trim END
vim9script
***************
*** 57,64 ****
--- 64,78 ----
return flag
enddef
let flag: bool = GetFlag()
+ assert_equal(true, flag)
flag = 0
+ # assert_equal(false, flag)
flag = 1
+ # assert_equal(true, flag)
+ # flag = 99 || 123
+ # assert_equal(true, flag)
+ # flag = 'yes' && []
+ # assert_equal(false, flag)
END
CheckScriptSuccess(lines)
CheckDefAndScriptFailure(['let x: bool = 2'], 'E1012:')
*** ../vim-8.2.1646/src/testdir/test_vim9_disassemble.vim 2020-09-08
22:45:31.113504961 +0200
--- src/testdir/test_vim9_disassemble.vim 2020-09-09 19:55:49.546803788
+0200
***************
*** 1199,1204 ****
--- 1199,1227 ----
assert_equal(true, InvertBool())
enddef
+ def ReturnBool(): bool
+ let var: bool = "no" && [] || 123
+ return var
+ enddef
+
+ def Test_disassemble_return_bool()
+ let instr = execute('disassemble ReturnBool')
+ assert_match('ReturnBool\_s*' ..
+ 'let var: bool = "no" && \[\] || 123\_s*' ..
+ '0 PUSHS "no"\_s*' ..
+ '1 JUMP_AND_KEEP_IF_FALSE -> 3\_s*' ..
+ '2 NEWLIST size 0\_s*' ..
+ '3 JUMP_AND_KEEP_IF_TRUE -> 5\_s*' ..
+ '4 PUSHNR 123\_s*' ..
+ '5 2BOOL (!!val)\_s*' ..
+ '\d STORE $0\_s*' ..
+ 'return var\_s*' ..
+ '\d LOAD $0\_s*' ..
+ '\d RETURN',
+ instr)
+ assert_equal(true, InvertBool())
+ enddef
+
def Test_disassemble_compare()
let cases = [
['true == isFalse', 'COMPAREBOOL =='],
*** ../vim-8.2.1646/src/version.c 2020-09-09 19:25:03.476425412 +0200
--- src/version.c 2020-09-09 20:01:57.257172578 +0200
***************
*** 752,753 ****
--- 752,755 ----
{ /* Add new patch number below this line */
+ /**/
+ 1647,
/**/
--
I just planted an Algebra tree. It has square roots.
/// 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/202009091804.089I4GUm405107%40masaka.moolenaar.net.