Skip to content

Commit

Permalink
fix compilation for older Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr3zee committed Nov 29, 2024
1 parent 6d72d10 commit 7a7956f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ plugins {
alias(libs.plugins.conventions.kover)
alias(libs.plugins.conventions.gradle.doctor)
alias(libs.plugins.atomicfu)
id("build-util")
}

configureProjectReport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package kotlinx.rpc.codegen

import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
Expand All @@ -16,10 +18,20 @@ interface FirVersionSpecificApi {
delegatedTypeRef: FirTypeRef? = null,
): FirResolvedTypeRef

fun ConeKotlinType.toClassSymbolVS(
session: FirSession,
): FirClassSymbol<*>?

var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType
}

fun <T> vsApi(body: FirVersionSpecificApi.() -> T) : T {
val klass = Class.forName("kotlinx.rpc.codegen.FirVersionSpecificApiImpl")
return (klass.kotlin.objectInstance as FirVersionSpecificApi).body()
val vsApiClass by lazy {
runCatching {
Class.forName("kotlinx.rpc.codegen.FirVersionSpecificApiImpl")
}.getOrNull()
}

inline fun <T> vsApi(body: FirVersionSpecificApi.() -> T): T {
val kClass = vsApiClass?.kotlin ?: error("FirVersionSpecificApi is not present")
return (kClass.objectInstance as FirVersionSpecificApi).body()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.rpc.codegen.FirRpcPredicates
import kotlinx.rpc.codegen.checkers.FirCheckedAnnotationHelper.checkTypeArguments
import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics
import kotlinx.rpc.codegen.common.RpcClassId
import kotlinx.rpc.codegen.vsApi
import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
Expand Down Expand Up @@ -76,7 +77,7 @@ class FirCheckedAnnotationTypeParameterChecker(
ctx = ctx,
origin = bound,
originMapper = { it.coneType },
symbolProvider = { it.toClassSymbol(context.session) },
symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } },
typeParameterSymbolsProvider = { it.typeParameterSymbols },
typeArgumentsProvider = { it.typeArguments.toList() },
typeArgumentsMapper = { it },
Expand All @@ -102,7 +103,7 @@ class FirCheckedAnnotationFirClassChecker(
ctx = ctx,
origin = superType,
originMapper = { it.coneType },
symbolProvider = { it.toClassSymbol(context.session) },
symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } },
typeParameterSymbolsProvider = { it.typeParameterSymbols },
typeArgumentsProvider = { it.typeArguments.toList() },
typeArgumentsMapper = { it },
Expand All @@ -127,7 +128,7 @@ class FirCheckedAnnotationFirFunctionChecker(
ctx = ctx,
origin = valueParameter.returnTypeRef,
originMapper = { it.coneType },
symbolProvider = { it.toClassSymbol(context.session) },
symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } },
typeParameterSymbolsProvider = { it.typeParameterSymbols },
typeArgumentsProvider = { it.typeArguments.toList() },
typeArgumentsMapper = { it },
Expand Down Expand Up @@ -172,7 +173,7 @@ object FirCheckedAnnotationHelper {
parametersWithAnnotations.forEach { (i, annotations) ->
val typeArgument = typeArguments[i]
val type = typeArgumentsMapper(typeArgument)?.type
val classSymbol = type?.toClassSymbol(context.session)
val classSymbol = vsApi { type?.toClassSymbolVS(context.session) }

val symbol = when {
classSymbol != null -> {
Expand Down Expand Up @@ -247,7 +248,7 @@ object FirCheckedAnnotationHelper {
}?.type
},
originTypeRefSource = extractedOriginSources.getOrNull(i) ?: nextOriginSource ?: originTypeRefSource,
symbolProvider = { it.toClassSymbol(context.session) },
symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } },
typeParameterSymbolsProvider = { it.typeParameterSymbols },
typeArgumentsProvider = { it.typeArguments.toList() },
typeArgumentsMapper = { it },
Expand Down Expand Up @@ -283,7 +284,7 @@ object FirCheckedAnnotationHelper {
visited: Set<FirBasedSymbol<*>> = emptySet(),
): List<FirClassSymbol<*>> {
return symbol.annotations.mapNotNull {
it.resolvedType.toClassSymbol(session)
vsApi { it.resolvedType.toClassSymbolVS(session) }
}.filter { annotation ->
when {
annotation in visited -> false
Expand Down Expand Up @@ -328,7 +329,7 @@ object FirCheckedAnnotationHelper {
symbol in visited -> false
symbol.hasAnnotation(annotationId, session) -> true
else -> symbol.annotations.any { annotation ->
annotation.resolvedType.toClassSymbol(session)?.let {
vsApi { annotation.resolvedType.toClassSymbolVS(session) }?.let {
hasCheckedAnnotation(session, it, annotationId, visited + symbol)
} == true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package kotlinx.rpc.codegen

import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.toFirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder
import org.jetbrains.kotlin.fir.types.toClassSymbol

@Suppress("unused")
object FirVersionSpecificApiImpl : FirVersionSpecificApi {
Expand All @@ -20,6 +23,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi {
return toFirResolvedTypeRef(source, delegatedTypeRef)
}

override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? {
return toClassSymbol(session)
}

override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType
get() = coneType
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package kotlinx.rpc.codegen

import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.toFirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder
import org.jetbrains.kotlin.fir.types.toClassSymbol

@Suppress("unused")
object FirVersionSpecificApiImpl : FirVersionSpecificApi {
Expand All @@ -20,6 +24,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi {
return toFirResolvedTypeRef(source, delegatedTypeRef)
}

override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? {
return (this as? ConeClassLikeType)?.toClassSymbol(session)
}

override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType
get() = type
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package kotlinx.rpc.codegen

import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.toFirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder
import org.jetbrains.kotlin.fir.types.toClassSymbol

@Suppress("unused")
object FirVersionSpecificApiImpl : FirVersionSpecificApi {
Expand All @@ -20,6 +23,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi {
return toFirResolvedTypeRef(source, delegatedTypeRef)
}

override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? {
return toClassSymbol(session)
}

override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType
get() = type
set(value) {
Expand Down

0 comments on commit 7a7956f

Please sign in to comment.