diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt index ed85fb7acba..e438b3d4a15 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt @@ -654,16 +654,28 @@ abstract class FirDataFlowAnalyzer( // Add `Any` to the set of possible types; the intersection type `T? & Any` will be reduced to `T` after smartcast. val (node, unionNode) = graphBuilder.exitCheckNotNullCall(checkNotNullCall, callCompleted) node.mergeIncomingFlow() - val argument = checkNotNullCall.argument - variableStorage.getOrCreateRealVariable(node.previousFlow, argument.symbol, argument)?.let { operandVariable -> - node.flow.addTypeStatement(operandVariable typeEq any) - logicSystem.approveStatementsInsideFlow( - node.flow, - operandVariable notEq null, - shouldRemoveSynthetics = true, - shouldForkFlow = false - ) + + fun FirExpression.propagateNotNullInfo() { + val symbol = this.symbol + if (symbol != null) { + variableStorage.getOrCreateRealVariable(node.previousFlow, symbol, this)?.let { operandVariable -> + node.flow.addTypeStatement(operandVariable typeEq any) + logicSystem.approveStatementsInsideFlow( + node.flow, + operandVariable notEq null, + shouldRemoveSynthetics = true, + shouldForkFlow = false + ) + } + } + when (this) { + is FirSafeCallExpression -> this.receiver.propagateNotNullInfo() + is FirTypeOperatorCall -> this.argument.propagateNotNullInfo() + } } + + checkNotNullCall.argument.propagateNotNullInfo() + unionNode?.let { unionFlowFromArguments(it) } } diff --git a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt b/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt deleted file mode 100644 index 1926dacd36d..00000000000 --- a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.fir.kt +++ /dev/null @@ -1,41 +0,0 @@ -package a - -interface A { - val b: B - val nb: B? -} - -interface B { - fun foo(): Int -} - -fun test(u: A?, x: A?, y: A?, z: A?, w: A, v: A?) { - u?.b?.foo()!! // was UNNECESSARY_SAFE_CALL everywhere, because result type (of 'foo()') wasn't made nullable - u!!.b?.foo()!! - x?.b!!.foo()!! - // x?.b is not null - x!!.b!!.foo()!! - - y?.nb?.foo()!! - y!!.nb?.foo()!! - z?.nb!!.foo()!! - // z?.nb is not null - z!!.nb!!.foo()!! - - w.b?.foo()!! - w.b!!.foo()!! - w.nb?.foo()!! - w.nb!!.foo()!! - - v!!.b.foo()!! -} - -fun B?.bar(): Int = 1 -fun B?.baz(): Int? = 1 - -fun doInt(i: Int) = i - -fun test(a: A?) { - doInt(a?.b.bar()!!) - doInt(a?.b.baz()!!) -} diff --git a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.kt b/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.kt index 79d463d9d7e..7a1dbc363b6 100644 --- a/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.kt +++ b/compiler/testData/diagnostics/tests/inference/nestedCalls/makeNullableIfSafeCall.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL package a interface A { diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt deleted file mode 100644 index e6b60586116..00000000000 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt702.fir.kt +++ /dev/null @@ -1,20 +0,0 @@ -//KT-702 Type inference failed -package a -//+JDK - -fun getJavaClass() : java.lang.Class { } - -public class Throwables() { - companion object { - public fun propagateIfInstanceOf(throwable : Throwable?, declaredType : Class?) : Unit { - if (((throwable != null) && declaredType?.isInstance(throwable)!!)) - { - throw declaredType?.cast(throwable)!! - } - } - public fun propagateIfPossible(throwable : Throwable?) : Unit { - propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints - propagateIfInstanceOf(throwable, getJavaClass()) // Type inference failed: Mismatch while expanding constraints - } - } -} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt702.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt702.kt index 189b7d2ebdb..f6fc14ee964 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt702.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt702.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL //KT-702 Type inference failed package a //+JDK diff --git a/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt b/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt index 56f432a40d0..2d0742ccce8 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt701.fir.kt @@ -6,7 +6,7 @@ public class Throwables() { public fun propagateIfInstanceOf(throwable : Throwable?, declaredType : Class?) { if (((throwable != null) && declaredType?.isInstance(throwable)!!)) { - throw declaredType?.cast(throwable)!! + throw declaredType?.cast(throwable)!! } } public fun propagateIfPossible(throwable : Throwable?) { diff --git a/compiler/testData/diagnostics/tests/smartCasts/level_1_0.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/level_1_0.fir.kt index 0707da7903b..11da125a2b8 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/level_1_0.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/level_1_0.fir.kt @@ -36,7 +36,7 @@ fun kt6840_2(s: String?) { fun kt1635(s: String?) { s?.hashCode()!! - s.hashCode() + s.hashCode() } fun kt2127() { diff --git a/compiler/testData/diagnostics/tests/smartCasts/safecalls/safeAccessReceiverNotNull.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/safecalls/safeAccessReceiverNotNull.fir.kt index 0dfb36ce12f..fc9c8040150 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/safecalls/safeAccessReceiverNotNull.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/safecalls/safeAccessReceiverNotNull.fir.kt @@ -18,7 +18,7 @@ fun kt6840_2(s: String?) { fun kt1635(s: String?) { s?.hashCode()!! - s.hashCode() + s.hashCode() } fun kt2127() {