From 8954a682c7371214a9a4cba82bd53871793a54b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 25 Jan 2024 12:22:48 +0100
Subject: [PATCH] Bytecode: error out when using det_cond_forecast with
 perfect_foresight shocks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

They’re not implemented in bytecode.

Closes: #1884
---
 mex/sources/bytecode/bytecode.cc | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/mex/sources/bytecode/bytecode.cc b/mex/sources/bytecode/bytecode.cc
index a1b497534b..daab14ccb0 100644
--- a/mex/sources/bytecode/bytecode.cc
+++ b/mex/sources/bytecode/bytecode.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007-2023 Dynare Team
+ * Copyright © 2007-2024 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -262,6 +262,22 @@ mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
       if (!shock_str_date_)
         mexErrMsgTxt(
             "The extended_path description structure does not contain the member: shock_str_date_");
+      mxArray* shock_perfect_foresight_
+          = mxGetField(extended_path_struct, 0, "shock_perfect_foresight_");
+      if (!shock_perfect_foresight_)
+        mexErrMsgTxt("The extended_path description structure does not contain the member: "
+                     "shock_perfect_foresight_");
+
+      // Check that there is no 'perfect_foresight' shocks, which are not implemented
+      double* constrained_pf = mxGetPr(constrained_perfect_foresight_);
+      double* shock_pf = mxGetPr(shock_perfect_foresight_);
+      if (auto is_pf = [](double v) { return v != 0; };
+          any_of(constrained_pf,
+                 constrained_pf + mxGetNumberOfElements(constrained_perfect_foresight_), is_pf)
+          || any_of(shock_pf, shock_pf + mxGetNumberOfElements(shock_perfect_foresight_), is_pf))
+        mexErrMsgTxt(
+            "Shocks of type 'perfect_foresight' are not supported with the bytecode option.");
+
       int nb_constrained = mxGetM(constrained_vars_) * mxGetN(constrained_vars_);
       int nb_controlled = 0;
       mxArray* options_cond_fcst_ = mxGetField(extended_path_struct, 0, "options_cond_fcst_");
-- 
GitLab