diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-09-09 14:55:31 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-09-09 14:55:31 +0200 |
commit | 29a86ffee794bc1a0efde215c203cf9529047252 (patch) | |
tree | e718319396ca7a66a071c72fb02af88db2426e3f /src | |
parent | f842cd9e285afb9d86c6f9bff400e5e576f873c0 (diff) | |
download | vim-git-29a86ffee794bc1a0efde215c203cf9529047252.tar.gz |
patch 8.2.1641: Vim9: cannot use 0 or 1 where a bool is expectedv8.2.1641
Problem: Vim9: cannot use 0 or 1 where a bool is expected.
Solution: Allow using 0 and 1 for a bool type. (closes #6903)
Diffstat (limited to 'src')
-rw-r--r-- | src/proto/vim9type.pro | 1 | ||||
-rw-r--r-- | src/structs.h | 1 | ||||
-rw-r--r-- | src/testdir/test_vim9_script.vim | 5 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 13 | ||||
-rw-r--r-- | src/vim9type.c | 6 |
6 files changed, 27 insertions, 1 deletions
diff --git a/src/proto/vim9type.pro b/src/proto/vim9type.pro index 1ac372732..90f8791c3 100644 --- a/src/proto/vim9type.pro +++ b/src/proto/vim9type.pro @@ -1,4 +1,5 @@ /* vim9type.c */ +type_T *alloc_type(garray_T *type_gap); void clear_type_list(garray_T *gap); type_T *get_list_type(type_T *member_type, garray_T *type_gap); type_T *get_dict_type(type_T *member_type, garray_T *type_gap); diff --git a/src/structs.h b/src/structs.h index dba9be65d..0899ae792 100644 --- a/src/structs.h +++ b/src/structs.h @@ -1373,6 +1373,7 @@ struct type_S { #define TTFLAG_VARARGS 1 // func args ends with "..." #define TTFLAG_OPTARG 2 // func arg type with "?" +#define TTFLAG_BOOL_OK 4 // can be converted to bool /* * Structure to hold an internal variable without a name. diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index 8d8111a83..5712e3842 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -45,6 +45,11 @@ def Test_assignment() let bool2: bool = false assert_equal(v:false, bool2) + let bool3: bool = 0 + assert_equal(0, bool3) + let bool4: bool = 1 + assert_equal(1, bool4) + CheckDefFailure(['let x:string'], 'E1069:') CheckDefFailure(['let x:string = "x"'], 'E1069:') CheckDefFailure(['let a:string = "x"'], 'E1069:') diff --git a/src/version.c b/src/version.c index 4f7de313a..7390d399c 100644 --- a/src/version.c +++ b/src/version.c @@ -755,6 +755,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1641, +/**/ 1640, /**/ 1639, diff --git a/src/vim9compile.c b/src/vim9compile.c index 8b1dcd5d5..f88f3a3df 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -751,12 +751,25 @@ need_type( generate_PUSHNR(cctx_T *cctx, varnumber_T number) { isn_T *isn; + garray_T *stack = &cctx->ctx_type_stack; RETURN_OK_IF_SKIP(cctx); if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) return FAIL; isn->isn_arg.number = number; + if (number == 0 || number == 1) + { + type_T *type = alloc_type(cctx->ctx_type_list); + + // A 0 or 1 number can also be used as a bool. + if (type != NULL) + { + type->tt_type = VAR_NUMBER; + type->tt_flags = TTFLAG_BOOL_OK; + ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; + } + } return OK; } diff --git a/src/vim9type.c b/src/vim9type.c index 29466d006..4f4876da0 100644 --- a/src/vim9type.c +++ b/src/vim9type.c @@ -24,7 +24,7 @@ * Allocate memory for a type_T and add the pointer to type_gap, so that it can * be freed later. */ - static type_T * + type_T * alloc_type(garray_T *type_gap) { type_T *type; @@ -359,6 +359,10 @@ check_type(type_T *expected, type_T *actual, int give_msg, int argidx) { if (expected->tt_type != actual->tt_type) { + if (expected->tt_type == VAR_BOOL && actual->tt_type == VAR_NUMBER + && (actual->tt_flags & TTFLAG_BOOL_OK)) + // Using number 0 or 1 for bool is OK. + return OK; if (give_msg) arg_type_mismatch(expected, actual, argidx); return FAIL; |