Skip to content

Commit ba7c0d7

Browse files
committed
patch 8.2.1644: Vim9: cannot assign 1 and 0 to bool at script level
Problem: Vim9: cannot assign 1 and 0 to bool at script level. Solution: Add the TTFLAG_BOOL_OK flag to the type. Fix name of test function.
1 parent 96f8f49 commit ba7c0d7

5 files changed

Lines changed: 43 additions & 11 deletions

File tree

src/testdir/test_vim9_expr.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ func Test_expr_fails()
23822382

23832383
call CheckDefFailure(["CallMe ('yes')"], 'E476:', 1)
23842384
call CheckScriptFailure(["CallMe ('yes')"], 'E492:', 1)
2385-
call CheckScriptAndDefFailure(["CallMe2('yes','no')"], 'E1069:', 1)
2385+
call CheckDefAndScriptFailure(["CallMe2('yes','no')"], 'E1069:', 1)
23862386
call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:', 1)
23872387

23882388
call CheckDefFailure(["v:nosuch += 3"], 'E1001:', 1)

src/testdir/test_vim9_script.vim

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let g:alist = [7]
3939
let g:astring = 'text'
4040
let g:anumber = 123
4141

42-
def Test_assignment()
42+
def Test_assignment_bool()
4343
let bool1: bool = true
4444
assert_equal(v:true, bool1)
4545
let bool2: bool = false
@@ -50,6 +50,25 @@ def Test_assignment()
5050
let bool4: bool = 1
5151
assert_equal(1, bool4)
5252

53+
let lines =<< trim END
54+
vim9script
55+
def GetFlag(): bool
56+
let flag: bool = 1
57+
return flag
58+
enddef
59+
let flag: bool = GetFlag()
60+
flag = 0
61+
flag = 1
62+
END
63+
CheckScriptSuccess(lines)
64+
CheckDefAndScriptFailure(['let x: bool = 2'], 'E1012:')
65+
CheckDefAndScriptFailure(['let x: bool = -1'], 'E1012:')
66+
CheckDefAndScriptFailure(['let x: bool = [1]'], 'E1012:')
67+
CheckDefAndScriptFailure(['let x: bool = {}'], 'E1012:')
68+
CheckDefAndScriptFailure(['let x: bool = "x"'], 'E1012:')
69+
enddef
70+
71+
def Test_assignment()
5372
CheckDefFailure(['let x:string'], 'E1069:')
5473
CheckDefFailure(['let x:string = "x"'], 'E1069:')
5574
CheckDefFailure(['let a:string = "x"'], 'E1069:')
@@ -164,8 +183,7 @@ def Test_assignment()
164183
assert_equal('xxx', &t_TI)
165184
&t_TI = save_TI
166185
END
167-
CheckDefSuccess(lines)
168-
CheckScriptSuccess(['vim9script'] + lines)
186+
CheckDefAndScriptSuccess(lines)
169187

170188
CheckDefFailure(['&t_TI = 123'], 'E1012:')
171189
CheckScriptFailure(['vim9script', '&t_TI = 123'], 'E928:')

src/testdir/vim9.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enddef
4848

4949
" Check that a command fails both when used in a :def function and when used
5050
" in Vim9 script.
51-
def CheckScriptAndDefFailure(lines: list<string>, error: string, lnum = -3)
51+
def CheckDefAndScriptFailure(lines: list<string>, error: string, lnum = -3)
5252
CheckDefFailure(lines, error, lnum)
5353
CheckScriptFailure(['vim9script'] + lines, error, lnum + 1)
5454
enddef

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+
1644,
757759
/**/
758760
1643,
759761
/**/

src/vim9type.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,23 @@ func_type_add_arg_types(
202202
type_T *
203203
typval2type(typval_T *tv, garray_T *type_gap)
204204
{
205-
type_T *actual;
205+
type_T *type;
206206
type_T *member_type;
207207

208208
if (tv->v_type == VAR_NUMBER)
209+
{
210+
if (tv->vval.v_number == 0 || tv->vval.v_number == 1)
211+
{
212+
// number 0 and 1 can also be used for bool
213+
type = alloc_type(type_gap);
214+
if (type == NULL)
215+
return NULL;
216+
type->tt_type = VAR_NUMBER;
217+
type->tt_flags = TTFLAG_BOOL_OK;
218+
return type;
219+
}
209220
return &t_number;
221+
}
210222
if (tv->v_type == VAR_BOOL)
211223
return &t_bool; // not used
212224
if (tv->v_type == VAR_STRING)
@@ -276,13 +288,13 @@ typval2type(typval_T *tv, garray_T *type_gap)
276288
}
277289
}
278290

279-
actual = alloc_type(type_gap);
280-
if (actual == NULL)
291+
type = alloc_type(type_gap);
292+
if (type == NULL)
281293
return NULL;
282-
actual->tt_type = tv->v_type;
283-
actual->tt_member = &t_any;
294+
type->tt_type = tv->v_type;
295+
type->tt_member = &t_any;
284296

285-
return actual;
297+
return type;
286298
}
287299

288300
/*

0 commit comments

Comments
 (0)