Regular checkType() is now called during condition analysis, TYPE_MISMATCH_IN_CONDITION removed #KT-11998 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e6ab2f1c2d
commit
6b945ba103
@@ -739,7 +739,6 @@ public interface Errors {
|
||||
DiagnosticFactory1<KtExpression, KotlinType> EXPECTED_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);
|
||||
DiagnosticFactory1<KtElement, KotlinType> TYPE_MISMATCH_IN_CONDITION = DiagnosticFactory1.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);
|
||||
|
||||
|
||||
-1
@@ -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,
|
||||
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(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");
|
||||
|
||||
+4
-8
@@ -69,15 +69,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
||||
@NotNull
|
||||
private DataFlowInfo checkCondition(@NotNull LexicalScope scope, @Nullable KtExpression condition, ExpressionTypingContext context) {
|
||||
if (condition != null) {
|
||||
KotlinTypeInfo typeInfo = facade.getTypeInfo(condition, context.replaceScope(scope)
|
||||
.replaceExpectedType(components.builtIns.getBooleanType()).replaceContextDependency(INDEPENDENT));
|
||||
KotlinType conditionType = typeInfo.getType();
|
||||
ExpressionTypingContext conditionContext = context.replaceScope(scope)
|
||||
.replaceExpectedType(components.builtIns.getBooleanType()).replaceContextDependency(INDEPENDENT);
|
||||
KotlinTypeInfo typeInfo = facade.getTypeInfo(condition, conditionContext);
|
||||
|
||||
if (conditionType != null && !components.builtIns.isBooleanOrSubtype(conditionType)) {
|
||||
context.trace.report(TYPE_MISMATCH_IN_CONDITION.on(condition, conditionType));
|
||||
}
|
||||
|
||||
return typeInfo.getDataFlowInfo();
|
||||
return components.dataFlowAnalyzer.checkType(typeInfo, condition, conditionContext).getDataFlowInfo();
|
||||
}
|
||||
return context.dataFlowInfo;
|
||||
}
|
||||
|
||||
+2
-4
@@ -322,10 +322,8 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
|
||||
newContext = newContext.replaceDataFlowInfo(typeInfo.dataFlowInfo)
|
||||
if (conditionExpected) {
|
||||
val booleanType = components.builtIns.booleanType
|
||||
if (!KotlinTypeChecker.DEFAULT.equalTypes(booleanType, type)) {
|
||||
newContext.trace.report(TYPE_MISMATCH_IN_CONDITION.on(expression, type))
|
||||
}
|
||||
else {
|
||||
val checkedTypeInfo = components.dataFlowAnalyzer.checkType(typeInfo, expression, newContext.replaceExpectedType(booleanType))
|
||||
if (KotlinTypeChecker.DEFAULT.equalTypes(booleanType, checkedTypeInfo.type ?: type)) {
|
||||
val ifInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, true, newContext)
|
||||
val elseInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(expression, false, newContext)
|
||||
return ConditionalDataFlowInfo(ifInfo, elseInfo)
|
||||
|
||||
@@ -7,10 +7,10 @@ fun foo() =
|
||||
when {
|
||||
cond1() -> 12
|
||||
cond2() -> 2
|
||||
<!TYPE_MISMATCH_IN_CONDITION!>4<!> -> 34
|
||||
<!TYPE_MISMATCH_IN_CONDITION!>Pair(1, 2)<!> -> 3
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!> -> 34
|
||||
<!TYPE_MISMATCH!>Pair(1, 2)<!> -> 3
|
||||
<!EXPECTED_CONDITION!>in 1..10<!> -> 34
|
||||
<!TYPE_MISMATCH_IN_CONDITION!>4<!> -> 38
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!> -> 38
|
||||
<!EXPECTED_CONDITION!>is Int<!> -> 33
|
||||
else -> 34
|
||||
}
|
||||
|
||||
+3
-3
@@ -22,15 +22,15 @@ fun test() {
|
||||
val platformJ = J.staticJ
|
||||
|
||||
if (platformNN) {}
|
||||
if (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>) {}
|
||||
if (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>) {}
|
||||
if (platformJ) {}
|
||||
|
||||
while (platformNN) {}
|
||||
while (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>) {}
|
||||
while (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>) {}
|
||||
while (platformJ) {}
|
||||
|
||||
do {} while (platformNN)
|
||||
do {} while (<!TYPE_MISMATCH, TYPE_MISMATCH_IN_CONDITION!>platformN<!>)
|
||||
do {} while (<!TYPE_MISMATCH, TYPE_MISMATCH!>platformN<!>)
|
||||
do {} while (platformJ)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyAsCondition.kt")
|
||||
public void testPropertyAsCondition() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyToNotNull.kt")
|
||||
public void testPropertyToNotNull() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/propertyToNotNull.kt");
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
// 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
|
||||
|
||||
fun String?.times(a: Int): Boolean = a == 0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: false
|
||||
// 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
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
Reference in New Issue
Block a user