Do not force coercion to Unit for nullable lambda return type

#KT-41005 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2020-08-11 23:17:01 +03:00
parent d9bac4d5e4
commit aafe41cf7a
10 changed files with 86 additions and 1 deletions
@@ -12469,6 +12469,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt");
}
@TestMetadata("coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt")
public void testCoercionToUnitForLambdaReturnTypeWithFlexibleConstraint() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt");
}
@TestMetadata("coercionToUnitWithLastLambdaExpression.kt")
public void testCoercionToUnitWithLastLambdaExpression() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitWithLastLambdaExpression.kt");
@@ -12529,6 +12534,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("noNothingValueInsideSpecialCall.kt")
public void testNoNothingValueInsideSpecialCall() throws Exception {
runTest("compiler/testData/codegen/box/inference/noNothingValueInsideSpecialCall.kt");
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.CoroutinePosition
import org.jetbrains.kotlin.resolve.calls.inference.model.LambdaArgumentConstraintPosition
@@ -122,7 +123,7 @@ class PostponedArgumentsAnalyzer(
c.canBeProper(rawReturnType) -> substitute(rawReturnType)
// For Unit-coercion
c.hasUpperOrEqualUnitConstraint(rawReturnType) -> builtIns.unitType
!rawReturnType.isMarkedNullable && c.hasUpperOrEqualUnitConstraint(rawReturnType) -> builtIns.unitType
else -> null
}
@@ -0,0 +1,22 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// FILE: TestJ.java
public class TestJ {
public static <T> In<T> materialize() {
return null;
}
}
// FILE: test.kt
class In<in T>
fun <T> inferred(e: In<T>?, l: () -> T): T = l()
fun box(): String {
// coercion to Unit for T!
val inferred = (inferred(TestJ.materialize<Unit>(), { null })).toString()
return if (inferred == "kotlin.Unit") "OK" else "fail : $inferred"
}
@@ -0,0 +1,7 @@
fun <T> nullableK(m: () -> T?) = m()
fun box(): String {
// no coercion to Unit for T?
val nullableK = (nullableK<Unit> { null }).toString()
return if (nullableK == "null") "OK" else "fail: $nullableK"
}
@@ -13694,6 +13694,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt");
}
@TestMetadata("coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt")
public void testCoercionToUnitForLambdaReturnTypeWithFlexibleConstraint() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt");
}
@TestMetadata("coercionToUnitWithLastLambdaExpression.kt")
public void testCoercionToUnitWithLastLambdaExpression() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitWithLastLambdaExpression.kt");
@@ -13754,6 +13759,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("noNothingValueInsideSpecialCall.kt")
public void testNoNothingValueInsideSpecialCall() throws Exception {
runTest("compiler/testData/codegen/box/inference/noNothingValueInsideSpecialCall.kt");
@@ -13694,6 +13694,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt");
}
@TestMetadata("coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt")
public void testCoercionToUnitForLambdaReturnTypeWithFlexibleConstraint() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt");
}
@TestMetadata("coercionToUnitWithLastLambdaExpression.kt")
public void testCoercionToUnitWithLastLambdaExpression() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitWithLastLambdaExpression.kt");
@@ -13754,6 +13759,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("noNothingValueInsideSpecialCall.kt")
public void testNoNothingValueInsideSpecialCall() throws Exception {
runTest("compiler/testData/codegen/box/inference/noNothingValueInsideSpecialCall.kt");
@@ -12469,6 +12469,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt");
}
@TestMetadata("coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt")
public void testCoercionToUnitForLambdaReturnTypeWithFlexibleConstraint() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt");
}
@TestMetadata("coercionToUnitWithLastLambdaExpression.kt")
public void testCoercionToUnitWithLastLambdaExpression() throws Exception {
runTest("compiler/testData/codegen/box/inference/coercionToUnitWithLastLambdaExpression.kt");
@@ -12529,6 +12534,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("noNothingValueInsideSpecialCall.kt")
public void testNoNothingValueInsideSpecialCall() throws Exception {
runTest("compiler/testData/codegen/box/inference/noNothingValueInsideSpecialCall.kt");
@@ -10784,6 +10784,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("plusAssignInsideLambda.kt")
public void testPlusAssignInsideLambda() throws Exception {
runTest("compiler/testData/codegen/box/inference/plusAssignInsideLambda.kt");
@@ -10784,6 +10784,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("plusAssignInsideLambda.kt")
public void testPlusAssignInsideLambda() throws Exception {
runTest("compiler/testData/codegen/box/inference/plusAssignInsideLambda.kt");
@@ -10849,6 +10849,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inference/lastExpressionOfLambdaWithNothingConstraint.kt");
}
@TestMetadata("noCoercionToUniForNullableLambdaReturnType.kt")
public void testNoCoercionToUniForNullableLambdaReturnType() throws Exception {
runTest("compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt");
}
@TestMetadata("plusAssignInsideLambda.kt")
public void testPlusAssignInsideLambda() throws Exception {
runTest("compiler/testData/codegen/box/inference/plusAssignInsideLambda.kt");