Do not force coercion to Unit for nullable lambda return type
#KT-41005 Fixed
This commit is contained in:
Generated
+10
@@ -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");
|
||||
|
||||
+2
-1
@@ -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
|
||||
}
|
||||
|
||||
Vendored
+22
@@ -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"
|
||||
}
|
||||
+7
@@ -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"
|
||||
}
|
||||
+10
@@ -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");
|
||||
|
||||
+10
@@ -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");
|
||||
|
||||
+10
@@ -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");
|
||||
|
||||
Generated
+5
@@ -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");
|
||||
|
||||
Generated
+5
@@ -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");
|
||||
|
||||
+5
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user