Skip to content

Commit

Permalink
[NF] Detect recursive bindings during evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Aug 10, 2020
1 parent 20d8634 commit 07e8590
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -105,6 +105,11 @@ public
list<ErrorTypes.TotalMessage> errors;
end INVALID_BINDING;

record EVALUATING_BINDING
"Used by the constant evaluation to detect recursive bindings."
Binding binding;
end EVALUATING_BINDING;

public
function fromAbsyn
input Option<Absyn.Exp> bindingExp;
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -65,6 +65,7 @@ import TypeCheck = NFTypeCheck;
import ExpandExp = NFExpandExp;
import ElementSource;
import Flags;
import Prefixes = NFPrefixes;

public
uniontype EvalTarget
Expand Down Expand Up @@ -399,6 +400,11 @@ algorithm
if binding.evaluated then
exp := binding.bindingExp;
else
// Mark the binding as currently being evaluated, to detect loops due
// to mutually dependent constants/parameters.
comp := Component.setBinding(Binding.EVALUATING_BINDING(binding), comp);
InstNode.updateComponent(comp, node);

exp := evalExp_impl(binding.bindingExp, target);

binding.bindingExp := exp;
Expand All @@ -417,6 +423,14 @@ algorithm
then
(defaultExp, false);

case Binding.EVALUATING_BINDING()
algorithm
Error.addSourceMessage(Error.CIRCULAR_PARAM,
{InstNode.name(node), Prefixes.variabilityString(Component.variability(comp))},
InstNode.info(node));
then
fail();

else
algorithm
Error.addInternalError(getInstanceName() + " failed on untyped binding", sourceInfo());
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -734,6 +734,7 @@ RecordConstructor2.mo \
RecordExtends1.mo \
RecordExtends2.mo \
RecordUnknownDim1.mo \
RecursiveConstants1.mo \
RecursiveExtends1.mo \
RecursiveExtends3.mo \
RecursiveInst1.mo \
Expand Down
20 changes: 20 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RecursiveConstants1.mo
@@ -0,0 +1,20 @@
// name: RecursiveConstants1
// keywords:
// status: incorrect
// cflags: -d=newInst
//

model RecursiveConstants1
constant Real x = y;
constant Real y = x;
end RecursiveConstants1;

// Result:
// Error processing file: RecursiveConstants1.mo
// [flattening/modelica/scodeinst/RecursiveConstants1.mo:9:3-9:22:writable] Error: Variable 'y' has a cyclic dependency and has variability constant.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
//
// Execution failed!
// endResult

0 comments on commit 07e8590

Please sign in to comment.