Skip to content

Commit 93ad147

Browse files
committed
patch 8.2.1490: Vim9: using /= with float and number doesn't work
Problem: Vim9: using /= with float and number doesn't work. Solution: Better support assignment with operator. (closes #6742)
1 parent 191929b commit 93ad147

3 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/testdir/test_vim9_script.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ def Test_assignment()
9090
&ts %= 4
9191
assert_equal(2, &ts)
9292

93+
if has('float')
94+
let f100: float = 100.0
95+
f100 /= 5
96+
assert_equal(20.0, f100)
97+
98+
let f200: float = 200.0
99+
f200 /= 5.0
100+
assert_equal(40.0, f200)
101+
102+
CheckDefFailure(['let nr: number = 200', 'nr /= 5.0'], 'E1012:')
103+
endif
104+
93105
lines =<< trim END
94106
vim9script
95107
&ts = 6

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+
1490,
757759
/**/
758760
1489,
759761
/**/

src/vim9compile.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,18 +4923,19 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
49234923
lvar->lv_type = stacktype;
49244924
}
49254925
}
4926-
else
4926+
else if (*op == '=')
49274927
{
49284928
type_T *use_type = lvar->lv_type;
49294929

4930+
// without operator type is here, otherwise below
49304931
if (has_index)
49314932
{
49324933
use_type = use_type->tt_member;
49334934
if (use_type == NULL)
49344935
use_type = &t_void;
49354936
}
49364937
if (need_type(stacktype, use_type, -1, cctx, FALSE)
4937-
== FAIL)
4938+
== FAIL)
49384939
goto theend;
49394940
}
49404941
}
@@ -5008,18 +5009,20 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
50085009

50095010
if (oplen > 0 && *op != '=')
50105011
{
5011-
type_T *expected = &t_number;
5012+
type_T *expected;
50125013
type_T *stacktype;
50135014

5014-
// TODO: if type is known use float or any operation
5015-
// TODO: check operator matches variable type
5016-
50175015
if (*op == '.')
50185016
expected = &t_string;
5019-
else if (*op == '+')
5017+
else
50205018
expected = member_type;
50215019
stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5022-
if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
5020+
if (
5021+
#ifdef FEAT_FLOAT
5022+
// If variable is float operation with number is OK.
5023+
!(expected == &t_float && stacktype == &t_number) &&
5024+
#endif
5025+
need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
50235026
goto theend;
50245027

50255028
if (*op == '.')
@@ -5034,20 +5037,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
50345037
member_type, stacktype) == FAIL)
50355038
goto theend;
50365039
}
5037-
else
5038-
{
5039-
isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5040-
5041-
if (isn == NULL)
5042-
goto theend;
5043-
switch (*op)
5044-
{
5045-
case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5046-
case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5047-
case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5048-
case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5049-
}
5050-
}
5040+
else if (generate_two_op(cctx, op) == FAIL)
5041+
goto theend;
50515042
}
50525043

50535044
if (has_index)

0 commit comments

Comments
 (0)