diff src/vim9compile.c @ 24152:c308076e225e v8.2.2617

patch 8.2.2617: Vim9: script variable in block not found by function Commit: https://github.com/vim/vim/commit/8863bda25df821fc79bebf9dc73c79776ae5f675 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Mar 17 18:42:08 2021 +0100 patch 8.2.2617: Vim9: script variable in block not found by function Problem: Vim9: script variable in a block scope not found by a nested function. Solution: Copy the block scope IDs before compiling the function.
author Bram Moolenaar <Bram@vim.org>
date Wed, 17 Mar 2021 18:45:03 +0100
parents f4061617c438
children 6e8f49a4a193
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5183,6 +5183,21 @@ compile_nested_function(exarg_T *eap, cc
 	r = eap->skip ? OK : FAIL;
 	goto theend;
     }
+
+    // copy over the block scope IDs before compiling
+    if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
+    {
+	int block_depth = cctx->ctx_ufunc->uf_block_depth;
+
+	ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
+	if (ufunc->uf_block_ids != NULL)
+	{
+	    mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
+						    sizeof(int) * block_depth);
+	    ufunc->uf_block_depth = block_depth;
+	}
+    }
+
     if (func_needs_compiling(ufunc, PROFILING(ufunc))
 	    && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
 								       == FAIL)
@@ -5209,25 +5224,12 @@ compile_nested_function(exarg_T *eap, cc
 	// Define a local variable for the function reference.
 	lvar_T	*lvar = reserve_local(cctx, name_start, name_end - name_start,
 						    TRUE, ufunc->uf_func_type);
-	int block_depth = cctx->ctx_ufunc->uf_block_depth;
 
 	if (lvar == NULL)
 	    goto theend;
 	if (generate_FUNCREF(cctx, ufunc) == FAIL)
 	    goto theend;
 	r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
-
-	// copy over the block scope IDs
-	if (block_depth > 0)
-	{
-	    ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
-	    if (ufunc->uf_block_ids != NULL)
-	    {
-		mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
-						    sizeof(int) * block_depth);
-		ufunc->uf_block_depth = block_depth;
-	    }
-	}
     }
     // TODO: warning for trailing text?