From 985116ae0b7b3ef17e0c0ea2669068dd6b3b39c7 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Sun, 12 Jul 2020 17:31:09 +0200
Subject: [PATCH] patch 8.2.1191: Vim9: crash when function calls itself

Problem:    Vim9: crash when function calls itself.
Solution:   Add status UF_COMPILING. (closes #6441)
---
 src/structs.h                  |  1 +
 src/testdir/test_vim9_func.vim | 12 ++++++++++++
 src/version.c                  |  2 ++
 src/vim9compile.c              |  2 ++
 4 files changed, 17 insertions(+)

diff --git a/src/structs.h b/src/structs.h
index bdc763b52d..4a0c10a459 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1539,6 +1539,7 @@ typedef struct funccall_S funccall_T;
 typedef enum {
     UF_NOT_COMPILED,
     UF_TO_BE_COMPILED,
+    UF_COMPILING,
     UF_COMPILED
 } def_status_T;
 
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index 5ea29457ee..53206dfb7b 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -999,5 +999,17 @@ func Test_silent_echo()
   call delete('XTest_silent_echo')
 endfunc
 
+def Fibonacci(n: number): number
+  if n < 2
+    return n
+  else
+    return Fibonacci(n - 1) + Fibonacci(n - 2)
+  endif
+enddef
+
+def Test_recursive_call()
+  assert_equal(6765, Fibonacci(20))
+enddef
+
 
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
diff --git a/src/version.c b/src/version.c
index 8ae2f29d58..487303edc9 100644
--- a/src/version.c
+++ b/src/version.c
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1191,
 /**/
     1190,
 /**/
diff --git a/src/vim9compile.c b/src/vim9compile.c
index fceb4811bc..8d6c1398da 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -6802,6 +6802,8 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
     else if (add_def_function(ufunc) == FAIL)
 	return FAIL;
 
+    ufunc->uf_def_status = UF_COMPILING;
+
     CLEAR_FIELD(cctx);
     cctx.ctx_ufunc = ufunc;
     cctx.ctx_lnum = -1;
-- 
GitLab