[FIR] Unwrap when subject expression recursively in DFA variable storage

#KT-37327 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-03-10 10:47:15 +03:00
parent 676ffff015
commit dd7ac1343e
5 changed files with 57 additions and 1 deletions
@@ -42,7 +42,7 @@ class VariableStorage(val session: FirSession) {
}
private fun FirElement.unwrapElement(): FirElement = when (this) {
is FirWhenSubjectExpression -> whenSubject.whenExpression.let { it.subjectVariable ?: it.subject } ?: this
is FirWhenSubjectExpression -> whenSubject.whenExpression.let { it.subjectVariable ?: it.subject }?.unwrapElement() ?: this
is FirExpressionWithSmartcast -> originalExpression.unwrapElement()
else -> this
}
@@ -0,0 +1,14 @@
// ISSUE: KT-37327
interface Q
sealed class A : Q
class B(val x: Int) : A()
fun Q.foo() {
if (this !is A) return
when (this) {
is B -> x // unresolved
}
}
@@ -0,0 +1,32 @@
FILE: kt37327.kt
public abstract interface Q : R|kotlin/Any| {
}
public sealed class A : R|Q| {
private constructor(): R|A| {
super<R|kotlin/Any|>()
}
}
public final class B : R|A| {
public constructor(x: R|kotlin/Int|): R|B| {
super<R|A|>()
}
public final val x: R|kotlin/Int| = R|<local>/x|
public get(): R|kotlin/Int|
}
public final fun R|Q|.foo(): R|kotlin/Unit| {
when () {
(this@R|/foo| !is R|A|) -> {
^foo Unit
}
}
when (this@R|/foo|) {
($subj$ is R|B|) -> {
this@R|/foo|.R|/B.x|
}
}
}
@@ -1574,6 +1574,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/equalsAndIdentity.kt");
}
@TestMetadata("kt37327.kt")
public void testKt37327() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/kt37327.kt");
}
@TestMetadata("multipleCasts.kt")
public void testMultipleCasts() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/multipleCasts.kt");
@@ -1574,6 +1574,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/resolve/testData/resolve/smartcasts/equalsAndIdentity.kt");
}
@TestMetadata("kt37327.kt")
public void testKt37327() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/kt37327.kt");
}
@TestMetadata("multipleCasts.kt")
public void testMultipleCasts() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/multipleCasts.kt");