summaryrefslogtreecommitdiff
path: root/src/vim9compile.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-09-09 20:03:46 +0200
committerBram Moolenaar <Bram@vim.org>2020-09-09 20:03:46 +0200
commit4ed124cc6c0c55385c3b2fed9f9357baf42edbcc (patch)
treef61059c92bfba6e8f8d0cca358b08a4a0c1e2275 /src/vim9compile.c
parent33e3346322e40b2877f63f2f37398832c7017e2f (diff)
downloadvim-git-4ed124cc6c0c55385c3b2fed9f9357baf42edbcc.tar.gz
patch 8.2.1647: Vim9: result of expression with && and || is not a boolv8.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.
Diffstat (limited to 'src/vim9compile.c')
-rw-r--r--src/vim9compile.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/vim9compile.c b/src/vim9compile.c
index f88f3a3df..7199e8577 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -729,6 +729,15 @@ need_type(
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,6 +3935,8 @@ compile_and_or(
{
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,6 +3996,20 @@ compile_and_or(
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;