fixed: smart cast of boolean variable didn't work in boolean expression
This commit is contained in:
+5
-10
@@ -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);
|
||||
}
|
||||
|
||||
@@ -37,5 +37,5 @@ fun f(): Unit {
|
||||
val y : Boolean? = true
|
||||
false || <!TYPE_MISMATCH!>y<!>
|
||||
<!TYPE_MISMATCH!>y<!> && true
|
||||
<!TYPE_MISMATCH!>y<!> && <!TYPE_MISMATCH!>1<!>
|
||||
<!TYPE_MISMATCH!>y<!> && <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo1(b: Boolean, c: Int) {
|
||||
if (b && <!TYPE_MISMATCH!>c<!>) {}
|
||||
if (b || <!TYPE_MISMATCH!>c<!>) {}
|
||||
if (<!TYPE_MISMATCH!>c<!> && b) {}
|
||||
if (<!TYPE_MISMATCH!>c<!> || b) {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo(b: Boolean?, c: Boolean) {
|
||||
if (b != null && <!DEBUG_INFO_AUTOCAST!>b<!>) {}
|
||||
if (b == null || <!DEBUG_INFO_AUTOCAST!>b<!>) {}
|
||||
if (b != null) {
|
||||
if (<!DEBUG_INFO_AUTOCAST!>b<!> && c) {}
|
||||
if (<!DEBUG_INFO_AUTOCAST!>b<!> || c) {}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user