Handling of try / catch / finally with 'Nothing' in all catch blocks #KT-10735 Fixed
This commit is contained in:
+13
-1
@@ -470,9 +470,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
List<KtCatchClause> catchClauses = expression.getCatchClauses();
|
List<KtCatchClause> catchClauses = expression.getCatchClauses();
|
||||||
KtFinallySection finallyBlock = expression.getFinallyBlock();
|
KtFinallySection finallyBlock = expression.getFinallyBlock();
|
||||||
List<KotlinType> types = new ArrayList<KotlinType>();
|
List<KotlinType> types = new ArrayList<KotlinType>();
|
||||||
|
boolean nothingInAllCatchBranches = true;
|
||||||
for (KtCatchClause catchClause : catchClauses) {
|
for (KtCatchClause catchClause : catchClauses) {
|
||||||
KtParameter catchParameter = catchClause.getCatchParameter();
|
KtParameter catchParameter = catchClause.getCatchParameter();
|
||||||
KtExpression catchBody = catchClause.getCatchBody();
|
KtExpression catchBody = catchClause.getCatchBody();
|
||||||
|
boolean nothingInCatchBranch = false;
|
||||||
if (catchParameter != null) {
|
if (catchParameter != null) {
|
||||||
components.identifierChecker.checkDeclaration(catchParameter, context.trace);
|
components.identifierChecker.checkDeclaration(catchParameter, context.trace);
|
||||||
ModifiersChecker.ModifiersCheckingProcedure modifiersChecking = components.modifiersChecker.withTrace(context.trace);
|
ModifiersChecker.ModifiersCheckingProcedure modifiersChecking = components.modifiersChecker.withTrace(context.trace);
|
||||||
@@ -494,18 +496,28 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
KotlinType type = facade.getTypeInfo(catchBody, context.replaceScope(catchScope)).getType();
|
KotlinType type = facade.getTypeInfo(catchBody, context.replaceScope(catchScope)).getType();
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
types.add(type);
|
types.add(type);
|
||||||
|
if (KotlinBuiltIns.isNothing(type)) {
|
||||||
|
nothingInCatchBranch = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!nothingInCatchBranch) {
|
||||||
|
nothingInAllCatchBranches = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
KotlinTypeInfo result = TypeInfoFactoryKt.noTypeInfo(context);
|
KotlinTypeInfo result = TypeInfoFactoryKt.noTypeInfo(context);
|
||||||
|
KotlinTypeInfo tryResult = facade.getTypeInfo(tryBlock, context);
|
||||||
if (finallyBlock != null) {
|
if (finallyBlock != null) {
|
||||||
result = facade.getTypeInfo(finallyBlock.getFinalExpression(),
|
result = facade.getTypeInfo(finallyBlock.getFinalExpression(),
|
||||||
context.replaceExpectedType(NO_EXPECTED_TYPE));
|
context.replaceExpectedType(NO_EXPECTED_TYPE));
|
||||||
}
|
}
|
||||||
|
else if (nothingInAllCatchBranches) {
|
||||||
|
result = tryResult;
|
||||||
|
}
|
||||||
|
|
||||||
KotlinType type = facade.getTypeInfo(tryBlock, context).getType();
|
KotlinType type = tryResult.getType();
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
types.add(type);
|
types.add(type);
|
||||||
}
|
}
|
||||||
|
|||||||
+80
@@ -0,0 +1,80 @@
|
|||||||
|
// See also KT-10735
|
||||||
|
fun test() {
|
||||||
|
var a: Int?
|
||||||
|
try {
|
||||||
|
a = 3
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
|
||||||
|
}
|
||||||
|
class A: Exception()
|
||||||
|
class B: Exception()
|
||||||
|
fun test2() {
|
||||||
|
var a: Int?
|
||||||
|
try {
|
||||||
|
a = 4
|
||||||
|
}
|
||||||
|
catch (e: A) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
catch (e: B) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
|
||||||
|
}
|
||||||
|
fun test3() {
|
||||||
|
var a: Int? = null
|
||||||
|
try {
|
||||||
|
a = 5
|
||||||
|
}
|
||||||
|
catch (e: A) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
catch (e: B) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
a<!UNSAFE_CALL!>.<!>hashCode() // a is nullable here
|
||||||
|
}
|
||||||
|
fun test4() {
|
||||||
|
var a: Int? = null
|
||||||
|
try {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
catch (e: A) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
catch (e: B) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
a<!UNSAFE_CALL!>.<!>hashCode() // a is nullable here
|
||||||
|
}
|
||||||
|
fun test5() {
|
||||||
|
var a: Int?// = null
|
||||||
|
try {
|
||||||
|
<!UNUSED_VALUE!>a =<!> 3
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Error: KT-9825
|
||||||
|
<!UNUSED_VALUE!>a =<!> 5
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>a<!>.hashCode() // a is never null here
|
||||||
|
}
|
||||||
|
fun test6() {
|
||||||
|
var a: Int?// = null
|
||||||
|
try {
|
||||||
|
<!UNUSED_VALUE!>a =<!> 3
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Error: KT-9825
|
||||||
|
<!UNUSED_VALUE!>a =<!> null
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_CONSTANT!>a<!><!UNSAFE_CALL!>.<!>hashCode() // a is null here
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun test(): kotlin.Unit
|
||||||
|
public fun test2(): kotlin.Unit
|
||||||
|
public fun test3(): kotlin.Unit
|
||||||
|
public fun test4(): kotlin.Unit
|
||||||
|
public fun test5(): kotlin.Unit
|
||||||
|
public fun test6(): kotlin.Unit
|
||||||
|
|
||||||
|
public final class A : java.lang.Exception {
|
||||||
|
public constructor A()
|
||||||
|
public final override /*1*/ /*fake_override*/ val cause: kotlin.Throwable?
|
||||||
|
public final override /*1*/ /*fake_override*/ val message: kotlin.String?
|
||||||
|
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||||
|
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class B : java.lang.Exception {
|
||||||
|
public constructor B()
|
||||||
|
public final override /*1*/ /*fake_override*/ val cause: kotlin.Throwable?
|
||||||
|
public final override /*1*/ /*fake_override*/ val message: kotlin.String?
|
||||||
|
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||||
|
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
@@ -17066,6 +17066,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("initInTryReturnInCatch.kt")
|
||||||
|
public void testInitInTryReturnInCatch() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varnotnull/initInTryReturnInCatch.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("initialization.kt")
|
@TestMetadata("initialization.kt")
|
||||||
public void testInitialization() throws Exception {
|
public void testInitialization() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varnotnull/initialization.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varnotnull/initialization.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user