From e4cd0e004fa240cf891a9ad68ff1b581439ff7a0 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 12 Nov 2012 18:45:01 +0400 Subject: [PATCH] Retain data flow info after boolean operations #KT-2825 In Progress --- .../BasicExpressionTypingVisitor.java | 9 ++++++-- .../BinaryExpressionBooleanOperations.kt | 23 +++++++++++++++++++ .../tests/dataFlowInfoTraversal/Condition.kt | 5 ++++ .../checkers/JetDiagnosticsTestGenerated.java | 10 ++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionBooleanOperations.kt create mode 100644 compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Condition.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index da041f7fdb1..51a206a5bb1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -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 ||"); diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionBooleanOperations.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionBooleanOperations.kt new file mode 100644 index 00000000000..06def90e8da --- /dev/null +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionBooleanOperations.kt @@ -0,0 +1,23 @@ +fun foo1(x: Number, cond: Boolean): Boolean { + val result = cond && ((x as Int) == 42) + 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) + x : Int + return result +} + +fun foo4(x: Number, cond: Boolean): Boolean { + val result = ((x as Int) == 42) || cond + x : Int + return result +} diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Condition.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Condition.kt new file mode 100644 index 00000000000..34d643e4933 --- /dev/null +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Condition.kt @@ -0,0 +1,5 @@ +fun foo(x: Int?): Boolean { + val result = ((x!! == 0) && ((x : Int) == 0)) + x : Int + return result +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index cd3faada80c..6b638f03ff4 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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");