Skip to content

Commit

Permalink
GROOVY-9854
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 8, 2022
1 parent 5d64fb8 commit 052c350
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,25 @@ public void testTypeChecked9844() {
runConformTest(sources, "[key:val][key:val]");
}

@Test
public void testTypeChecked9854() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" switch (42) {\n" +
" case { it > 1 }:\n" +
" print 'works'\n" +
" }\n" +
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources, "works");
}

@Test(expected = AssertionError.class)
public void testTypeChecked9873() {
Map<String, String> options = getCompilerOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4719,7 +4719,20 @@ public BinaryExpression findInstanceOfNotReturnExpression(IfStatement ifElse) {
public void visitSwitch(final SwitchStatement statement) {
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
try {
/* GRECLIPSE edit -- GROOVY-9854
super.visitSwitch(statement);
*/
visitStatement(statement);
statement.getExpression().visit(this);
ClassNode type = getType(statement.getExpression());
for (CaseStatement caseStatement : statement.getCaseStatements()) {
if (caseStatement.getExpression() instanceof ClosureExpression) // propagate the type
caseStatement.getExpression().putNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS, new ClassNode[]{type});

caseStatement.visit(this);
}
statement.getDefaultStatement().visit(this);
// GRECLIPSE end
} finally {
popAssignmentTracking(oldTracker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4495,6 +4495,12 @@ protected void afterSwitchConditionExpressionVisited(final SwitchStatement state

@Override
public void visitCaseStatement(final CaseStatement statement) {
Expression expression = statement.getExpression();
if (expression instanceof ClosureExpression) { // GROOVY-9854: propagate the switch type
SwitchStatement switchStatement = typeCheckingContext.getEnclosingSwitchStatement();
ClassNode inf = switchStatement.getExpression().getNodeMetaData(TYPE);
expression.putNodeMetaData(CLOSURE_ARGUMENTS, new ClassNode[]{inf});
}
super.visitCaseStatement(statement);
restoreTypeBeforeConditional();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4122,6 +4122,12 @@ protected void afterSwitchConditionExpressionVisited(final SwitchStatement state

@Override
public void visitCaseStatement(final CaseStatement statement) {
Expression expression = statement.getExpression();
if (expression instanceof ClosureExpression) { // GROOVY-9854: propagate the switch type
SwitchStatement switchStatement = typeCheckingContext.getEnclosingSwitchStatement();
ClassNode inf = switchStatement.getExpression().getNodeMetaData(TYPE);
expression.putNodeMetaData(CLOSURE_ARGUMENTS, new ClassNode[]{inf});
}
super.visitCaseStatement(statement);
restoreTypeBeforeConditional();
}
Expand Down

0 comments on commit 052c350

Please sign in to comment.