diff --git a/src/DataTree.cc b/src/DataTree.cc index 5649aa0ea2076f3de8ff4632b0c4c8f890bc237f..1906212ab59d0dac59e04d055a77129a42bf6e32 100644 --- a/src/DataTree.cc +++ b/src/DataTree.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2003-2020 Dynare Team + * Copyright © 2003-2021 Dynare Team * * This file is part of Dynare. * @@ -99,9 +99,12 @@ DataTree::operator=(const DataTree &d) initConstants(); /* Model local variables must be next, because they can be evaluated in Add* - methods when the model equations are added */ - for (const auto &it : d.local_variables_table) - local_variables_table[it.first] = it.second->clone(*this); + methods when the model equations are added. They need to be cloned in + order of appearance in the model block (hence with + local_variables_vector), because if there is a model_local_variable statement + the symbol IDs ordering may not be the right one (see dynare#1782) */ + for (const auto &id : d.local_variables_vector) + local_variables_table[id] = d.local_variables_table.at(id)->clone(*this); for (const auto &it : d.node_list) it->clone(*this); diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index 4670113b327f8cc762c9bbcc63a0bd7ec7be57e0..51cd294354ea889d30e545d31633adf46ebc8c31 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -2368,7 +2368,9 @@ ParsingDriver::declare_and_init_model_local_variable(const string &name, expr_t } catch (SymbolTable::AlreadyDeclaredException &e) { - // It can have already been declared in a steady_state_model block, check that it is indeed a ModelLocalVariable + /* It can have already been declared in a steady_state_model block or + model_local_variable statement, check that it is indeed a + ModelLocalVariable */ symb_id = mod_file->symbol_table.getID(name); if (mod_file->symbol_table.getType(symb_id) != SymbolType::modelLocalVariable) error(name + " has wrong type or was already used on the right-hand side. You cannot use it on the left-hand side of a pound ('#') expression");