From fa0f9a920168021212407b051c42b92f97ad8e46 Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Tue, 11 Apr 2023 13:48:47 +0000 Subject: [PATCH] [K/JS] Change strategy for implicitly exported declarations if there is a cycled reference ^KT-57356 Fixed --- .../ir/backend/js/export/ExportModelGenerator.kt | 8 ++++++-- .../implicit-export/implicit-export.d.ts | 14 ++++++++++++++ .../implicit-export/implicit-export.kt | 15 ++++++++++++++- .../strict-implicit-export.d.ts | 14 ++++++++++++++ .../strict-implicit-export.kt | 13 +++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt index 43c07206b62..42750a06303 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/export/ExportModelGenerator.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.ir.backend.js.export import org.jetbrains.kotlin.backend.common.ir.isExpect -import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.DescriptorVisibilities @@ -556,13 +555,17 @@ class ExportModelGenerator(val context: JsIrBackendContext, val generateNamespac return this } + private val currentlyProcessedTypes = hashSetOf() + private fun exportType(type: IrType, shouldCalculateExportedSupertypeForImplicit: Boolean = true): ExportedType { - if (type is IrDynamicType) + if (type is IrDynamicType || type in currentlyProcessedTypes) return ExportedType.Primitive.Any if (type !is IrSimpleType) return ExportedType.ErrorType("NonSimpleType ${type.render()}") + currentlyProcessedTypes.add(type) + val classifier = type.classifier val isMarkedNullable = type.isMarkedNullable() val nonNullType = type.makeNotNull() as IrSimpleType @@ -632,6 +635,7 @@ class ExportModelGenerator(val context: JsIrBackendContext, val generateNamespac } return exportedType.withNullability(isMarkedNullable) + .also { currentlyProcessedTypes.remove(type) } } private fun IrDeclarationWithName.getExportedIdentifier(): String = diff --git a/js/js.translator/testData/typescript-export/implicit-export/implicit-export.d.ts b/js/js.translator/testData/typescript-export/implicit-export/implicit-export.d.ts index 5c59bada09e..b258855feb4 100644 --- a/js/js.translator/testData/typescript-export/implicit-export/implicit-export.d.ts +++ b/js/js.translator/testData/typescript-export/implicit-export/implicit-export.d.ts @@ -45,5 +45,19 @@ declare namespace JS_TESTS { class TheNewException extends Error { constructor(); } + interface Service, TEvent extends foo.Event> { + readonly __doNotUseOrImplementIt: { + readonly "foo.Service": unique symbol; + }; + } + interface Event> { + readonly __doNotUseOrImplementIt: { + readonly "foo.Event": unique symbol; + }; + } + class SomeServiceRequest implements foo.Service/* foo.SomeEvent */> { + constructor(); + readonly __doNotUseOrImplementIt: foo.Service/* foo.SomeEvent */>["__doNotUseOrImplementIt"]; + } } } diff --git a/js/js.translator/testData/typescript-export/implicit-export/implicit-export.kt b/js/js.translator/testData/typescript-export/implicit-export/implicit-export.kt index 1be9bd11fff..a24a4a211c9 100644 --- a/js/js.translator/testData/typescript-export/implicit-export/implicit-export.kt +++ b/js/js.translator/testData/typescript-export/implicit-export/implicit-export.kt @@ -95,4 +95,17 @@ fun functionWithTypeAliasInside(x: NotExportedTypeAlias): NotExportedTypeAlias { } @JsExport -class TheNewException: Throwable() \ No newline at end of file +class TheNewException: Throwable() + +// Recursive definition KT-57356 +@JsExport +interface Service, in TEvent : Event> + +@JsExport +interface Event> + +class SomeService : Service +class SomeEvent : Event + +@JsExport +class SomeServiceRequest : Service diff --git a/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.d.ts b/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.d.ts index e9be5f3539a..0d9620f11e6 100644 --- a/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.d.ts +++ b/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.d.ts @@ -79,6 +79,20 @@ declare namespace JS_TESTS { } function acceptForthLike(forth: T): void; function acceptMoreGenericForthLike(forth: T): void; + interface Service, TEvent extends foo.Event> { + readonly __doNotUseOrImplementIt: { + readonly "foo.Service": unique symbol; + }; + } + interface Event> { + readonly __doNotUseOrImplementIt: { + readonly "foo.Event": unique symbol; + }; + } + class SomeServiceRequest implements foo.Service/* foo.SomeEvent */> { + constructor(); + readonly __doNotUseOrImplementIt: foo.Service/* foo.SomeEvent */>["__doNotUseOrImplementIt"]; + } interface NonExportedParent { readonly __doNotUseOrImplementIt: { readonly "foo.NonExportedParent": unique symbol; diff --git a/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.kt b/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.kt index fdfab4ccd53..79f36a403b3 100644 --- a/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.kt +++ b/js/js.translator/testData/typescript-export/strict-implicit-export/strict-implicit-export.kt @@ -167,3 +167,16 @@ fun acceptMoreGenericForthLike(forth: T) where T: IB, T: IC, T: Third {} @JsExport val forth = Forth() + +// Recursive definition KT-57356 +@JsExport +interface Service, in TEvent : Event> + +@JsExport +interface Event> + +class SomeService : Service +class SomeEvent : Event + +@JsExport +class SomeServiceRequest : Service