Skip to content

Commit ac822ee

Browse files
committed
Improve Ceval error messages
- Include info when evaluating record bindings so that error messages are shown. - Add a "from here" message when evaluating component bindings to make it clearer where the error is coming from. Fixes OpenModelica#14902
1 parent 4daace8 commit ac822ee

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

OMCompiler/Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ protected
336336
Option<Expression> start_exp;
337337
Type cref_ty, exp_ty;
338338
Integer dim_diff;
339+
list<Integer> errors;
339340
algorithm
340341
exp_context := InstContext.nodeContext(node, target.context);
341342
Typing.typeComponentBinding(node, exp_context, typeChildren = false);
@@ -370,13 +371,18 @@ algorithm
370371
// Mark the binding as currently being evaluated, to detect loops due
371372
// to mutually dependent constants/parameters.
372373
Mutable.update(binding.evalState, NFBinding.EvalState.EVALUATING);
374+
ErrorExt.setCheckpoint(getInstanceName());
373375

374376
// Evaluate the binding expression.
375377
try
376378
exp := evalExp(binding.bindingExp, target);
379+
ErrorExt.delCheckpoint(getInstanceName());
377380
else
378381
// Reset the flag if the evaluation failed.
379382
Mutable.update(binding.evalState, NFBinding.EvalState.NOT_EVALUATED);
383+
errors := ErrorExt.popCheckPoint(getInstanceName());
384+
Error.addSourceMessage(Error.ERROR_FROM_HERE, {}, binding.info);
385+
ErrorExt.pushMessages(errors);
380386
fail();
381387
end try;
382388

@@ -746,7 +752,6 @@ algorithm
746752
else
747753
// Ignore components that don't have a binding, it might not be an error
748754
// and if it is we can give better error messages in other places.
749-
arg := Expression.EMPTY(ty);
750755
end try;
751756
end if;
752757

OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ algorithm
948948
comp_var := Component.variability(comp);
949949
if comp_var <= Variability.STRUCTURAL_PARAMETER or binding_var <= Variability.STRUCTURAL_PARAMETER then
950950
// Constant evaluate parameters that are structural/constant.
951-
binding_exp := Ceval.evalExp(binding_exp);
951+
binding_exp := Ceval.evalExp(binding_exp, Ceval.EvalTarget.new(info, NFInstContext.BINDING));
952952
binding_exp := flattenExp(binding_exp, prefix, Binding.getInfo(binding));
953953
elseif binding_var == Variability.PARAMETER and Component.isFinal(comp) then
954954
// Try to use inlining first.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// name: CevalRecord8
2+
// keywords:
3+
// status: incorrect
4+
//
5+
// Checks that the division by zero error is shown when in a record.
6+
//
7+
8+
record R
9+
parameter Real x[:];
10+
end R;
11+
12+
record R2
13+
parameter Real x;
14+
end R2;
15+
16+
function f
17+
input R r;
18+
output R2 r2;
19+
algorithm
20+
r2.x := if max(r.x) < 0 then 0 else 1;
21+
end f;
22+
23+
model CevalRecord8
24+
parameter R r(x = 1/0*{0, 1, 2});
25+
parameter R2 r2 = f(r) annotation(Evaluate=true);
26+
end CevalRecord8;
27+
28+
// Result:
29+
// Error processing file: CevalRecord8.mo
30+
// [flattening/modelica/scodeinst/CevalRecord8.mo:24:17-24:34:writable] Notification: From here:
31+
// [flattening/modelica/scodeinst/CevalRecord8.mo:20:3-20:40:writable] Error: Division by zero in 1.0 / 0.0
32+
//
33+
// # Error encountered! Exiting...
34+
// # Please check the error message and the flags.
35+
//
36+
// Execution failed!
37+
// endResult

testsuite/flattening/modelica/scodeinst/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ CevalRecord4.mo \
199199
CevalRecord5.mo \
200200
CevalRecord6.mo \
201201
CevalRecord7.mo \
202+
CevalRecord8.mo \
202203
CevalRecordArray1.mo \
203204
CevalRecordArray2.mo \
204205
CevalRecordArray3.mo \

testsuite/flattening/modelica/scodeinst/RecursiveConstants1.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ end RecursiveConstants1;
1010

1111
// Result:
1212
// Error processing file: RecursiveConstants1.mo
13+
// [flattening/modelica/scodeinst/RecursiveConstants1.mo:8:3-8:22:writable] Notification: From here:
14+
// [flattening/modelica/scodeinst/RecursiveConstants1.mo:7:3-7:22:writable] Notification: From here:
1315
// [flattening/modelica/scodeinst/RecursiveConstants1.mo:8:3-8:22:writable] Error: Variable 'y' has a cyclic dependency and has variability constant.
1416
//
1517
// # Error encountered! Exiting...

testsuite/flattening/modelica/scodeinst/loop1.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ end A;
1515

1616
// Result:
1717
// Error processing file: loop1.mo
18+
// [flattening/modelica/scodeinst/loop1.mo:11:3-11:25:writable] Notification: From here:
1819
// [flattening/modelica/scodeinst/loop1.mo:12:3-12:24:writable] Error: Dimension 1 of x, 'i', could not be evaluated due to a cyclic dependency.
1920
//
2021
// # Error encountered! Exiting...

0 commit comments

Comments
 (0)