Skip to content

Commit a650485

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.1917: Vim9: incorrect type inference with mkdir()
Problem: Vim9: incorrect type inference with mkdir() (dezza) Solution: Before compiling a RHS expression in an assignment, save the new local variable contents (Yegappan Lakshmanan) fixes: #18751 closes: #18751 Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 61b73b8 commit a650485

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/testdir/test_vim9_func.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,6 +4771,20 @@ def Test_call_modified_import_func()
47714771
v9.CheckScriptSuccess(lines)
47724772
enddef
47734773

4774+
" Test for assigning the return value of mkdir() to a new local variable.
4775+
" This used to result in the "E1012: Type mismatch; expected list<any> but
4776+
" got number" error message.
4777+
def Test_assign_mkdir_ret_value()
4778+
var lines =<< trim END
4779+
vim9script
4780+
def Fn()
4781+
var ret: number = mkdir('./foo/bar/baz', 'p')
4782+
enddef
4783+
defcompile
4784+
END
4785+
v9.CheckScriptSuccess(lines)
4786+
enddef
4787+
47744788
" The following messes up syntax highlight, keep near the end.
47754789
if has('python3')
47764790
def Test_python3_command()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
1917,
732734
/**/
733735
1916,
734736
/**/

src/vim9compile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,6 +3170,8 @@ compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
31703170
int ret = OK;
31713171
char_u *whitep;
31723172
lhs_T *lhs = &cac->cac_lhs;
3173+
lvar_T *lvp;
3174+
lvar_T save_lhs_lvar;
31733175

31743176
// Compile the expression.
31753177
if (cac->cac_incdec)
@@ -3178,7 +3180,14 @@ compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
31783180
// Temporarily hide the new local variable here, it is
31793181
// not available to this expression.
31803182
if (lhs->lhs_new_local)
3183+
{
31813184
--cctx->ctx_locals.ga_len;
3185+
3186+
// Save the local variable value (compiling the RHS expression may
3187+
// create new local variables).
3188+
lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len;
3189+
save_lhs_lvar = *lvp;
3190+
}
31823191
whitep = cac->cac_op + cac->cac_oplen;
31833192

31843193
if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
@@ -3190,7 +3199,15 @@ compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
31903199

31913200
ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
31923201
if (lhs->lhs_new_local)
3202+
{
3203+
// Restore the local variable value. Update lhs_lvar as the index of
3204+
// the local variable might have changed.
3205+
lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len;
3206+
*lvp = save_lhs_lvar;
3207+
lhs->lhs_lvar = lvp;
3208+
31933209
++cctx->ctx_locals.ga_len;
3210+
}
31943211

31953212
return ret;
31963213
}

0 commit comments

Comments
 (0)