Skip to content

Commit

Permalink
Don't fail on invalid modifiers in instance API (#12048)
Browse files Browse the repository at this point in the history
Fixes #12032
  • Loading branch information
perost committed Feb 29, 2024
1 parent a94493d commit a894ed4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
14 changes: 12 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1486,14 +1486,24 @@ algorithm
if InstNode.isProtected(node) and not (InstNode.isExtends(parent) or InstNode.isBaseClass(parent)) then
Error.addMultiSourceMessage(Error.NF_MODIFY_PROTECTED,
{InstNode.name(node), Modifier.toString(mod)}, {Modifier.info(mod), InstNode.info(node)});
fail();

if InstContext.inInstanceAPI(context) then
continue;
else
fail();
end if;
end if;

if InstNode.isOnlyOuter(node) then
Error.addSourceMessage(Error.OUTER_ELEMENT_MOD,
{Modifier.toString(mod, printName = false), Modifier.name(mod)},
Modifier.info(mod));
fail();

if InstContext.inInstanceAPI(context) then
continue;
else
fail();
end if;
end if;

if InstNode.isComponent(node) then
Expand Down
68 changes: 68 additions & 0 deletions testsuite/openmodelica/instance-API/GetModelInstanceMod5.mos
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// name: GetModelInstanceMod5
// keywords:
// status: correct
// cflags: -d=newInst
//
//

loadString("
model A
protected
Real x;
end A;

model M
A a(x = 1.0);
end M;
");

getModelInstance(M, prettyPrint = true);
getErrorString();

// Result:
// true
// "{
// \"name\": \"M\",
// \"restriction\": \"model\",
// \"elements\": [
// {
// \"$kind\": \"component\",
// \"name\": \"a\",
// \"type\": {
// \"name\": \"A\",
// \"restriction\": \"model\",
// \"elements\": [
// {
// \"$kind\": \"component\",
// \"name\": \"x\",
// \"type\": \"Real\",
// \"prefixes\": {
// \"public\": false
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 2,
// \"columnStart\": 3,
// \"lineEnd\": 5,
// \"columnEnd\": 8
// }
// },
// \"modifiers\": {
// \"x\": \"1.0\"
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 7,
// \"columnStart\": 3,
// \"lineEnd\": 9,
// \"columnEnd\": 8
// }
// }"
// "[<interactive>:8:9-8:16:writable] Notification: From here:
// [<interactive>:4:5-4:11:writable] Error: Protected element 'x' may not be modified, got 'x = 1.0'.
// "
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ GetModelInstanceMod1.mos \
GetModelInstanceMod2.mos \
GetModelInstanceMod3.mos \
GetModelInstanceMod4.mos \
GetModelInstanceMod5.mos \
GetModelInstanceReplaceable1.mos \
GetModelInstanceReplaceable2.mos \
GetModelInstanceReplaceable3.mos \
Expand Down

0 comments on commit a894ed4

Please sign in to comment.