From d61cef091563e4c2570be54f9ff87ee957dfbb92 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Fri, 9 Dec 2022 18:14:42 +0100 Subject: [PATCH] Fix deserialization exception for DNN types from Java useCorrectedNullabilityForTypeParameters = true only might lead to something becomes a DNN when otherwise it wasn't. It seems safe to use it here, since if compiler has generated DNN, then it's OK to assume that it checked necessary conditions, and it's likely that it had useCorrectedNullabilityForTypeParameters = true as well, there. Anyway, it looks saner than having an exception here. Also, we assume here that metadata leading to exception might only be generated with ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated LF (at least, we don't have contradicting evidences), thus it's mostly a preparations in case we decide to enable ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated in 1.9. ^KT-55357 Fixed ^KT-55388 Related ^KT-36770 Related --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++ .../box/javaInterop/delegationToJavaDnn.kt | 29 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../deserialization/TypeDeserializer.kt | 3 +- 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 3e5b62bf0ef7f..6ee15c928c0f1 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -28087,6 +28087,12 @@ public void testConflictingOverloadsForThrowableInheritors2() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/conflictingOverloadsForThrowableInheritors2.kt"); } + @Test + @TestMetadata("delegationToJavaDnn.kt") + public void testDelegationToJavaDnn() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt"); + } + @Test @TestMetadata("genericSamProjectedOut.kt") public void testGenericSamProjectedOut() throws Exception { diff --git a/compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt b/compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt new file mode 100644 index 0000000000000..9691aa792af84 --- /dev/null +++ b/compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt @@ -0,0 +1,29 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// LANGUAGE: +ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +// ISSUE: KT-55357 + +// MODULE: lib +// FILE: J.java +import org.jetbrains.annotations.NotNull; + +public interface J { + @NotNull + T foo(); +} + +// FILE: JImpl.java +public class JImpl implements J { + public T foo() { + return (T) "OK"; + } +} +// FILE: a.kt +class C(val x: J) : J by x + +// MODULE: main(lib) +// FILE: a.kt + +fun box(): String { + return C(JImpl()).foo() +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index a6fd1a1dc9004..8f6c68b2fa27b 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -27343,6 +27343,12 @@ public void testConflictingOverloadsForThrowableInheritors2() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/conflictingOverloadsForThrowableInheritors2.kt"); } + @Test + @TestMetadata("delegationToJavaDnn.kt") + public void testDelegationToJavaDnn() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt"); + } + @Test @TestMetadata("genericSamProjectedOut.kt") public void testGenericSamProjectedOut() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 961e0caf66e8c..94c78df7a2700 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -28087,6 +28087,12 @@ public void testConflictingOverloadsForThrowableInheritors2() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/conflictingOverloadsForThrowableInheritors2.kt"); } + @Test + @TestMetadata("delegationToJavaDnn.kt") + public void testDelegationToJavaDnn() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt"); + } + @Test @TestMetadata("genericSamProjectedOut.kt") public void testGenericSamProjectedOut() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d564a27777b87..e5a6ac904e7b0 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -23082,6 +23082,11 @@ public void testConflictingOverloadsForThrowableInheritors2() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/conflictingOverloadsForThrowableInheritors2.kt"); } + @TestMetadata("delegationToJavaDnn.kt") + public void testDelegationToJavaDnn() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/delegationToJavaDnn.kt"); + } + @TestMetadata("genericSamProjectedOut.kt") public void testGenericSamProjectedOut() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/genericSamProjectedOut.kt"); diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index 4064bcf1dd460..d0047ba3b42c1 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -126,7 +126,8 @@ class TypeDeserializer( else -> KotlinTypeFactory.simpleType(attributes, constructor, arguments, proto.nullable).let { if (Flags.DEFINITELY_NOT_NULL_TYPE.get(proto.flags)) - DefinitelyNotNullType.makeDefinitelyNotNull(it) ?: error("null DefinitelyNotNullType for '$it'") + DefinitelyNotNullType.makeDefinitelyNotNull(it, useCorrectedNullabilityForTypeParameters = true) + ?: error("null DefinitelyNotNullType for '$it'") else it }