0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.2.1812: Vim9: nested closure throws an internal error

Problem:    Vim9: nested closure throws an internal error.
Solution:   Do not skip a local variable with a partial. (closes #7065)
This commit is contained in:
Bram Moolenaar
2020-10-07 19:08:04 +02:00
parent 9a033d7b18
commit 0876c78527
3 changed files with 20 additions and 2 deletions

View File

@@ -1388,6 +1388,20 @@ def Test_double_closure_fails()
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
enddef enddef
def Test_nested_closure_used()
var lines =<< trim END
vim9script
def Func()
var x = 'hello'
var Closure = {-> x}
g:Myclosure = {-> Closure()}
enddef
Func()
assert_equal('hello', g:Myclosure())
END
CheckScriptSuccess(lines)
enddef
def Test_nested_closure_fails() def Test_nested_closure_fails()
var lines =<< trim END var lines =<< trim END
vim9script vim9script

View File

@@ -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 */
/**/
1812,
/**/ /**/
1811, 1811,
/**/ /**/

View File

@@ -377,10 +377,11 @@ handle_closure_in_use(ectx_T *ectx, int free_arguments)
tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
// Do not copy a partial created for a local function. // Do not copy a partial created for a local function.
// TODO: this won't work if the closure actually uses it. But when // TODO: This won't work if the closure actually uses it. But when
// keeping it it gets complicated: it will create a reference cycle // keeping it it gets complicated: it will create a reference cycle
// inside the partial, thus needs special handling for garbage // inside the partial, thus needs special handling for garbage
// collection. // collection.
// For now, decide on the reference count.
if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
{ {
int i; int i;
@@ -389,7 +390,8 @@ handle_closure_in_use(ectx_T *ectx, int free_arguments)
{ {
partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
- closure_count + i]; - closure_count + i];
if (tv->vval.v_partial == pt)
if (tv->vval.v_partial == pt && pt->pt_refcount < 2)
break; break;
} }
if (i < closure_count) if (i < closure_count)