[FIR] Transform when conditions with boolean expected type

#KT-42130 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-09-24 11:35:07 +03:00
parent 68a47d6efb
commit 0d29d6d361
10 changed files with 71 additions and 3 deletions
@@ -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");
@@ -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)
ResolutionMode.WithStatus(this)
@@ -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<FirWhenBranch> {
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()
}
+21
View File
@@ -0,0 +1,21 @@
// ISSUE: KT-42130
interface A
interface B : A
fun B.foo(): Boolean = true
fun <T> 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())
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");