Skip to content

Commit 5503b93

Browse files
authored
[K1] Handle exception for recursive type aliases (#3582)
Updated the `DefaultDescriptorToDocumentableTranslator` to avoid throwing an exception when encountering recursive type aliases. The changes ensure that the `defaultType` is safely accessed. Also, added a new test for this scenario to avoid regression in the future.
1 parent f1ea0c9 commit 5503b93

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

dokka-subprojects/analysis-kotlin-descriptors-compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/translator/DefaultDescriptorToDocumentableTranslator.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,15 +840,20 @@ private class DokkaDescriptorVisitor(
840840
private suspend fun visitTypeAliasDescriptor(descriptor: TypeAliasDescriptor) =
841841
with(descriptor) {
842842
coroutineScope {
843+
// `defaultType` can throw an exception for a recursive typealias A = A
844+
// for more details see https://github.com/Kotlin/dokka/issues/3565
845+
val defaultType = runCatching { defaultType }.getOrNull()
843846
val generics = async { descriptor.declaredTypeParameters.parallelMap { it.toVariantTypeParameter() } }
844-
val info = buildAncestryInformation(defaultType).copy(
845-
superclass = buildAncestryInformation(underlyingType),
846-
interfaces = emptyList()
847-
)
847+
val info = defaultType?.let {
848+
buildAncestryInformation(it).copy(
849+
superclass = buildAncestryInformation(underlyingType),
850+
interfaces = emptyList()
851+
)
852+
}
848853
DTypeAlias(
849854
dri = DRI.from(this@with),
850855
name = name.asString(),
851-
type = defaultType.toBound(),
856+
type = defaultType?.toBound() ?: UnresolvedBound(name.asString()),
852857
expectPresentInSet = null,
853858
underlyingType = underlyingType.toBound().toSourceSetDependent(),
854859
visibility = visibility.toDokkaVisibility().toSourceSetDependent(),
@@ -858,7 +863,7 @@ private class DokkaDescriptorVisitor(
858863
sources = descriptor.createSources(),
859864
extra = PropertyContainer.withAll(
860865
descriptor.getAnnotations().toSourceSetDependent().toAnnotations(),
861-
info.exceptionInSupertypesOrNull(),
866+
info?.exceptionInSupertypesOrNull(),
862867
)
863868
)
864869
}

dokka-subprojects/plugin-base/src/test/kotlin/translators/DefaultDescriptorToDocumentableTranslatorTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
package translators
66

77
import org.jetbrains.dokka.DokkaConfiguration
8+
import org.jetbrains.dokka.Platform
89
import org.jetbrains.dokka.analysis.kotlin.markdown.MARKDOWN_ELEMENT_FILE_NAME
910
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.modifiers
1011
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
1112
import org.jetbrains.dokka.links.DRI
1213
import org.jetbrains.dokka.links.PointingToDeclaration
1314
import org.jetbrains.dokka.model.*
1415
import org.jetbrains.dokka.model.doc.*
16+
import utils.OnlyDescriptors
1517
import utils.text
1618
import kotlin.test.*
1719

@@ -1076,6 +1078,38 @@ val soapXml = node("soap-env:Envelope", soapAttrs,
10761078
}
10771079
}
10781080
}
1081+
1082+
@Test
1083+
@OnlyDescriptors("In K2 the types of recursive typealias is resolved")
1084+
fun `a translator should not fail for a recursive typealias A = A #3565`() {
1085+
val configuration = dokkaConfiguration {
1086+
sourceSets {
1087+
sourceSet {
1088+
name = "androidJvm"
1089+
analysisPlatform = Platform.common.key // an androidJvm source set has a common platform
1090+
sourceRoots = listOf("src/main/kotlin")
1091+
classpath = listOf(commonStdlibPath!!)
1092+
}
1093+
}
1094+
}
1095+
// `java.io.File` is unavailable in a common platform
1096+
// so `typealias File = File` is recursive
1097+
testInline(
1098+
"""
1099+
|/src/main/kotlin/test/typealias.jvmAndAndroid.kt
1100+
|package test
1101+
|
1102+
|import java.io.File
1103+
|typealias File = File
1104+
""".trimIndent(),
1105+
configuration
1106+
) {
1107+
documentablesMergingStage = { module ->
1108+
val ta = module.dfs { it.name == "File" } as DTypeAlias
1109+
assertTrue { ta.type is UnresolvedBound }
1110+
}
1111+
}
1112+
}
10791113
}
10801114

10811115
private sealed class TestSuite {

0 commit comments

Comments
 (0)