From aafe41cf7a01d1a7d276cb936336649f370a23ca Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 11 Aug 2020 23:17:01 +0300 Subject: [PATCH] Do not force coercion to Unit for nullable lambda return type #KT-41005 Fixed --- .../ir/FirBlackBoxCodegenTestGenerated.java | 10 +++++++++ .../components/PostponedArgumentsAnalyzer.kt | 3 ++- ...rLambdaReturnTypeWithFlexibleConstraint.kt | 22 +++++++++++++++++++ ...oercionToUniForNullableLambdaReturnType.kt | 7 ++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 +++++++++ .../LightAnalysisModeTestGenerated.java | 10 +++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 +++++++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 10 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt create mode 100644 compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt 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 dabdf90aeea..3c2ee813f72 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 @@ -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"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt index 2a82e15663f..17ab0a172bc 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.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 } diff --git a/compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt b/compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt new file mode 100644 index 00000000000..508097cad2a --- /dev/null +++ b/compiler/testData/codegen/box/inference/coercionToUnitForLambdaReturnTypeWithFlexibleConstraint.kt @@ -0,0 +1,22 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM + +// FILE: TestJ.java + +public class TestJ { + public static In materialize() { + return null; + } +} + +// FILE: test.kt + +class In + +fun inferred(e: In?, l: () -> T): T = l() + +fun box(): String { + // coercion to Unit for T! + val inferred = (inferred(TestJ.materialize(), { null })).toString() + return if (inferred == "kotlin.Unit") "OK" else "fail : $inferred" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt b/compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt new file mode 100644 index 00000000000..cfe86b99a58 --- /dev/null +++ b/compiler/testData/codegen/box/inference/noCoercionToUniForNullableLambdaReturnType.kt @@ -0,0 +1,7 @@ +fun nullableK(m: () -> T?) = m() + +fun box(): String { + // no coercion to Unit for T? + val nullableK = (nullableK { null }).toString() + return if (nullableK == "null") "OK" else "fail: $nullableK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index f2026a05ad6..b2aca54122f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 4e553f9cb2c..a8706a3e18c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 467882ce3b5..1259ee15c24 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -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"); 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 af7ec258d8b..d8da68b5abc 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 @@ -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"); 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 4c4ba8c4223..57700872b9d 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 @@ -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"); 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 b714de2082a..471823fa815 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 @@ -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");