Skip to content

Commit

Permalink
[K1] Handle exception for recursive type aliases (#3582)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vmishenev committed Apr 25, 2024
1 parent f1ea0c9 commit 5503b93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Expand Up @@ -840,15 +840,20 @@ private class DokkaDescriptorVisitor(
private suspend fun visitTypeAliasDescriptor(descriptor: TypeAliasDescriptor) =
with(descriptor) {
coroutineScope {
// `defaultType` can throw an exception for a recursive typealias A = A
// for more details see https://github.com/Kotlin/dokka/issues/3565
val defaultType = runCatching { defaultType }.getOrNull()
val generics = async { descriptor.declaredTypeParameters.parallelMap { it.toVariantTypeParameter() } }
val info = buildAncestryInformation(defaultType).copy(
superclass = buildAncestryInformation(underlyingType),
interfaces = emptyList()
)
val info = defaultType?.let {
buildAncestryInformation(it).copy(
superclass = buildAncestryInformation(underlyingType),
interfaces = emptyList()
)
}
DTypeAlias(
dri = DRI.from(this@with),
name = name.asString(),
type = defaultType.toBound(),
type = defaultType?.toBound() ?: UnresolvedBound(name.asString()),
expectPresentInSet = null,
underlyingType = underlyingType.toBound().toSourceSetDependent(),
visibility = visibility.toDokkaVisibility().toSourceSetDependent(),
Expand All @@ -858,7 +863,7 @@ private class DokkaDescriptorVisitor(
sources = descriptor.createSources(),
extra = PropertyContainer.withAll(
descriptor.getAnnotations().toSourceSetDependent().toAnnotations(),
info.exceptionInSupertypesOrNull(),
info?.exceptionInSupertypesOrNull(),
)
)
}
Expand Down
Expand Up @@ -5,13 +5,15 @@
package translators

import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.analysis.kotlin.markdown.MARKDOWN_ELEMENT_FILE_NAME
import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.modifiers
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.PointingToDeclaration
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.doc.*
import utils.OnlyDescriptors
import utils.text
import kotlin.test.*

Expand Down Expand Up @@ -1076,6 +1078,38 @@ val soapXml = node("soap-env:Envelope", soapAttrs,
}
}
}

@Test
@OnlyDescriptors("In K2 the types of recursive typealias is resolved")
fun `a translator should not fail for a recursive typealias A = A #3565`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
name = "androidJvm"
analysisPlatform = Platform.common.key // an androidJvm source set has a common platform
sourceRoots = listOf("src/main/kotlin")
classpath = listOf(commonStdlibPath!!)
}
}
}
// `java.io.File` is unavailable in a common platform
// so `typealias File = File` is recursive
testInline(
"""
|/src/main/kotlin/test/typealias.jvmAndAndroid.kt
|package test
|
|import java.io.File
|typealias File = File
""".trimIndent(),
configuration
) {
documentablesMergingStage = { module ->
val ta = module.dfs { it.name == "File" } as DTypeAlias
assertTrue { ta.type is UnresolvedBound }
}
}
}
}

private sealed class TestSuite {
Expand Down

0 comments on commit 5503b93

Please sign in to comment.