Frontend part of KT-910 Type of try/catch/finally
This commit is contained in:
+4
-7
@@ -411,7 +411,8 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
JetParameter catchParameter = catchClause.getCatchParameter();
|
JetParameter catchParameter = catchClause.getCatchParameter();
|
||||||
JetExpression catchBody = catchClause.getCatchBody();
|
JetExpression catchBody = catchClause.getCatchBody();
|
||||||
if (catchParameter != null) {
|
if (catchParameter != null) {
|
||||||
VariableDescriptor variableDescriptor = context.expressionTypingServices.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, catchParameter, context.trace);
|
VariableDescriptor variableDescriptor = context.expressionTypingServices.getDescriptorResolver().resolveLocalVariableDescriptor(
|
||||||
|
context.scope.getContainingDeclaration(), context.scope, catchParameter, context.trace);
|
||||||
JetType throwableType = JetStandardLibrary.getInstance().getThrowable().getDefaultType();
|
JetType throwableType = JetStandardLibrary.getInstance().getThrowable().getDefaultType();
|
||||||
DataFlowUtils.checkType(variableDescriptor.getType(), catchParameter, context.replaceExpectedType(throwableType));
|
DataFlowUtils.checkType(variableDescriptor.getType(), catchParameter, context.replaceExpectedType(throwableType));
|
||||||
if (catchBody != null) {
|
if (catchBody != null) {
|
||||||
@@ -425,13 +426,9 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (finallyBlock != null) {
|
if (finallyBlock != null) {
|
||||||
types.clear(); // Do not need the list for the check, but need the code above to typecheck catch bodies
|
facade.getType(finallyBlock.getFinalExpression(), context.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE));
|
||||||
JetType type = facade.getType(finallyBlock.getFinalExpression(), context.replaceScope(context.scope));
|
|
||||||
if (type != null) {
|
|
||||||
types.add(type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
JetType type = facade.getType(tryBlock, context.replaceScope(context.scope));
|
JetType type = facade.getType(tryBlock, context);
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
types.add(type);
|
types.add(type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package a
|
||||||
|
|
||||||
|
fun foo() : Int {
|
||||||
|
try {
|
||||||
|
doSmth()
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
<!UNREACHABLE_CODE!>return <!TYPE_MISMATCH!>""<!><!>
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
return <!TYPE_MISMATCH!>""<!>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar() : Int =
|
||||||
|
try {
|
||||||
|
<!TYPE_MISMATCH!>doSmth()<!>
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
<!TYPE_MISMATCH!>""<!>
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
<!UNUSED_EXPRESSION!>""<!>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun doSmth() {}
|
||||||
@@ -158,7 +158,8 @@ public class JetTypeCheckerTest extends JetLiteFixture {
|
|||||||
|
|
||||||
public void testTry() throws Exception {
|
public void testTry() throws Exception {
|
||||||
assertType("try {1} finally{2}", "Int");
|
assertType("try {1} finally{2}", "Int");
|
||||||
assertType("try {1} catch (e : Exception) {'a'} finally{2}", "Int");
|
assertType("try {1} catch (e : Exception) {'a'} finally{2}", "Any");
|
||||||
|
assertType("try {1} catch (e : Exception) {2} finally{'a'}", "Int");
|
||||||
assertType("try {1} catch (e : Exception) {'a'} finally{'2'}", "Any");
|
assertType("try {1} catch (e : Exception) {'a'} finally{'2'}", "Any");
|
||||||
assertType("try {1} catch (e : Exception) {'a'}", "Any");
|
assertType("try {1} catch (e : Exception) {'a'}", "Any");
|
||||||
assertType("try {1} catch (e : Exception) {'a'} catch (e : Exception) {null}", "Any?");
|
assertType("try {1} catch (e : Exception) {'a'} catch (e : Exception) {null}", "Any?");
|
||||||
|
|||||||
Reference in New Issue
Block a user