Skip to content

Commit affa5bd

Browse files
committed
Simplify lifetime management (#347)
1 parent 1e4a13d commit affa5bd

File tree

193 files changed

+908
-2248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+908
-2248
lines changed

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/RpcDeclarationScanner.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal object RpcDeclarationScanner {
8181
private fun unsupportedDeclaration(service: IrClass, declaration: IrDeclaration, logger: MessageCollector): Nothing? {
8282
logger.report(
8383
severity = CompilerMessageSeverity.LOGGING,
84-
message = "Unsupported declaration in RemoteService interface ${service.name}: ${declaration.dumpKotlinLike()}",
84+
message = "Unsupported declaration in @Rpc interface ${service.name}: ${declaration.dumpKotlinLike()}",
8585
)
8686

8787
return null

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/RpcIrContext.kt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ internal class RpcIrContext(
3636
getIrClassSymbol("kotlinx.coroutines", "CoroutineScope")
3737
}
3838

39-
val coroutineContext by lazy {
40-
getIrClassSymbol("kotlin.coroutines", "CoroutineContext")
41-
}
42-
4339
val kTypeClass by lazy {
4440
getIrClassSymbol("kotlin.reflect", "KType")
4541
}
@@ -56,14 +52,6 @@ internal class RpcIrContext(
5652
getIrClassSymbol("kotlinx.coroutines.flow", "Flow")
5753
}
5854

59-
val sharedFlow by lazy {
60-
getIrClassSymbol("kotlinx.coroutines.flow", "SharedFlow")
61-
}
62-
63-
val stateFlow by lazy {
64-
getIrClassSymbol("kotlinx.coroutines.flow", "StateFlow")
65-
}
66-
6755
val pair by lazy {
6856
getIrClassSymbol("kotlin", "Pair")
6957
}
@@ -139,10 +127,6 @@ internal class RpcIrContext(
139127
rpcClient.namedFunction("callServerStreaming")
140128
}
141129

142-
val provideStubContext by lazy {
143-
rpcClient.namedFunction("provideStubContext")
144-
}
145-
146130
val asArray by lazy {
147131
rpcMethodClass.namedFunction("asArray")
148132
}
@@ -155,10 +139,6 @@ internal class RpcIrContext(
155139
namedFunction("kotlin", "emptyArray")
156140
}
157141

158-
val scopedClientCall by lazy {
159-
namedFunction("kotlinx.rpc.internal", "scopedClientCall")
160-
}
161-
162142
val emptyList by lazy {
163143
namedFunction("kotlin.collections", "emptyList")
164144
}
@@ -201,10 +181,6 @@ internal class RpcIrContext(
201181
val properties = Properties()
202182

203183
inner class Properties {
204-
val rpcClientCoroutineContext by lazy {
205-
rpcClient.namedProperty("coroutineContext")
206-
}
207-
208184
val rpcServiceDescriptorFqName by lazy {
209185
rpcServiceDescriptor.namedProperty("fqName")
210186
}

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/RpcStubGenerator.kt

Lines changed: 12 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ internal class RpcStubGenerator(
120120
stubIdProperty()
121121

122122
clientProperty()
123-
124-
coroutineContextProperty()
125123
}
126124

127125
private var stubIdProperty: IrProperty by Delegates.notNull()
@@ -191,64 +189,6 @@ internal class RpcStubGenerator(
191189
}
192190
}
193191

194-
private var coroutineContextProperty: IrProperty by Delegates.notNull()
195-
196-
/**
197-
* `coroutineContext` property from `RemoteService` interface
198-
*
199-
* ```kotlin
200-
* final override val coroutineContext: CoroutineContext = __rpc_client.provideStubContext(__rpc_stub_id)
201-
* ```
202-
*/
203-
private fun IrClass.coroutineContextProperty() {
204-
coroutineContextProperty = addProperty {
205-
name = Name.identifier("coroutineContext")
206-
visibility = DescriptorVisibilities.PUBLIC
207-
modality = Modality.FINAL
208-
}.apply {
209-
overriddenSymbols = listOf(ctx.properties.rpcClientCoroutineContext)
210-
211-
addBackingFieldUtil {
212-
visibility = DescriptorVisibilities.PRIVATE
213-
type = ctx.coroutineContext.defaultType
214-
vsApi { isFinalVS = true }
215-
}.apply {
216-
val coroutineContextClass = ctx.coroutineContext.owner
217-
218-
initializer = factory.createExpressionBody(
219-
vsApi {
220-
IrCallImplVS(
221-
startOffset = UNDEFINED_OFFSET,
222-
endOffset = UNDEFINED_OFFSET,
223-
type = coroutineContextClass.typeWith(),
224-
symbol = ctx.functions.provideStubContext.symbol,
225-
valueArgumentsCount = 1,
226-
typeArgumentsCount = 0,
227-
)
228-
}.apply {
229-
arguments {
230-
dispatchReceiver = irCallProperty(stubClass, clientProperty)
231-
232-
values {
233-
+irCallProperty(stubClass, stubIdProperty)
234-
}
235-
}
236-
}
237-
)
238-
}
239-
240-
addDefaultGetter(this@coroutineContextProperty, ctx.irBuiltIns) {
241-
val serviceCoroutineContext = declaration.service.getPropertyGetter("coroutineContext")
242-
?: error(
243-
"RPC services expected to have \"coroutineContext\" property with getter: " +
244-
declaration.service.dump()
245-
)
246-
247-
overriddenSymbols = listOf(serviceCoroutineContext)
248-
}
249-
}
250-
}
251-
252192
private fun IrClass.generateMethods() {
253193
declaration.methods.forEach {
254194
generateRpcMethod(it)
@@ -307,8 +247,6 @@ internal class RpcStubGenerator(
307247
}
308248
}
309249

310-
val declaredFunction = this
311-
312250
val arguments = method.arguments.memoryOptimizedMap { arg ->
313251
addValueParameter {
314252
name = arg.value.name
@@ -333,63 +271,19 @@ internal class RpcStubGenerator(
333271
return@irBlockBody
334272
}
335273

336-
+irReturn(
337-
irCall(
338-
callee = ctx.functions.scopedClientCall,
339-
type = method.function.returnType,
340-
).apply {
341-
// suspend lambda
342-
// it's type is not available at runtime, but in fact exists
343-
val lambdaType = ctx.suspendFunction0.typeWith(method.function.returnType)
344-
345-
val functionLambda = factory.buildFun {
346-
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
347-
name = SpecialNames.ANONYMOUS
348-
visibility = DescriptorVisibilities.LOCAL
349-
modality = Modality.FINAL
350-
returnType = method.function.returnType
351-
isSuspend = true
352-
}.apply {
353-
parent = declaredFunction
354-
355-
body = irBuilder(symbol).irBlockBody {
356-
val call = irRpcMethodClientCall(
357-
method = method,
358-
functionThisReceiver = functionThisReceiver,
359-
isMethodObject = isMethodObject,
360-
methodClass = methodClass,
361-
arguments = arguments,
362-
)
363-
364-
if (method.function.returnType == ctx.irBuiltIns.unitType) {
365-
+call
366-
} else {
367-
+irReturn(call)
368-
}
369-
}
370-
}
371-
372-
val lambda = IrFunctionExpressionImpl(
373-
startOffset = UNDEFINED_OFFSET,
374-
endOffset = UNDEFINED_OFFSET,
375-
type = lambdaType,
376-
origin = IrStatementOrigin.LAMBDA,
377-
function = functionLambda,
378-
)
379-
380-
arguments {
381-
types {
382-
+method.function.returnType
383-
}
384-
385-
values {
386-
+irGet(ctx.coroutineScope.defaultType, functionThisReceiver.symbol)
387-
388-
+lambda
389-
}
390-
}
391-
}
274+
val call = irRpcMethodClientCall(
275+
method = method,
276+
functionThisReceiver = functionThisReceiver,
277+
isMethodObject = isMethodObject,
278+
methodClass = methodClass,
279+
arguments = arguments,
392280
)
281+
282+
if (method.function.returnType == ctx.irBuiltIns.unitType) {
283+
+call
284+
} else {
285+
+irReturn(call)
286+
}
393287
}
394288
}
395289
}

compiler-plugin/compiler-plugin-common/src/main/core/kotlinx/rpc/codegen/common/Names.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.name.FqName
1010
import org.jetbrains.kotlin.name.Name
1111

1212
object RpcClassId {
13-
val remoteServiceInterface = ClassId(FqName("kotlinx.rpc"), Name.identifier("RemoteService"))
1413
val rpcAnnotation = ClassId(FqName("kotlinx.rpc.annotations"), Name.identifier("Rpc"))
1514
val checkedTypeAnnotation = ClassId(FqName("kotlinx.rpc.annotations"), Name.identifier("CheckedTypeAnnotation"))
1615

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirRpcExtensionRegistrar.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,5 @@ class FirRpcExtensionRegistrar(private val configuration: CompilerConfiguration)
3030
}
3131

3232
+CFactory { FirRpcAdditionalCheckers(it, serializationIsPresent, configuration) }
33-
34-
+SFactory { FirRpcSupertypeGenerator(it, logger) }
3533
}
3634
}

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirRpcSupertypeGeneratorAbstract.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirRpcUtils.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.rpc.codegen
66

7-
import kotlinx.rpc.codegen.common.RpcClassId
87
import org.jetbrains.kotlin.KtSourceElement
98
import org.jetbrains.kotlin.fir.FirSession
109
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassId
@@ -15,17 +14,11 @@ import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
1514
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
1615
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
1716
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
18-
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
1917
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
20-
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
2118
import org.jetbrains.kotlin.fir.types.FirTypeRef
2219
import org.jetbrains.kotlin.fir.types.coneTypeSafe
2320
import org.jetbrains.kotlin.name.ClassId
2421

25-
fun FirClassSymbol<*>.isRemoteService(session: FirSession): Boolean = resolvedSuperTypeRefs.any {
26-
it.doesMatchesClassId(session, RpcClassId.remoteServiceInterface)
27-
}
28-
2922
fun FirBasedSymbol<*>.rpcAnnotationSource(
3023
session: FirSession,
3124
predicate: DeclarationPredicate,
@@ -53,14 +46,6 @@ fun List<FirAnnotation>.rpcAnnotation(session: FirSession, predicate: Declaratio
5346
}
5447
}
5548

56-
fun FirClassSymbol<*>.remoteServiceSupertypeSource(session: FirSession): KtSourceElement? {
57-
return remoteServiceSupertype(session)?.source
58-
}
59-
60-
fun FirClassSymbol<*>.remoteServiceSupertype(session: FirSession): FirResolvedTypeRef? {
61-
return resolvedSuperTypeRefs.find { it.doesMatchesClassId(session, RpcClassId.remoteServiceInterface) }
62-
}
63-
6449
@OptIn(SymbolInternals::class)
6550
internal fun FirTypeRef.doesMatchesClassId(session: FirSession, classId: ClassId): Boolean {
6651
return coneTypeSafe<ConeClassLikeType>()?.fullyExpandedType(session)?.lookupTag?.classId == classId

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirRpcAnnotationChecker.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import kotlinx.rpc.codegen.FirCheckersContext
88
import kotlinx.rpc.codegen.FirRpcPredicates
99
import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics
1010
import kotlinx.rpc.codegen.common.RpcClassId
11-
import kotlinx.rpc.codegen.isRemoteService
12-
import kotlinx.rpc.codegen.remoteServiceSupertypeSource
1311
import kotlinx.rpc.codegen.rpcAnnotation
1412
import kotlinx.rpc.codegen.rpcAnnotationSource
1513
import org.jetbrains.kotlin.descriptors.ClassKind
@@ -51,14 +49,6 @@ object FirRpcAnnotationChecker {
5149
)
5250
}
5351

54-
if (declaration.symbol.isRemoteService(context.session) && !rpcAnnotated) {
55-
reporter.reportOn(
56-
source = declaration.symbol.remoteServiceSupertypeSource(context.session),
57-
factory = FirRpcDiagnostics.MISSING_RPC_ANNOTATION,
58-
context = context,
59-
)
60-
}
61-
6252
if (rpcAnnotated && !ctx.serializationIsPresent && isMetaAnnotated) {
6353
reporter.reportOn(
6454
source = declaration.symbol.rpcAnnotationSource(

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.psi.KtElement
2626
// ###########################################################################
2727

2828
object FirRpcDiagnostics : RpcKtDiagnosticsContainer() {
29-
val MISSING_RPC_ANNOTATION by error0<KtAnnotated>()
3029
val MISSING_SERIALIZATION_MODULE by error0<KtAnnotated>()
3130
val WRONG_RPC_ANNOTATION_TARGET by error1<KtAnnotated, ConeKotlinType>()
3231
val CHECKED_ANNOTATION_VIOLATION by error3<KtAnnotated, Int, ConeKotlinType, FirBasedSymbol<*>>()

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers
1212

1313
object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() {
1414
override val MAP by RpcKtDiagnosticFactoryToRendererMap("Rpc") { map ->
15-
map.put(
16-
factory = FirRpcDiagnostics.MISSING_RPC_ANNOTATION,
17-
message = "Missing @Rpc annotation. " +
18-
"All services children of kotlinx.rpc.RemoteService " +
19-
"must be annotated with kotlinx.rpc.annotations.Rpc",
20-
)
21-
2215
map.put(
2316
factory = FirRpcDiagnostics.MISSING_SERIALIZATION_MODULE,
2417
message = "Missing kotlinx.serialization plugin in the module. " +

0 commit comments

Comments
 (0)