Skip to content

Commit e38d63a

Browse files
committed
GROOVY-11450: STC: nested conditional assignments
1 parent af29aed commit e38d63a

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -4014,15 +4014,8 @@ public void visitIfElse(final IfStatement ifElse) {
40144014
restoreTypeBeforeConditional();
40154015

40164016
ifElse.getElseBlock().visit(this);
4017-
4018-
// GROOVY-9786: if chaining: "if (...) x=?; else if (...) x=?;"
4019-
Map<VariableExpression, ClassNode> updates =
4020-
ifElse.getElseBlock().getNodeMetaData("assignments");
4021-
if (updates != null) {
4022-
updates.forEach(this::recordAssignment);
4023-
}
40244017
} finally {
4025-
ifElse.putNodeMetaData("assignments", popAssignmentTracking(oldTracker));
4018+
popAssignmentTracking(oldTracker);
40264019
}
40274020

40284021
if (!typeCheckingContext.enclosingBlocks.isEmpty()) {
@@ -4201,6 +4194,10 @@ protected Map<VariableExpression, ClassNode> popAssignmentTracking(final Map<Var
42014194
});
42024195
});
42034196
typeCheckingContext.ifElseForWhileAssignmentTracker = oldTracker;
4197+
// GROOVY-9786, GROOVY-11450: nested conditional assignments
4198+
if (oldTracker != null) {
4199+
assignments.forEach(this::recordAssignment);
4200+
}
42044201
return assignments;
42054202
}
42064203

src/test/groovy/transform/stc/STCAssignmentTest.groovy

+21
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,27 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
658658
}
659659
}
660660

661+
// GROOVY-11450
662+
void testIfElseIfInNestedBlock() {
663+
shouldFailWithMessages '''
664+
class C {
665+
def m() { }
666+
}
667+
668+
def x
669+
x = new C()
670+
if (false) {
671+
x = new C()
672+
} else {
673+
if (true) {
674+
x = 1
675+
}
676+
}
677+
x.m() // x should be LUB(C,int)
678+
''',
679+
'Cannot find matching method java.lang.Object#m()'
680+
}
681+
661682
void testForLoopWithAssignment() {
662683
shouldFailWithMessages '''
663684
def x = '123'

0 commit comments

Comments
 (0)