From 0d464f06d1cf28da672158433aafe1c19ec0925d Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 3 Jul 2017 13:44:01 +0300 Subject: [PATCH] [NI] Consider CS as nullable if one of the types isn't subtype of `Any` Note that this isn't fully correct, consider the following situation: S : T, T : Any? => CS(S, T) = T, but for now it will be T?, which is reliable but not so specific as just T --- .../resolve/calls/NewCommonSuperTypeCalculator.kt | 11 ++++------- .../nullabilityForCommonCapturedSupertypes.kt | 14 ++++++++++++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/LightAnalysisModeTestGenerated.java | 6 ++++++ .../test/semantics/JsCodegenBoxTestGenerated.java | 6 ++++++ 6 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index c753446d3ac..539e8174ad1 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -19,10 +19,7 @@ package org.jetbrains.kotlin.resolve.calls import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.types.* -import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker -import org.jetbrains.kotlin.types.checker.TypeCheckerContext -import org.jetbrains.kotlin.types.checker.anySuperTypeConstructor -import org.jetbrains.kotlin.types.checker.intersectTypes +import org.jetbrains.kotlin.types.checker.* import org.jetbrains.kotlin.types.typeUtil.asTypeProjection object NewCommonSuperTypeCalculator { @@ -56,12 +53,12 @@ object NewCommonSuperTypeCalculator { private fun commonSuperTypeForSimpleTypes(types: List): SimpleType { // i.e. result type also should be marked nullable - val anyMarkedNullable = types.any { it.isMarkedNullable } - val notNullTypes = if (anyMarkedNullable) types.map { it.makeNullableAsSpecified(false) } else types + val notAllNotNull = types.any { !NullabilityChecker.isSubtypeOfAny(it) } + val notNullTypes = if (notAllNotNull) types.map { it.makeNullableAsSpecified(false) } else types val commonSuperTypes = commonSuperTypeForNotNullTypes(notNullTypes) - return if (anyMarkedNullable) commonSuperTypes.makeNullableAsSpecified(true) else commonSuperTypes + return if (notAllNotNull) commonSuperTypes.makeNullableAsSpecified(true) else commonSuperTypes } private fun List.uniquify(): List { diff --git a/compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt b/compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt new file mode 100644 index 00000000000..336e9fa73a7 --- /dev/null +++ b/compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt @@ -0,0 +1,14 @@ +val targetArgument = id2(star(), star()) // error + +fun id2(x: T, y: T): T = x + +fun star(): Sample<*> { + return Sample() +} + +class Sample + +fun box(): String { + targetArgument + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index d5fd252dc3b..af6a71ec609 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -16928,6 +16928,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("nullabilityForCommonCapturedSupertypes.kt") + public void testNullabilityForCommonCapturedSupertypes() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b4309dd8d18..66cb3556ba1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -16928,6 +16928,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("nullabilityForCommonCapturedSupertypes.kt") + public void testNullabilityForCommonCapturedSupertypes() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a83130a0531..b989687e0e4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16928,6 +16928,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("nullabilityForCommonCapturedSupertypes.kt") + public void testNullabilityForCommonCapturedSupertypes() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.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 12e30b61877..0250749457d 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 @@ -20702,6 +20702,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); } + @TestMetadata("nullabilityForCommonCapturedSupertypes.kt") + public void testNullabilityForCommonCapturedSupertypes() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/nullabilityForCommonCapturedSupertypes.kt"); + doTest(fileName); + } + @TestMetadata("objectCaptureOuterConstructorProperty.kt") public void testObjectCaptureOuterConstructorProperty() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectCaptureOuterConstructorProperty.kt");