Skip to content

Commit 83634b0

Browse files
authored
Improve checking of protected modifications (#8556)
- Add the source location of the modifier to the error message, to make it easier to see why the error occured. - Allow modification of protected elements in base classes even in cases where it shouldn't be allowed, since we can't correctly detect those cases yet.
1 parent b1522d8 commit 83634b0

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

OMCompiler/Compiler/NFFrontEnd/NFInst.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,9 @@ algorithm
12771277
for node_ptr in node_ptrs loop
12781278
node := InstNode.resolveOuter(Mutable.access(node_ptr));
12791279

1280-
if InstNode.isProtected(node) and not InstNode.isExtends(parent) then
1281-
Error.addSourceMessage(Error.NF_MODIFY_PROTECTED,
1282-
{InstNode.name(node), Modifier.toString(mod)}, InstNode.info(node));
1280+
if InstNode.isProtected(node) and not (InstNode.isExtends(parent) or InstNode.isBaseClass(parent)) then
1281+
Error.addMultiSourceMessage(Error.NF_MODIFY_PROTECTED,
1282+
{InstNode.name(node), Modifier.toString(mod)}, {Modifier.info(mod), InstNode.info(node)});
12831283
fail();
12841284
end if;
12851285

testsuite/flattening/modelica/scodeinst/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,10 @@ PropagateExtends.mo \
873873
ProtectedMod1.mo \
874874
ProtectedMod2.mo \
875875
ProtectedMod3.mo \
876-
ProtectedMod4.mo \
877876
ProtectedMod5.mo \
878877
ProtectedMod6.mo \
879878
ProtectedMod7.mo \
879+
ProtectedMod8.mo \
880880
RangeInvalidStep1.mo \
881881
RangeInvalidStep2.mo \
882882
RangeInvalidStep3.mo \
@@ -1093,6 +1093,7 @@ sts.mos \
10931093
# test that currently fail. Move up when fixed.
10941094
# Run make testfailing
10951095
FAILINGTESTFILES=\
1096+
ProtectedMod4.mo \
10961097
CevalFuncRecord3.mo \
10971098
CevalFuncRecord4.mo \
10981099
FinalParameter1.mo \

testsuite/flattening/modelica/scodeinst/ProtectedMod1.mo

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

1616
// Result:
1717
// Error processing file: ProtectedMod1.mo
18+
// [flattening/modelica/scodeinst/ProtectedMod1.mo:13:7-13:14:writable] Notification: From here:
1819
// [flattening/modelica/scodeinst/ProtectedMod1.mo:9:13-9:25:writable] Error: Protected element ‘x‘ may not be modified, got ‘x = 2.0‘.
1920
//
2021
// # Error encountered! Exiting...

testsuite/flattening/modelica/scodeinst/ProtectedMod3.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ end ProtectedMod3;
1919

2020
// Result:
2121
// Error processing file: ProtectedMod3.mo
22+
// [flattening/modelica/scodeinst/ProtectedMod3.mo:17:15-17:22:writable] Notification: From here:
2223
// [flattening/modelica/scodeinst/ProtectedMod3.mo:9:13-9:25:writable] Error: Protected element ‘x‘ may not be modified, got ‘x = 2.0‘.
2324
//
2425
// # Error encountered! Exiting...

testsuite/flattening/modelica/scodeinst/ProtectedMod6.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ end ProtectedMod6;
2020

2121
// Result:
2222
// Error processing file: ProtectedMod6.mo
23+
// [flattening/modelica/scodeinst/ProtectedMod6.mo:18:7-18:14:writable] Notification: From here:
2324
// [flattening/modelica/scodeinst/ProtectedMod6.mo:9:3-9:15:writable] Error: Protected element ‘x‘ may not be modified, got ‘x = 2.0‘.
2425
//
2526
// # Error encountered! Exiting...
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// name: ProtectedMod8
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
model A
9+
protected
10+
Real x = 1.0;
11+
end A;
12+
13+
model B = A(x = 2.0);
14+
15+
model ProtectedMod8
16+
B b;
17+
end ProtectedMod8;
18+
19+
// Result:
20+
// class ProtectedMod8
21+
// protected Real b.x = 2.0;
22+
// end ProtectedMod8;
23+
// endResult

0 commit comments

Comments
 (0)