Patch 8.2.1641

4 views
Skip to first unread message

Bram Moolenaar

unread,
Sep 9, 2020, 8:56:50 AM9/9/20
to vim...@googlegroups.com

Patch 8.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)
Files: src/vim9compile.c, src/vim9type.c, src/proto/vim9type.pro,
src/structs.h, src/testdir/test_vim9_script.vim


*** ../vim-8.2.1640/src/vim9compile.c 2020-09-08 22:45:31.109504973 +0200
--- src/vim9compile.c 2020-09-09 14:49:10.033028545 +0200
***************
*** 751,762 ****
--- 751,775 ----
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;
}

*** ../vim-8.2.1640/src/vim9type.c 2020-08-30 23:24:17.219401371 +0200
--- src/vim9type.c 2020-09-09 14:42:30.966493955 +0200
***************
*** 24,30 ****
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
* be freed later.
*/
! static type_T *
alloc_type(garray_T *type_gap)
{
type_T *type;
--- 24,30 ----
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
* be freed later.
*/
! type_T *
alloc_type(garray_T *type_gap)
{
type_T *type;
***************
*** 359,364 ****
--- 359,368 ----
{
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;
*** ../vim-8.2.1640/src/proto/vim9type.pro 2020-08-30 23:24:17.219401371 +0200
--- src/proto/vim9type.pro 2020-09-09 14:49:08.673033527 +0200
***************
*** 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);
*** ../vim-8.2.1640/src/structs.h 2020-09-08 22:06:12.825040939 +0200
--- src/structs.h 2020-09-09 14:36:25.499844709 +0200
***************
*** 1373,1378 ****
--- 1373,1379 ----

#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.
*** ../vim-8.2.1640/src/testdir/test_vim9_script.vim 2020-09-06 22:26:53.418275627 +0200
--- src/testdir/test_vim9_script.vim 2020-09-09 14:51:36.984486824 +0200
***************
*** 45,50 ****
--- 45,55 ----
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:')
*** ../vim-8.2.1640/src/version.c 2020-09-09 13:01:23.349432433 +0200
--- src/version.c 2020-09-09 14:01:01.971846204 +0200
***************
*** 756,757 ****
--- 756,759 ----
{ /* Add new patch number below this line */
+ /**/
+ 1641,
/**/

--
An indication you must be a manager:
You can explain to somebody the difference between "re-engineering",
"down-sizing", "right-sizing", and "firing people's asses".

/// Bram Moolenaar -- Br...@Moolenaar.net -- 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 ///
Reply all
Reply to author
Forward
0 new messages