extract DataFlowInfo from when-condition + more tests for kt2146
This commit is contained in:
+7
-1
@@ -263,13 +263,19 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
public void visitExpressionPattern(JetExpressionPattern pattern) {
|
public void visitExpressionPattern(JetExpressionPattern pattern) {
|
||||||
JetExpression expression = pattern.getExpression();
|
JetExpression expression = pattern.getExpression();
|
||||||
if (expression == null) return;
|
if (expression == null) return;
|
||||||
JetType type = facade.getTypeInfo(expression, context.replaceScope(scopeToExtend)).getType();
|
JetTypeInfo typeInfo = facade.getTypeInfo(expression, context.replaceScope(scopeToExtend));
|
||||||
|
JetType type = typeInfo.getType();
|
||||||
if (type == null) return;
|
if (type == null) return;
|
||||||
if (conditionExpected) {
|
if (conditionExpected) {
|
||||||
JetType booleanType = JetStandardLibrary.getInstance().getBooleanType();
|
JetType booleanType = JetStandardLibrary.getInstance().getBooleanType();
|
||||||
if (!JetTypeChecker.INSTANCE.equalTypes(booleanType, type)) {
|
if (!JetTypeChecker.INSTANCE.equalTypes(booleanType, type)) {
|
||||||
context.trace.report(TYPE_MISMATCH_IN_CONDITION.on(pattern, type));
|
context.trace.report(TYPE_MISMATCH_IN_CONDITION.on(pattern, type));
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
DataFlowInfo ifInfo = DataFlowUtils.extractDataFlowInfoFromCondition(expression, true, scopeToExtend, context);
|
||||||
|
DataFlowInfo elseInfo = DataFlowUtils.extractDataFlowInfoFromCondition(expression, false, null, context);
|
||||||
|
result.set(Pair.create(ifInfo, elseInfo));
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
checkTypeCompatibility(type, subjectType, pattern);
|
checkTypeCompatibility(type, subjectType, pattern);
|
||||||
|
|||||||
@@ -1,9 +1,40 @@
|
|||||||
//KT-2146 Nullability casts in when.
|
//KT-2146 Nullability casts in when.
|
||||||
package kt2146
|
package kt2146
|
||||||
|
|
||||||
fun f(s : Int?) : Int {
|
fun f1(s: Int?): Int {
|
||||||
return when (s) {
|
return when (s) {
|
||||||
null -> 3
|
null -> 3
|
||||||
else -> s // type mismatch
|
else -> s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun f2(s: Int?): Int {
|
||||||
|
return when (s) {
|
||||||
|
is 4 -> s
|
||||||
|
is null -> <!TYPE_MISMATCH!>s<!>
|
||||||
|
else -> s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun f3(s: Int?): Int {
|
||||||
|
return when (s) {
|
||||||
|
is Int -> s
|
||||||
|
else -> <!TYPE_MISMATCH!>s<!>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun f4(s: Int?): Int {
|
||||||
|
return when {
|
||||||
|
s == 4 -> s
|
||||||
|
s == null -> <!TYPE_MISMATCH!>s<!>
|
||||||
|
else -> s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun f5(s: Int?): Int {
|
||||||
|
return when (s) {
|
||||||
|
s!! -> s
|
||||||
|
s -> <!TYPE_MISMATCH!>s<!>
|
||||||
|
else -> 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user