patch 8.2.4103: Vim9: variable declared in for loop not initialzed
Problem: Vim9: variable declared in for loop not initialzed.
Solution: Always initialze the variable. (closes #9535)
diff --git a/src/vim9instr.c b/src/vim9instr.c
index 5057297..493683a 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -1845,6 +1845,25 @@
return FAIL;
}
+/*
+ * Return TRUE when inside a "for" or "while" loop.
+ */
+ int
+inside_loop_scope(cctx_T *cctx)
+{
+ scope_T *scope = cctx->ctx_scope;
+
+ for (;;)
+ {
+ if (scope == NULL)
+ break;
+ if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
+ return TRUE;
+ scope = scope->se_outer;
+ }
+ return FALSE;
+}
+
int
generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
{
@@ -1869,8 +1888,9 @@
varnumber_T val = isn->isn_arg.number;
garray_T *stack = &cctx->ctx_type_stack;
- if (val == 0 && is_decl)
+ if (val == 0 && is_decl && !inside_loop_scope(cctx))
{
+ // zero is the default value, no need to do anything
--instr->ga_len;
}
else