Skip to content

Commit e9af4d2

Browse files
committed
Handling of try / catch / finally with 'Nothing' in all catch blocks #KT-10735 Fixed
1 parent 60f5d9e commit e9af4d2

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,11 @@ public KotlinTypeInfo visitTryExpression(@NotNull KtTryExpression expression, Ex
470470
List<KtCatchClause> catchClauses = expression.getCatchClauses();
471471
KtFinallySection finallyBlock = expression.getFinallyBlock();
472472
List<KotlinType> types = new ArrayList<KotlinType>();
473+
boolean nothingInAllCatchBranches = true;
473474
for (KtCatchClause catchClause : catchClauses) {
474475
KtParameter catchParameter = catchClause.getCatchParameter();
475476
KtExpression catchBody = catchClause.getCatchBody();
477+
boolean nothingInCatchBranch = false;
476478
if (catchParameter != null) {
477479
components.identifierChecker.checkDeclaration(catchParameter, context.trace);
478480
ModifiersChecker.ModifiersCheckingProcedure modifiersChecking = components.modifiersChecker.withTrace(context.trace);
@@ -494,18 +496,28 @@ public KotlinTypeInfo visitTryExpression(@NotNull KtTryExpression expression, Ex
494496
KotlinType type = facade.getTypeInfo(catchBody, context.replaceScope(catchScope)).getType();
495497
if (type != null) {
496498
types.add(type);
499+
if (KotlinBuiltIns.isNothing(type)) {
500+
nothingInCatchBranch = true;
501+
}
497502
}
498503
}
499504
}
505+
if (!nothingInCatchBranch) {
506+
nothingInAllCatchBranches = false;
507+
}
500508
}
501509

502510
KotlinTypeInfo result = TypeInfoFactoryKt.noTypeInfo(context);
511+
KotlinTypeInfo tryResult = facade.getTypeInfo(tryBlock, context);
503512
if (finallyBlock != null) {
504513
result = facade.getTypeInfo(finallyBlock.getFinalExpression(),
505514
context.replaceExpectedType(NO_EXPECTED_TYPE));
506515
}
516+
else if (nothingInAllCatchBranches) {
517+
result = tryResult;
518+
}
507519

508-
KotlinType type = facade.getTypeInfo(tryBlock, context).getType();
520+
KotlinType type = tryResult.getType();
509521
if (type != null) {
510522
types.add(type);
511523
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// See also KT-10735
2+
fun test() {
3+
var a: Int?
4+
try {
5+
a = 3
6+
}
7+
catch (e: Exception) {
8+
return
9+
}
10+
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
11+
}
12+
class A: Exception()
13+
class B: Exception()
14+
fun test2() {
15+
var a: Int?
16+
try {
17+
a = 4
18+
}
19+
catch (e: A) {
20+
return
21+
}
22+
catch (e: B) {
23+
return
24+
}
25+
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
26+
}
27+
fun test3() {
28+
var a: Int? = null
29+
try {
30+
a = 5
31+
}
32+
catch (e: A) {
33+
// do nothing
34+
}
35+
catch (e: B) {
36+
return
37+
}
38+
a<!UNSAFE_CALL!>.<!>hashCode() // a is nullable here
39+
}
40+
fun test4() {
41+
var a: Int? = null
42+
try {
43+
// do nothing
44+
}
45+
catch (e: A) {
46+
return
47+
}
48+
catch (e: B) {
49+
return
50+
}
51+
a<!UNSAFE_CALL!>.<!>hashCode() // a is nullable here
52+
}
53+
fun test5() {
54+
var a: Int?// = null
55+
try {
56+
<!UNUSED_VALUE!>a =<!> 3
57+
}
58+
catch (e: Exception) {
59+
return
60+
}
61+
finally {
62+
// Error: KT-9825
63+
<!UNUSED_VALUE!>a =<!> 5
64+
}
65+
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
66+
}
67+
fun test6() {
68+
var a: Int?// = null
69+
try {
70+
<!UNUSED_VALUE!>a =<!> 3
71+
}
72+
catch (e: Exception) {
73+
return
74+
}
75+
finally {
76+
// Error: KT-9825
77+
<!UNUSED_VALUE!>a =<!> null
78+
}
79+
<!DEBUG_INFO_CONSTANT!>a<!><!UNSAFE_CALL!>.<!>hashCode() // a is null here
80+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package
2+
3+
public fun test(): kotlin.Unit
4+
public fun test2(): kotlin.Unit
5+
public fun test3(): kotlin.Unit
6+
public fun test4(): kotlin.Unit
7+
public fun test5(): kotlin.Unit
8+
public fun test6(): kotlin.Unit
9+
10+
public final class A : java.lang.Exception {
11+
public constructor A()
12+
public final override /*1*/ /*fake_override*/ val cause: kotlin.Throwable?
13+
public final override /*1*/ /*fake_override*/ val message: kotlin.String?
14+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
15+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
16+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
17+
}
18+
19+
public final class B : java.lang.Exception {
20+
public constructor B()
21+
public final override /*1*/ /*fake_override*/ val cause: kotlin.Throwable?
22+
public final override /*1*/ /*fake_override*/ val message: kotlin.String?
23+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
24+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
25+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
26+
}

compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17066,6 +17066,12 @@ public void testInfix() throws Exception {
1706617066
doTest(fileName);
1706717067
}
1706817068

17069+
@TestMetadata("initInTryReturnInCatch.kt")
17070+
public void testInitInTryReturnInCatch() throws Exception {
17071+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varnotnull/initInTryReturnInCatch.kt");
17072+
doTest(fileName);
17073+
}
17074+
1706917075
@TestMetadata("initialization.kt")
1707017076
public void testInitialization() throws Exception {
1707117077
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varnotnull/initialization.kt");

0 commit comments

Comments
 (0)