Skip to content

Commit 7f843da

Browse files
committed
Fix simplification of subscript expressions
- Avoid simplification to single element if the subscript expression is sliced and should result in an array. Fixes OpenModelica#14785
1 parent a0e536d commit 7f843da

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ algorithm
13551355
subscriptedExp := simplify(e);
13561356
subs := Subscript.simplifyList(subs, Type.arrayDims(Expression.typeOf(e)));
13571357

1358-
if not split and not List.all(subs, Subscript.isLiteral) then
1358+
if not split and not List.all(subs, Subscript.isLiteral) and Type.isScalar(ty) then
13591359
// Select the first element as long as the subscripted expression is an
13601360
// array where all elements are equal, unless all the subscripts are literal
13611361
// in which case it's cheaper to just apply them.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// name: CevalArrayConstructor3
2+
// keywords:
3+
// status: correct
4+
//
5+
//
6+
7+
model CevalArrayConstructor3
8+
parameter Real x[3] = {2, 2, 2};
9+
parameter Real y[:] = {sum(x[1:i]) for i in 1:2} annotation(Evaluate=true);
10+
end CevalArrayConstructor3;
11+
12+
// Result:
13+
// class CevalArrayConstructor3
14+
// final parameter Real x[1] = 2.0;
15+
// final parameter Real x[2] = 2.0;
16+
// final parameter Real x[3] = 2.0;
17+
// final parameter Real y[1] = 2.0;
18+
// final parameter Real y[2] = 4.0;
19+
// end CevalArrayConstructor3;
20+
// endResult

testsuite/flattening/modelica/scodeinst/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ CevalArrayConstant2.mo \
119119
CevalArrayConstant3.mo \
120120
CevalArrayConstructor1.mo \
121121
CevalArrayConstructor2.mo \
122+
CevalArrayConstructor3.mo \
122123
CevalAsin1.mo \
123124
CevalAtan1.mo \
124125
CevalAtan21.mo \

0 commit comments

Comments
 (0)