mirror of
https://github.com/vim/vim.git
synced 2025-07-04 23:07:33 -04:00
patch 8.2.2770: Vim9: type of loop variable is not used
Problem: Vim9: type of loop variable is not used. Solution: Parse and check the variable type. (closes #8107)
This commit is contained in:
parent
6bc00699c5
commit
fe090eb58f
@ -2343,6 +2343,12 @@ def Test_for_loop()
|
|||||||
endfor
|
endfor
|
||||||
assert_equal(6, total)
|
assert_equal(6, total)
|
||||||
|
|
||||||
|
var chars = ''
|
||||||
|
for s: string in 'foobar'
|
||||||
|
chars ..= s
|
||||||
|
endfor
|
||||||
|
assert_equal('foobar', chars)
|
||||||
|
|
||||||
# unpack with type
|
# unpack with type
|
||||||
var res = ''
|
var res = ''
|
||||||
for [n: number, s: string] in [[1, 'a'], [2, 'b']]
|
for [n: number, s: string] in [[1, 'a'], [2, 'b']]
|
||||||
@ -2408,6 +2414,12 @@ def Test_for_loop_fails()
|
|||||||
endfor
|
endfor
|
||||||
END
|
END
|
||||||
CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
|
CheckDefAndScriptFailure2(lines, 'E1018:', 'E46:', 3)
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
for nr: number in ['foo']
|
||||||
|
endfor
|
||||||
|
END
|
||||||
|
CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got string', 1)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_for_loop_script_var()
|
def Test_for_loop_script_var()
|
||||||
|
@ -750,6 +750,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
2770,
|
||||||
/**/
|
/**/
|
||||||
2769,
|
2769,
|
||||||
/**/
|
/**/
|
||||||
|
@ -7514,12 +7514,16 @@ compile_for(char_u *arg_start, cctx_T *cctx)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
|
if (vartype->tt_type == VAR_STRING)
|
||||||
|
item_type = &t_string;
|
||||||
|
else if (vartype->tt_type == VAR_LIST
|
||||||
|
&& vartype->tt_member->tt_type != VAR_ANY)
|
||||||
{
|
{
|
||||||
if (var_count == 1)
|
if (var_count == 1)
|
||||||
item_type = vartype->tt_member;
|
item_type = vartype->tt_member;
|
||||||
else if (vartype->tt_member->tt_type == VAR_LIST
|
else if (vartype->tt_member->tt_type == VAR_LIST
|
||||||
&& vartype->tt_member->tt_member->tt_type != VAR_ANY)
|
&& vartype->tt_member->tt_member->tt_type != VAR_ANY)
|
||||||
|
// TODO: should get the type from
|
||||||
item_type = vartype->tt_member->tt_member;
|
item_type = vartype->tt_member->tt_member;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7557,12 +7561,19 @@ compile_for(char_u *arg_start, cctx_T *cctx)
|
|||||||
int opt_flags = 0;
|
int opt_flags = 0;
|
||||||
int vimvaridx = -1;
|
int vimvaridx = -1;
|
||||||
type_T *type = &t_any;
|
type_T *type = &t_any;
|
||||||
|
type_T *lhs_type = &t_any;
|
||||||
|
where_T where;
|
||||||
|
|
||||||
p = skip_var_one(arg, FALSE);
|
p = skip_var_one(arg, FALSE);
|
||||||
varlen = p - arg;
|
varlen = p - arg;
|
||||||
name = vim_strnsave(arg, varlen);
|
name = vim_strnsave(arg, varlen);
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
goto failed;
|
goto failed;
|
||||||
|
if (*p == ':')
|
||||||
|
{
|
||||||
|
p = skipwhite(p + 1);
|
||||||
|
lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: script var not supported?
|
// TODO: script var not supported?
|
||||||
if (get_var_dest(name, &dest, CMD_for, &opt_flags,
|
if (get_var_dest(name, &dest, CMD_for, &opt_flags,
|
||||||
@ -7589,8 +7600,15 @@ compile_for(char_u *arg_start, cctx_T *cctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reserve a variable to store "var".
|
// Reserve a variable to store "var".
|
||||||
// TODO: check for type
|
where.wt_index = var_count > 1 ? idx + 1 : 0;
|
||||||
var_lvar = reserve_local(cctx, arg, varlen, TRUE, &t_any);
|
where.wt_variable = TRUE;
|
||||||
|
if (lhs_type == &t_any)
|
||||||
|
lhs_type = item_type;
|
||||||
|
else if (item_type != &t_unknown
|
||||||
|
&& !(var_count > 1 && item_type == &t_any)
|
||||||
|
&& check_type(lhs_type, item_type, TRUE, where) == FAIL)
|
||||||
|
goto failed;
|
||||||
|
var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
|
||||||
if (var_lvar == NULL)
|
if (var_lvar == NULL)
|
||||||
// out of memory or used as an argument
|
// out of memory or used as an argument
|
||||||
goto failed;
|
goto failed;
|
||||||
@ -7602,8 +7620,6 @@ compile_for(char_u *arg_start, cctx_T *cctx)
|
|||||||
generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
|
generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*p == ':')
|
|
||||||
p = skip_type(skipwhite(p + 1), FALSE);
|
|
||||||
if (*p == ',' || *p == ';')
|
if (*p == ',' || *p == ';')
|
||||||
++p;
|
++p;
|
||||||
arg = skipwhite(p);
|
arg = skipwhite(p);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user