Skip to content

Commit

Permalink
GROOVY-11457: STC: try/catch/finally conditional assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 4, 2024
1 parent 35478b7 commit 6e6f304
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4532,18 +4532,25 @@ private static boolean isEmptyMap(final Expression expr) {

@Override
public void visitTryCatchFinally(final TryCatchStatement statement) {
List<CatchStatement> catchStatements = statement.getCatchStatements();
for (CatchStatement catchStatement : catchStatements) {
ClassNode exceptionType = catchStatement.getExceptionType();
typeCheckingContext.controlStructureVariables.put(catchStatement.getVariable(), exceptionType);
}
Map<Parameter, ClassNode> vars = typeCheckingContext.controlStructureVariables;
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
try {
super.visitTryCatchFinally(statement);
} finally {
for (CatchStatement catchStatement : catchStatements) {
typeCheckingContext.controlStructureVariables.remove(catchStatement.getVariable());
visitStatement(statement);
statement.getResourceStatements().forEach(rsrc -> rsrc.visit(this));
statement.getTryStatement().visit(this);
for (CatchStatement catchStatement : statement.getCatchStatements()) {
vars.put(catchStatement.getVariable(), catchStatement.getExceptionType());
try {
restoreTypeBeforeConditional();
catchStatement.visit(this);
} finally {
vars.remove(catchStatement.getVariable());
}
}
} finally {
popAssignmentTracking(oldTracker);
}
statement.getFinallyStatement().visit(this);
}

protected void storeType(final Expression exp, ClassNode cn) {
Expand Down
31 changes: 30 additions & 1 deletion src/test/groovy/transform/stc/STCAssignmentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'Cannot find matching method java.io.Serializable#toInteger()'
}

void testTernaryInitWithAssignment() {
void testTernaryWithNestedAssignment() {
shouldFailWithMessages '''
def x = '123'
def y = (false ? (x = new HashSet()) : 42)
Expand All @@ -611,6 +611,35 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
'Cannot find matching method java.io.Serializable#toInteger()'
}

// GROOVY-11457
void testTryCatchFinallyWithAssignment() {
assertScript '''
def x = (Appendable) null
if (true) {
x = 1
}
try {
;
} finally {
x = (Appendable) null
}
Appendable y = x
'''
shouldFailWithMessages '''
def x = (Appendable) null
if (true) {
x = 1
}
try {
;
} catch (e) {
x = (Appendable) null
}
Appendable y = x // Cannot cast object '1' with class 'Integer' to class 'Appendable'
''',
'Cannot assign value of type java.lang.Object to variable of type java.lang.Appendable'
}

void testFloatSub() {
assertScript '''
float x = 1.0f
Expand Down

0 comments on commit 6e6f304

Please sign in to comment.