Skip to content

Commit

Permalink
Improve type resolution of iterator parameter inside for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
pkourouklidis committed May 30, 2024
1 parent 50a28a8 commit 96701fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.eol
*.model
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public void testBuiltinMethods() throws Exception {
public void testMultiplePossibleTypes() throws Exception {
StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri='sa'};");
st.append("for (a in B.all) {\n"
+ " var v1 = a.foo();\n" //why no resolved type for this assignment statement?
st.append("for (a in A.all) {\n"
+ " var v1 = a.foo();\n"
+ " var v2 = v1.foo();\n"
+ "}\n"
+ "operation B foo() : B {return self;}\n"
Expand All @@ -276,7 +276,7 @@ public void testTypeResolutionWithAll() throws Exception {
StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri='sa'};");
st.append("for (a in B.all) {\n"
+ " /*B*/var v = a;\n" //why no resolved type for this assignment statement?
+ " var v = (/*B*/a);\n"
+ "}\n");
assertValid(st.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,15 @@ public void visit(ForStatement forStatement) {
context.getFrameStack().enterLocal(FrameType.UNPROTECTED, forStatement.getBodyStatementBlock(),
new Variable("loopCount", EolPrimitiveType.Integer), new Variable("hasMore", EolPrimitiveType.Boolean));

forStatement.getIteratorParameter().accept(this);
Parameter iteratorParameter = forStatement.getIteratorParameter();
iteratorParameter.accept(this);
if (getType(iteratorParameter) == EolAnyType.Instance) {
EolType iteratorType = getResolvedType(forStatement.getIteratedExpression());
if (iteratorType instanceof EolCollectionType) {
context.getFrameStack().get(iteratorParameter.getName())
.setType(((EolCollectionType) iteratorType).getContentType());
}
}
forStatement.getBodyStatementBlock().accept(this);
context.getFrameStack().leaveLocal(forStatement.getBodyStatementBlock());

Expand Down

0 comments on commit 96701fb

Please sign in to comment.