Skip to content

Commit

Permalink
GROOVY-11450: STC: nested conditional assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 4, 2024
1 parent af29aed commit e38d63a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4014,15 +4014,8 @@ public void visitIfElse(final IfStatement ifElse) {
restoreTypeBeforeConditional();

ifElse.getElseBlock().visit(this);

// GROOVY-9786: if chaining: "if (...) x=?; else if (...) x=?;"
Map<VariableExpression, ClassNode> updates =
ifElse.getElseBlock().getNodeMetaData("assignments");
if (updates != null) {
updates.forEach(this::recordAssignment);
}
} finally {
ifElse.putNodeMetaData("assignments", popAssignmentTracking(oldTracker));
popAssignmentTracking(oldTracker);
}

if (!typeCheckingContext.enclosingBlocks.isEmpty()) {
Expand Down Expand Up @@ -4201,6 +4194,10 @@ protected Map<VariableExpression, ClassNode> popAssignmentTracking(final Map<Var
});
});
typeCheckingContext.ifElseForWhileAssignmentTracker = oldTracker;
// GROOVY-9786, GROOVY-11450: nested conditional assignments
if (oldTracker != null) {
assignments.forEach(this::recordAssignment);
}
return assignments;
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/groovy/transform/stc/STCAssignmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,27 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
}
}

// GROOVY-11450
void testIfElseIfInNestedBlock() {
shouldFailWithMessages '''
class C {
def m() { }
}
def x
x = new C()
if (false) {
x = new C()
} else {
if (true) {
x = 1
}
}
x.m() // x should be LUB(C,int)
''',
'Cannot find matching method java.lang.Object#m()'
}

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

0 comments on commit e38d63a

Please sign in to comment.