comparison src/vim9compile.c @ 22196:d835f2fdfcfc v8.2.1647

patch 8.2.1647: Vim9: result of expression with && and || is not a bool Commit: https://github.com/vim/vim/commit/4ed124cc6c0c55385c3b2fed9f9357baf42edbcc Author: Bram Moolenaar <Bram@vim.org> Date: Wed Sep 9 20:03:46 2020 +0200 patch 8.2.1647: Vim9: result of expression with && and || is not a bool 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.
author Bram Moolenaar <Bram@vim.org>
date Wed, 09 Sep 2020 20:15:03 +0200
parents 2463b3d89ce2
children f9b4576a618b
comparison
equal deleted inserted replaced
22195:efcf140faa04 22196:d835f2fdfcfc
727 type_T *expected, 727 type_T *expected,
728 int offset, 728 int offset,
729 cctx_T *cctx, 729 cctx_T *cctx,
730 int silent) 730 int silent)
731 { 731 {
732 if (expected == &t_bool && actual != &t_bool
733 && (actual->tt_flags & TTFLAG_BOOL_OK))
734 {
735 // Using "0", "1" or the result of an expression with "&&" or "||" as a
736 // boolean is OK but requires a conversion.
737 generate_2BOOL(cctx, FALSE);
738 return OK;
739 }
740
732 if (check_type(expected, actual, FALSE, 0) == OK) 741 if (check_type(expected, actual, FALSE, 0) == OK)
733 return OK; 742 return OK;
734 if (actual->tt_type != VAR_ANY 743 if (actual->tt_type != VAR_ANY
735 && actual->tt_type != VAR_UNKNOWN 744 && actual->tt_type != VAR_UNKNOWN
736 && !(actual->tt_type == VAR_FUNC 745 && !(actual->tt_type == VAR_FUNC
3924 3933
3925 if (p[0] == opchar && p[1] == opchar) 3934 if (p[0] == opchar && p[1] == opchar)
3926 { 3935 {
3927 garray_T *instr = &cctx->ctx_instr; 3936 garray_T *instr = &cctx->ctx_instr;
3928 garray_T end_ga; 3937 garray_T end_ga;
3938 garray_T *stack = &cctx->ctx_type_stack;
3939 type_T **typep;
3929 3940
3930 /* 3941 /*
3931 * Repeat until there is no following "||" or "&&" 3942 * Repeat until there is no following "||" or "&&"
3932 */ 3943 */
3933 ga_init2(&end_ga, sizeof(int), 10); 3944 ga_init2(&end_ga, sizeof(int), 10);
3983 isn = ((isn_T *)instr->ga_data) 3994 isn = ((isn_T *)instr->ga_data)
3984 + *(((int *)end_ga.ga_data) + end_ga.ga_len); 3995 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3985 isn->isn_arg.jump.jump_where = instr->ga_len; 3996 isn->isn_arg.jump.jump_where = instr->ga_len;
3986 } 3997 }
3987 ga_clear(&end_ga); 3998 ga_clear(&end_ga);
3999
4000 // The resulting type can be used as a bool.
4001 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4002 if (*typep != &t_bool)
4003 {
4004 type_T *type = alloc_type(cctx->ctx_type_list);
4005
4006 if (type != NULL)
4007 {
4008 *type = **typep;
4009 type->tt_flags |= TTFLAG_BOOL_OK;
4010 *typep = type;
4011 }
4012 }
3988 } 4013 }
3989 4014
3990 return OK; 4015 return OK;
3991 } 4016 }
3992 4017