From 99c80f56c15a966c4f4f0f31e39fcc35fd454593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Mon, 17 Jun 2024 12:41:43 +0200 Subject: [PATCH] Add error for assignment of iterator (#12591) - Add error message for assigning for an iterator inside a for-loop. - Improve the error message for using reinit on an iterator. Fixes #12585 --- OMCompiler/Compiler/NFFrontEnd/NFInst.mo | 9 +++++++ OMCompiler/Compiler/NFFrontEnd/NFTyping.mo | 15 ++++++++++- OMCompiler/Compiler/Util/Error.mo | 2 ++ .../modelica/scodeinst/ForStatementAssign1.mo | 23 +++++++++++++++++ .../flattening/modelica/scodeinst/Makefile | 2 ++ .../modelica/scodeinst/ReinitInvalid8.mo | 25 +++++++++++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 testsuite/flattening/modelica/scodeinst/ForStatementAssign1.mo create mode 100644 testsuite/flattening/modelica/scodeinst/ReinitInvalid8.mo diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo index 1735efc9de..54b4c753e9 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo @@ -3541,6 +3541,15 @@ protected Restriction res; algorithm () := match lhs + case Expression.CREF() + guard ComponentRef.isIterator(lhs.cref) + algorithm + // Give an error if assigning to an iterator. + Error.addSourceMessage(Error.ASSIGN_ITERATOR_ERROR, + {ComponentRef.toString(lhs.cref)}, info); + then + fail(); + case Expression.CREF() guard ComponentRef.isCref(lhs.cref) algorithm diff --git a/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo b/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo index de51361f3e..dc095328dd 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFTyping.mo @@ -3411,6 +3411,10 @@ function checkAssignment input InstContext.Type context; input SourceInfo info; algorithm + if InstContext.inInstanceAPI(context) then + return; + end if; + () := match lhsExp local Integer i; @@ -3709,7 +3713,16 @@ algorithm // The first argument must be a cref. cref := match crefExp - case Expression.CREF() then crefExp.cref; + case Expression.CREF() + algorithm + if ComponentRef.isIterator(crefExp.cref) then + Error.addSourceMessage(Error.ASSIGN_ITERATOR_ERROR, + {ComponentRef.toString(crefExp.cref)}, info); + fail(); + end if; + then + crefExp.cref; + else algorithm Error.addSourceMessage(Error.REINIT_MUST_BE_VAR_OR_ARRAY, {}, info); diff --git a/OMCompiler/Compiler/Util/Error.mo b/OMCompiler/Compiler/Util/Error.mo index f48d8d4afd..3a4c6c0c03 100644 --- a/OMCompiler/Compiler/Util/Error.mo +++ b/OMCompiler/Compiler/Util/Error.mo @@ -901,6 +901,8 @@ public constant ErrorTypes.Message NON_FIXED_CONSTANT = ErrorTypes.MESSAGE(413, Gettext.gettext("Constant '%s' must be fixed but has 'fixed = false'")); public constant ErrorTypes.Message CONFLICTING_INHERITED_ANNOTATIONS = ErrorTypes.MESSAGE(414, ErrorTypes.SCRIPTING(), ErrorTypes.WARNING(), Gettext.gettext("Conflicting '%s' annotations inherited by class '%s':\n %s from 'extends %s'\n %s from 'extends %s'")); +public constant ErrorTypes.Message ASSIGN_ITERATOR_ERROR = ErrorTypes.MESSAGE(415, ErrorTypes.TRANSLATION(), ErrorTypes.ERROR(), + Gettext.gettext("Assignment to iterator '%s'.")); public constant ErrorTypes.Message INITIALIZATION_NOT_FULLY_SPECIFIED = ErrorTypes.MESSAGE(496, ErrorTypes.TRANSLATION(), ErrorTypes.WARNING(), Gettext.gettext("The initial conditions are not fully specified. %s.")); diff --git a/testsuite/flattening/modelica/scodeinst/ForStatementAssign1.mo b/testsuite/flattening/modelica/scodeinst/ForStatementAssign1.mo new file mode 100644 index 0000000000..ca4a6f72e6 --- /dev/null +++ b/testsuite/flattening/modelica/scodeinst/ForStatementAssign1.mo @@ -0,0 +1,23 @@ +// name: ForStatementAssign1 +// keywords: +// status: incorrect +// cflags: -d=newInst +// +// + +model ForStatementAssign1 +algorithm + for i in 1:2 loop + i := 1; + end for; +end ForStatementAssign1; + +// Result: +// Error processing file: ForStatementAssign1.mo +// [flattening/modelica/scodeinst/ForStatementAssign1.mo:11:5-11:11:writable] Error: Assignment to iterator 'i'. +// +// # Error encountered! Exiting... +// # Please check the error message and the flags. +// +// Execution failed! +// endResult diff --git a/testsuite/flattening/modelica/scodeinst/Makefile b/testsuite/flattening/modelica/scodeinst/Makefile index 1cb07a5342..f8710a4f6c 100644 --- a/testsuite/flattening/modelica/scodeinst/Makefile +++ b/testsuite/flattening/modelica/scodeinst/Makefile @@ -542,6 +542,7 @@ ForStatement1.mo \ ForStatement2.mo \ ForStatement3.mo \ ForStatementArray.mo \ +ForStatementAssign1.mo \ ForStatementNonVector.mo \ ForStatementPrefix.mo \ func1.mo \ @@ -1078,6 +1079,7 @@ ReinitInvalid4.mo \ ReinitInvalid5.mo \ ReinitInvalid6.mo \ ReinitInvalid7.mo \ +ReinitInvalid8.mo \ Return1.mo \ Return2.mo \ Size1.mo \ diff --git a/testsuite/flattening/modelica/scodeinst/ReinitInvalid8.mo b/testsuite/flattening/modelica/scodeinst/ReinitInvalid8.mo new file mode 100644 index 0000000000..50f03c9b88 --- /dev/null +++ b/testsuite/flattening/modelica/scodeinst/ReinitInvalid8.mo @@ -0,0 +1,25 @@ +// name: ReinitInvalid8 +// keywords: +// status: incorrect +// cflags: -d=newInst +// + +model ReinitInvalid8 + Real x; +equation + for i in 1.0:2.0 loop + when time > 0 then + reinit(i, 1.0); + end when; + end for; +end ReinitInvalid8; + +// Result: +// Error processing file: ReinitInvalid8.mo +// [flattening/modelica/scodeinst/ReinitInvalid8.mo:12:7-12:21:writable] Error: Assignment to iterator 'i'. +// +// # Error encountered! Exiting... +// # Please check the error message and the flags. +// +// Execution failed! +// endResult