diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java index 1ebf5e950c3..8c9a88a245a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java @@ -294,9 +294,10 @@ public class ExpressionTypingServices { } } if (mightBeUnit) { - // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments + // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments, + // but (for correct assignment / initialization analysis) data flow info must be preserved assert result.getType() == null || KotlinBuiltIns.isUnit(result.getType()); - result = result.replaceType(expressionTypingComponents.builtIns.getUnitType()).replaceDataFlowInfo(context.dataFlowInfo); + result = result.replaceType(expressionTypingComponents.builtIns.getUnitType()); } } return result; diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.kt new file mode 100644 index 00000000000..429a34e22c4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.kt @@ -0,0 +1,23 @@ +trait A { + fun ok(): Boolean +} + +class B: A { + override fun ok(): Boolean { return true } +} + +class C: A { + override fun ok(): Boolean { return false } +} + +fun foo(): Boolean { + var v: A + v = B() + // No smart cast needed, but not a problem if ever + if (v.ok()) { + v = C() + } + // No smart cast needed, and no smart cast possible! + // We cannot choose between B and C + return v.ok() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.txt b/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.txt new file mode 100644 index 00000000000..31db881b731 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.txt @@ -0,0 +1,26 @@ +package + +internal fun foo(): kotlin.Boolean + +internal trait A { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + internal abstract fun ok(): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +internal final class B : A { + public constructor B() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + internal open override /*1*/ fun ok(): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +internal final class C : A { + public constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + internal open override /*1*/ fun ok(): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index ebedb8470d4..f09cca0be9e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -11672,6 +11672,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("kt7599.kt") + public void testKt7599() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/variables/kt7599.kt"); + doTest(fileName); + } + @TestMetadata("property.kt") public void testProperty() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/variables/property.kt");