Skip to content

Commit d51a0ef

Browse files
committed
Fixed @rpc annotation diagnostic (#258)
1 parent 5ce1f7d commit d51a0ef

File tree

7 files changed

+35
-12
lines changed

7 files changed

+35
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.codegen
@@ -12,6 +12,10 @@ object FirRpcPredicates {
1212
annotated(RpcClassId.rpcAnnotation.asSingleFqName()) // @Rpc
1313
}
1414

15+
internal val rpcMeta = DeclarationPredicate.create {
16+
metaAnnotated(RpcClassId.rpcAnnotation.asSingleFqName(), includeItself = true)
17+
}
18+
1519
internal val checkedAnnotationMeta = DeclarationPredicate.create {
1620
metaAnnotated(RpcClassId.checkedTypeAnnotation.asSingleFqName(), includeItself = false)
1721
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.codegen.checkers
@@ -9,7 +9,9 @@ import kotlinx.rpc.codegen.FirRpcPredicates
99
import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics
1010
import kotlinx.rpc.codegen.isRemoteService
1111
import kotlinx.rpc.codegen.remoteServiceSupertypeSource
12+
import kotlinx.rpc.codegen.rpcAnnotation
1213
import kotlinx.rpc.codegen.rpcAnnotationSource
14+
import org.jetbrains.kotlin.descriptors.ClassKind
1315
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
1416
import org.jetbrains.kotlin.diagnostics.reportOn
1517
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind
@@ -18,6 +20,7 @@ import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirRegularClassChe
1820
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
1921
import org.jetbrains.kotlin.fir.declarations.utils.isInterface
2022
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
23+
import org.jetbrains.kotlin.fir.types.resolvedType
2124

2225
class FirRpcAnnotationChecker(private val ctx: FirCheckersContext) : FirRegularClassChecker(MppCheckerKind.Common) {
2326
override fun check(
@@ -26,12 +29,15 @@ class FirRpcAnnotationChecker(private val ctx: FirCheckersContext) : FirRegularC
2629
reporter: DiagnosticReporter,
2730
) {
2831
val rpcAnnotated = context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)
32+
val rpcMetaAnnotated = context.session.predicateBasedProvider.matches(FirRpcPredicates.rpcMeta, declaration)
2933

30-
if (!declaration.isInterface && rpcAnnotated) {
34+
if (!declaration.isInterface && declaration.classKind != ClassKind.ANNOTATION_CLASS && rpcMetaAnnotated) {
3135
reporter.reportOn(
3236
source = declaration.symbol.rpcAnnotationSource(context.session),
3337
factory = FirRpcDiagnostics.WRONG_RPC_ANNOTATION_TARGET,
3438
context = context,
39+
a = declaration.symbol.rpcAnnotation(context.session)?.resolvedType
40+
?: error("Unexpected unresolved annotation type for declaration: ${declaration.symbol.classId.asSingleFqName()}"),
3541
)
3642
}
3743

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.codegen.checkers.diagnostics
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
1919
object FirRpcDiagnostics {
2020
val MISSING_RPC_ANNOTATION by error0<KtAnnotationEntry>()
2121
val MISSING_SERIALIZATION_MODULE by warning0<KtAnnotationEntry>()
22-
val WRONG_RPC_ANNOTATION_TARGET by error0<KtAnnotationEntry>()
22+
val WRONG_RPC_ANNOTATION_TARGET by error1<KtAnnotationEntry, ConeKotlinType>()
2323
val CHECKED_ANNOTATION_VIOLATION by error1<KtAnnotationEntry, ConeKotlinType>()
2424
val NON_SUSPENDING_REQUEST_WITHOUT_STREAMING_RETURN_TYPE by error0<PsiElement>()
2525
val AD_HOC_POLYMORPHISM_IN_RPC_SERVICE by error2<PsiElement, Int, Name>()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.codegen.checkers.diagnostics
@@ -29,7 +29,8 @@ object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() {
2929

3030
put(
3131
factory = FirRpcDiagnostics.WRONG_RPC_ANNOTATION_TARGET,
32-
message = "@Rpc annotation is only applicable to interfaces.",
32+
message = "@{0} annotation is only applicable to interfaces and annotation classes.",
33+
rendererA = FirDiagnosticRenderers.RENDER_TYPE,
3334
)
3435

3536
put(

tests/compiler-plugin-tests/src/test-gen/kotlinx/rpc/codegen/test/runners/BoxTestGenerated.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.jetbrains.kotlin.test.TargetBackend;
1212
import org.jetbrains.kotlin.test.TestMetadata;
1313
import org.junit.Ignore;
14+
import org.junit.jupiter.api.Disabled;
1415
import org.junit.jupiter.api.Test;
1516

1617
import java.io.File;
@@ -20,7 +21,7 @@
2021
@SuppressWarnings("all")
2122
@TestMetadata("src/testData/box")
2223
@TestDataPath("$PROJECT_ROOT")
23-
@Ignore
24+
@Disabled("KRPC-137")
2425
public class BoxTestGenerated extends AbstractBoxTest {
2526
@Test
2627
public void testAllFilesPresentInBox() {

tests/compiler-plugin-tests/src/test-gen/kotlinx/rpc/codegen/test/runners/DiagnosticTestGenerated.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
import com.intellij.testFramework.TestDataPath;
1010
import org.jetbrains.kotlin.test.util.KtTestUtil;
1111
import org.jetbrains.kotlin.test.TestMetadata;
12-
import org.junit.Ignore;
12+
import org.junit.jupiter.api.Disabled;
1313
import org.junit.jupiter.api.Test;
14-
1514
import java.io.File;
1615
import java.util.regex.Pattern;
1716

1817
/** This class is generated by {@link kotlinx.rpc.codegen.test.GenerateTestsKt}. DO NOT MODIFY MANUALLY */
1918
@SuppressWarnings("all")
2019
@TestMetadata("src/testData/diagnostics")
2120
@TestDataPath("$PROJECT_ROOT")
22-
@Ignore
21+
@Disabled("KRPC-137")
2322
public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
2423
@Test
2524
public void testAllFilesPresentInDiagnostics() {

tests/compiler-plugin-tests/src/testData/diagnostics/rpcChecked.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
@file:OptIn(ExperimentalRpcApi::class)
@@ -61,3 +61,15 @@ inline suspend fun <reified T : Any> fail(client: RpcClient, server: RpcServer,
6161
serviceDescriptorOf<<!CHECKED_ANNOTATION_VIOLATION!>NotAService<!>>()
6262
serviceDescriptorOf<<!CHECKED_ANNOTATION_VIOLATION!>T<!>>()
6363
}
64+
65+
@Rpc
66+
annotation class Grpc
67+
68+
@Grpc
69+
interface MyGrpcService
70+
71+
@Grpc
72+
class WrongGrpcTarget
73+
74+
@Rpc
75+
class WrongRpcTarget

0 commit comments

Comments
 (0)