From 576c50c465c7b9eadb8ff1e3ab4429103c8d1064 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 20 May 2022 12:35:23 +0200
Subject: [PATCH] Fix bug in bytecode compilation of 1st deriv of external
 functions

(cherry picked from commit d1a3b227fd23b62c49f1feee7d4490884b531cab)
---
 src/ExprNode.cc | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/ExprNode.cc b/src/ExprNode.cc
index 4bf7d466..4de5a55d 100644
--- a/src/ExprNode.cc
+++ b/src/ExprNode.cc
@@ -7503,7 +7503,18 @@ FirstDerivExternalFunctionNode::compileExternalFunctionOutput(ostream &CompileCo
   int first_deriv_symb_id = datatree.external_functions_table.getFirstDerivSymbID(symb_id);
   assert(first_deriv_symb_id != ExternalFunctionsTable::IDSetButNoNameProvided);
 
-  if (first_deriv_symb_id == symb_id || alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms))
+  /* For a node with derivs provided by the user function, call the method
+     on the non-derived node */
+  if (first_deriv_symb_id == symb_id)
+    {
+      expr_t parent = datatree.AddExternalFunction(symb_id, arguments);
+      parent->compileExternalFunctionOutput(CompileCode, instruction_number, lhs_rhs,
+                                            temporary_terms, temporary_terms_idxs,
+                                            dynamic, steady_dynamic, tef_terms);
+      return;
+    }
+
+  if (alreadyWrittenAsTefTerm(first_deriv_symb_id, tef_terms))
     return;
 
   unsigned int nb_add_input_arguments = compileExternalFunctionArguments(CompileCode, instruction_number, lhs_rhs, temporary_terms,
-- 
GitLab