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.
diff --git a/src/structs.h b/src/structs.h
index c532dde..e46446d 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -2108,6 +2108,9 @@
     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 @@
     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 @@
 
     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 @@
     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;
 
 /*