From 4d39dc38da1e4f31e48f3c06548a988b92573cdc Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Mon, 9 Dec 2013 18:54:04 +0400 Subject: [PATCH] fixed: smart cast of boolean variable didn't work in boolean expression --- .../expressions/BasicExpressionTypingVisitor.java | 15 +++++---------- .../tests/BinaryCallsOnNullableValues.kt | 2 +- .../tests/checkArguments/booleanExpressions.kt | 6 ++++++ .../smartCastsAndBooleanExpressions.kt | 8 ++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 10 ++++++++++ 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/checkArguments/booleanExpressions.kt create mode 100644 compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastsAndBooleanExpressions.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 8db1abdb655..623f5301f89 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 @@ -956,9 +956,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { @NotNull ExpressionTypingContext context ) { JetType booleanType = KotlinBuiltIns.getInstance().getBooleanType(); - JetTypeInfo leftTypeInfo = getTypeInfoOrNullType(left, context, facade); - - JetType leftType = leftTypeInfo.getType(); + JetTypeInfo leftTypeInfo = getTypeInfoOrNullType(left, context.replaceExpectedType(booleanType), facade); DataFlowInfo dataFlowInfo = leftTypeInfo.getDataFlowInfo(); WritableScopeImpl leftScope = newWritableScopeImpl(context, "Left scope of && or ||"); @@ -967,13 +965,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { DataFlowInfo flowInfoLeft = DataFlowUtils.extractDataFlowInfoFromCondition(left, isAnd, context).and(dataFlowInfo); WritableScopeImpl rightScope = isAnd ? leftScope : newWritableScopeImpl(context, "Right scope of && or ||"); - ExpressionTypingContext contextForRightExpr = context.replaceDataFlowInfo(flowInfoLeft).replaceScope(rightScope); - JetType rightType = right != null ? facade.getTypeInfo(right, contextForRightExpr).getType() : null; - if (left != null && leftType != null && !isBoolean(leftType)) { - context.trace.report(TYPE_MISMATCH.on(left, booleanType, leftType)); - } - if (rightType != null && !isBoolean(rightType)) { - context.trace.report(TYPE_MISMATCH.on(right, booleanType, rightType)); + ExpressionTypingContext contextForRightExpr = + context.replaceDataFlowInfo(flowInfoLeft).replaceScope(rightScope).replaceExpectedType(booleanType); + if (right != null) { + facade.getTypeInfo(right, contextForRightExpr); } return JetTypeInfo.create(booleanType, dataFlowInfo); } diff --git a/compiler/testData/diagnostics/tests/BinaryCallsOnNullableValues.kt b/compiler/testData/diagnostics/tests/BinaryCallsOnNullableValues.kt index 784ac90e25e..db72b5d2cfe 100644 --- a/compiler/testData/diagnostics/tests/BinaryCallsOnNullableValues.kt +++ b/compiler/testData/diagnostics/tests/BinaryCallsOnNullableValues.kt @@ -37,5 +37,5 @@ fun f(): Unit { val y : Boolean? = true false || y y && true - y && 1 + y && 1 } diff --git a/compiler/testData/diagnostics/tests/checkArguments/booleanExpressions.kt b/compiler/testData/diagnostics/tests/checkArguments/booleanExpressions.kt new file mode 100644 index 00000000000..9f15afaee8d --- /dev/null +++ b/compiler/testData/diagnostics/tests/checkArguments/booleanExpressions.kt @@ -0,0 +1,6 @@ +fun foo1(b: Boolean, c: Int) { + if (b && c) {} + if (b || c) {} + if (c && b) {} + if (c || b) {} +} diff --git a/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastsAndBooleanExpressions.kt b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastsAndBooleanExpressions.kt new file mode 100644 index 00000000000..b3c1deb6f42 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastsAndBooleanExpressions.kt @@ -0,0 +1,8 @@ +fun foo(b: Boolean?, c: Boolean) { + if (b != null && b) {} + if (b == null || b) {} + if (b != null) { + if (b && c) {} + if (b || c) {} + } +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 82f439812c0..a149b2cc6f8 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1331,6 +1331,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/checkArguments"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("booleanExpressions.kt") + public void testBooleanExpressions() throws Exception { + doTest("compiler/testData/diagnostics/tests/checkArguments/booleanExpressions.kt"); + } + @TestMetadata("kt1897_diagnostic_part.kt") public void testKt1897_diagnostic_part() throws Exception { doTest("compiler/testData/diagnostics/tests/checkArguments/kt1897_diagnostic_part.kt"); @@ -4717,6 +4722,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastedReceiverWithGenerics.kt"); } + @TestMetadata("smartCastsAndBooleanExpressions.kt") + public void testSmartCastsAndBooleanExpressions() throws Exception { + doTest("compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/smartCastsAndBooleanExpressions.kt"); + } + } @TestMetadata("compiler/testData/diagnostics/tests/nullableTypes")