Retain data flow info after boolean operations
#KT-2825 In Progress
This commit is contained in:
+7
-2
@@ -1148,9 +1148,14 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
result = booleanType;
|
||||
}
|
||||
else if (OperatorConventions.BOOLEAN_OPERATIONS.containsKey(operationType)) {
|
||||
JetType leftType = facade.getTypeInfo(left, context.replaceScope(context.scope)).getType();
|
||||
JetTypeInfo leftTypeInfo = facade.getTypeInfo(left, context);
|
||||
JetType leftType = leftTypeInfo.getType();
|
||||
dataFlowInfo = leftTypeInfo.getDataFlowInfo();
|
||||
|
||||
WritableScopeImpl leftScope = newWritableScopeImpl(context, "Left scope of && or ||");
|
||||
DataFlowInfo flowInfoLeft = DataFlowUtils.extractDataFlowInfoFromCondition(left, operationType == JetTokens.ANDAND, context); // TODO: This gets computed twice: here and in extractDataFlowInfoFromCondition() for the whole condition
|
||||
// TODO: This gets computed twice: here and in extractDataFlowInfoFromCondition() for the whole condition
|
||||
DataFlowInfo flowInfoLeft =
|
||||
DataFlowUtils.extractDataFlowInfoFromCondition(left, operationType == JetTokens.ANDAND, context).and(dataFlowInfo);
|
||||
WritableScopeImpl rightScope = operationType == JetTokens.ANDAND
|
||||
? leftScope
|
||||
: newWritableScopeImpl(context, "Right scope of && or ||");
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
fun foo1(x: Number, cond: Boolean): Boolean {
|
||||
val result = cond && ((x as Int) == 42)
|
||||
<!TYPE_MISMATCH!>x<!> : Int
|
||||
return result
|
||||
}
|
||||
|
||||
fun foo2(x: Number, cond: Boolean): Boolean {
|
||||
val result = ((x as Int) == 42) && cond
|
||||
x : Int
|
||||
return result
|
||||
}
|
||||
|
||||
fun foo3(x: Number, cond: Boolean): Boolean {
|
||||
val result = cond || ((x as Int) == 42)
|
||||
<!TYPE_MISMATCH!>x<!> : Int
|
||||
return result
|
||||
}
|
||||
|
||||
fun foo4(x: Number, cond: Boolean): Boolean {
|
||||
val result = ((x as Int) == 42) || cond
|
||||
x : Int
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Int?): Boolean {
|
||||
val result = ((x!! == 0) && ((x : Int) == 0))
|
||||
x : Int
|
||||
return result
|
||||
}
|
||||
@@ -1146,6 +1146,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("BinaryExpressionBooleanOperations.kt")
|
||||
public void testBinaryExpressionBooleanOperations() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionBooleanOperations.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("BinaryExpressionCompareToConvention.kt")
|
||||
public void testBinaryExpressionCompareToConvention() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionCompareToConvention.kt");
|
||||
@@ -1171,6 +1176,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionPlusConvention.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("Condition.kt")
|
||||
public void testCondition() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Condition.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("DeepIf.kt")
|
||||
public void testDeepIf() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/DeepIf.kt");
|
||||
|
||||
Reference in New Issue
Block a user