From f45de9d8fb36dba1694bd026a60a369fbd42f6e7 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 4 Aug 2020 16:03:59 +0300 Subject: [PATCH] NI: approximate not top-level captured types during code generation ^KT-40693 Fixed --- .../jetbrains/kotlin/codegen/ExpressionCodegen.java | 2 +- .../codegen/ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../approximateNonTopLevelCapturedTypes.kt | 13 +++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../semantics/IrJsCodegenBoxES6TestGenerated.java | 10 ++++++++++ .../ir/semantics/IrJsCodegenBoxTestGenerated.java | 10 ++++++++++ .../test/semantics/JsCodegenBoxTestGenerated.java | 10 ++++++++++ 9 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index dd2a4f9e2ae..9f51754270b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2835,7 +2835,7 @@ public class ExpressionCodegen extends KtVisitor impleme TypeApproximator approximator = new TypeApproximator(state.getModule().getBuiltIns()); KotlinType approximatedType = - CapturedTypeConstructorKt.isCaptured(type) ? + TypeUtils.contains(type, (containedType) -> CapturedTypeConstructorKt.isCaptured(containedType)) ? (KotlinType) approximator.approximateToSuperType( type, TypeApproximatorConfiguration.InternalTypesApproximation.INSTANCE ) : null; 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 2c345588754..463e5c1b740 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 @@ -12414,6 +12414,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInference.kt") public void testBuilderInference() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference.kt"); diff --git a/compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt b/compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt new file mode 100644 index 00000000000..7f55ced5e07 --- /dev/null +++ b/compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt @@ -0,0 +1,13 @@ +interface BasePublisher + +interface Flow + +fun asFlow(x: BasePublisher): Flow = null as Flow + +class Document + +interface DerivedPublisher: BasePublisher> {} + +class Foo(val x: DerivedPublisher) : Flow> by asFlow(x) + +fun box() = "OK" \ 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 1fa96890f47..1bf197919d3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -13639,6 +13639,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInference.kt") public void testBuilderInference() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 7c4b6261449..7368dccaa72 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -13639,6 +13639,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInference.kt") public void testBuilderInference() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 64807b760fd..4a52ed803a9 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -12414,6 +12414,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInference.kt") public void testBuilderInference() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference.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 f6734fee0a3..2b3f2d75828 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 @@ -41,6 +41,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/annotations"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true); } + @TestMetadata("annotatedObjectLiteral.kt") + public void testAnnotatedObjectLiteral() throws Exception { + runTest("compiler/testData/codegen/box/annotations/annotatedObjectLiteral.kt"); + } + @TestMetadata("nestedAnnotation.kt") public void testNestedAnnotation() throws Exception { runTest("compiler/testData/codegen/box/annotations/nestedAnnotation.kt"); @@ -10689,6 +10694,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInferenceLeakingVariable.kt") public void testBuilderInferenceLeakingVariable() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInferenceLeakingVariable.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 3949f5b3dcb..15a7d4accaa 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 @@ -41,6 +41,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/annotations"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } + @TestMetadata("annotatedObjectLiteral.kt") + public void testAnnotatedObjectLiteral() throws Exception { + runTest("compiler/testData/codegen/box/annotations/annotatedObjectLiteral.kt"); + } + @TestMetadata("nestedAnnotation.kt") public void testNestedAnnotation() throws Exception { runTest("compiler/testData/codegen/box/annotations/nestedAnnotation.kt"); @@ -10689,6 +10694,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInferenceLeakingVariable.kt") public void testBuilderInferenceLeakingVariable() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInferenceLeakingVariable.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 023d525705b..24a81894edd 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 @@ -41,6 +41,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/annotations"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } + @TestMetadata("annotatedObjectLiteral.kt") + public void testAnnotatedObjectLiteral() throws Exception { + runTest("compiler/testData/codegen/box/annotations/annotatedObjectLiteral.kt"); + } + @TestMetadata("nestedAnnotation.kt") public void testNestedAnnotation() throws Exception { runTest("compiler/testData/codegen/box/annotations/nestedAnnotation.kt"); @@ -10754,6 +10759,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } + @TestMetadata("approximateNonTopLevelCapturedTypes.kt") + public void testApproximateNonTopLevelCapturedTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/approximateNonTopLevelCapturedTypes.kt"); + } + @TestMetadata("builderInferenceLeakingVariable.kt") public void testBuilderInferenceLeakingVariable() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInferenceLeakingVariable.kt");