Elvis DFA: now only left part is taken into account #KT-9100 Fixed

I had to fix a few incorrect tests using something like x!! in Elvis right part, and also one bug in our code
This commit is contained in:
Mikhail Glukhikh
2015-09-09 15:59:06 +03:00
committed by Mikhail Glukhikh
parent 9fd968d59e
commit 9b11b5300c
13 changed files with 103 additions and 13 deletions
@@ -1173,10 +1173,16 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
boolean loopBreakContinuePossible = leftTypeInfo.getJumpOutPossible() || rightTypeInfo.getJumpOutPossible();
JetType rightType = rightTypeInfo.getType();
DataFlowInfo dataFlowInfo = resolvedCall.getDataFlowInfoForArguments().getResultInfo();
if (leftType != null && rightType != null && KotlinBuiltIns.isNothingOrNullableNothing(rightType) && !rightType.isMarkedNullable()) {
DataFlowValue value = createDataFlowValue(left, leftType, context);
dataFlowInfo = dataFlowInfo.disequate(value, DataFlowValue.NULL);
// Only left argument DFA is taken into account here: we cannot be sure that right argument is executed
DataFlowInfo dataFlowInfo = resolvedCall.getDataFlowInfoForArguments().getInfo(call.getValueArguments().get(1));
if (leftType != null) {
DataFlowValue leftValue = createDataFlowValue(left, leftType, context);
DataFlowInfo rightDataFlowInfo = resolvedCall.getDataFlowInfoForArguments().getResultInfo();
// left argument is considered not-null if it's not-null also in right part or if we have jump in right part
if ((rightType != null && KotlinBuiltIns.isNothingOrNullableNothing(rightType) && !rightType.isMarkedNullable())
|| !rightDataFlowInfo.getNullability(leftValue).canBeNull()) {
dataFlowInfo = dataFlowInfo.disequate(leftValue, DataFlowValue.NULL);
}
}
JetType type = resolvedCall.getResultingDescriptor().getReturnType();
if (type == null || rightType == null) return TypeInfoFactoryPackage.noTypeInfo(dataFlowInfo);
@@ -21,12 +21,14 @@ fun testElvis(a: Int?, b: Int?) {
fun testDataFlowInfo1(a: Int?, b: Int?) {
val c: Int = a ?: b!!
doInt(c)
<!DEBUG_INFO_SMARTCAST!>b<!> + 1
// b is nullable if a != null
b <!UNSAFE_INFIX_CALL!>+<!> 1
}
fun testDataFlowInfo2(a: Int?, b: Int?) {
doInt(a ?: b!!)
<!DEBUG_INFO_SMARTCAST!>b<!> + 1
// b is nullable if a != null
b <!UNSAFE_INFIX_CALL!>+<!> 1
}
fun testTypeMismatch(a: String?, b: Any) {
@@ -0,0 +1,9 @@
// Based on KT-9100
fun test(x: Any?, y: Any?): Any {
val z = x ?: y!!
y<!UNSAFE_CALL!>.<!>hashCode()
// !! / ?. is necessary here, because y!! above may not be executed
y?.hashCode()
y!!.hashCode()
return z
}
@@ -0,0 +1,3 @@
package
public fun test(/*0*/ x: kotlin.Any?, /*1*/ y: kotlin.Any?): kotlin.Any
@@ -0,0 +1,6 @@
fun test(x: Any?): Any {
val z = x ?: x!!
// x is not null in both branches
<!DEBUG_INFO_SMARTCAST!>x<!>.hashCode()
return z
}
@@ -0,0 +1,3 @@
package
public fun test(/*0*/ x: kotlin.Any?): kotlin.Any
@@ -0,0 +1,27 @@
// FILE: p/My.java
package p;
import org.jetbrains.annotations.*;
class My {
@Nullable static String create() {
return "";
}
}
// FILE: test.kt
package p
fun bar(x: String) = x
fun test(x: String?): Any {
val y = My.create()
val z = x ?: y!!
bar(<!TYPE_MISMATCH!>y<!>)
// !! / ?. is necessary here, because y!! above may not be executed
y?.hashCode()
y!!.hashCode()
return z
}
@@ -0,0 +1,16 @@
package
package p {
public fun bar(/*0*/ x: kotlin.String): kotlin.String
public fun test(/*0*/ x: kotlin.String?): kotlin.Any
public/*package*/ open class My {
public/*package*/ constructor My()
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
// Static members
org.jetbrains.annotations.Nullable() public/*package*/ open fun create(): kotlin.String?
}
}
@@ -3,8 +3,8 @@ public fun foo(x: String?, y: String?): Int {
val z = x ?: if (y == null) break else <!DEBUG_INFO_SMARTCAST!>y<!>
// z is not null in both branches
z.length()
// y is not null in both branches
<!DEBUG_INFO_SMARTCAST!>y<!>.length()
// y is nullable if x != null
y<!UNSAFE_CALL!>.<!>length()
}
// y is null because of the break
return y<!UNSAFE_CALL!>.<!>length()
@@ -1,8 +1,8 @@
public fun foo(x: String?, y: String?): Int {
while (true) {
x ?: if (y == null) break
// y is not null in both branches
<!DEBUG_INFO_SMARTCAST!>y<!>.length()
// y is nullable if x != null
y<!UNSAFE_CALL!>.<!>length()
}
// y is null because of the break
return y<!UNSAFE_CALL!>.<!>length()
@@ -1,8 +1,8 @@
public fun foo(x: String?, y: String?): Int {
while (true) {
(if (x != null) break else y) ?: y!!
// x is not null in both branches
<!DEBUG_INFO_SMARTCAST!>y<!>.length()
// y is not null in both branches but it's hard to determine
y<!UNSAFE_CALL!>.<!>length()
}
// y can be null because of the break
return y<!UNSAFE_CALL!>.<!>length()
@@ -13167,6 +13167,24 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("elvisExclExcl.kt")
public void testElvisExclExcl() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/elvisExclExcl.kt");
doTest(fileName);
}
@TestMetadata("elvisExclExclMerge.kt")
public void testElvisExclExclMerge() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/elvisExclExclMerge.kt");
doTest(fileName);
}
@TestMetadata("elvisExclExclPlatform.kt")
public void testElvisExclExclPlatform() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/elvisExclExclPlatform.kt");
doTest(fileName);
}
@TestMetadata("elvisNothingRHS.kt")
public void testElvisNothingRHS() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/elvisNothingRHS.kt");
@@ -81,7 +81,7 @@ public class DebugTextByStubTest : LightCodeInsightFixtureTestCase() {
val toCheckAgainst = "STUB: " + (expectedText ?: classByPsi!!.getText())
Assert.assertEquals(toCheckAgainst, psiFromStub.getDebugText())
if (expectedText != null) {
Assert.assertNotEquals("Expected text should not be specified", classByPsi.getDebugText(), psiFromStub.getDebugText())
Assert.assertNotEquals("Expected text should not be specified", classByPsi!!.getDebugText(), psiFromStub.getDebugText())
}
}