Regular checkType() is now called during condition analysis, TYPE_MISMATCH_IN_CONDITION removed #KT-11998 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-04-25 16:23:20 +03:00
committed by Mikhail Glukhikh
parent e6ab2f1c2d
commit 6b945ba103
11 changed files with 65 additions and 22 deletions
@@ -739,7 +739,6 @@ public interface Errors {
DiagnosticFactory1<KtExpression, KotlinType> EXPECTED_TYPE_MISMATCH = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<KtExpression, KotlinType> EXPECTED_TYPE_MISMATCH = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtBinaryExpression, KotlinType> ASSIGNMENT_TYPE_MISMATCH = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<KtBinaryExpression, KotlinType> ASSIGNMENT_TYPE_MISMATCH = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<KtTypeReference, KotlinType, KotlinType> TYPE_MISMATCH_IN_FOR_LOOP = DiagnosticFactory2.create(ERROR); DiagnosticFactory2<KtTypeReference, KotlinType, KotlinType> TYPE_MISMATCH_IN_FOR_LOOP = DiagnosticFactory2.create(ERROR);
DiagnosticFactory1<KtElement, KotlinType> TYPE_MISMATCH_IN_CONDITION = DiagnosticFactory1.create(ERROR);
DiagnosticFactory3<KtExpression, String, KotlinType, KotlinType> RESULT_TYPE_MISMATCH = DiagnosticFactory3.create(ERROR); DiagnosticFactory3<KtExpression, String, KotlinType, KotlinType> RESULT_TYPE_MISMATCH = DiagnosticFactory3.create(ERROR);
DiagnosticFactory0<KtWhenConditionInRange> TYPE_MISMATCH_IN_RANGE = DiagnosticFactory0.create(ERROR, WHEN_CONDITION_IN_RANGE); DiagnosticFactory0<KtWhenConditionInRange> TYPE_MISMATCH_IN_RANGE = DiagnosticFactory0.create(ERROR, WHEN_CONDITION_IN_RANGE);
@@ -540,7 +540,6 @@ public class DefaultErrorMessages {
MAP.put(TYPE_MISMATCH_IN_FOR_LOOP, "The loop iterates over values of type {0} but the parameter is declared to be {1}", RENDER_TYPE, MAP.put(TYPE_MISMATCH_IN_FOR_LOOP, "The loop iterates over values of type {0} but the parameter is declared to be {1}", RENDER_TYPE,
RENDER_TYPE); RENDER_TYPE);
MAP.put(TYPE_MISMATCH_IN_CONDITION, "Condition must be of type Boolean, but is of type {0}", RENDER_TYPE);
MAP.put(INCOMPATIBLE_TYPES, "Incompatible types: {0} and {1}", RENDER_TYPE, RENDER_TYPE); MAP.put(INCOMPATIBLE_TYPES, "Incompatible types: {0} and {1}", RENDER_TYPE, RENDER_TYPE);
MAP.put(IMPLICIT_NOTHING_RETURN_TYPE, "'Nothing' return type needs to be specified explicitly"); MAP.put(IMPLICIT_NOTHING_RETURN_TYPE, "'Nothing' return type needs to be specified explicitly");
MAP.put(IMPLICIT_NOTHING_PROPERTY_TYPE, "'Nothing' property type needs to be specified explicitly"); MAP.put(IMPLICIT_NOTHING_PROPERTY_TYPE, "'Nothing' property type needs to be specified explicitly");
@@ -69,15 +69,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
@NotNull @NotNull
private DataFlowInfo checkCondition(@NotNull LexicalScope scope, @Nullable KtExpression condition, ExpressionTypingContext context) { private DataFlowInfo checkCondition(@NotNull LexicalScope scope, @Nullable KtExpression condition, ExpressionTypingContext context) {
if (condition != null) { if (condition != null) {
KotlinTypeInfo typeInfo = facade.getTypeInfo(condition, context.replaceScope(scope) ExpressionTypingContext conditionContext = context.replaceScope(scope)
.replaceExpectedType(components.builtIns.getBooleanType()).replaceContextDependency(INDEPENDENT)); .replaceExpectedType(components.builtIns.getBooleanType()).replaceContextDependency(INDEPENDENT);
KotlinType conditionType = typeInfo.getType(); KotlinTypeInfo typeInfo = facade.getTypeInfo(condition, conditionContext);
if (conditionType != null && !components.builtIns.isBooleanOrSubtype(conditionType)) { return components.dataFlowAnalyzer.checkType(typeInfo, condition, conditionContext).getDataFlowInfo();
context.trace.report(TYPE_MISMATCH_IN_CONDITION.on(condition, conditionType));
}
return typeInfo.getDataFlowInfo();
} }
return context.dataFlowInfo; return context.dataFlowInfo;
} }
@@ -322,10 +322,8 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
newContext = newContext.replaceDataFlowInfo(typeInfo.dataFlowInfo) newContext = newContext.replaceDataFlowInfo(typeInfo.dataFlowInfo)
if (conditionExpected) { if (conditionExpected) {
val booleanType = components.builtIns.booleanType val booleanType = components.builtIns.booleanType
if (!KotlinTypeChecker.DEFAULT.equalTypes(booleanType, type)) { val checkedTypeInfo = components.dataFlowAnalyzer.checkType(typeInfo, expression, newContext.replaceExpectedType(booleanType))
newContext.trace.report(TYPE_MISMATCH_IN_CONDITION.on(expression, type)) if (KotlinTypeChecker.DEFAULT.equalTypes(booleanType, checkedTypeInfo.type ?: type)) {
}
else {
val ifInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, true, newContext) val ifInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, true, newContext)
val elseInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, false, newContext) val elseInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, false, newContext)
return ConditionalDataFlowInfo(ifInfo, elseInfo) return ConditionalDataFlowInfo(ifInfo, elseInfo)
@@ -7,10 +7,10 @@ fun foo() =
when { when {
cond1() -> 12 cond1() -> 12
cond2() -> 2 cond2() -> 2
<!TYPE_MISMATCH_IN_CONDITION!>4<!> -> 34 <!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!> -> 34
<!TYPE_MISMATCH_IN_CONDITION!>Pair(1, 2)<!> -> 3 <!TYPE_MISMATCH!>Pair(1, 2)<!> -> 3
<!EXPECTED_CONDITION!>in 1..10<!> -> 34 <!EXPECTED_CONDITION!>in 1..10<!> -> 34
<!TYPE_MISMATCH_IN_CONDITION!>4<!> -> 38 <!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!> -> 38
<!EXPECTED_CONDITION!>is Int<!> -> 33 <!EXPECTED_CONDITION!>is Int<!> -> 33
else -> 34 else -> 34
} }
@@ -22,15 +22,15 @@ fun test() {
val platformJ = J.staticJ val platformJ = J.staticJ
if (platformNN) {} if (platformNN) {}
if (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>) {} if (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>) {}
if (platformJ) {} if (platformJ) {}
while (platformNN) {} while (platformNN) {}
while (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>) {} while (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>) {}
while (platformJ) {} while (platformJ) {}
do {} while (platformNN) do {} while (platformNN)
do {} while (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>) do {} while (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>)
do {} while (platformJ) do {} while (platformJ)
platformNN && false platformNN && false
@@ -0,0 +1,30 @@
// See also: KT-11998
data class My(val x: Boolean?)
fun doIt() {}
fun foo(my: My) {
if (my.x != null) {
// my.x should be smart-cast
if (<!DEBUG_INFO_SMARTCAST!>my.x<!>) doIt()
when (<!DEBUG_INFO_SMARTCAST!>my.x<!>) {
true -> doIt()
}
when {
<!DEBUG_INFO_SMARTCAST!>my.x<!> -> doIt()
}
}
}
fun bar(x: Boolean?) {
if (x != null) {
// x should be smart-cast
if (<!DEBUG_INFO_SMARTCAST!>x<!>) doIt()
when (<!DEBUG_INFO_SMARTCAST!>x<!>) {
true -> doIt()
}
when {
<!DEBUG_INFO_SMARTCAST!>x<!> -> doIt()
}
}
}
@@ -0,0 +1,15 @@
package
public fun bar(/*0*/ x: kotlin.Boolean?): kotlin.Unit
public fun doIt(): kotlin.Unit
public fun foo(/*0*/ my: My): kotlin.Unit
public final data class My {
public constructor My(/*0*/ x: kotlin.Boolean?)
public final val x: kotlin.Boolean?
public final operator /*synthesized*/ fun component1(): kotlin.Boolean?
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Boolean? = ...): 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
}
@@ -16764,6 +16764,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("propertyAsCondition.kt")
public void testPropertyAsCondition() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt");
doTest(fileName);
}
@TestMetadata("propertyToNotNull.kt") @TestMetadata("propertyToNotNull.kt")
public void testPropertyToNotNull() throws Exception { public void testPropertyToNotNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/propertyToNotNull.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/propertyToNotNull.kt");
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// IS_APPLICABLE: false // IS_APPLICABLE: false
// ERROR: Type mismatch: inferred type is Int but Boolean was expected // ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Condition must be of type Boolean, but is of type Int // ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Infix call corresponds to a dot-qualified call 'foo.times(10)' which is not allowed on a nullable receiver 'foo'. Use '?.'-qualified call instead // ERROR: Infix call corresponds to a dot-qualified call 'foo.times(10)' which is not allowed on a nullable receiver 'foo'. Use '?.'-qualified call instead
fun String?.times(a: Int): Boolean = a == 0 fun String?.times(a: Int): Boolean = a == 0
@@ -1,6 +1,6 @@
// IS_APPLICABLE: false // IS_APPLICABLE: false
// ERROR: Type mismatch: inferred type is Int but Boolean was expected // ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Condition must be of type Boolean, but is of type Int // ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Infix call corresponds to a dot-qualified call 'foo.times(10)' which is not allowed on a nullable receiver 'foo'. Use '?.'-qualified call instead // ERROR: Infix call corresponds to a dot-qualified call 'foo.times(10)' which is not allowed on a nullable receiver 'foo'. Use '?.'-qualified call instead
fun main(args: Array<String>) { fun main(args: Array<String>) {