patch 8.2.0818: Vim9: using a discovery phase doesn't work well

Problem:    Vim9: using a discovery phase doesn't work well.
Solution:   Remove the discovery phase, instead compile a function only when
            it is used.  Add :defcompile to compile def functions earlier.
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 416198a..d41dd53 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1418,7 +1418,7 @@
 	return FAIL;
     }
 
-    if (ufunc->uf_dfunc_idx >= 0)
+    if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
     {
 	int		i;
 
@@ -1442,12 +1442,16 @@
 		return FAIL;
 	    }
 	}
+	if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
+	    if (compile_def_function(ufunc, TRUE, cctx) == FAIL)
+		return FAIL;
     }
 
     if ((isn = generate_instr(cctx,
-		    ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
+		    ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL
+							 : ISN_UCALL)) == NULL)
 	return FAIL;
-    if (ufunc->uf_dfunc_idx >= 0)
+    if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
     {
 	isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
 	isn->isn_arg.dfunc.cdf_argcount = argcount;
@@ -4454,9 +4458,12 @@
     eap->cookie = cctx;
     eap->skip = cctx->ctx_skip == TRUE;
     eap->forceit = FALSE;
-    ufunc = def_function(eap, name, cctx, TRUE);
+    ufunc = def_function(eap, name);
 
-    if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
+    if (ufunc == NULL)
+	return NULL;
+    if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
+	    && compile_def_function(ufunc, TRUE, cctx) == FAIL)
 	return NULL;
 
     // Define a local variable for the function reference.
@@ -6302,7 +6309,7 @@
  * Add a function to the list of :def functions.
  * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet.
  */
-    int
+    static int
 add_def_function(ufunc_T *ufunc)
 {
     dfunc_T *dfunc;
@@ -6328,8 +6335,9 @@
  * "outer_cctx" is set for a nested function.
  * This can be used recursively through compile_lambda(), which may reallocate
  * "def_functions".
+ * Returns OK or FAIL.
  */
-    void
+    int
 compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
 {
     char_u	*line = NULL;
@@ -6352,7 +6360,7 @@
 	delete_def_function_contents(dfunc);
     }
     else if (add_def_function(ufunc) == FAIL)
-	return;
+	return FAIL;
 
     CLEAR_FIELD(cctx);
     cctx.ctx_ufunc = ufunc;
@@ -6816,7 +6824,7 @@
 	    delete_instr(((isn_T *)instr->ga_data) + idx);
 	ga_clear(instr);
 
-	ufunc->uf_dfunc_idx = -1;
+	ufunc->uf_dfunc_idx = UF_NOT_COMPILED;
 	if (!dfunc->df_deleted)
 	    --def_functions.ga_len;
 
@@ -6836,6 +6844,7 @@
     free_imported(&cctx);
     free_locals(&cctx);
     ga_clear(&cctx.ctx_type_stack);
+    return ret;
 }
 
 /*