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 77276ef commit 35478b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4178,13 +4178,8 @@ public void visitIfElse(final IfStatement ifElse) {
if (elsePath.isEmpty() && !GeneralUtils.maybeFallsThrough(thenPath)) {
tti.forEach(this::putNotInstanceOfTypeInfo);
}
// GROOVY-9786: if chaining: "if (...) x=?; else if (...) x=?;"
Map<VariableExpression, ClassNode> updates = elsePath.getNodeMetaData("assignments");
if (updates != null) {
updates.forEach(this::recordAssignment);
}
} finally {
ifElse.putNodeMetaData("assignments", popAssignmentTracking(oldTracker));
popAssignmentTracking(oldTracker);
}
}

Expand Down Expand Up @@ -4288,6 +4283,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 @@ -559,6 +559,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 35478b7

Please sign in to comment.