From 7e6abffb625110b87ab7898c19885e07dd3d3380 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Thu, 28 Jan 2021 18:15:15 +0300 Subject: [PATCH] FIR: Fix incorrectly serialized type See the test added: there's a non-denotable T!! type inside flexible type that wasn't handled before. ConeKotlinType::contains handles flexible types content and some other cases Also, it has better asymptotics --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++ .../jetbrains/kotlin/fir/types/TypeUtils.kt | 14 +---- ...tiModuleJsCompilerRunnerTestGenerated.java | 2 +- ...duleJsKlibCompilerRunnerTestGenerated.java | 2 +- .../approximationForDefinitelyNotNull.kt | 57 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++ .../LightAnalysisModeTestGenerated.java | 5 ++ ...ileToSourceStubConverterTestGenerated.java | 1 - ...ileToSourceStubConverterTestGenerated.java | 1 - 10 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 7e2ca5a9370..48f5ada576c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -33314,6 +33314,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/regressions/approximateIntersectionType.kt"); } + @Test + @TestMetadata("approximationForDefinitelyNotNull.kt") + public void testApproximationForDefinitelyNotNull() throws Exception { + runTest("compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt"); + } + @Test @TestMetadata("arrayLengthNPE.kt") public void testArrayLengthNPE() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index 5faf26003be..7b2e341b402 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.classId import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic -import org.jetbrains.kotlin.fir.expressions.classId import org.jetbrains.kotlin.fir.fakeElement import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.resolve.fullyExpandedType @@ -270,17 +269,8 @@ fun FirTypeRef.approximatedForPublicPosition(approximator: AbstractTypeApproxima else this -private fun ConeKotlinType.requiresApproximationInPublicPosition(): Boolean { - return when (this) { - is ConeIntegerLiteralType, - is ConeCapturedType, - is ConeDefinitelyNotNullType, - is ConeIntersectionType -> true - is ConeClassLikeType -> typeArguments.any { - it is ConeKotlinTypeProjection && it.type.requiresApproximationInPublicPosition() - } - else -> false - } +private fun ConeKotlinType.requiresApproximationInPublicPosition(): Boolean = contains { + it is ConeIntegerLiteralType || it is ConeCapturedType || it is ConeDefinitelyNotNullType || it is ConeIntersectionType } /* diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsCompilerRunnerTestGenerated.java b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsCompilerRunnerTestGenerated.java index d87b986749e..1c1a8920b15 100644 --- a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsCompilerRunnerTestGenerated.java +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsCompilerRunnerTestGenerated.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsKlibCompilerRunnerTestGenerated.java b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsKlibCompilerRunnerTestGenerated.java index 945baa3bb5d..48018e4d2a8 100644 --- a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsKlibCompilerRunnerTestGenerated.java +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalMultiModuleJsKlibCompilerRunnerTestGenerated.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ diff --git a/compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt b/compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt new file mode 100644 index 00000000000..998f226ac30 --- /dev/null +++ b/compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt @@ -0,0 +1,57 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +// MODULE: lib +// FILE: CachedValuesManager.java +import org.jetbrains.annotations.NotNull; +public class CachedValuesManager { + public @NotNull CachedValue createCachedValue(final @NotNull CachedValueProvider provider) { + return new CachedValue() { + public T getValue() { + return provider.compute().value; + } + }; + } +} + +// FILE: CachedValueProvider.java +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; + +public interface CachedValueProvider { + @Nullable + Result compute(); + + class Result { + public final T value; + public Result(@Nullable T value) { + this.value = value; + } + } +} + +// FILE: CachedValue.java +public interface CachedValue { + T getValue(); +} + +// FILE: lib.kt + +// Inferred as CachedValue>! and T!! should be approximated +fun cachedValue(manager: CachedValuesManager, createValue: () -> T) = + manager.createCachedValue { + CachedValueProvider.Result( + createValue() + ) + } + +// MODULE: main(lib) +// FILE: main.kt + +fun box(): String { + val value = cachedValue(CachedValuesManager()) { Pair("O", "K") }.value + + return value.first + value.second +} 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 ffb1a202405..29dde57cd24 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 @@ -33514,6 +33514,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/regressions/approximateIntersectionType.kt"); } + @Test + @TestMetadata("approximationForDefinitelyNotNull.kt") + public void testApproximationForDefinitelyNotNull() throws Exception { + runTest("compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt"); + } + @Test @TestMetadata("arrayLengthNPE.kt") public void testArrayLengthNPE() 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 b3d0bb8aa11..18fedd1f27f 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 @@ -33314,6 +33314,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/regressions/approximateIntersectionType.kt"); } + @Test + @TestMetadata("approximationForDefinitelyNotNull.kt") + public void testApproximationForDefinitelyNotNull() throws Exception { + runTest("compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt"); + } + @Test @TestMetadata("arrayLengthNPE.kt") public void testArrayLengthNPE() 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 25047ce0e46..d8e5fe91f0b 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -27298,6 +27298,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/regressions/approximateIntersectionType.kt"); } + @TestMetadata("approximationForDefinitelyNotNull.kt") + public void testApproximationForDefinitelyNotNull() throws Exception { + runTest("compiler/testData/codegen/box/regressions/approximationForDefinitelyNotNull.kt"); + } + @TestMetadata("arrayLengthNPE.kt") public void testArrayLengthNPE() throws Exception { runTest("compiler/testData/codegen/box/regressions/arrayLengthNPE.kt"); diff --git a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java index 3a738140417..5c4b619d774 100644 --- a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java @@ -74,7 +74,6 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi runTest("plugins/kapt3/kapt3-compiler/testData/converter/comments.kt"); } - /** Regression test for KT-43593. */ @TestMetadata("commentsRemoved.kt") public void testCommentsRemoved() throws Exception { runTest("plugins/kapt3/kapt3-compiler/testData/converter/commentsRemoved.kt"); diff --git a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/IrClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/IrClassFileToSourceStubConverterTestGenerated.java index f1e2da8eca4..d9de74b45ed 100644 --- a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/IrClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/IrClassFileToSourceStubConverterTestGenerated.java @@ -75,7 +75,6 @@ public class IrClassFileToSourceStubConverterTestGenerated extends AbstractIrCla runTest("plugins/kapt3/kapt3-compiler/testData/converter/comments.kt"); } - /** Regression test for KT-43593. */ @TestMetadata("commentsRemoved.kt") public void testCommentsRemoved() throws Exception { runTest("plugins/kapt3/kapt3-compiler/testData/converter/commentsRemoved.kt");