diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorage.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorage.kt index 145298ce4e8..fdddbfd3db8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorage.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorage.kt @@ -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 } diff --git a/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.kt b/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.kt new file mode 100644 index 00000000000..dd476fa0e59 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.kt @@ -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 + } +} \ No newline at end of file diff --git a/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.txt b/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.txt new file mode 100644 index 00000000000..1aa8f2fa51e --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/smartcasts/kt37327.txt @@ -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() + } + + } + public final class B : R|A| { + public constructor(x: R|kotlin/Int|): R|B| { + super() + } + + public final val x: R|kotlin/Int| = R|/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| + } + } + + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 1587f1f2e2a..d0b7080e78d 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -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"); diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 155aae3db6a..90e5acdc20d 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -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");