Skip to content

Commit 052c350

Browse files
committed
GROOVY-9854
1 parent 5d64fb8 commit 052c350

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,25 @@ public void testTypeChecked9844() {
26712671
runConformTest(sources, "[key:val][key:val]");
26722672
}
26732673

2674+
@Test
2675+
public void testTypeChecked9854() {
2676+
//@formatter:off
2677+
String[] sources = {
2678+
"Main.groovy",
2679+
"@groovy.transform.TypeChecked\n" +
2680+
"void test() {\n" +
2681+
" switch (42) {\n" +
2682+
" case { it > 1 }:\n" +
2683+
" print 'works'\n" +
2684+
" }\n" +
2685+
"}\n" +
2686+
"test()\n",
2687+
};
2688+
//@formatter:on
2689+
2690+
runConformTest(sources, "works");
2691+
}
2692+
26742693
@Test(expected = AssertionError.class)
26752694
public void testTypeChecked9873() {
26762695
Map<String, String> options = getCompilerOptions();

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+13
Original file line numberDiff line numberDiff line change
@@ -4719,7 +4719,20 @@ public BinaryExpression findInstanceOfNotReturnExpression(IfStatement ifElse) {
47194719
public void visitSwitch(final SwitchStatement statement) {
47204720
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
47214721
try {
4722+
/* GRECLIPSE edit -- GROOVY-9854
47224723
super.visitSwitch(statement);
4724+
*/
4725+
visitStatement(statement);
4726+
statement.getExpression().visit(this);
4727+
ClassNode type = getType(statement.getExpression());
4728+
for (CaseStatement caseStatement : statement.getCaseStatements()) {
4729+
if (caseStatement.getExpression() instanceof ClosureExpression) // propagate the type
4730+
caseStatement.getExpression().putNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS, new ClassNode[]{type});
4731+
4732+
caseStatement.visit(this);
4733+
}
4734+
statement.getDefaultStatement().visit(this);
4735+
// GRECLIPSE end
47234736
} finally {
47244737
popAssignmentTracking(oldTracker);
47254738
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+6
Original file line numberDiff line numberDiff line change
@@ -4495,6 +4495,12 @@ protected void afterSwitchConditionExpressionVisited(final SwitchStatement state
44954495

44964496
@Override
44974497
public void visitCaseStatement(final CaseStatement statement) {
4498+
Expression expression = statement.getExpression();
4499+
if (expression instanceof ClosureExpression) { // GROOVY-9854: propagate the switch type
4500+
SwitchStatement switchStatement = typeCheckingContext.getEnclosingSwitchStatement();
4501+
ClassNode inf = switchStatement.getExpression().getNodeMetaData(TYPE);
4502+
expression.putNodeMetaData(CLOSURE_ARGUMENTS, new ClassNode[]{inf});
4503+
}
44984504
super.visitCaseStatement(statement);
44994505
restoreTypeBeforeConditional();
45004506
}

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+6
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,12 @@ protected void afterSwitchConditionExpressionVisited(final SwitchStatement state
41224122

41234123
@Override
41244124
public void visitCaseStatement(final CaseStatement statement) {
4125+
Expression expression = statement.getExpression();
4126+
if (expression instanceof ClosureExpression) { // GROOVY-9854: propagate the switch type
4127+
SwitchStatement switchStatement = typeCheckingContext.getEnclosingSwitchStatement();
4128+
ClassNode inf = switchStatement.getExpression().getNodeMetaData(TYPE);
4129+
expression.putNodeMetaData(CLOSURE_ARGUMENTS, new ClassNode[]{inf});
4130+
}
41254131
super.visitCaseStatement(statement);
41264132
restoreTypeBeforeConditional();
41274133
}

0 commit comments

Comments
 (0)