diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt index 508e17438..ae9b9ed22 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt @@ -6,14 +6,11 @@ package kotlinx.rpc.codegen import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.declarations.FirFunction import org.jetbrains.kotlin.fir.declarations.FirRegularClass -import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol -import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt index 5a9e913e3..adada6411 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.diagnostics.error0 import org.jetbrains.kotlin.diagnostics.error1 import org.jetbrains.kotlin.diagnostics.error2 import org.jetbrains.kotlin.diagnostics.error3 -import org.jetbrains.kotlin.diagnostics.rendering.RootDiagnosticRendererFactory +import org.jetbrains.kotlin.diagnostics.rendering.BaseDiagnosticRendererFactory import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.name.Name @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.psi.KtElement // # Instead use KtElement, otherwise problems in IDE and in tests may arise # // ########################################################################### -object FirRpcDiagnostics { +object FirRpcDiagnostics : RpcKtDiagnosticsContainer() { val MISSING_RPC_ANNOTATION by error0() val MISSING_SERIALIZATION_MODULE by error0() val WRONG_RPC_ANNOTATION_TARGET by error1() @@ -35,13 +35,13 @@ object FirRpcDiagnostics { val TYPE_PARAMETERS_IN_RPC_FUNCTION by error0(SourceElementPositioningStrategies.TYPE_PARAMETERS_LIST) val TYPE_PARAMETERS_IN_RPC_INTERFACE by error0(SourceElementPositioningStrategies.TYPE_PARAMETERS_LIST) - init { - RootDiagnosticRendererFactory.registerFactory(RpcDiagnosticRendererFactory) + override fun getRendererFactoryVs(): BaseDiagnosticRendererFactory { + return RpcDiagnosticRendererFactory } } @Suppress("PropertyName", "detekt.VariableNaming") -class FirRpcStrictModeDiagnostics(val modes: StrictModeAggregator) { +class FirRpcStrictModeDiagnostics(val modes: StrictModeAggregator) : RpcKtDiagnosticsContainer() { val STATE_FLOW_IN_RPC_SERVICE by modded0(modes.stateFlow) val SHARED_FLOW_IN_RPC_SERVICE by modded0(modes.sharedFlow) val NESTED_STREAMING_IN_RPC_SERVICE by modded0(modes.nestedFlow) @@ -50,7 +50,7 @@ class FirRpcStrictModeDiagnostics(val modes: StrictModeAggregator) { val NON_TOP_LEVEL_SERVER_STREAMING_IN_RPC_SERVICE by modded0(modes.notTopLevelServerFlow) val FIELD_IN_RPC_SERVICE by modded0(modes.fields) - init { - RootDiagnosticRendererFactory.registerFactory(RpcStrictModeDiagnosticRendererFactory(this)) + override fun getRendererFactoryVs(): BaseDiagnosticRendererFactory { + return RpcStrictModeDiagnosticRendererFactory(this) } } diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt index 2717da2aa..792fb24d5 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt @@ -6,34 +6,33 @@ package kotlinx.rpc.codegen.checkers.diagnostics import kotlinx.rpc.codegen.StrictMode import kotlinx.rpc.codegen.StrictModeAggregator -import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap import org.jetbrains.kotlin.diagnostics.rendering.BaseDiagnosticRendererFactory import org.jetbrains.kotlin.diagnostics.rendering.Renderer import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() { - override val MAP = KtDiagnosticFactoryToRendererMap("Rpc").apply { - put( + override val MAP by RpcKtDiagnosticFactoryToRendererMap("Rpc") { map -> + map.put( factory = FirRpcDiagnostics.MISSING_RPC_ANNOTATION, message = "Missing @Rpc annotation. " + "All services children of kotlinx.rpc.RemoteService " + "must be annotated with kotlinx.rpc.annotations.Rpc", ) - put( + map.put( factory = FirRpcDiagnostics.MISSING_SERIALIZATION_MODULE, message = "Missing kotlinx.serialization plugin in the module. " + "Service generation will not be available. " + "Add kotlin(\"plugin.serialization\") to your build.gradle.kts plugins section." ) - put( + map.put( factory = FirRpcDiagnostics.WRONG_RPC_ANNOTATION_TARGET, message = "@{0} annotation is only applicable to interfaces and annotation classes.", rendererA = FirDiagnosticRenderers.RENDER_TYPE, ) - put( + map.put( factory = FirRpcDiagnostics.CHECKED_ANNOTATION_VIOLATION, message = "{0} type argument is marked with @{1} annotation, but inferred type is {2}. " + "Provide a type that is marked with @{1} annotation explicitly " + @@ -43,24 +42,24 @@ object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() { rendererC = FirDiagnosticRenderers.SYMBOL, ) - put( + map.put( factory = FirRpcDiagnostics.NON_SUSPENDING_REQUEST_WITHOUT_STREAMING_RETURN_TYPE, message = "Non suspending request function is not allowed for functions that doesn't return Flow.", ) - put( + map.put( factory = FirRpcDiagnostics.AD_HOC_POLYMORPHISM_IN_RPC_SERVICE, message = "Ad-hoc polymorphism is not allowed in @Rpc services. Found {0} '{1}' functions.", rendererA = Renderer { it.toString() }, rendererB = Renderer { it.asString() }, ) - put( + map.put( factory = FirRpcDiagnostics.TYPE_PARAMETERS_IN_RPC_FUNCTION, message = "Type parameters are not allowed in Rpc functions.", ) - put( + map.put( factory = FirRpcDiagnostics.TYPE_PARAMETERS_IN_RPC_INTERFACE, message = "Type parameters are not allowed in @Rpc interfaces.", ) @@ -82,51 +81,51 @@ private fun Int.indexPositionSpelled(): String { class RpcStrictModeDiagnosticRendererFactory( private val diagnostics: FirRpcStrictModeDiagnostics, ) : BaseDiagnosticRendererFactory() { - override val MAP = KtDiagnosticFactoryToRendererMap("Rpc").apply { + override val MAP by RpcKtDiagnosticFactoryToRendererMap("RpcStrictMode") { map -> diagnostics.STATE_FLOW_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("StateFlow") { stateFlow }, ) } diagnostics.SHARED_FLOW_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("SharedFlow") { sharedFlow }, ) } diagnostics.NESTED_STREAMING_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("Nested streaming") { nestedFlow }, ) } diagnostics.STREAM_SCOPE_FUNCTION_IN_RPC?.let { - put( + map.put( factory = it, message = message("Stream scope usage") { streamScopedFunctions }, ) } diagnostics.SUSPENDING_SERVER_STREAMING_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("Suspend function declaration with server streaming") { suspendingServerStreaming }, ) } diagnostics.NON_TOP_LEVEL_SERVER_STREAMING_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("Not top-level server-side streaming") { sharedFlow }, ) } diagnostics.FIELD_IN_RPC_SERVICE?.let { - put( + map.put( factory = it, message = message("Field declaration") { fields }, ) diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainerCore.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainerCore.kt new file mode 100644 index 000000000..35432279b --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainerCore.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.rendering.BaseDiagnosticRendererFactory + +interface RpcKtDiagnosticsContainerCore { + fun getRendererFactoryVs() : BaseDiagnosticRendererFactory +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt similarity index 95% rename from compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt rename to compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt index 06fe0db53..6f7b2e0e1 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ @file:Suppress("unused") diff --git a/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt new file mode 100644 index 000000000..50f2a99fb --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap + +fun RpcKtDiagnosticFactoryToRendererMap( + name: String, + init: (KtDiagnosticFactoryToRendererMap) -> Unit, +): Lazy { + return lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + KtDiagnosticFactoryToRendererMap(name).also(init) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt new file mode 100644 index 000000000..b1c80d91a --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +abstract class RpcKtDiagnosticsContainer : RpcKtDiagnosticsContainerCore diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt new file mode 100644 index 000000000..6f7b2e0e1 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.rpc.codegen.checkers.diagnostics + +import kotlinx.rpc.codegen.StrictMode +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies +import org.jetbrains.kotlin.utils.DummyDelegate +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KClass +import kotlin.reflect.KProperty + +inline fun modded0(mode: StrictMode): DiagnosticFactory0DelegateProviderOnNull { + return DiagnosticFactory0DelegateProviderOnNull(mode, T::class) +} + +class DiagnosticFactory0DelegateProviderOnNull( + private val mode: StrictMode, + private val psiType: KClass<*>, +) { + operator fun provideDelegate( + @Suppress("unused") + thisRef: Any?, + prop: KProperty<*>, + ): ReadOnlyProperty { + val severity = when (mode) { + StrictMode.ERROR -> Severity.ERROR + StrictMode.WARNING -> Severity.WARNING + StrictMode.NONE -> null + } ?: return DummyDelegate(null) + + return DummyDelegate( + KtDiagnosticFactory0( + name = prop.name, + severity = severity, + defaultPositioningStrategy = SourceElementPositioningStrategies.DEFAULT, + psiType = psiType, + ), + ) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt new file mode 100644 index 000000000..50f2a99fb --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap + +fun RpcKtDiagnosticFactoryToRendererMap( + name: String, + init: (KtDiagnosticFactoryToRendererMap) -> Unit, +): Lazy { + return lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + KtDiagnosticFactoryToRendererMap(name).also(init) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt new file mode 100644 index 000000000..b1c80d91a --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +abstract class RpcKtDiagnosticsContainer : RpcKtDiagnosticsContainerCore diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt new file mode 100644 index 000000000..6f7b2e0e1 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.rpc.codegen.checkers.diagnostics + +import kotlinx.rpc.codegen.StrictMode +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies +import org.jetbrains.kotlin.utils.DummyDelegate +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KClass +import kotlin.reflect.KProperty + +inline fun modded0(mode: StrictMode): DiagnosticFactory0DelegateProviderOnNull { + return DiagnosticFactory0DelegateProviderOnNull(mode, T::class) +} + +class DiagnosticFactory0DelegateProviderOnNull( + private val mode: StrictMode, + private val psiType: KClass<*>, +) { + operator fun provideDelegate( + @Suppress("unused") + thisRef: Any?, + prop: KProperty<*>, + ): ReadOnlyProperty { + val severity = when (mode) { + StrictMode.ERROR -> Severity.ERROR + StrictMode.WARNING -> Severity.WARNING + StrictMode.NONE -> null + } ?: return DummyDelegate(null) + + return DummyDelegate( + KtDiagnosticFactory0( + name = prop.name, + severity = severity, + defaultPositioningStrategy = SourceElementPositioningStrategies.DEFAULT, + psiType = psiType, + ), + ) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt new file mode 100644 index 000000000..50f2a99fb --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap + +fun RpcKtDiagnosticFactoryToRendererMap( + name: String, + init: (KtDiagnosticFactoryToRendererMap) -> Unit, +): Lazy { + return lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + KtDiagnosticFactoryToRendererMap(name).also(init) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt new file mode 100644 index 000000000..b1c80d91a --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +abstract class RpcKtDiagnosticsContainer : RpcKtDiagnosticsContainerCore diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt index f46edc617..167001fb6 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt @@ -15,7 +15,6 @@ 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.declarations.FirFunction import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt new file mode 100644 index 000000000..6f7b2e0e1 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.rpc.codegen.checkers.diagnostics + +import kotlinx.rpc.codegen.StrictMode +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies +import org.jetbrains.kotlin.utils.DummyDelegate +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KClass +import kotlin.reflect.KProperty + +inline fun modded0(mode: StrictMode): DiagnosticFactory0DelegateProviderOnNull { + return DiagnosticFactory0DelegateProviderOnNull(mode, T::class) +} + +class DiagnosticFactory0DelegateProviderOnNull( + private val mode: StrictMode, + private val psiType: KClass<*>, +) { + operator fun provideDelegate( + @Suppress("unused") + thisRef: Any?, + prop: KProperty<*>, + ): ReadOnlyProperty { + val severity = when (mode) { + StrictMode.ERROR -> Severity.ERROR + StrictMode.WARNING -> Severity.WARNING + StrictMode.NONE -> null + } ?: return DummyDelegate(null) + + return DummyDelegate( + KtDiagnosticFactory0( + name = prop.name, + severity = severity, + defaultPositioningStrategy = SourceElementPositioningStrategies.DEFAULT, + psiType = psiType, + ), + ) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt new file mode 100644 index 000000000..50f2a99fb --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap + +fun RpcKtDiagnosticFactoryToRendererMap( + name: String, + init: (KtDiagnosticFactoryToRendererMap) -> Unit, +): Lazy { + return lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + KtDiagnosticFactoryToRendererMap(name).also(init) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt new file mode 100644 index 000000000..b1c80d91a --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +abstract class RpcKtDiagnosticsContainer : RpcKtDiagnosticsContainerCore diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirRpcSupertypeGenerator.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirRpcSupertypeGenerator.kt new file mode 100644 index 000000000..90269d1d8 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirRpcSupertypeGenerator.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen + +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.FirClassLikeDeclaration +import org.jetbrains.kotlin.fir.types.ConeKotlinType +import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef +import org.jetbrains.kotlin.fir.types.constructClassLikeType + +class FirRpcSupertypeGenerator( + session: FirSession, + logger: MessageCollector, +) : FirRpcSupertypeGeneratorAbstract(session, logger) { + override fun computeAdditionalSupertypes( + classLikeDeclaration: FirClassLikeDeclaration, + resolvedSupertypes: List, + typeResolver: TypeResolveService, + ): List { + return computeAdditionalSupertypesAbstract(resolvedSupertypes).map { + it.constructClassLikeType(emptyArray(), isMarkedNullable = false) + } + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt new file mode 100644 index 000000000..167001fb6 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen + +import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.declarations.declaredFunctions +import org.jetbrains.kotlin.fir.declarations.constructors +import org.jetbrains.kotlin.fir.resolve.toClassSymbol +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.symbols.impl.FirFunctionSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol +import org.jetbrains.kotlin.fir.declarations.FirRegularClass +import org.jetbrains.kotlin.fir.declarations.processAllDeclarations +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol + +object FirVersionSpecificApiImpl : FirVersionSpecificApi { + override fun ConeKotlinType.toFirResolvedTypeRefVS( + source: KtSourceElement?, + delegatedTypeRef: FirTypeRef?, + ): FirResolvedTypeRef { + return toFirResolvedTypeRef(source, delegatedTypeRef) + } + + override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? { + return toClassSymbol(session) + } + + override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType + get() = coneType + set(value) { + coneType = value + } + + override fun FirClassSymbol<*>.declaredFunctionsVS(session: FirSession): List> { + return declaredFunctions(session) + } + + override fun FirRegularClassSymbol.constructorsVS(session: FirSession): List { + return constructors(session) + } + + override fun FirRegularClass.declarationsVS(session: FirSession): List> { + val declarations = mutableListOf>() + processAllDeclarations(session) { symbol -> + declarations.add(symbol) + } + return declarations + } + + override val FirResolvedTypeRef.coneTypeVS: ConeKotlinType get() = coneType +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/FirRpcCheckersVS.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/FirRpcCheckersVS.kt new file mode 100644 index 000000000..684070a55 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/FirRpcCheckersVS.kt @@ -0,0 +1,88 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers + +import kotlinx.rpc.codegen.FirCheckersContext +import org.jetbrains.kotlin.diagnostics.DiagnosticReporter +import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext +import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirClassChecker +import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirFunctionChecker +import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirRegularClassChecker +import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirTypeParameterChecker +import org.jetbrains.kotlin.fir.analysis.checkers.expression.FirFunctionCallChecker +import org.jetbrains.kotlin.fir.declarations.FirClass +import org.jetbrains.kotlin.fir.declarations.FirFunction +import org.jetbrains.kotlin.fir.declarations.FirRegularClass +import org.jetbrains.kotlin.fir.declarations.FirTypeParameter +import org.jetbrains.kotlin.fir.expressions.FirFunctionCall + +class FirCheckedAnnotationFunctionCallCheckerVS( + private val ctx: FirCheckersContext, +) : FirFunctionCallChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(expression: FirFunctionCall) { + FirCheckedAnnotationFunctionCallChecker.check(ctx, expression, context, reporter) + } +} + +class FirCheckedAnnotationTypeParameterCheckerVS( + private val ctx: FirCheckersContext, +) : FirTypeParameterChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirTypeParameter) { + FirCheckedAnnotationTypeParameterChecker.check(ctx, declaration, context, reporter) + } +} + +class FirCheckedAnnotationFirClassCheckerVS( + private val ctx: FirCheckersContext, +) : FirClassChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirClass) { + FirCheckedAnnotationFirClassChecker.check(ctx, declaration, context, reporter) + } +} + +class FirCheckedAnnotationFirFunctionCheckerVS( + private val ctx: FirCheckersContext, +) : FirFunctionChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirFunction) { + FirCheckedAnnotationFirFunctionChecker.check(ctx, declaration, context, reporter) + } +} + +class FirRpcAnnotationCheckerVS(private val ctx: FirCheckersContext) : FirRegularClassChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirRegularClass) { + FirRpcAnnotationChecker.check(ctx, declaration, context, reporter) + } +} + +class FirRpcServiceDeclarationCheckerVS( + private val ctx: FirCheckersContext, +) : FirRegularClassChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirRegularClass) { + FirRpcServiceDeclarationChecker.check(ctx, declaration, context, reporter) + } +} + +class FirRpcStrictModeExpressionCheckerVS( + private val ctx: FirCheckersContext, +) : FirFunctionCallChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(expression: FirFunctionCall) { + FirRpcStrictModeExpressionChecker.check(ctx, expression, context, reporter) + } +} + +class FirRpcStrictModeClassCheckerVS(private val ctx: FirCheckersContext) : FirRegularClassChecker(MppCheckerKind.Common) { + context(context: CheckerContext, reporter: DiagnosticReporter) + override fun check(declaration: FirRegularClass) { + FirRpcStrictModeClassChecker.check(ctx, declaration, context, reporter) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt new file mode 100644 index 000000000..84e3750cf --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/DiagnosticFactories.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.rpc.codegen.checkers.diagnostics + +import kotlinx.rpc.codegen.StrictMode +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 +import org.jetbrains.kotlin.diagnostics.KtDiagnosticsContainer +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies +import org.jetbrains.kotlin.utils.DummyDelegate +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KClass +import kotlin.reflect.KProperty + +context(container: KtDiagnosticsContainer) +inline fun modded0(mode: StrictMode): DiagnosticFactory0DelegateProviderOnNull { + return DiagnosticFactory0DelegateProviderOnNull(mode, T::class, container) +} + +class DiagnosticFactory0DelegateProviderOnNull( + private val mode: StrictMode, + private val psiType: KClass<*>, + private val container: KtDiagnosticsContainer, +) { + operator fun provideDelegate( + @Suppress("unused") + thisRef: Any?, + prop: KProperty<*>, + ): ReadOnlyProperty { + val severity = when (mode) { + StrictMode.ERROR -> Severity.ERROR + StrictMode.WARNING -> Severity.WARNING + StrictMode.NONE -> null + } ?: return DummyDelegate(null) + + return DummyDelegate( + KtDiagnosticFactory0( + name = prop.name, + severity = severity, + defaultPositioningStrategy = SourceElementPositioningStrategies.DEFAULT, + psiType = psiType, + rendererFactory = container.getRendererFactory(), + ), + ) + } +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt new file mode 100644 index 000000000..10750d1f2 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticFactoryToRendererMap.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap + +fun RpcKtDiagnosticFactoryToRendererMap( + name: String, + init: (KtDiagnosticFactoryToRendererMap) -> Unit, +): Lazy { + return KtDiagnosticFactoryToRendererMap(name, init) +} diff --git a/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt new file mode 100644 index 000000000..69e15c901 --- /dev/null +++ b/compiler-plugin/compiler-plugin-k2/src/main/v_2_2_2/kotlinx/rpc/codegen/checkers/diagnostics/RpcKtDiagnosticsContainer.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.rpc.codegen.checkers.diagnostics + +import org.jetbrains.kotlin.diagnostics.KtDiagnosticsContainer +import org.jetbrains.kotlin.diagnostics.rendering.BaseDiagnosticRendererFactory + +abstract class RpcKtDiagnosticsContainer : KtDiagnosticsContainer(), RpcKtDiagnosticsContainerCore { + override fun getRendererFactory(): BaseDiagnosticRendererFactory { + return getRendererFactoryVs() + } +} diff --git a/gradle-conventions/common/src/main/kotlin/util/ProjectKotlinConfig.kt b/gradle-conventions/common/src/main/kotlin/util/ProjectKotlinConfig.kt index aea92ee86..8028f1db5 100644 --- a/gradle-conventions/common/src/main/kotlin/util/ProjectKotlinConfig.kt +++ b/gradle-conventions/common/src/main/kotlin/util/ProjectKotlinConfig.kt @@ -68,9 +68,11 @@ class ProjectKotlinConfig( } } + val kotlinMasterBuild by optionalProperty() + fun nativeTargets(kmp: KotlinMultiplatformExtension) = kmp::class.memberFunctions .filter { targetFunction -> - targetFunction.parameters.size == 1 && isIncluded( + !kotlinMasterBuild && targetFunction.parameters.size == 1 && isIncluded( targetName = targetFunction.name, lookupTable = nativeLookup, ) diff --git a/gradle-conventions/latest-only/src/main/kotlin/util/npm.kt b/gradle-conventions/latest-only/src/main/kotlin/util/npm.kt index 5f791f4e3..059aa427c 100644 --- a/gradle-conventions/latest-only/src/main/kotlin/util/npm.kt +++ b/gradle-conventions/latest-only/src/main/kotlin/util/npm.kt @@ -15,6 +15,8 @@ import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootEnvSpec import java.io.File fun Project.configureNpm() { + val kotlinMasterBuild by optionalProperty() + val executeNpmLogin by tasks.registering { if (!useProxyRepositories) { return@registering @@ -80,9 +82,10 @@ fun Project.configureNpm() { ignoreScripts = false download = true - yarnLockMismatchReport = when (useProxy) { - true -> YarnLockMismatchReport.FAIL - false -> YarnLockMismatchReport.WARNING + yarnLockMismatchReport = if (useProxy && !kotlinMasterBuild) { + YarnLockMismatchReport.FAIL + } else { + YarnLockMismatchReport.WARNING } if (useProxy) { diff --git a/gradle.properties b/gradle.properties index 1f2386b10..af680ec82 100644 --- a/gradle.properties +++ b/gradle.properties @@ -42,5 +42,8 @@ kotlin.suppressGradlePluginWarnings=IncorrectCompileOnlyDependencyWarning # Add this property to ~/.gradle/gradle.properties to avoid polluting git with unwanted changes #kotlinx.rpc.develocity.skipGitTags=true +# Uncomment to sync IDEA when working with Kotlin master builds +#kotlinx.rpc.kotlinMasterBuild=true + # set to true when building IDE compiler plugin artifacts kotlinx.rpc.forIdeBuild=false