[FIR] Extract non-null info from x?.y!! ^KT-44513 Fixed

This commit is contained in:
Ivan Kochurkin
2021-10-04 14:23:08 +03:00
parent 2e1f455d9c
commit 55839aab91
8 changed files with 26 additions and 73 deletions
@@ -654,16 +654,28 @@ abstract class FirDataFlowAnalyzer<FLOW : Flow>(
// 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) }
}
@@ -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<!UNNECESSARY_SAFE_CALL!>?.<!>foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
x?.b!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
// x?.b is not null
x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.b<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
y?.nb?.foo()!!
y!!.nb?.foo()!!
z?.nb!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
// z?.nb is not null
z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.nb!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.b<!UNNECESSARY_SAFE_CALL!>?.<!>foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.b<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
w.nb?.foo()!!
w.nb!!.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
v!!.b.foo()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
}
fun B?.bar(): Int = 1
fun B?.baz(): Int? = 1
fun doInt(i: Int) = i
fun test(a: A?) {
doInt(a?.b.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
doInt(a?.b.baz()!!)
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
package a
interface A {
@@ -1,20 +0,0 @@
//KT-702 Type inference failed
package a
//+JDK
fun <T> getJavaClass() : java.lang.Class<T> { <!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
public class Throwables() {
companion object {
public fun <X : Throwable?> propagateIfInstanceOf(throwable : Throwable?, declaredType : Class<X?>?) : Unit {
if (((throwable != null) && declaredType?.isInstance(throwable)!!))
{
throw declaredType?.cast(throwable)!!
}
}
public fun propagateIfPossible(throwable : Throwable?) : Unit {
propagateIfInstanceOf(throwable, getJavaClass<Error?>()) // Type inference failed: Mismatch while expanding constraints
propagateIfInstanceOf(throwable, getJavaClass<RuntimeException?>()) // Type inference failed: Mismatch while expanding constraints
}
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-702 Type inference failed
package a
//+JDK
@@ -6,7 +6,7 @@ public class Throwables() {
public fun <X : Throwable?> propagateIfInstanceOf(throwable : Throwable?, declaredType : Class<X?>?) {
if (((throwable != null) && declaredType?.isInstance(throwable)!!))
{
throw declaredType?.cast(throwable)!!
throw declaredType<!UNNECESSARY_SAFE_CALL!>?.<!>cast(throwable)!!
}
}
public fun propagateIfPossible(throwable : Throwable?) {
@@ -36,7 +36,7 @@ fun kt6840_2(s: String?) {
fun kt1635(s: String?) {
s?.hashCode()!!
s<!UNSAFE_CALL!>.<!>hashCode()
s.hashCode()
}
fun kt2127() {
@@ -18,7 +18,7 @@ fun kt6840_2(s: String?) {
fun kt1635(s: String?) {
s?.hashCode()!!
s<!UNSAFE_CALL!>.<!>hashCode()
s.hashCode()
}
fun kt2127() {