diff src/structs.h @ 30333:fc0830246f49 v9.0.0502

patch 9.0.0502: a closure in a nested loop in a :def function does not work Commit: https://github.com/vim/vim/commit/cc34181f9994d64f8c8fa2f5845eaf0cc963067f Author: Bram Moolenaar <Bram@vim.org> Date: Mon Sep 19 15:54:34 2022 +0100 patch 9.0.0502: a closure in a nested loop in a :def function does not work Problem: A closure in a nested loop in a :def function does not work. Solution: Use an array of loopvars, one per loop level.
author Bram Moolenaar <Bram@vim.org>
date Mon, 19 Sep 2022 17:00:07 +0200
parents 029c59bf78f1
children 7fc27d7ce3b0
line wrap: on
line diff
--- a/src/structs.h
+++ b/src/structs.h
@@ -2108,6 +2108,9 @@ struct loopvars_S
     int		lvs_copyID;	// for garbage collection
 };
 
+// maximum nesting of :while and :for loops in a :def function
+#define MAX_LOOP_DEPTH 10
+
 typedef struct outer_S outer_T;
 struct outer_S {
     garray_T	*out_stack;	    // stack from outer scope, or a copy
@@ -2116,11 +2119,13 @@ struct outer_S {
     outer_T	*out_up;	    // outer scope of outer scope or NULL
     partial_T	*out_up_partial;    // partial owning out_up or NULL
 
-    garray_T	*out_loop_stack;    // stack from outer scope, or a copy
+    struct {
+	garray_T *stack;	    // stack from outer scope, or a copy
 				    // containing only vars inside the loop
-    short	out_loop_var_idx;   // first variable defined in a loop
-				    // in out_loop_stack
-    short	out_loop_var_count; // number of variables defined in a loop
+	short	 var_idx;	    // first variable defined in a loop in
+				    // out_loop_stack
+	short	 var_count;	    // number of variables defined in a loop
+    } out_loop[MAX_LOOP_DEPTH];
 };
 
 struct partial_S
@@ -2141,7 +2146,8 @@ struct partial_S
 
     funcstack_T	*pt_funcstack;	// copy of stack, used after context
 				// function returns
-    loopvars_T	*pt_loopvars;	// copy of loop variables, used after loop
+    loopvars_T	*(pt_loopvars[MAX_LOOP_DEPTH]);
+				// copy of loop variables, used after loop
 				// block ends
 
     typval_T	*pt_argv;	// arguments in allocated array
@@ -2151,6 +2157,14 @@ struct partial_S
     dict_T	*pt_dict;	// dict for "self"
 };
 
+typedef struct {
+    short	lvi_depth;	    // current nested loop depth
+    struct {
+	short	var_idx;	    // index of first variable inside loop
+	short	var_count;	    // number of variables inside loop
+    } lvi_loop[MAX_LOOP_DEPTH];
+} loopvarinfo_T;
+
 typedef struct AutoPatCmd_S AutoPatCmd_T;
 
 /*