Skip to content

Commit 29a86ff

Browse files
committed
patch 8.2.1641: Vim9: cannot use 0 or 1 where a bool is expected
Problem: Vim9: cannot use 0 or 1 where a bool is expected. Solution: Allow using 0 and 1 for a bool type. (closes #6903)
1 parent f842cd9 commit 29a86ff

6 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/proto/vim9type.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* vim9type.c */
2+
type_T *alloc_type(garray_T *type_gap);
23
void clear_type_list(garray_T *gap);
34
type_T *get_list_type(type_T *member_type, garray_T *type_gap);
45
type_T *get_dict_type(type_T *member_type, garray_T *type_gap);

src/structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ struct type_S {
13731373

13741374
#define TTFLAG_VARARGS 1 // func args ends with "..."
13751375
#define TTFLAG_OPTARG 2 // func arg type with "?"
1376+
#define TTFLAG_BOOL_OK 4 // can be converted to bool
13761377

13771378
/*
13781379
* Structure to hold an internal variable without a name.

src/testdir/test_vim9_script.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def Test_assignment()
4545
let bool2: bool = false
4646
assert_equal(v:false, bool2)
4747

48+
let bool3: bool = 0
49+
assert_equal(0, bool3)
50+
let bool4: bool = 1
51+
assert_equal(1, bool4)
52+
4853
CheckDefFailure(['let x:string'], 'E1069:')
4954
CheckDefFailure(['let x:string = "x"'], 'E1069:')
5055
CheckDefFailure(['let a:string = "x"'], 'E1069:')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ static char *(features[]) =
754754

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1641,
757759
/**/
758760
1640,
759761
/**/

src/vim9compile.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,25 @@ need_type(
751751
generate_PUSHNR(cctx_T *cctx, varnumber_T number)
752752
{
753753
isn_T *isn;
754+
garray_T *stack = &cctx->ctx_type_stack;
754755

755756
RETURN_OK_IF_SKIP(cctx);
756757
if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
757758
return FAIL;
758759
isn->isn_arg.number = number;
759760

761+
if (number == 0 || number == 1)
762+
{
763+
type_T *type = alloc_type(cctx->ctx_type_list);
764+
765+
// A 0 or 1 number can also be used as a bool.
766+
if (type != NULL)
767+
{
768+
type->tt_type = VAR_NUMBER;
769+
type->tt_flags = TTFLAG_BOOL_OK;
770+
((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
771+
}
772+
}
760773
return OK;
761774
}
762775

src/vim9type.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
2525
* be freed later.
2626
*/
27-
static type_T *
27+
type_T *
2828
alloc_type(garray_T *type_gap)
2929
{
3030
type_T *type;
@@ -359,6 +359,10 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
359359
{
360360
if (expected->tt_type != actual->tt_type)
361361
{
362+
if (expected->tt_type == VAR_BOOL && actual->tt_type == VAR_NUMBER
363+
&& (actual->tt_flags & TTFLAG_BOOL_OK))
364+
// Using number 0 or 1 for bool is OK.
365+
return OK;
362366
if (give_msg)
363367
arg_type_mismatch(expected, actual, argidx);
364368
return FAIL;

0 commit comments

Comments
 (0)