KT-5200 Mark unreachable code in lambdas
#KT-5200 Fixed
This commit is contained in:
@@ -125,9 +125,12 @@ public class JetFlowInformationProvider {
|
|||||||
|
|
||||||
public void checkFunction(@Nullable JetType expectedReturnType) {
|
public void checkFunction(@Nullable JetType expectedReturnType) {
|
||||||
UnreachableCode unreachableCode = collectUnreachableCode();
|
UnreachableCode unreachableCode = collectUnreachableCode();
|
||||||
checkDefiniteReturn(expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, unreachableCode);
|
|
||||||
reportUnreachableCode(unreachableCode);
|
reportUnreachableCode(unreachableCode);
|
||||||
|
|
||||||
|
if (subroutine instanceof JetFunctionLiteral) return;
|
||||||
|
|
||||||
|
checkDefiniteReturn(expectedReturnType != null ? expectedReturnType : NO_EXPECTED_TYPE, unreachableCode);
|
||||||
|
|
||||||
markTailCalls();
|
markTailCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +199,7 @@ public class JetFlowInformationProvider {
|
|||||||
JetElement element = localDeclarationInstruction.getElement();
|
JetElement element = localDeclarationInstruction.getElement();
|
||||||
if (element instanceof JetDeclarationWithBody) {
|
if (element instanceof JetDeclarationWithBody) {
|
||||||
JetDeclarationWithBody localDeclaration = (JetDeclarationWithBody) element;
|
JetDeclarationWithBody localDeclaration = (JetDeclarationWithBody) element;
|
||||||
if (localDeclaration instanceof JetFunctionLiteral) continue;
|
|
||||||
CallableDescriptor functionDescriptor =
|
CallableDescriptor functionDescriptor =
|
||||||
(CallableDescriptor) trace.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, localDeclaration);
|
(CallableDescriptor) trace.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, localDeclaration);
|
||||||
JetType expectedType = functionDescriptor != null ? functionDescriptor.getReturnType() : null;
|
JetType expectedType = functionDescriptor != null ? functionDescriptor.getReturnType() : null;
|
||||||
|
|||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
//KT-5200 Mark unreachable code in lambdas
|
||||||
|
|
||||||
|
fun test1(): String {
|
||||||
|
doCall @local {
|
||||||
|
() : String ->
|
||||||
|
throw NullPointerException()
|
||||||
|
<!UNREACHABLE_CODE!>"b3"<!> //unmarked
|
||||||
|
}
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test2(nonLocal: String, b: Boolean): String {
|
||||||
|
doCall @local {
|
||||||
|
() : String ->
|
||||||
|
if (b) {
|
||||||
|
return@local "b1"
|
||||||
|
} else {
|
||||||
|
return@local "b2"
|
||||||
|
}
|
||||||
|
<!UNREACHABLE_CODE!>"b3"<!> //unmarked
|
||||||
|
}
|
||||||
|
|
||||||
|
return nonLocal
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun doCall(block: ()-> String) = block()
|
||||||
@@ -37,7 +37,7 @@ fun t3() : String {
|
|||||||
else {
|
else {
|
||||||
<!RETURN_NOT_ALLOWED!>return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2<!><!>
|
<!RETURN_NOT_ALLOWED!>return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2<!><!>
|
||||||
}
|
}
|
||||||
<!RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED!>return@l 0<!>
|
<!RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED, UNREACHABLE_CODE!>return@l 0<!>
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
invoker(
|
invoker(
|
||||||
|
|||||||
@@ -1546,6 +1546,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
|||||||
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt2585_3.kt");
|
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt2585_3.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt5200DeadCodeInLambdas.kt")
|
||||||
|
public void testKt5200DeadCodeInLambdas() throws Exception {
|
||||||
|
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/kt5200DeadCodeInLambdas.kt");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/definiteReturn")
|
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/definiteReturn")
|
||||||
|
|||||||
Reference in New Issue
Block a user