Skip to content

Commit

Permalink
Add error for assignment of iterator (OpenModelica#12591)
Browse files Browse the repository at this point in the history
- Add error message for assigning for an iterator inside a for-loop.
- Improve the error message for using reinit on an iterator.

Fixes OpenModelica#12585
  • Loading branch information
perost committed Jun 17, 2024
1 parent 32fe465 commit 99c80f5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
9 changes: 9 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFTyping.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Error.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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."));
Expand Down
23 changes: 23 additions & 0 deletions testsuite/flattening/modelica/scodeinst/ForStatementAssign1.mo
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ ForStatement1.mo \
ForStatement2.mo \
ForStatement3.mo \
ForStatementArray.mo \
ForStatementAssign1.mo \
ForStatementNonVector.mo \
ForStatementPrefix.mo \
func1.mo \
Expand Down Expand Up @@ -1078,6 +1079,7 @@ ReinitInvalid4.mo \
ReinitInvalid5.mo \
ReinitInvalid6.mo \
ReinitInvalid7.mo \
ReinitInvalid8.mo \
Return1.mo \
Return2.mo \
Size1.mo \
Expand Down
25 changes: 25 additions & 0 deletions testsuite/flattening/modelica/scodeinst/ReinitInvalid8.mo
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 99c80f5

Please sign in to comment.