Retain data flow info after elvis operator
#KT-2825 In Progress
This commit is contained in:
+6
-4
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user