Skip to content

Commit

Permalink
GROOVY-11474: STC: re-check types for vars assigned within while loops
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 10, 2024
1 parent 35ee676 commit dc30537
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2192,9 +2192,15 @@ protected boolean isSecondPassNeededForControlStructure(final Map<VariableExpres

@Override
public void visitWhileLoop(final WhileStatement loop) {
Map<VariableExpression, ClassNode> startTypes = new HashMap<>();
loop.getLoopBlock().visit(new VariableExpressionTypeMemoizer(startTypes));
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();

super.visitWhileLoop(loop);
popAssignmentTracking(oldTracker);

if (isSecondPassNeededForControlStructure(startTypes, oldTracker)) { // GROOVY-11474
visitWhileLoop(loop);
}
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/test/groovy/transform/stc/STCAssignmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,19 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'Cannot find matching method java.io.Serializable#toInteger()'
}

// GROOVY-11474
void testWhileLoopWithAssignment2() {
shouldFailWithMessages '''
def i = 0
def x = 1
while (i++ < 1) {
Integer y = x
x = " "
}
''',
'Cannot assign value of type (java.io.Serializable & java.lang.Comparable',') to variable of type java.lang.Integer'
}

void testTernaryWithNestedAssignment() {
shouldFailWithMessages '''
def x = '123'
Expand Down

0 comments on commit dc30537

Please sign in to comment.