Retain data flow info after elvis operator

#KT-2825 In Progress
This commit is contained in:
Alexander Udalov
2012-11-12 18:49:07 +04:00
parent e4cd0e004f
commit d19a824b14
3 changed files with 25 additions and 4 deletions
@@ -1171,10 +1171,12 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
result = booleanType;
}
else if (operationType == JetTokens.ELVIS) {
JetType leftType = facade.getTypeInfo(left, context.replaceScope(context.scope)).getType();
JetType rightType = right == null
? null
: facade.getTypeInfo(right, contextWithExpectedType.replaceScope(context.scope)).getType();
JetTypeInfo leftTypeInfo = facade.getTypeInfo(left, context);
JetType leftType = leftTypeInfo.getType();
dataFlowInfo = leftTypeInfo.getDataFlowInfo();
ExpressionTypingContext newContext = contextWithExpectedType.replaceDataFlowInfo(dataFlowInfo).replaceScope(context.scope);
JetType rightType = right == null ? null : facade.getTypeInfo(right, newContext).getType();
if (leftType != null) {
if (!leftType.isNullable()) {
context.trace.report(USELESS_ELVIS.on(left, leftType));
@@ -0,0 +1,14 @@
fun foo(x: Int?): Int = x!!
fun elvis(x: Number?): Int {
val result = (x as Int?) ?: foo(x)
x : Int?
return result
}
fun elvisWithRHSTypeInfo(x: Number?): Any? {
val result = x ?: x!!
<!TYPE_MISMATCH!>x<!> : Int?
return result
}
@@ -1161,6 +1161,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionContainsConvention.kt");
}
@TestMetadata("BinaryExpressionElvis.kt")
public void testBinaryExpressionElvis() throws Exception {
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionElvis.kt");
}
@TestMetadata("BinaryExpressionEqualsConvention.kt")
public void testBinaryExpressionEqualsConvention() throws Exception {
doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/BinaryExpressionEqualsConvention.kt");