diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index cdf5140a327..cd956d283d6 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -13179,6 +13179,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolutionMode.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolutionMode.kt index 6123548ddb6..b3622423aad 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolutionMode.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ResolutionMode.kt @@ -6,8 +6,10 @@ package org.jetbrains.kotlin.fir.resolve import org.jetbrains.kotlin.fir.declarations.FirDeclarationStatus +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef sealed class ResolutionMode { object ContextDependent : ResolutionMode() @@ -23,5 +25,12 @@ sealed class ResolutionMode { fun withExpectedType(expectedTypeRef: FirTypeRef?): ResolutionMode = expectedTypeRef?.let { ResolutionMode.WithExpectedType(it) } ?: ResolutionMode.ContextDependent +fun withExpectedType(coneType: ConeKotlinType): ResolutionMode { + val typeRef = buildResolvedTypeRef { + type = coneType + } + return ResolutionMode.WithExpectedType(typeRef) +} + fun FirDeclarationStatus.mode(): ResolutionMode = - ResolutionMode.WithStatus(this) \ No newline at end of file + ResolutionMode.WithStatus(this) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt index 248be29f27e..9ace81aa110 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirControlFlowStatementsResolveTransformer.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference import org.jetbrains.kotlin.fir.resolve.ResolutionMode import org.jetbrains.kotlin.fir.resolve.transformers.FirSyntheticCallGenerator import org.jetbrains.kotlin.fir.resolve.transformers.FirWhenExhaustivenessTransformer +import org.jetbrains.kotlin.fir.resolve.withExpectedType import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef @@ -114,8 +115,10 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirBodyResolveTran override fun transformWhenBranch(whenBranch: FirWhenBranch, data: ResolutionMode): CompositeTransformResult { return whenBranch.also { dataFlowAnalyzer.enterWhenBranchCondition(whenBranch) } - .transformCondition(transformer, data).also { dataFlowAnalyzer.exitWhenBranchCondition(it) } - .transformResult(transformer, data).also { dataFlowAnalyzer.exitWhenBranchResult(it) } + .transformCondition(transformer, withExpectedType(session.builtinTypes.booleanType)) + .also { dataFlowAnalyzer.exitWhenBranchCondition(it) } + .transformResult(transformer, data) + .also { dataFlowAnalyzer.exitWhenBranchResult(it) } .compose() } diff --git a/compiler/testData/codegen/box/inference/kt42130.kt b/compiler/testData/codegen/box/inference/kt42130.kt new file mode 100644 index 00000000000..63617e05034 --- /dev/null +++ b/compiler/testData/codegen/box/inference/kt42130.kt @@ -0,0 +1,21 @@ +// ISSUE: KT-42130 + +interface A +interface B : A + +fun B.foo(): Boolean = true +fun run(action: () -> T): T = action() + +fun foo(a: A): String { + return when (a) { + is B -> when { + run { a.foo() } -> "OK" + else -> "Fail 1" + } + else -> "Fail 2" + } +} + +class C : B + +fun box(): String = foo(C()) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 3d3871051bb..cba207c3b9a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -14574,6 +14574,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index ff50721023b..a586ed8ef99 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14574,6 +14574,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 2f32b8765e7..326a99bcf77 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -13179,6 +13179,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 9ce50970d08..aefe96a0841 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -11319,6 +11319,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 7b4324ec2df..ff1e4b82961 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -11319,6 +11319,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 8db9e2552da..f082534769e 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -11384,6 +11384,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inference/kt39824.kt"); } + @TestMetadata("kt42130.kt") + public void testKt42130() throws Exception { + runTest("compiler/testData/codegen/box/inference/kt42130.kt"); + } + @TestMetadata("lambdasWithExtensionFunctionType.kt") public void testLambdasWithExtensionFunctionType() throws Exception { runTest("compiler/testData/codegen/box/inference/lambdasWithExtensionFunctionType.kt");