From f1cf446e10c1a227c022375d91c109b81ec9b1a0 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Mon, 15 Sep 2014 18:24:13 +0400 Subject: [PATCH] KT-5773 Smart cast doesn't work with Nothing on the rhs of "?:" #KT-5773 FIxed --- .../expressions/BasicExpressionTypingVisitor.java | 4 ++++ .../testData/diagnostics/tests/smartCasts/elvis.kt | 11 +++++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ 3 files changed, 21 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/elvis.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index fb22940f987..086a21148e6 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -1064,6 +1064,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { JetType rightType = rightTypeInfo.getType(); DataFlowInfo dataFlowInfo = resolvedCall.getDataFlowInfoForArguments().getResultInfo(); + if (leftType != null && rightType != null && KotlinBuiltIns.getInstance().isNothingOrNullableNothing(rightType) && !rightType.isNullable()) { + DataFlowValue value = DataFlowValueFactory.createDataFlowValue(left, leftType, context.trace.getBindingContext()); + dataFlowInfo = dataFlowInfo.disequate(value, DataFlowValue.NULL); + } JetType type = resolvedCall.getResultingDescriptor().getReturnType(); if (type == null || rightType == null) return JetTypeInfo.create(null, dataFlowInfo); diff --git a/compiler/testData/diagnostics/tests/smartCasts/elvis.kt b/compiler/testData/diagnostics/tests/smartCasts/elvis.kt new file mode 100644 index 00000000000..f7300ef3527 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/elvis.kt @@ -0,0 +1,11 @@ +class C { + fun foo() {} +} + +fun test(a: C?, nn: () -> Nothing?) { + a ?: nn() + a.foo() + + a ?: return + a.foo() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 3b9e1d4c358..0027562f4ee 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -9014,6 +9014,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("elvis.kt") + public void testElvis() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/elvis.kt"); + doTest(fileName); + } + @TestMetadata("kt1461.kt") public void testKt1461() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/kt1461.kt");