Skip to content

Commit 3e06a1e

Browse files
committed
patch 8.2.1416: Vim9: boolean evaluation does not work as intended
Problem: Vim9: boolean evaluation does not work as intended. Solution: Use tv2bool() in Vim9 script. (closes #6681)
1 parent 6f8f733 commit 3e06a1e

4 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/eval.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ eval_to_bool(
192192
*error = FALSE;
193193
if (!skip)
194194
{
195-
retval = (tv_get_number_chk(&tv, error) != 0);
195+
if (in_vim9script())
196+
retval = tv2bool(&tv);
197+
else
198+
retval = (tv_get_number_chk(&tv, error) != 0);
196199
clear_tv(&tv);
197200
}
198201
}
@@ -3098,7 +3101,8 @@ eval7(
30983101

30993102
// Apply prefixed "-" and "+" now. Matters especially when
31003103
// "->" follows.
3101-
if (ret == OK && evaluate && end_leader > start_leader)
3104+
if (ret == OK && evaluate && end_leader > start_leader
3105+
&& rettv->v_type != VAR_BLOB)
31023106
ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
31033107
break;
31043108

@@ -3281,7 +3285,10 @@ eval7_leader(
32813285
f = rettv->vval.v_float;
32823286
else
32833287
#endif
3284-
val = tv_get_number_chk(rettv, &error);
3288+
if (in_vim9script() && end_leader[-1] == '!')
3289+
val = tv2bool(rettv);
3290+
else
3291+
val = tv_get_number_chk(rettv, &error);
32853292
if (error)
32863293
{
32873294
clear_tv(rettv);

src/testdir/test_vim9_expr.vim

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,31 +1750,35 @@ enddef
17501750

17511751

17521752
def Test_expr7_not()
1753-
assert_equal(true, !'')
1754-
assert_equal(true, ![])
1755-
assert_equal(false, !'asdf')
1756-
assert_equal(false, ![2])
1757-
assert_equal(true, !!'asdf')
1758-
assert_equal(true, !![2])
1759-
1760-
assert_equal(true, !test_null_partial())
1761-
assert_equal(false, !{-> 'yes'})
1762-
1763-
assert_equal(true, !test_null_dict())
1764-
assert_equal(true, !{})
1765-
assert_equal(false, !{'yes': 'no'})
1766-
1767-
if has('channel')
1768-
assert_equal(true, !test_null_job())
1769-
assert_equal(true, !test_null_channel())
1770-
endif
1771-
1772-
assert_equal(true, !test_null_blob())
1773-
assert_equal(true, !0z)
1774-
assert_equal(false, !0z01)
1775-
1776-
assert_equal(true, !test_void())
1777-
assert_equal(true, !test_unknown())
1753+
let lines =<< trim END
1754+
assert_equal(true, !'')
1755+
assert_equal(true, ![])
1756+
assert_equal(false, !'asdf')
1757+
assert_equal(false, ![2])
1758+
assert_equal(true, !!'asdf')
1759+
assert_equal(true, !![2])
1760+
1761+
assert_equal(true, !test_null_partial())
1762+
assert_equal(false, !{-> 'yes'})
1763+
1764+
assert_equal(true, !test_null_dict())
1765+
assert_equal(true, !{})
1766+
assert_equal(false, !{'yes': 'no'})
1767+
1768+
if has('channel')
1769+
assert_equal(true, !test_null_job())
1770+
assert_equal(true, !test_null_channel())
1771+
endif
1772+
1773+
assert_equal(true, !test_null_blob())
1774+
assert_equal(true, !0z)
1775+
assert_equal(false, !0z01)
1776+
1777+
assert_equal(true, !test_void())
1778+
assert_equal(true, !test_unknown())
1779+
END
1780+
CheckDefSuccess(lines)
1781+
CheckScriptSuccess(['vim9script'] + lines)
17781782
enddef
17791783

17801784
func Test_expr7_fails()

src/testdir/vim9.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
" Utility functions for testing vim9 script
22

3+
" Check that "lines" inside ":def" has no error.
4+
func CheckDefSuccess(lines)
5+
call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], 'Xdef')
6+
so Xdef
7+
call Func()
8+
call delete('Xdef')
9+
endfunc
10+
311
" Check that "lines" inside ":def" results in an "error" message.
412
func CheckDefFailure(lines, error)
513
call writefile(['def Func()'] + a:lines + ['enddef', 'defcompile'], 'Xdef')

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+
1416,
757759
/**/
758760
1415,
759761
/**/

0 commit comments

Comments
 (0)